EMB_84
EMBEDDED 84 • MAGGIO • 2022 57 DATA TRANSMISSION | SOFTWARE /* Rui Santos Complete project details at https://RandomNerdTutorials. com/esp-now-two-way-communication-esp8266-nodemcu/ Permission is hereby granted, free of charge, to any per- son 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. */ #include <ESP8266WiFi.h> #include <espnow.h> #include <Adafruit_Sensor.h> #include <DHT.h> // REPLACE WITH THE MAC Address of your receiver uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Digital pin connected to the DHT sensor #define DHTPIN 5 // Uncomment the type of sensor in use: //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); // Define variables to store DHT readings to be sent float temperature; float humidity; // Define variables to store incoming readings float incomingTemp; float incomingHum; // Updates DHT readings every 10 seconds const long interval = 10000; unsigned long previousMillis = 0; // will store last time DHT was updated // 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; float hum; } struct_message; // Create a struct_message called DHTReadings to hold sensor readings struct_message DHTReadings; // Create a struct_message to hold incoming sensor readin- gs struct_message incomingReadings; // Callback when data is sent void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { Serial.print(“Last Packet Send Status: “); if (sendStatus == 0){ Serial.println(“Delivery success”); } else{ Serial.println(“Delivery fail”); } } // Callback when data is received void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) { memcpy(&incomingReadings, incomingData, sizeof(inco- mingReadings)); Serial.print(“Bytes received: “); Serial.println(len); incomingTemp = incomingReadings.temp; incomingHum = incomingReadings.hum; } Codice che realizza una comunicazione bidirezionale tra i due ESP void getReadings(){ // Read Temperature temperature = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) //float t = dht.readTemperature(true); if (isnan(temperature)){ Serial.println(“Failed to read from DHT”); temperature = 0.0; } humidity = dht.readHumidity(); if (isnan(humidity)){ Serial.println(“Failed to read from DHT”); humidity = 0.0; } } void printIncomingReadings(){ // Display Readings in Serial Monitor Serial.println(“INCOMING READINGS”); Serial.print(“Temperature: “); Serial.print(incomingTemp); Serial.println(“ ºC”); Serial.print(“Humidity: “); Serial.print(incomingHum); Serial.println(“ %”); } void setup() { // Init Serial Monitor Serial.begin(115200); // Init DHT sensor dht.begin(); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); WiFi.disconnect(); // Init ESP-NOW if (esp_now_init() != 0) { Serial.println(“Error initializing ESP-NOW”); return; } // Set ESP-NOW Role esp_now_set_self_role(ESP_NOW_ROLE_COMBO); // 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_add_peer(broadcastAddress, ESP_NOW_ROLE_ COMBO, 1, NULL, 0); // Register for a callback function that will be called when data is received esp_now_register_recv_cb(OnDataRecv); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you updated the DHT values previousMillis = currentMillis; //Get DHT readings getReadings(); //Set values to send DHTReadings.temp = temperature; DHTReadings.hum = humidity; // Send message via ESP-NOW esp_now_send(broadcastAddress, (uint8_t *) &DHTRea- dings, sizeof(DHTReadings)); // Print incoming readings printIncomingReadings(); } }
Made with FlippingBook
RkJQdWJsaXNoZXIy Mzg4NjYz