prjbrook

SantosMaster6.ino

Aug 28th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. //Master code. For the wemos that's bluetacked to Mancave window that sends out orders etc.
  2. //Master code. Similar to Sensor3 code,but not identical..
  3. /* C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster0\SantosMaster0.ino. Wed Aug 10 13:57:59 NZST 2022.
  4. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster1\SantosMaster1.ino
  5. Wed Aug 10 16:49:19 NZST 2022 **
  6. Going to add more items to sruc including stac[10]Sat Aug 13 17:05:30 NZST 2022
  7. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster3\SantosMaster3.ino
  8. Changing C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster5\SantosMaster5.ino
  9. ..so that the Master sends nothing back to sensor except orders(at this stage
  10. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino .. trying to fix some timing problems, viz response from master too quick, no time for slave-sensor to output Serial.print message. Try adding delay(100)
  11.  
  12. //10
  13. */
  14. #include <ESP8266WiFi.h>
  15. #include <espnow.h>
  16. #include <stdio.h> //for sprintf
  17.  
  18. uint8_t broadcastAddress[] = {0x18,0xFE,0x34,0xF9,0x2E,0x4A}; // Wemos 1
  19. // {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2.
  20. //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1
  21.  
  22. // Define variables to store Master Sensor readings to be sent
  23. float temperature;
  24. float humidity;
  25. unsigned long milliSecsA;
  26. uint8_t orders=0;
  27. uint8_t stack[10];
  28. uint8_t stackPtr;
  29. uint8_t bufferPtr;
  30. uint8_t bigBuffer[200];
  31.  
  32. // Define variables to store incoming readings from Slave
  33. float incomingTemp;
  34. float incomingHum;
  35. unsigned long incomingMillisA;
  36. uint8_t incomingOrders;
  37. uint8_t incomingStac[10];
  38. uint8_t incomingStackPtr;
  39. uint8_t incomingBufferPtr;
  40. uint8_t incomingBigBuffer[200];
  41.  
  42.  
  43. // Updates Sensor readings every 10 seconds
  44. const long interval = 10000;
  45. unsigned long previousMillis = 0; // will store last time Sensor was updated
  46. char myBuf[80]; //for sprintf
  47.  
  48.  
  49. // Variable to store if sending data was successful
  50. String success;
  51. uint8_t incomingPacketArrived=0; //Flag for sensor packet-has-arrived
  52.  
  53. //Structure example to send data
  54. //Must match the receiver structure
  55. typedef struct struct_message {
  56. float temp;
  57. float hum;
  58. unsigned long millisA;
  59. uint8_t ord;
  60. uint8_t stac[10];
  61. uint8_t stacPtr;
  62. uint8_t bufPtr;
  63. uint8_t bigBuf[200];
  64.  
  65.  
  66. } struct_message;
  67.  
  68. // Create a struct_message called SensorReadings to hold readings FROM MASTER
  69. struct_message SensorReadings;
  70.  
  71. // Create a struct_message to hold incoming FROM SENSOR readings
  72. struct_message incomingReadings;
  73.  
  74. // Callback when data is sent
  75. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  76. Serial.print("Last Wemos 2 Packet Send Status: ");
  77. if (sendStatus == 0){
  78. Serial.println("Delivery success");
  79. }
  80. else{
  81. Serial.println("Delivery fail");
  82. }
  83. }
  84.  
  85. // Callback when data is received
  86. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  87. //unsigned long currentMillis = millis();
  88. previousMillis = millis(); //Start timer
  89. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  90. Serial.print("\n\n\nBytes received: ");
  91. Serial.println(len);
  92. incomingTemp = incomingReadings.temp;
  93. incomingHum = incomingReadings.hum;
  94. incomingMillisA= incomingReadings.millisA;
  95. incomingOrders=incomingReadings.ord;
  96. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  97. incomingStackPtr=incomingReadings.stacPtr;
  98. incomingBufferPtr=incomingReadings.bufPtr;
  99. for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  100. //Now all the struct fields from have been put into Master variables ...
  101. //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
  102. incomingPacketArrived=1; //Flag for loop to find
  103. }
  104.  
  105. void getReadings(){
  106. // Read the Master's "sensor data". Fake,right now.
  107. temperature = 992.3 ; //dht.readTemperature();
  108. // Read temperature as Fahrenheit (isFahrenheit = true)
  109. //float t = dht.readTemperature(true);
  110. if (isnan(temperature)){
  111. Serial.println("Failed to read from Master");
  112. temperature = 0.0;
  113. }
  114. humidity = 9945.6; //dht.readHumidity();
  115. if (isnan(humidity)){
  116. Serial.println("Failed to read from Master");
  117. humidity = 0.0;
  118. }
  119. }
  120.  
  121. void printIncomingReadings(){
  122. // Display Readings in Serial Monitor. Nothing so far
  123. Serial.println("INCOMING READINGS");
  124. Serial.print("Temperature: ");
  125. Serial.print(incomingTemp);
  126. Serial.println(" ºC");
  127. Serial.print("Humidity: ");
  128. Serial.print(incomingHum);
  129. Serial.println(" %");
  130. Serial.print("Millies: ");
  131. Serial.println(incomingMillisA);
  132. Serial.print("IncomingOrders: ");
  133. Serial.println(incomingOrders);
  134. Serial.print("IncomingStackPtr: ");
  135. Serial.println(incomingStackPtr);
  136.  
  137. for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
  138. Serial.println(" <--incomingStac[]");
  139. for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
  140. Serial.println(" <--incomingBigBuffer[]");
  141.  
  142.  
  143. /* To make copying easier
  144. float incomingTemp;
  145. float incomingHum;
  146. unsigned long incomingMillisA;
  147. uint8_t incomingOrders;
  148. uint8_t incomingStac[8];
  149. uint8_t incomingStackPtr;
  150. uint8_t incomingBufferPtr;
  151. uint8_t incomingBigBuffer[200]; */
  152.  
  153. }
  154.  
  155. void setup() {
  156. // Init Serial Monitor
  157. Serial.begin(115200);
  158.  
  159.  
  160.  
  161. // Set device as a Wi-Fi Station
  162. WiFi.mode(WIFI_STA);
  163. WiFi.disconnect();
  164.  
  165. // Init ESP-NOW
  166. if (esp_now_init() != 0) {
  167. Serial.println("Error initializing ESP-NOW");
  168. return;
  169. }
  170.  
  171. // Set ESP-NOW Role
  172. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  173.  
  174. // Once ESPNow is successfully Init, we will register for Send CB to
  175. // get the status of Trasnmitted packet
  176. esp_now_register_send_cb(OnDataSent);
  177.  
  178. // Register peer
  179. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  180.  
  181. // Register for a callback function that will be called when data is received
  182. esp_now_register_recv_cb(OnDataRecv);
  183. }
  184.  
  185. void loop() {
  186. //unsigned long currentMillis = millis();
  187. if (incomingPacketArrived==1){
  188. //if (currentMillis - previousMillis >= interval) {
  189. // save the last time you updated the DHT values
  190. //previousMillis = currentMillis; //NB programmable delay.
  191.  
  192. //Get Master's readings
  193. Serial.println("INCOMING packet-has-arrived.***\n\n\n");
  194. getReadings();
  195. orders=112; //replace later with orders via serial input.
  196. //Set values to send
  197. //SensorReadings.temp = temperature;
  198. //SensorReadings.hum = humidity;
  199. SensorReadings.ord = orders; //123; //orders++; Only thing Master changes.
  200.  
  201. // Send message via ESP-NOW
  202. delay(100); //to give SEnsor time to Serial.print its message.
  203. esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
  204.  
  205. // Print incoming readings
  206. printIncomingReadings();
  207. incomingPacketArrived=0; //Push flag back down
  208.  
  209. unsigned long currentMillis = millis();
  210. unsigned long duration = currentMillis-previousMillis;
  211. sprintf(myBuf, "Current millis :%lu Previous Millis: %lu Duration is: %lu",currentMillis,previousMillis, duration);
  212. puts(myBuf);
  213.  
  214.  
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment