prjbrook

Santos_1

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