#include <Arduino.h>
#include <Wire.h>
#include "displays.h"
#include "com.h"
#include "sensors.h"
#include "leds.h"
#include <esp_now.h>
#include <WiFi.h>
void setup() {
// Init Serial Monitor
Serial.begin(115200);
lcd.init();
lcd.backlight();
dht.begin();
// 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 Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, peerMac, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
#ifndef ESP8266
while (!Serial)
; // wait for serial port to connect. Needed for native USB
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
display.setBrightness(0x0f); // Max brightness
display.clear();
}
void loop() {
getReadings();
// Set values to send
timereadings.hour = hour;
timereadings.minute = minute;
timereadings.second = second;
timereadings.year = year;
timereadings.month = month;
timereadings.day = day;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(peerMac, (uint8_t *)&timereadings, sizeof(timereadings));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
updateDisplay();
delay(1000); //change here to decrease time between packages
displayData();
}