#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <Fonts/FreeMonoBoldOblique12pt7b.h> // Font
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <esp_now.h>
#include <Adafruit_SSD1306.h>
#define TFT_CS 17
#define TFT_RST 16
#define TFT_DC 4
// For 1.14", 1.3", 1.54", 1.69", and 2.0" TFT with ST7789:
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//Code to set up receiving the string from Nina's ESP
uint8_t broadcastAddress[] = {0xC8, 0xF0, 0x9E, 0x47, 0xDF, 0x68};
// Define variables to store incoming readings
float incomingTemp;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp;
} struct_message;
struct_message incomingReadings;
esp_now_peer_info_t peerInfo;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status == 0) {
success = "Delivery Success :)";
}
else {
success = "Delivery Fail :(";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
}
void setup() {
// LED Screen stuff
Serial.begin(115200);
Serial.print(F("Hello! ST77xx TFT Test"));
// Use this initializer if using a 1.14" 240x135 TFT:
// tft.init(135, 240); // Init ST7789 240x135
// SPI speed defaults to SPI_DEFAULT_FREQ defined in the library, you can override it here
// Note that speed allowable depends on chip and quality of wiring, if you go too fast, you
// may end up with a black screen some times, or all the time.
//tft.setSPISpeed(40000000);
// Serial.println(F("Initialized"));
// uint16_t time = millis();
// tft.fillScreen(ST77XX_BLACK);
// time = millis() - time;
// WiFi stuff
WiFi.mode(WIFI_STA);
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
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, broadcastAddress, 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(OnDataRecv);
}
void loop() {
Serial.println(incomingTemp); //Print fact from Nina to serial monitor
delay(1000);
/* tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 75);
tft.setTextWrap(true);
tft.setRotation(1);
tft.setTextColor(ST77XX_BLUE);
tft.setFont(&FreeMonoBoldOblique12pt7b);
tft.setTextSize(1);
tft.println(incomingFact);*/
}
/*
//Format and print fact to LCD display
void setRotation(uint8_t 3);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 75);
tft.setTextWrap(true);
tft.setTextColor(ST77XX_BLUE);
tft.setFont(&FreeMonoBoldOblique12pt7b);
tft.setTextSize(2);
tft.println(incomingFact);
*/
/*
const int buttonPin = 18;
pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
send signal to the other ESP32
} else {
stop sending signal to other ESP32
}
*/