prjbrook

HTU_0SantosSensor7.ino Best so far

Aug 28th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 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
  10. */
  11. #include <ESP8266WiFi.h>
  12. //03
  13. #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
  14. #include <Wire.h>
  15. #include "SparkFunHTU21D.h"
  16.  
  17. //Create an instance of the object
  18. HTU21D myHumidity;
  19.  
  20.  
  21. uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
  22. //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
  23.  
  24. // Define variables to store Sensor readings to be sent to Master
  25. float temperature;
  26. float humidity;
  27. unsigned long milliSecsA;
  28. uint8_t orders;
  29. uint8_t stack[10];
  30. uint8_t stackPtr;
  31. uint8_t bufferPtr;
  32. uint8_t bigBuffer[200];
  33.  
  34. // Define variables to store incoming readings from Master
  35. float incomingTemp;
  36. float incomingHum;
  37. unsigned long incomingMillisA;
  38. uint8_t incomingOrders;
  39. uint8_t incomingStac[10];
  40. uint8_t incomingStackPtr;
  41. uint8_t incomingBufferPtr;
  42. uint8_t incomingBigBuffer[200];
  43.  
  44. // Updates Sensor readings every 10 seconds
  45. const long interval = 10000;
  46. unsigned long previousMillis = 0; // will store last time Sensor was updated
  47.  
  48. // Variable to store if sending data was successful
  49. String success;
  50.  
  51. //Structure example to send data
  52. //Must match the receiver structure
  53. typedef struct struct_message {
  54. float temp;
  55. float hum;
  56. unsigned long millisA; //for timing
  57. uint8_t ord;
  58. uint8_t stac[10];
  59. uint8_t stacPtr;
  60. uint8_t bufPtr;
  61. uint8_t bigBuf[200];
  62.  
  63. } struct_message;
  64.  
  65. // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
  66. struct_message SensorReadings;
  67.  
  68. // Create a struct_message to hold incoming master readings
  69. struct_message incomingReadings;
  70.  
  71. // Callback when data is sent
  72. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  73. Serial.print("Last Packet Send Status (From Sensor I: ");
  74. if (sendStatus == 0){
  75. Serial.println("Delivery success");
  76. }
  77. else{
  78. Serial.println("Delivery fail");
  79. }
  80. }
  81.  
  82. // Callback when data is received
  83. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  84. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  85. Serial.print("OnDataRecv() running. Bytes received: ");
  86. Serial.println(len);
  87. incomingTemp = incomingReadings.temp;
  88. incomingHum = incomingReadings.hum;
  89. incomingMillisA= incomingReadings.millisA;
  90. incomingOrders=incomingReadings.ord;
  91. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  92. incomingStackPtr=incomingReadings.stacPtr;
  93. incomingBufferPtr=incomingReadings.bufPtr;
  94. for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  95. Serial.println("OnDataRecv() finished");
  96. //Now all the struct fields from have been put into slave sensor variables ...
  97. //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
  98. }
  99.  
  100. void getReadings(){
  101. // Read the sender's sensor data. Read Temperature. Fake,right now.
  102. Serial.println("\n now doing getReadings()))))))))))))))))");
  103. temperature = 1112.3 ; //dht.readTemperature();
  104. // Read temperature as Fahrenheit (isFahrenheit = true)
  105. //float t = dht.readTemperature(true);
  106. if (isnan(temperature)){
  107. Serial.println("Failed to read from Sensor");
  108. temperature = 0.0;
  109. }
  110. humidity = 1145.6; //dht.readHumidity();
  111. if (isnan(humidity)){
  112. Serial.println("Failed to read from Sensor");
  113. humidity = 0.0;
  114. }
  115. }
  116.  
  117. void printIncomingReadings(){
  118. // Display Readings in Serial Monitor. Nothing so far
  119. Serial.println("\nINCOMING READINGS from Master ----------------------");
  120. Serial.print("Temperature: ");
  121. Serial.print(incomingTemp);
  122. Serial.println(" ºC");
  123. Serial.print("Humidity: ");
  124. Serial.print(incomingHum);
  125. Serial.println(" %");
  126. Serial.print(incomingMillisA);
  127. Serial.println(" ..m..");
  128. Serial.print(incomingOrders);
  129. Serial.println(" incomingOrders..");
  130. Serial.print(incomingStackPtr);
  131. Serial.println("incomingStackPtr");
  132. for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
  133. Serial.println(" <--incomingStac[]");
  134. for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
  135. Serial.println(" <--incomingBigBuffer[]");
  136. }
  137.  
  138. void setup() {
  139. // Init Serial Monitor
  140. Serial.begin(115200);
  141. myHumidity.begin();
  142.  
  143.  
  144.  
  145.  
  146.  
  147. // Set device as a Wi-Fi Station
  148. WiFi.mode(WIFI_STA);
  149. WiFi.disconnect();
  150.  
  151. // Init ESP-NOW
  152. if (esp_now_init() != 0) {
  153. Serial.println("Error initializing ESP-NOW");
  154. return;
  155. }
  156.  
  157. // Set ESP-NOW Role
  158. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  159.  
  160. // Once ESPNow is successfully Init, we will register for Send CB to
  161. // get the status of Trasnmitted packet
  162. esp_now_register_send_cb(OnDataSent);
  163.  
  164. // Register peer
  165. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  166.  
  167. // Register for a callback function that will be called when data is received
  168. esp_now_register_recv_cb(OnDataRecv);
  169. }
  170.  
  171. void loop() {
  172. unsigned long currentMillis = millis();
  173. if (currentMillis - previousMillis >= interval) {
  174. // save the last time you updated the DHT values
  175. previousMillis = currentMillis; //NB programmable delay.
  176.  
  177. //Get Sensor 1 readings
  178. getReadings();
  179.  
  180. //Set values to send
  181. SensorReadings.temp = temperature;
  182. SensorReadings.hum = humidity;
  183. SensorReadings.millisA=millis();
  184.  
  185. // Send message via ESP-NOW
  186. esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
  187.  
  188. // Print incoming readings
  189. printIncomingReadings();
  190. Serial.print(" ================Orders= ");
  191. Serial.println( incomingReadings.ord);
  192. }
  193. }
  194.  
  195. void getHDU() { //read via HDU21 the temp and humidity
  196. Serial.println("Nothing HDUish here yet.");
  197. float humd = myHumidity.readHumidity();
  198. float temp = myHumidity.readTemperature();
  199.  
  200. Serial.print("Time:");
  201. Serial.print(millis());
  202. Serial.print(" Temperature:");
  203. Serial.print(temp, 1);
  204. Serial.print("C");
  205. Serial.print(" Humidity:");
  206. Serial.print(humd, 1);
  207. Serial.print("%");
  208.  
  209. Serial.println();
  210. delay(1000);
  211. }
  212. /*
  213. HTU21D Humidity Sensor Example Code
  214. By: Nathan Seidle
  215. SparkFun Electronics
  216. Date: September 15th, 2013
  217. License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
  218.  
  219. Uses the HTU21D library to display the current humidity and temperature
  220.  
  221. Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
  222.  
  223. Hardware Connections (Breakoutboard to Arduino):
  224. -VCC = 3.3V
  225. -GND = GND
  226. -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
  227. -SCL = A5 (use inline 330 ohm resistor if your board is 5V)
  228.  
  229. */
  230. /*
  231. #include <Wire.h>
  232. #include "SparkFunHTU21D.h"
  233.  
  234. //Create an instance of the object
  235. HTU21D myHumidity;
  236.  
  237. void setup()
  238. {
  239. Serial.begin(9600);
  240. Serial.println("HTU21D Example!");
  241.  
  242. myHumidity.begin();
  243. }
  244.  
  245. void loop()
  246. {
  247. float humd = myHumidity.readHumidity();
  248. float temp = myHumidity.readTemperature();
  249.  
  250. Serial.print("Time:");
  251. Serial.print(millis());
  252. Serial.print(" Temperature:");
  253. Serial.print(temp, 1);
  254. Serial.print("C");
  255. Serial.print(" Humidity:");
  256. Serial.print(humd, 1);
  257. Serial.print("%");
  258.  
  259. Serial.println();
  260. delay(1000);
  261.  
  262. */
  263.  
  264. /***************************************************
  265. This is an example for the HTU21D-F Humidity & Temp Sensor
  266.  
  267. Designed specifically to work with the HTU21D-F sensor from Adafruit
  268. ----> https://www.adafruit.com/products/1899
  269.  
  270. These displays use I2C to communicate, 2 pins are required to
  271. interface
  272. ****************************************************/
  273. /*
  274. #include <Wire.h>
  275. #include "Adafruit_HTU21DF.h"
  276.  
  277. // Connect Vin to 3-5VDC
  278. // Connect GND to ground
  279. // Connect SCL to I2C clock pin (A5 on UNO)
  280. // Connect SDA to I2C data pin (A4 on UNO)
  281.  
  282. Adafruit_HTU21DF htu = Adafruit_HTU21DF();
  283.  
  284. void setup() {
  285. Serial.begin(9600);
  286. Serial.println("HTU21D-F test");
  287.  
  288. if (!htu.begin()) {
  289. Serial.println("Couldn't find sensor!");
  290. while (1);
  291. }
  292. }
  293.  
  294. void loop() {
  295. float temp = htu.readTemperature();
  296. float rel_hum = htu.readHumidity();
  297. Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
  298. Serial.print("\t\t");
  299. Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
  300. delay(500);
  301. }
  302. */
Advertisement
Add Comment
Please, Sign In to add comment