modify the following code to instead of display.write use i2c wire write the data…
- User
modify the following code to instead of display.write use i2c wire write the data directly to I2C_DEV_ADDR 0x3C using #include <Wire.h> :
#include <esp_now.h> #include <WiFi.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define CHANNEL 1 #define MAX_DATA_SIZE 10 // Define the maximum size of the data array
uint8_t dataArray[MAX_DATA_SIZE]; // Create an array to store the received data uint8_t dataIndex = 0; // Index to keep track of the current position in the data array
// Init ESP Now with fallback void InitESPNow() { WiFi.disconnect(); if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); } else { Serial.println("ESPNow Init Failed"); // Retry InitESPNow, add a counte and then restart? // InitESPNow(); // or Simply Restart ESP.restart(); } }
// config AP SSID void configDeviceAP() { const char *SSID = "Slave_1"; bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0); if (!result) { Serial.println("AP Config failed."); } else { Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); Serial.print("AP CHANNEL "); Serial.println(WiFi.channel()); } }
void setup() { Serial.begin(115200); if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } display.clearDisplay(); //Serial.println("ESPNow/Basic/Slave Example"); //Set device in AP mode to begin with WiFi.mode(WIFI_AP); // configure device AP mode configDeviceAP(); // This is the mac address of the Slave in AP Mode //Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); // Init ESPNow with a fallback logic InitESPNow(); // Once ESPNow is successfully Init, we will register for recv CB to // get recv packer info. esp_now_register_recv_cb(OnDataRecv); }
// callback when data is recv from Master void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
// Store the received data in the data array for (int i = 0; i < data_len; i++) { dataArray[dataIndex++] = data[i]; if (dataIndex >= MAX_DATA_SIZE) { dataIndex = 0; } }
// Print the received data for (int i = 0; i < data_len; i++) { Serial.print(data[i]); Serial.print(" "); }
Serial.println();
display.write(data, data_len); // Write the received data to the display display.display(); }
void loop() { // Chill }
- Assistant