Mini proiect (29) - Comunicatie intre doua placi ESP32 folosind ESP-NOW, si afișarea informațiilor pe un display TFT

 Comunicație între două placi ESP32 folosind ESP-NOW, și afișarea informațiilor pe un display TFT

În acest proiect am încercat să programez  doua placi ESP32 pentru a comunica folosind ESP-NOW. Informațiile le-am afișat pe un display TFT de 1.8" 128x160.  Am realizat un proiect concept , care se poate extinde și folosi pentru a conecta la un ESP32 (sender) mai mulți senzori, iar celălalt ESP32 (receiver) v-a procesa informațiile și le va trimite pe internet.

Configurarea cheie a receiverului ar fi apelul funcției WiFi.mode(WIFI_AP_STA), care configurează ESP32-ul ca și "Access Point" și "Station" în același timp.

Componente:

Schema electronica/sistem:

  Conectarea plăcilor și a TFT-ului... m-a chinuit un pic până am găsit conexiunile potrivite...
Comunicatie intre doua placi ESP32 folosind ESP-NOW, si afișarea informațiilor pe un display TFT
Pe display afișez cele trei variabile primite prin intermediul ESP-NOW, rețeaua wifi la care am făcut conectarea, data și ora ,care au fost primite de la serverul ntp europe.pool.ntp.org.

Aici se observă conectarea firelor la TFT.

 Iar aici se observă conectarea firelor la ESP32.

În screenshotul de mai sus am afișat si pe serial schimbul de informații dintre cele două plăci. 

Vă las pe voi să descifrați codul de mai jos. :)

Cod de test pentru ESP32 sender:
/*
Florin Simedru
Complete project details at https://automatic-house.blogspot.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/********************************************************************************************************************
* Preferences--> Aditional boards Manager URLs : https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Board Settings:
* Board: "ESP32 Wrover Module"
* Upload Speed: "921600"
* Flash Frequency: "80MHz"
* Flash Mode: "QIO"
* Partition Scheme: "Hue APP (3MB No OTA/1MB SPIFFS)"
* Core Debug Level: "None"
* COM Port: Depends *On Your System*
*
*/
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Insert your SSID
constexpr char WIFI_SSID[] = "YOURWIFI_SSID";
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// 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");
}
int32_t getWiFiChannel(const char *ssid) {
if (int32_t n = WiFi.scanNetworks()) {
for (uint8_t i=0; i<n; i++) {
if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
return WiFi.channel(i);
}
}
}
return 0;
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
int32_t channel = getWiFiChannel(WIFI_SSID);
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
// 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
esp_now_peer_info_t 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;
}
}
void loop() {
// Set values to send
strcpy(myData.a, "THIS IS A CHAR");
myData.b = random(1,20);
myData.c = 1.2;
myData.d = false;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(2000);
}

Cod de test pentru ESP32 receiver:
/*
Florin Simedru
Complete project details at https://automatic-house.blogspot.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/********************************************************************************************************************
* Preferences--> Aditional boards Manager URLs : https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Board Settings:
* Board: "ESP32 Wrover Module"
* Upload Speed: "921600"
* Flash Frequency: "80MHz"
* Flash Mode: "QIO"
* Partition Scheme: "Hue APP (3MB No OTA/1MB SPIFFS)"
* Core Debug Level: "None"
* COM Port: Depends *On Your System*
*
*/
#include <esp_now.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
const char* ssid = "WIFI_SSID";
const char* password = "password";
// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8" TFT shield
#define TFT_MOSI 23 // SDA Pin on ESP32
#define TFT_SCLK 18 // SCL Pin on ESP32
#define TFT_CS 5 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
#define TFT_RST 4 // Reset pin (could connect to RST pin)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 160 // OLED display height, in pixels
/* TFT ST7735 configuration */
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
String temperature;
String humidity;
String pressure;
unsigned long previousMillis = 0;
const long interval = 5000;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
const char* ntpServer = "europe.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
/* Function to get timeinfo from ntpServer*/
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %y %H:%M:%S");
display.println(&timeinfo, "%a, %b %d %y %H:%M:%S");
}
// callback function that will be executed when data is received from ESP-NOW sender
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Char: ");
Serial.println(myData.a);
Serial.print("Int: ");
Serial.println(myData.b);
Serial.print("Float: ");
Serial.println(myData.c);
Serial.print("Bool: ");
Serial.println(myData.d);
Serial.println();
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station and access point
WiFi.mode(WIFI_AP_STA);
Serial.println(WiFi.macAddress());
Serial.print("Hello! ST7735 TFT Test");
// Use this initializer if you're using a 1.8" TFT
display.initR(INITR_GREENTAB); // Init ST7735 display 128x160 pixel
display.setRotation(3);
display.fillScreen(ST77XX_BLACK);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
// Display connection status
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,10);
display.print("Connecting to ");
display.print(ssid);
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
display.setTextSize(1);
display.setCursor(10, 20);
display.print("Connected to WiFi with IP:");
display.print(WiFi.localIP());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
display.setTextSize(1);
display.setCursor(10, 40);
display.print("Error initializing ESP-NOW ");
display.print("return");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
delay(2000);
}
void PrintInfoServer() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED ){
// copy data received from ESP-NOW
temperature = myData.b;
humidity = myData.c;
pressure = myData.d;
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " % - Pressure: " + pressure + " hPa");
display.fillScreen(ST77XX_BLACK);
// display temperature
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,10);
display.print("T: ");
display.print(temperature);
display.print(" ");
display.setTextSize(1);
display.write(248);
display.setTextSize(1);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(10, 20);
display.print("H: ");
display.print(humidity);
display.print(" %");
// display pressure
display.setTextSize(1);
display.setCursor(10, 30);
display.print("P:");
display.print(pressure);
display.setTextSize(1);
display.setCursor(50, 30);
display.print("hPa");
// display wifi
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,40);
display.print("wifi: ");
display.print(ssid);
display.print(" ");
display.setTextSize(1);
display.setTextColor(ST77XX_GREEN);
display.setCursor(10,50);
printLocalTime();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
void loop() {
display.invertDisplay(false);
PrintInfoServer();
delay(1000);
}

Documentatie proiect:

 

To Do:

  • Să conectez mai mulți senzori 
  • Să conectez un buton și să trimit poziția 
  • Să conectez un senzor PIR la ESP32 sender 

Pentru întrebari și/sau consultanță tehnică vă stau la dispozitie pe blog sau pe email simedruflorin@automatic-house.ro. O seară/zi plăcută tuturor !

Etichete

Afișați mai multe

Arhiva

Afișați mai multe