prjbrook

HTU_3SantosSensor7.ino Latest. 290822

Aug 28th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 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. Next version uses HTU sensor in subroutine,Thu Aug 18 13:00:29 NZST 2022
  9. C:\Users\Dell\Documents\Arduino\WorkingAugust22\HTU_0SantosSensor7\HTU_0SantosSensor7.ino compiles, but not tested yet. Thu Aug 18 14:10:16 NZST 2022. Now just going to call subroutine
  10. that tests HTU21D
  11. */
  12. #include <ESP8266WiFi.h>
  13. //09
  14. #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
  15. #include <Wire.h>
  16. #include "SparkFunHTU21D.h"
  17.  
  18. //Create an instance of the object
  19. HTU21D myHumidity;
  20.  
  21.  
  22. uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
  23. //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
  24.  
  25. // Define variables to store Sensor readings to be sent to Master
  26. float temperature;
  27. float humidity;
  28. unsigned long milliSecsA;
  29. uint8_t orders;
  30. uint8_t stack[10];
  31. uint8_t stackPtr;
  32. uint8_t bufferPtr;
  33. uint8_t bigBuffer[200];
  34.  
  35. // Define variables to store incoming readings from Master
  36. float incomingTemp;
  37. float incomingHum;
  38. unsigned long incomingMillisA;
  39. uint8_t incomingOrders;
  40. uint8_t incomingStac[10];
  41. uint8_t incomingStackPtr;
  42. uint8_t incomingBufferPtr;
  43. uint8_t incomingBigBuffer[200];
  44.  
  45. // Updates Sensor readings every 10 seconds, 5 secs
  46. const long interval = 5000;
  47. unsigned long previousMillis = 0; // will store last time Sensor was updated
  48. int t=0; //for light blinking
  49.  
  50. // Variable to store if sending data was successful
  51. String success;
  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; //for timing
  59. uint8_t ord;
  60. uint8_t stac[10];
  61. uint8_t stacPtr;
  62. uint8_t bufPtr;
  63. uint8_t bigBuf[200];
  64.  
  65. } struct_message;
  66.  
  67. // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
  68. struct_message SensorReadings;
  69.  
  70. // Create a struct_message to hold incoming master readings
  71. struct_message incomingReadings;
  72.  
  73. // Callback when data is sent
  74. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  75. //delay(100);
  76. Serial.print("\nLast Packet Send Status (From Sensor I: ");
  77. //delay(110); //above delays make sure something gets serial-written
  78. if (sendStatus == 0){
  79. Serial.println("Delivery success");
  80. }
  81. else{
  82. Serial.println("Delivery fail");
  83. }
  84. }
  85.  
  86. // Callback when data is received
  87. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  88. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  89.  
  90. //led on
  91. //digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  92. //delay(100) ;
  93. Serial.print("OnDataRecv() running. Bytes received: ");
  94. Serial.println(len);
  95. incomingTemp = incomingReadings.temp;
  96. incomingHum = incomingReadings.hum;
  97. incomingMillisA= incomingReadings.millisA;
  98. incomingOrders=incomingReadings.ord;
  99. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  100. incomingStackPtr=incomingReadings.stacPtr;
  101. incomingBufferPtr=incomingReadings.bufPtr;
  102. for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  103. Serial.println("OnDataRecv() finished");
  104. //led off
  105. //digitalWrite(LED_BUILTIN, LOW); delay(100);
  106. //Now all the struct fields from have been put into slave sensor variables ...
  107. //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
  108. }
  109.  
  110. void getReadings(){
  111. // Read the sender's sensor data. Read Temperature. Fake,right now.
  112. Serial.println("\n now doing getReadings()))))))))))))))))");
  113. temperature = 1112.3 ; //dht.readTemperature();
  114. // Read temperature as Fahrenheit (isFahrenheit = true)
  115. //float t = dht.readTemperature(true);
  116. if (isnan(temperature)){
  117. Serial.println("Failed to read from Sensor");
  118. temperature = 0.0;
  119. }
  120. humidity = 1145.6; //dht.readHumidity();
  121. if (isnan(humidity)){
  122. Serial.println("Failed to read from Sensor");
  123. humidity = 0.0;
  124. }
  125. }
  126.  
  127. void printIncomingReadings(){
  128. // Display Readings in Serial Monitor. Nothing so far
  129. Serial.println("\nINCOMING READINGS from Master ----------------------");
  130. Serial.print("Temperature: ");
  131. Serial.print(incomingTemp);
  132. Serial.println(" ºC");
  133. Serial.print("Humidity: ");
  134. Serial.print(incomingHum);
  135. Serial.println(" %");
  136. Serial.print(incomingMillisA);
  137. Serial.println(" ..m..");
  138. Serial.print(incomingOrders);
  139. Serial.println(" incomingOrders..");
  140. Serial.print(incomingStackPtr);
  141. Serial.println("incomingStackPtr");
  142. for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
  143. Serial.println(" <--incomingStac[]");
  144. for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
  145. Serial.println(" <--incomingBigBuffer[]");
  146. }
  147.  
  148. void setup() {
  149. // Init Serial Monitor
  150. Serial.begin(115200);
  151. myHumidity.begin();
  152. pinMode(LED_BUILTIN, OUTPUT);
  153.  
  154.  
  155.  
  156.  
  157.  
  158. // Set device as a Wi-Fi Station
  159. WiFi.mode(WIFI_STA);
  160. WiFi.disconnect();
  161.  
  162. // Init ESP-NOW
  163. if (esp_now_init() != 0) {
  164. Serial.println("Error initializing ESP-NOW");
  165. return;
  166. }
  167.  
  168. // Set ESP-NOW Role
  169. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  170.  
  171. // Once ESPNow is successfully Init, we will register for Send CB to
  172. // get the status of Trasnmitted packet
  173. esp_now_register_send_cb(OnDataSent);
  174.  
  175. // Register peer
  176. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  177.  
  178. // Register for a callback function that will be called when data is received
  179. esp_now_register_recv_cb(OnDataRecv);
  180. }
  181.  
  182. void loop() {
  183.  
  184. //loopLED();
  185. //getHDU();
  186. unsigned long currentMillis = millis();
  187. if (currentMillis - previousMillis >= interval) {
  188. // save the last time you updated the DHT values
  189. previousMillis = currentMillis; //NB programmable delay.
  190. doLED();
  191. //Get Sensor 1 readings
  192. //getReadings();
  193. getReadings2(); //read the Sensor
  194. //Set values to send
  195. SensorReadings.temp = temperature;
  196. SensorReadings.hum = humidity;
  197. SensorReadings.millisA=millis();
  198.  
  199. // Send message via ESP-NOW
  200. esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
  201. //delay(100);
  202. // Print incoming readings
  203. printIncomingReadings();
  204. Serial.print(" ================Orders0= ");
  205. Serial.println( incomingReadings.ord);
  206. }
  207. }
  208.  
  209. void getHDU() { //read via HDU21 the temp and humidity
  210. while(1)
  211. {
  212.  
  213. Serial.println("Nothing HDUish here yet.");
  214. float humd = myHumidity.readHumidity();
  215. float temp = myHumidity.readTemperature();
  216.  
  217. Serial.print("Time:");
  218. Serial.print(millis());
  219. Serial.print(" Temperature:");
  220. Serial.print(temp, 1);
  221. Serial.print("C");
  222. Serial.print(" Humidity*:");
  223. Serial.print(humd, 1);
  224. Serial.print("%");
  225.  
  226. Serial.println();
  227. delay(1000);
  228. }
  229.  
  230. }
  231.  
  232.  
  233. void getReadings2(){
  234. //Replaces fake readings with real ones from the breadboad of Wemos Mini 1
  235. // float humd = myHumidity.readHumidity();
  236. //float temp = myHumidity.readTemperature(); //readTemperature();
  237. temperature = myHumidity.readTemperature(); //readHumidity(); //both temperature and humidity are global floats
  238. humidity= myHumidity.readHumidity(); //readTemperature();
  239.  
  240. Serial.print("Time:");
  241. Serial.print(millis());
  242. Serial.print(" Temperature:");
  243. Serial.print(temperature, 1);
  244. Serial.print("C");
  245. Serial.print(" humidity:");
  246. Serial.print(humidity, 1);
  247. Serial.print("%");
  248.  
  249. Serial.println();
  250. }
  251.  
  252.  
  253. void doLED() {
  254. t++; if(t==2) t=0;
  255. // Serial.print("--------t=");
  256. Serial.println(t);
  257. //Serial.print(LED_BUILTIN);
  258.  
  259. if (t==1) digitalWrite(LED_BUILTIN, HIGH);
  260. if (t==0) digitalWrite(LED_BUILTIN, LOW);
  261. }
  262. /*
  263. void setup() {
  264. // initialize digital pin LED_BUILTIN as an output.
  265. pinMode(LED_BUILTIN, OUTPUT);
  266. }
  267.  
  268. // the loop function runs over and over again forever
  269. void loopLED() {
  270. while(1)
  271. {
  272. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  273. delay(100); // wait for a second
  274. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  275. delay(1000); // wait for a second
  276. }
  277. }
  278. */
  279.  
Advertisement
Add Comment
Please, Sign In to add comment