#include <esp_now.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int a;
float b;
int c;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Int: ");
Serial.println(myData.a);
Serial.print("Float: ");
Serial.println(myData.b);
Serial.print("Int: ");
Serial.println(myData.c);
}
int already0 = 0;
int already1 = 0;
float storeprevtemp = 0;
//This records the low value over the last 24 hrs
float outMin = 0; //outdoor min temp in time interval
float outMax = 0; //outdoor min temp in time interval
unsigned long previousMillis = millis(); // Last time the counter was reset (initialize to present time).
const long interval24 = 86400000; // 24 hour interval in milliseconds.
unsigned long soFar; // Time since the last low reading.
unsigned long time1 = 0;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
lcd.init();
lcd.backlight();
// 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;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
//print current pool temp if value changed
if (myData.b != storeprevtemp){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pool: ");
lcd.print(myData.b);
lcd.print((char)223);
lcd.print("C");
}
//do something if "button pressed" on the transmitter
if (myData.a == 0){ // recognise a "button pressed" on the transmitter
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pressed");
}
//save min and max temps over 24hrs
unsigned long currentMillis = millis(); // Record time now.
if (currentMillis - previousMillis >= interval24 || // Has 24 hours gone by...
myData.b <= outMin || outMin == 0) { // ...or has a new low temperature occurred?
outMin = myData.b; // If so, record the new low temperature and...
previousMillis = currentMillis;
} // ...start the 24 hour counter over.
if (currentMillis - previousMillis >= interval24 || // Has 24 hours gone by...
myData.b >= outMax ) { // ...or has a new low temperature occurred?
outMax = myData.b; // If so, record the new low temperature and...
previousMillis = currentMillis;
} // ...start the 24 hour counter over.
soFar = currentMillis - previousMillis; // Calculate the time since the last reading, in milliseconds.
//print out min and max temps
lcd.setCursor(0,1); // code to print min and max temps over 24 hrs on lcd
lcd.print("L");
lcd.print(outMin);
lcd.print(" H");
lcd.print(outMax);
if(myData.c == 0){
time1 = millis();
}
if (millis() >= time1 + 1200000){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("NO SIGNAL");
lcd.setCursor(0,1);
lcd.print((millis()-time1)/1000);
}
storeprevtemp = myData.b;
Serial.println("---------");
Serial.print("my data a: ");
Serial.println(myData.a);
Serial.print("my data b: ");
Serial.println(myData.b);
Serial.print("my data c: ");
Serial.println(myData.c);
Serial.println("changing my data c to 1");
myData.c = 1;
Serial.println(myData.c);
Serial.println("---------");
//end of loop, delay
delay(1000); // Wait 1 second between readings.
}