prjbrook

SpudSensor94.ino Sends data to WemosII

Sep 17th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.23 KB | None | 0 0
  1.  
  2. //New direction. Add littleFS to HTU_0SantosSensor7.ino .Now SpudSensor Mon Aug 29 14:44:53 NZST 2022
  3. /*
  4. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor0\SpudSensor0.ino works ok by sending
  5. good temp and humid data from HDU21 to C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino using Wemos 1
  6. ..to WemosII
  7. Going to adorn the HTU_0SantosSensor7.ino so that it's LittleFS intelligent. Bit by bit. Also cleaning up old code and comments to get SpudSensor as strong and sparse as possible. Also want to try out #include files for some parts of the code that get in the way,
  8. The include file,tryme works. Now try to fill it useful LittleFS stuff
  9. Tidied up some of below. Put LittleFs into next version.
  10. Working: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor3\SpudSensor3.ino
  11. Now using C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor5\SpudSensor5.ino
  12. Going to add sampling string stuff to fill up struc to espnow send
  13. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor5\SpudSensor5.ino Works ok
  14. ..above works with boss: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino
  15. Sun Sep 4 11:47:30 NZST 2022. Now starting on ..C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor6\SpudSensor6.ino
  16. Now going to add lots of small routines to take readings and send them to boss and littlefs logfile. Mon Sep 5 14:46:38 NZST 2022. File is :
  17. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor7\SpudSensor7.ino and talks to ...master6
  18. Spud..7 worked out with a lot of cleanup and several new buffer[] helpers
  19. Nw going to work on: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor8\SpudSensor8.ino
  20. same Boss: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino
  21. Going to add more routines to get sensor readings into litleFs and boss side.
  22. Some buffer problems in previous version. Have gone away? Now going to tidy up code and try to use ic files
  23. ...to help witj comprehension. Haven't done much with boss side yet.
  24. Now onto C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor91\SpudSensor91.ino..still tidying up look of terminal output to hel with debugging. Fri Sep 9 12:02:24 NZST 2022. Also, take out stringplay. Now onto C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor91\SpudSensor91.ino at Sat Sep 10 13:16:48 NZST 2022. Getting strange millis times for packets coming and going. Will get exact millis time by writing it down during callbacks.
  25. Next, just tidying up. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor93\SpudSensor93.ino Mon Sep 12 12:16:30 NZST 2022 putting in fixFullBuffer() to reset buffer when no room to fit more readings. Other little tidy ups. Going well.
  26. Decided to time main loop so I can see when sensing of temperature etc starts and stops as well as espNow stuff
  27. Thu Sep 15 12:29:29 NZST 2022, C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor94\SpudSensor94.ino
  28.  
  29. */
  30. //28
  31. #include <FS.h>
  32. #include <LittleFS.h>
  33. #include <time.h>
  34. #include <ESP8266WiFi.h>
  35. #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
  36. #include <Wire.h>
  37. #include "SparkFunHTU21D.h"
  38.  
  39. //#include "C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor1\tryme.txt" //
  40. #include "helperCode.h"
  41. #include "globals.h"
  42.  
  43.  
  44.  
  45. HTU21D myHumidity;
  46.  
  47.  
  48. // Callback when data is sent
  49. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  50. //delay(100);
  51. // Serial.print("\nLast Packet Send Status (From Sensor I: ");
  52. //delay(110); //above delays make sure something gets serial-written
  53. if (sendStatus == 0){
  54. Serial.print("\nPacket sent.");
  55. Serial.printf(" At time %lu",millis());
  56. }
  57. else{
  58. Serial.println("Delivery fail");
  59. }
  60. }
  61.  
  62. // Callback when data is received
  63. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  64. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  65.  
  66.  
  67. incomingTemp = incomingReadings.temp;
  68. incomingHum = incomingReadings.hum;
  69. incomingMillisA= incomingReadings.millisA;
  70. incomingOrders=incomingReadings.ord;
  71. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  72. incomingStackPtr=incomingReadings.stacPtr;
  73. incomingBufferPtr=incomingReadings.bufPtr;
  74. for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  75.  
  76. RxFlag =1; //tell loop() we've dgot a packet
  77. globLen = len; //Need this to output length in loop()
  78. onDataRecvMillis =millis();
  79.  
  80.  
  81. }
  82.  
  83.  
  84. void printIncomingReadings(){
  85. // Display Readings in Serial Monitor. Nothing so far
  86. Serial.println("\n\n---------------INCOMING READINGS from Master --0--------------------");
  87. Serial.print("Temperature: ");
  88. Serial.print(incomingTemp);
  89. Serial.println(" ºC");
  90. Serial.print("Humidity: ");
  91. Serial.print(incomingHum);
  92. Serial.println(" %");
  93. Serial.print("Millies: ");
  94. Serial.println(incomingMillisA);
  95. Serial.print("IncomingOrders: ");
  96. Serial.println(incomingOrders);
  97. Serial.print("IncomingStackPtr: ");
  98. Serial.println(incomingStackPtr);
  99.  
  100. for(int i =0;i<10;i++) {Serial.print( incomingStac[i]);}
  101. Serial.println(" <--incomingStac[]");
  102. for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
  103. Serial.println(" <--incomingBigBuffer[]");
  104. }
  105.  
  106. void setup() {
  107. // Init Serial Monitor
  108. Serial.begin(115200);
  109. myHumidity.begin();
  110. pinMode(LED_BUILTIN, OUTPUT);
  111.  
  112. // Set device as a Wi-Fi Station
  113. WiFi.mode(WIFI_STA);
  114. WiFi.disconnect();
  115.  
  116. // Init ESP-NOW
  117. if (esp_now_init() != 0) {
  118. Serial.println("Error initializing ESP-NOW");
  119. return;
  120. }
  121.  
  122. // Set ESP-NOW Role
  123. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  124.  
  125. // Once ESPNow is successfully Init, we will register for Send CB to
  126. // get the status of Trasnmitted packet
  127. esp_now_register_send_cb(OnDataSent);
  128.  
  129. // Register peer
  130. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  131.  
  132. // Register for a callback function that will be called when data is received
  133. esp_now_register_recv_cb(OnDataRecv);
  134.  
  135. startLittleFs();
  136. //stringPlay();
  137. clearBuffer();
  138.  
  139. }
  140.  
  141. //--------------------------------------------------------------
  142. void loop() {
  143. unsigned long currentMillis = millis();
  144. getStopWatch(0);
  145. if (currentMillis - previousMillis >= interval) {
  146. previousMillis = currentMillis; //NB programmable delay.
  147.  
  148. doLED2();
  149. getStopWatch(1);
  150. getReadings2(); //read the sensor and note start time
  151. numReadings++; //Serial.printf("\nNumber of readings is %lu: ",numReadings);
  152. getStopWatch(2);
  153.  
  154. //Set values to send to Master Wemos2
  155. sensorReadings.temp = temperature;
  156. sensorReadings.hum = humidity;
  157. sensorReadings.millisA=millis();
  158.  
  159. saveReadings(); //readings to strings,files save?,fill bigBuf[]?
  160.  
  161. // Send message via ESP-NOW
  162. getStopWatch(3);
  163. esp_now_send(broadcastAddress, (uint8_t *) &sensorReadings, sizeof(sensorReadings));
  164. getStopWatch(4);
  165. // Serial.print("\nOrders is=> ");
  166. //Serial.println( incomingReadings.ord); }
  167.  
  168. if (RxFlag==1) {
  169. Serial.printf("\nGot a packet, length is: %d Time is:%lu and onDataRecvMillis is:%lu and Loop Counter is:%lu\n\n",globLen,millis(),onDataRecvMillis,loopCounter);
  170. RxFlag=0; }
  171. loopCounter=0;
  172. getStopWatch(5);
  173. showStopWatchSettings();
  174. Serial.printf("eeeeeeeeeeeeeeeeeEnd %lu",millis()); //better value for stop time than getStopWatch(5) above
  175. }
  176. loopCounter++;
  177. }
  178.  
  179.  
  180.  
  181. void getReadings2(){
  182. temperature = myHumidity.readTemperature(); //both temperature and humidity are global floats
  183. humidity= myHumidity.readHumidity();
  184. Serial.printf("\n=======================New Reading of Sensors ==%d========2=====\n",numReadings);
  185. Serial.print("Time:");
  186. Serial.print(millis());
  187. Serial.print(" Temperature:");
  188. Serial.print(temperature, 1);
  189. Serial.print("C");
  190. Serial.print(" Humidity:");
  191. Serial.print(humidity, 1);
  192. Serial.print("%");
  193. }
  194.  
  195.  
  196. void startLittleFs() {
  197. // LittleFS.format(); //take this out after first go!
  198. LittleFS.setTimeCallback(myTimeCallback);
  199.  
  200. Serial.println("Mount LittleFS using *LittleFS.begin()*");
  201. if (!LittleFS.begin()) {
  202. Serial.println("LittleFS mount failed");
  203. return;
  204. }Serial.println("Now going to use *listDir* from root");
  205. listDir("/");
  206. Serial.println("Mount done^^");
  207. appendFile("/hello.txt", "----Wor\nld5!\n"); //small change
  208. listDir("/");
  209. appendFile("/hello.txt", "123456\n"); //small change
  210. const char *ssidx = "asdfg1234\n";
  211. appendFile("/hello.txt", ssidx);
  212. listDir("/");
  213. } //string play starts
  214.  
  215.  
  216. void saveReadings() { //Take readings and make them strings and put into File? Also put strings into buffer[] and, if it's big enough save to file and send to espNow boss.
  217.  
  218. unsigned long myTime;
  219. myTime=millis();
  220. //char text[20]; //might have to be global
  221. int irdTemp= (uint16_t)temperature;
  222. int irdHum= (uint16_t)humidity; //both from HTU21D read
  223.  
  224. //Serial.print("555555555555555Millis is ");Serial.println(millis());
  225. sprintf(text, "%d\t%d\t%lu]", irdTemp,irdHum,myTime);
  226. Serial.printf("\nText buf is: %s In hex: ", text);
  227.  
  228. for(int i = 0;i<20;i++) {
  229. Serial.printf("%x,",text[i]);
  230. }
  231. Serial.println();
  232. //showBufferHex(); //is it filling up?
  233.  
  234.  
  235. displayBufferStatsBanner();
  236. showFirst10();
  237. showFirstZero();
  238. showBufferLength();
  239. showNumOfBracs();
  240. showTextLen();
  241. showNumReadings();
  242. showLast10() ;
  243. displayBufferStatsBanner();
  244. //appendToLogFile();
  245. addTextToBuffer2(); //filling up too fast, uncomment later
  246.  
  247. }
  248.  
  249. void showBufferHex() { //print the intermediate buffer to see all the sensor strings
  250. for(int i = 0;i<20;i++) {
  251. Serial.printf("buffer[%u] is : %x ",i,buffer[i]);
  252. }
  253. Serial.printf("\nLength of buffer[] is %d ", strlen(buffer));
  254. }
  255. void clearBuffer() {
  256. for (int i=0;i<200;i++) buffer[i]=0; //clear intermediate buffer
  257. }
  258. void showFirst10() { //show start of buffer[]
  259. for(int i = 0;i<15;i++) Serial.printf("%x,",buffer[i]); //Actually 15 not 10
  260. }
  261. void showFirstZero() { //check out zeros in buffer.
  262. int gotAZero = 0;
  263. int numZeros = 0;
  264. int firstZeroPos = 0;
  265. for(int i = 0;i<200;i++) {
  266. if(buffer[i]==0) {
  267. if(gotAZero==0) { //must be first zero
  268. gotAZero=1;
  269. firstZeroPos=i;
  270. }
  271. numZeros++;
  272. }
  273.  
  274. }
  275. Serial.printf(" fz%d and %dnz",firstZeroPos,numZeros);
  276. }
  277. void showBufferLength() { //Buffer is 200 bytes long but what's len of string inside?
  278. int strLen=0;
  279. strLen = strlen(buffer); //char,char ..char then \0. Maybe whole buffer
  280. Serial.printf(" BL%d ",strLen);
  281.  
  282. }
  283. void showNumOfBracs() { //each reading produces one ']'
  284. int numBrac=0;
  285. for(int i = 0;i<200;i++) {
  286. if(buffer[i]==']') {numBrac++; }
  287. }
  288. Serial.printf(" [%d] ",numBrac);
  289. }
  290. //showTestLen() below: little buffer called text[] takes readings as strings then to buffer along with the other readings
  291. void showTextLen() {
  292. int textLen=0;
  293. textLen= strlen(text);
  294. Serial.printf(" TL%dTL ",textLen);
  295. }
  296. void showLast10() { //show end part of buffer[]
  297. for(int i = 185;i<200;i++) Serial.printf("%x.",buffer[i]); //Last 15 actually
  298. }
  299. void displayBufferStatsBanner() { //Otherwise hard to find
  300. Serial.printf("\n------------------Stats for buffer[] --------------------\n");
  301. }
  302. void appendToLogFile() { //When buffer gets full save it in log file
  303. int temp=2;
  304. buffer[0]=21;
  305. appendFile("/log0.txt",buffer); //filled buffer goes onto end of log file. Works!
  306. listDir("/");
  307. //appendFile("/hello.txt", "----Wor\nld5!\n");
  308. }
  309. //In addTextToBuffer() below : In each reading produces two ints, temperature and humidityas well as millis and a ']' tacked on the end. Also \n. It will be a string because it's put there by sprint() (which tacks on the zero?). This little buffer, ie text[] gets appended to bigger buffer[] if there's space for it.(See: textCanFitQ() )
  310.  
  311. void addTextToBuffer() {
  312. if(strlen((buffer) + strlen(text)) < 180) {
  313. strcat(buffer,text); //buffer = buffer + text
  314. Serial.printf("\nAdded to buffer.");
  315. }
  316. else{
  317. Serial.printf("Buffer overflow333333333333333333333. Readings num is:%d\n------------------Stats",numReadings);
  318. }
  319. }
  320.  
  321. void showNumReadings() { //how many times have we asked HTU21Dfor temperature etc?
  322. Serial.printf(" R%dR ",numReadings);
  323. }
  324.  
  325. void addTextToBuffer2() {
  326. int slenB=strlen(buffer);
  327. int slenT = strlen(text);
  328. int slenTotal = slenB+slenT;
  329.  
  330. if((slenB + slenT) < 195) {
  331. strcat(buffer,text); //buffer = buffer + text
  332. Serial.printf("\nAdded to buffer.");
  333. }
  334. else{
  335. Serial.printf("***Buffer overflow333333333333333333333. Readings num is:%d",numReadings);
  336. fixFullBuffer();
  337. }
  338. Serial.printf("Lengths B%d, T%d, Sum%d", slenB, slenT, slenTotal);
  339. }
  340. void fixFullBuffer() { //come here when buffer overflows if next reading inserted
  341. clearBuffer(); //200 zeros. Hope old buffer saved.
  342. strcat(buffer,text); //reading now goes in front.
  343. Serial.printf("@@@Buffer cleared.\n");
  344. showBufferHex();
  345. }
  346. void getStopWatch(int num) { //insedrt to see time. Used with showStopWatchSettings()
  347. unsigned long millisNow=millis();
  348. stopWatchSettings[num] = millisNow;
  349. }
  350. void showStopWatchSettings() { //Find timing and duration of parts of code.
  351. stopWatchSettings[6]=millis(); //So can print time now in loop below.
  352. for(int i=0;i<6;i++) {
  353. Serial.printf("sssssss%dsssss%lu;%lu , ",i,stopWatchSettings[i],stopWatchSettings[i+1]-stopWatchSettings[i]); //Time then distance to next time
  354. }
  355.  
  356. }
  357.  
Advertisement
Add Comment
Please, Sign In to add comment