prjbrook

SantosSensor7.ino REceiver in WemosII

Oct 2nd, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. //Sender,Sensor code. Similar to Master, Receiver but not identical.,
  2. //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantoSensor_2.ino\SantoSensor_2.ino.ino
  3. //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor3\SantosSensor3.ino, Wed Aug 10 13:32:09 NZST 2022.
  4. //Compiled above and ran but had not plugged in Master Wemos with slightly different code.
  5. /* SantosSensor4.ino Wed Aug 10 16:38:10 NZST 2022. Making strucs better. Representing Master/Slave idea
  6. SantosSensor4.ino
  7. Tue Aug 16 12:05:02 NZST 2022 Include new struc variables to make it compatible with SantosMaster3.ino C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor7\SantosSensor7.ino */
  8. #include <ESP8266WiFi.h>
  9. //9
  10. #include <espnow.h> #include <Adafruit_Sensor.h>
  11.  
  12. uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
  13. //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
  14.  
  15. // Define variables to store Sensor readings to be sent to Master
  16. float temperature;
  17. float humidity;
  18. unsigned long milliSecsA;
  19. uint8_t orders;
  20. uint8_t stack[10];
  21. uint8_t stackPtr;
  22. uint8_t bufferPtr;
  23. uint8_t bigBuffer[200];
  24.  
  25. // Define variables to store incoming readings from Master
  26. float incomingTemp;
  27. float incomingHum;
  28. unsigned long incomingMillisA;
  29. uint8_t incomingOrders;
  30. uint8_t incomingStac[10];
  31. uint8_t incomingStackPtr;
  32. uint8_t incomingBufferPtr;
  33. uint8_t incomingBigBuffer[200];
  34.  
  35. // Updates Sensor readings every 10 seconds
  36. const long interval = 10000;
  37. unsigned long previousMillis = 0; // will store last time Sensor was updated
  38.  
  39. // Variable to store if sending data was successful
  40. String success;
  41.  
  42. //Structure example to send data
  43. //Must match the receiver structure
  44. typedef struct struct_message {
  45. float temp;
  46. float hum;
  47. unsigned long millisA; //for timing
  48. uint8_t ord;
  49. uint8_t stac[10];
  50. uint8_t stacPtr;
  51. uint8_t bufPtr;
  52. uint8_t bigBuf[200];
  53.  
  54. } struct_message;
  55.  
  56. // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
  57. struct_message SensorReadings;
  58.  
  59. // Create a struct_message to hold incoming master readings
  60. struct_message incomingReadings;
  61.  
  62. // Callback when data is sent
  63. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  64. Serial.print("Last Packet Send Status (From Sensor I: ");
  65. if (sendStatus == 0){
  66. Serial.println("Delivery success");
  67. }
  68. else{
  69. Serial.println("Delivery fail");
  70. }
  71. }
  72.  
  73. // Callback when data is received
  74. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  75. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  76. Serial.print("OnDataRecv() running. Bytes received: ");
  77. Serial.println(len);
  78. incomingTemp = incomingReadings.temp;
  79. incomingHum = incomingReadings.hum;
  80. incomingMillisA= incomingReadings.millisA;
  81. incomingOrders=incomingReadings.ord;
  82. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  83. incomingStackPtr=incomingReadings.stacPtr;
  84. incomingBufferPtr=incomingReadings.bufPtr;
  85. for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  86. Serial.println("OnDataRecv() finished");
  87. //Now all the struct fields from have been put into slave sensor variables ...
  88. //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
  89. }
  90.  
  91. void getReadings(){
  92. // Read the sender's sensor data. Read Temperature. Fake,right now.
  93. Serial.println("\n now doing getReadings()))))))))))))))))");
  94. temperature = 1112.3 ; //dht.readTemperature();
  95. // Read temperature as Fahrenheit (isFahrenheit = true)
  96. //float t = dht.readTemperature(true);
  97. if (isnan(temperature)){
  98. Serial.println("Failed to read from Sensor");
  99. temperature = 0.0;
  100. }
  101. humidity = 1145.6; //dht.readHumidity();
  102. if (isnan(humidity)){
  103. Serial.println("Failed to read from Sensor");
  104. humidity = 0.0;
  105. }
  106. }
  107.  
  108. void printIncomingReadings(){
  109. // Display Readings in Serial Monitor. Nothing so far
  110. Serial.println("\nINCOMING READINGS from Master ----------------------");
  111. Serial.print("Temperature: ");
  112. Serial.print(incomingTemp);
  113. Serial.println(" ºC");
  114. Serial.print("Humidity: ");
  115. Serial.print(incomingHum);
  116. Serial.println(" %");
  117. Serial.print(incomingMillisA);
  118. Serial.println(" ..m..");
  119. Serial.print(incomingOrders);
  120. Serial.println(" incomingOrders..");
  121. Serial.print(incomingStackPtr);
  122. Serial.println("incomingStackPtr");
  123. for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
  124. Serial.println(" <--incomingStac[]");
  125. for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
  126. Serial.println(" <--incomingBigBuffer[]");
  127. }
  128.  
  129. void setup() {
  130. // Init Serial Monitor
  131. Serial.begin(115200);
  132.  
  133.  
  134.  
  135. // Set device as a Wi-Fi Station
  136. WiFi.mode(WIFI_STA);
  137. WiFi.disconnect();
  138.  
  139. // Init ESP-NOW
  140. if (esp_now_init() != 0) {
  141. Serial.println("Error initializing ESP-NOW");
  142. return;
  143. }
  144.  
  145. // Set ESP-NOW Role
  146. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  147.  
  148. // Once ESPNow is successfully Init, we will register for Send CB to
  149. // get the status of Trasnmitted packet
  150. esp_now_register_send_cb(OnDataSent);
  151.  
  152. // Register peer
  153. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  154.  
  155. // Register for a callback function that will be called when data is received
  156. esp_now_register_recv_cb(OnDataRecv);
  157. }
  158.  
  159. void loop() {
  160. unsigned long currentMillis = millis();
  161. if (currentMillis - previousMillis >= interval) {
  162. // save the last time you updated the DHT values
  163. previousMillis = currentMillis; //NB programmable delay.
  164.  
  165. //Get Sensor 1 readings
  166. getReadings();
  167.  
  168. //Set values to send
  169. SensorReadings.temp = temperature;
  170. SensorReadings.hum = humidity;
  171. SensorReadings.millisA=millis();
  172.  
  173. // Send message via ESP-NOW
  174. esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
  175.  
  176. // Print incoming readings
  177. printIncomingReadings();
  178. Serial.print(" ================Orders= ");
  179. Serial.println( incomingReadings.ord);
  180. }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment