Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

the following code is working, i can read the i2c data from a microcontroller on my…

  1. User

    the following code is working, i can read the i2c data from a microcontroller on my esp32, now i need to send this data over espnow to a second esp32 and display that data on a 1306 i2c screen. #include "Wire.h"

    #define I2C_DEV_ADDR 0x3C

    uint32_t i = 0;

    void onRequest(){ Wire.print(i++); Wire.print(" Packets."); Serial.println("onRequest"); }

    void onReceive(int len){ Serial.printf("onReceive[%d]: ", len); while(Wire.available()){ Serial.write(Wire.read()); } Serial.println(); }

    void setup() { Serial.begin(115200); Serial.setDebugOutput(true); Wire.onReceive(onReceive); Wire.onRequest(onRequest); Wire.begin((uint8_t)I2C_DEV_ADDR);

    #if CONFIG_IDF_TARGET_ESP32 char message[64]; snprintf(message, 64, "%u Packets.", i++); Wire.slaveWrite((uint8_t *)message, strlen(message)); #endif }

    void loop() {

    }

  2. Assistant

    To send the data received over ESP-NOW to a second ESP32 and display it on a 1306 I2C OLED screen, you need to do the following:

    1. Install the necessary libraries:

      • ESPAsyncWebServer
      • ESPAsyncWiFiManager
      • Adafruit GFX Library
      • Adafruit SSD1306 Library
    2. Include the necessary libraries at the beginning of your code:

    #include <WiFi.h>
    #include <esp_now.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    
    1. Set up the OLED display:
    #define SCREEN_WIDTH 128
    #define SCREEN_HEIGHT 64
    #define OLED_RESET -1
    
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    
    1. Set up the ESP-NOW sender and receiver:
    struct __attribute__((packed)) DataPacket {
      uint32_t counter;
    };
    
    DataPacket receivedData;
    
    void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
      memcpy(&receivedData, data, sizeof(receivedData));
      Serial.print("Received data: ");
      Serial.println(receivedData.counter);
    }
    
    void sendData() {
      static uint32_t counter = 0;
      DataPacket data;
      data.counter = counter;
      
      esp_now_send(NULL, (const uint8_t *)&data, sizeof(data));
      Serial.print("Sent data: ");
      Serial.println(counter);
    
      counter++;
    }
    
    void setup() {
      Serial.begin(115200);
    
      // Initialize I2C communication for OLED display
      Wire.begin();
      display.begin(SSD1306_SWITCHCAPVCC, I2C_DEV_ADDR);
    
      esp_err_t result = esp_now_init();
      if (result != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
      }
    
      esp_now_register_recv_cb(OnDataRecv);
    
      // Set up WiFi mode and channel
      WiFi.mode(WIFI_STA);
      WiFi.disconnect();
      delay(100);
    
      uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
      esp_now_peer_info_t peerInfo;
      memcpy(peerInfo.peer_addr, broadcastAddress, 6);
      peerInfo.channel = 0;
      peerInfo.encrypt = false;
    
      // Add the peer to ESP-NOW
      if (esp_now_add_peer(&peerInfo) != ESP_OK) {
        Serial.println("Failed to add peer");
        return;
      }
    
      // Start sending data every second
      setInterval(1000, sendData);
    }
    
    void loop() {
      esp_now_poll();
      displayData();
    }
    
    void displayData() {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("Received data:");
      display.println(receivedData.counter);
      display.display();
    }
    

    This code will continuously send data over ESP-NOW from one ESP32 to another and display the received data on a 1306 I2C OLED screen.