prjbrook

Santos2_1, ESPnow exchange, worked

Jul 31st, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. //Santos2, for ESP Wemos mini 2. program. In Notion.
  2. //First get rid of DHT stuff
  3. //Wemos Min1 2, sender, currently C0M11
  4. #include <ESP8266WiFi.h>
  5. #include <espnow.h>
  6. // #include <Adafruit_Sensor.h>
  7. // #include <DHT.h>
  8. // REPLACE WITH THE MAC Address of your receiver
  9. uint8_t broadcastAddress[] = {0x18,0xFE,0x34,0xF9,0x2E,0x4A} ;
  10. //{0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  11.  
  12. // Digital pin connected to the DHT sensor
  13. // #define DHTPIN 5 // Uncomment the type of sensor in use:
  14. //#define DHTTYPE DHT11 // DHT 11
  15. // #define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)
  16.  
  17. // DHT dht(DHTPIN, DHTTYPE);
  18.  
  19. // Define variables to store DHT readings to be sent
  20. float temperature;
  21. float humidity;
  22.  
  23. // Define variables to store incoming readings
  24. float incomingTemp;
  25. float incomingHum;
  26.  
  27. // Updates DHT readings every 10 seconds
  28. const long interval = 10000;
  29. unsigned long previousMillis = 0; // will store last time DHT was updated
  30.  
  31. // Variable to store if sending data was successful
  32. String success;
  33.  
  34. //Structure example to send data
  35. //Must match the receiver structure
  36. typedef struct struct_message {
  37. float temp;
  38. float hum;
  39. } struct_message;
  40.  
  41. // Create a struct_message called DHTReadings to hold sensor readings
  42. struct_message DHTReadings;
  43.  
  44. // Create a struct_message to hold incoming sensor readings
  45. struct_message incomingReadings;
  46.  
  47. // Callback when data is sent
  48. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  49. Serial.print("Last Packet Send Status: ");
  50. if (sendStatus == 0){
  51. Serial.println("Delivery success");
  52. }
  53. else{
  54. Serial.println("Delivery fail");
  55. }
  56. }
  57.  
  58. // Callback when data is received
  59. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  60. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  61. Serial.print("Bytes received: ");
  62. Serial.println(len);
  63. incomingTemp = incomingReadings.temp;
  64. incomingHum = incomingReadings.hum;
  65. }
  66.  
  67. void getReadings(){
  68. // Read Temperature
  69. temperature = 1112.3 ; //dht.readTemperature();
  70. // Read temperature as Fahrenheit (isFahrenheit = true)
  71. //float t = dht.readTemperature(true);
  72. if (isnan(temperature)){
  73. Serial.println("Failed to read from DHT");
  74. temperature = 0.0;
  75. }
  76. humidity = 1145.6; //dht.readHumidity();
  77. if (isnan(humidity)){
  78. Serial.println("Failed to read from DHT");
  79. humidity = 0.0;
  80. }
  81. }
  82.  
  83. void printIncomingReadings(){
  84. // Display Readings in Serial Monitor
  85. Serial.println("INCOMING READINGS");
  86. Serial.print("Temperature: ");
  87. Serial.print(incomingTemp);
  88. Serial.println(" ºC");
  89. Serial.print("Humidity: ");
  90. Serial.print(incomingHum);
  91. Serial.println(" %");
  92. }
  93.  
  94. void setup() {
  95. // Init Serial Monitor
  96. Serial.begin(115200);
  97.  
  98. // Init DHT sensor
  99. // dht.begin(); //==
  100.  
  101. // Set device as a Wi-Fi Station
  102. WiFi.mode(WIFI_STA);
  103. WiFi.disconnect();
  104.  
  105. // Init ESP-NOW
  106. if (esp_now_init() != 0) {
  107. Serial.println("Error initializing ESP-NOW");
  108. return;
  109. }
  110.  
  111. // Set ESP-NOW Role
  112. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  113.  
  114. // Once ESPNow is successfully Init, we will register for Send CB to
  115. // get the status of Trasnmitted packet
  116. esp_now_register_send_cb(OnDataSent);
  117.  
  118. // Register peer
  119. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  120.  
  121. // Register for a callback function that will be called when data is received
  122. esp_now_register_recv_cb(OnDataRecv);
  123. }
  124.  
  125. void loop() {
  126. unsigned long currentMillis = millis();
  127. if (currentMillis - previousMillis >= interval) {
  128. // save the last time you updated the DHT values
  129. previousMillis = currentMillis;
  130.  
  131. //Get DHT readings
  132. getReadings();
  133.  
  134. //Set values to send
  135. DHTReadings.temp = temperature;
  136. DHTReadings.hum = humidity;
  137.  
  138. // Send message via ESP-NOW
  139. esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));
  140.  
  141. // Print incoming readings
  142. printIncomingReadings();
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment