#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <esp_now.h>
#include <WiFi.h>
const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define SCREEN_ADDRESS 0x3C
// REPLACE WITH THE MAC Address of your sender
uint8_t senderAddress[] = {0x24, 0x0A, 0xC4, 0x00, 0x01, 0x10};
typedef struct struct_message
{
float waterLevel;
} struct_message;
struct_message waterLevelReading;
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
memcpy(&waterLevelReading, incomingData, sizeof(waterLevelReading));
}
void setup()
{
Serial.begin(115200);
// Init OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(2000);
display.clearDisplay();
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback for receiving data
esp_now_register_recv_cb(OnDataRecv);
// Add peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, senderAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
}
void loop()
{
// Display water level on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Water Level: ");
display.print(waterLevelReading.waterLevel);
display.display();
delay(5000); // Adjust the delay as needed
}