prjbrook

C:\Users\Dell\Documents\Arduino\WorkingNov22\Listener04_C2\Listener04_C2.ino

Dec 2nd, 2022
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 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. Now going to next cleaner version. Think Listener need not send espNow pkt edach time it gets one.
  9. ..Only when it needs to order Sensor (WemosI) to eg. send log file. Now working on SantosSensor8.ino
  10. Need clearer output from struc coming from Santos sensor Wemos I. Bit cluttered on screen.Wed Oct 5 12:02:23 NZDT 2022
  11. Why do readings on this Wemos II? It's just the listener and the boss who orders Sensor WemosI.
  12. Started LittleFs. Sent some code to sa.
  13. Sat Oct 8 12:24:36 NZDT 2022. SantosSensor9.ino. File tx from WemosI to Wemos II
  14. Is taking too long. Putting some stopwatch info. ..Now doing Sun Oct 9 15:41:30 NZDT 2022 C:\Users\Dell\Documents\Arduino\WorkingOctober22\SantosSensor91\SantosSensor91.ino ..Trying to clean up all the unnecessary Serial.prints System slower than I thought.
  15. ..Still trying to nail down timing for packets sent and received along with putting packets..away into LittleFS file. Latter takes about 170ms per packet, a lot. Too much? Will start next version. Calling it Listener00.ino.(Not SantosSensor92..) C:\Users\Dell\Documents\Arduino\WorkingOctober22\Listener001\Listener001.ino
  16. Listener02.ino below. Tidy up file that receives log infro from Sender.
  17. Thu Oct 20 11:49:31 NZDT 2022. ..Now doing C:\Users\Dell\Documents\Arduino\WorkingOctober22\Listener03\Listener03.ino. Extras in previous version, ie packets with orders =59 not being written nto file saveMe0. So fixing this. Fri Oct 21 11:25:03 NZDT 2022..Now going to send
  18. rreadings from file from WEmosI to Python in PC.This is Listener04.ino
  19. Having problems with Python reading sent LittleFs files. Come thru oK in terminalbut get garbage in Python. One sulution is to drop baud rate to 9600 in Wemos II
  20. Now going to try to work in Norway commands for serial orders from Python to Wemos. C:\Users\Dell\Documents\Arduino\WorkingNov22\Listener04_B\Listener04_B.inoPracticed on Uno. Sat Oct 29 15:26:46 NZDT 2022//..Fri Nov 4 11:26:08 NZDT 2022: sendFileToPython3 working Ok in Bray++ teminal. Next
  21. ..send dir output listing all litlle fs files.Listener04_C.ino
  22. */
  23.  
  24. //35
  25.  
  26. #include <ESP8266WiFi.h>
  27. #include <espnow.h>
  28. #include <Adafruit_Sensor.h>
  29. #include <FS.h>
  30. #include <LittleFS.h>
  31. #include <time.h>
  32. #include "helperCode.h"
  33.  
  34.  
  35. uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
  36. //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
  37.  
  38. // Define variables to store Sensor readings to be sent to Master
  39. float temperature;
  40. float humidity;
  41. unsigned long milliSecsA;
  42. uint8_t orders;
  43. uint8_t stack[10];
  44. uint8_t stackPtr;
  45. uint8_t bufferPtr;
  46. uint8_t bigBuffer[200];
  47.  
  48. // Define variables to store incoming readings from Master
  49. float incomingTemp;
  50. float incomingHum;
  51. unsigned long incomingMillisA;
  52. uint8_t incomingOrders;
  53. uint8_t incomingStac[10];
  54. uint8_t incomingStackPtr;
  55. uint8_t incomingBufferPtr;
  56. char incomingBigBuffer[200]; //try this. 'Char' makes it work.
  57. uint8_t gotAPktflag =1; //flag for packet interrrupt
  58. uint8_t pktCount=0; //bump 1 for each packet arrival
  59. //uint32_t loopCounter=0; //bump 1 for each time thru main loop
  60. uint16_t globLen =0; //to pass rx callback's len to loop
  61. uint32_t millisODR=0;
  62.  
  63. // Variable to store if sending data was successful
  64. //String success;
  65.  
  66. //Structure example to send data
  67. //Must match the receiver structure
  68. typedef struct struct_message {
  69. float temp;
  70. float hum;
  71. unsigned long millisA; //for timing
  72. uint8_t ord;
  73. uint8_t stac[10];
  74. uint8_t stacPtr;
  75. uint8_t bufPtr;
  76. uint8_t bigBuf[200];
  77.  
  78. } struct_message;
  79.  
  80. // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
  81. struct_message SensorReadings;
  82.  
  83. // Create a struct_message to hold incoming master readings
  84. struct_message incomingReadings;
  85.  
  86. // Callback when data is sent
  87. void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  88. Serial.print("Last Packet Send Status (From Sensor I: ");
  89. if (sendStatus == 0){
  90. Serial.println("Delivery success");
  91. }
  92. else{
  93. Serial.println("Delivery fail");
  94. }
  95. }
  96.  
  97.  
  98. // Callback when data is received
  99. void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  100. memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  101. globLen=len;
  102.  
  103. incomingTemp = incomingReadings.temp;
  104. incomingHum = incomingReadings.hum;
  105. incomingMillisA= incomingReadings.millisA;
  106. incomingOrders=incomingReadings.ord;
  107. for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
  108. incomingStackPtr=incomingReadings.stacPtr;
  109. incomingBufferPtr=incomingReadings.bufPtr;
  110. for(int i =0;i<200;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
  111.  
  112. gotAPktflag=1; //used by loop
  113. pktCount++;
  114. millisODR=millis();
  115.  
  116. }
  117.  
  118. void printIncomingReadings2(){ //leaner version
  119.  
  120. Serial.println("\nNew packet-------");
  121. Serial.print(" Incoming millis = ");
  122. Serial.println(incomingMillisA);
  123. Serial.print(" Millis at ODR ="); //take time when callback recv acutally used.ie when espNow Packet arrives.
  124. Serial.println(millisODR);
  125.  
  126. Serial.print(" IncomingOrders=");
  127. Serial.println(incomingOrders);
  128.  
  129. }
  130.  
  131. void setup() {
  132. // Init Serial Monitor
  133. delay(1000);
  134. Serial.printf("\n***Starting WemosII,Listener***\n");
  135. // Serial.begin(115200);
  136. Serial.begin(9600);
  137.  
  138.  
  139. // Set device as a Wi-Fi Station
  140. WiFi.mode(WIFI_STA);
  141. WiFi.disconnect();
  142.  
  143. // Init ESP-NOW
  144. if (esp_now_init() != 0) {
  145. Serial.println("Error initializing ESP-NOW");
  146. return;
  147. }
  148.  
  149. // Set ESP-NOW Role
  150. esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  151.  
  152. // Once ESPNow is successfully Init, we will register for Send CB to
  153. // get the status of Trasnmitted packet
  154. esp_now_register_send_cb(OnDataSent);
  155.  
  156. // Register peer
  157. esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  158.  
  159. // Register for a callback function that will be called when data is received
  160. esp_now_register_recv_cb(OnDataRecv);
  161. Serial.printf("\n***3Starting WemosII,Listener3***\n");
  162.  
  163. startLittleFs();
  164. // sendFileToPython();
  165. // sendFileToPython2();
  166. delay(2000);
  167. Serial.println("Awaiting Serial comands like *sfi ");
  168. }
  169.  
  170. void loop() {
  171. norwayCommand2();
  172. delay(500);
  173. if (gotAPktflag==1){
  174. gotAPktflag=0;
  175. Serial.print("Bytes received: ");
  176. Serial.println(globLen);
  177. printIncomingReadings2();
  178. // if(incomingOrders==51) {
  179. if((incomingOrders==51)||(incomingOrders==59)) {
  180. doSaveChunk2();
  181. }
  182. }
  183. }
  184.  
  185. void startLittleFs() {
  186. // LittleFS.format(); //take this out after first go!
  187. LittleFS.setTimeCallback(myTimeCallback); //best I can do without getting date from internet
  188. Serial.println("Mount LittleFS using *LittleFS.begin()*");
  189. if (!LittleFS.begin()) {
  190. Serial.println("LittleFS mount failed");
  191. return;
  192. }
  193. Serial.println("Mount done^*^");
  194. Serial.println("Now going to use *listDir* from root");
  195. listDir("/");
  196. }
  197.  
  198. void doSaveChunk2(void) { //Slimmer version, got a '51' packet. Save it to file.
  199. for(int i =0;i<2;i++) Serial.printf( " %d",incomingStac[i]);
  200.  
  201. uint32_t millA,millB;
  202. millA=millis();
  203. Serial.printf(" Millis A = %lu ",millis());
  204. // appendFile("/saveMe0.txt",incomingBigBuffer);
  205. appendFile("/logSave0.txt",incomingBigBuffer);
  206. millB=millis();
  207. Serial.printf(" Millis B = %lu ",millB);
  208. Serial.printf("Millis diff = %lu ",millB-millA);
  209. }
  210.  
  211. /*
  212. void sendFileToPython() { //for data from Wemos II
  213. // File f=LittleFS.open(path,"r");
  214. File f=LittleFS.open("saveMe0.txt","r");
  215. if(!f){
  216. Serial.print("Cannot open file in sendFile fn.");
  217. return;
  218. }
  219. for(int i=0;i<100;i++) {
  220. Serial.print(f.read()); // first 100 chars
  221. }
  222. f.close();
  223.  
  224. } */
  225. void sendFileToPython2() { //for data from Wemos II
  226. // File f=LittleFS.open(path,"r");
  227. Serial.println("Going to send file now");
  228. File f=LittleFS.open("/logSave0.txt","r");
  229. size_t filesize = f.size(); Serial.println(filesize); //new
  230. if(!f){
  231. Serial.print("Cannot open file in sendFile fn.");
  232. return;
  233. }
  234. for(int i=0;i<100;i++) {
  235. //Serial.printf(f.read()); // first 100 chars
  236. //Serial.printf(f.read()); // first 100 chars
  237. Serial.printf("%d %d ",f.read(),i); // first 100 chars
  238. delay(50);
  239. }
  240. f.close();
  241. Serial.println("File sent.");
  242. }
  243.  
  244. void sendFileToPython3() { //for data from Wemos II
  245. // File f=LittleFS.open(path,"r");
  246. //Serial.println("Going to send3 file now");
  247. File f=LittleFS.open("/logSave0.txt","r");
  248. size_t filesize = f.size();// Serial.println(filesize); //new
  249. if(!f){
  250. Serial.print("Cannot open file in sendFile fn.");
  251. return;
  252. }
  253. for(int i=0;i<filesize;i++) {
  254. //Serial.printf(f.read()); // first 100 chars
  255. //Serial.print(f.read());
  256. Serial.print(f.read());
  257. //if(i%100==0) { //send every hundredth val
  258. //Serial.printf("%d %d ",f.read(),i); // first 100 chars
  259.  
  260. // delay(1);
  261. delay(10);
  262. }
  263. f.close();
  264. Serial.println("File sent.");
  265. }
  266.  
  267. void sendFileToPython4() { //for data from Wemos II..experimental Thu Nov 24 12:15:54 NZDT 2022
  268. File f=LittleFS.open("/logSave0.txt","r");
  269. size_t filesize = f.size();// Serial.println(filesize); //new
  270. if(!f){
  271. Serial.print("Cannot open file in sendFile fn.");
  272. return;
  273. }
  274. //for(int i=0;i<filesize;i++) {
  275. for(int i=0;i<50;i++) {
  276.  
  277. //Serial.printf(f.read()); // first 100 chars
  278. char ch=(f.read());
  279. //Serial.print(f.read());
  280. if (ch=='\t') ch= ',';
  281. if (ch==']') {Serial.println();}
  282. else {
  283. Serial.print(ch);}
  284. //Serial.print(" ");
  285. //if(i%100==0) { //send every hundredth val
  286. //Serial.printf("%d %d ",f.read(),i); // first 100 chars
  287.  
  288. // delay(1);
  289. delay(10);
  290. }
  291. f.close();
  292. //Serial.println("File sent.");
  293. }
  294.  
  295.  
  296. void sendDir() { //send vial serial LittleFS file dir
  297. //void listDir(const char * dirname) {
  298. Serial.println("Now going to use *listDir* from root");
  299. listDir("/");
  300. }
  301.  
  302. void norwayCommand() {
  303. Serial.print("N");
  304. }
  305. void norwayCommand2(void){ //trying better instruction method from serial https://www.norwegiancreations.com/2017/12/arduino-tutorial-serial-inputs/
  306. //Having problemswith if ..else if.. Could be OK now
  307. String command;
  308. char stringBox[20];
  309.  
  310. if(Serial.available()){
  311. command = Serial.readStringUntil('\n');
  312. // Serial.printf("len of command is %d and serial available is %d",command.length(),Serial.available());
  313. if(command.equals("*tst")){ //test string
  314. Serial.print("0123456789");
  315. //Serial.println();
  316. }
  317.  
  318. else if(command.equals("*dir")){
  319. Serial.println("Listing the Little FS directory ");
  320. sendDir();
  321. Serial.println(" Did the -->Listing the Little FS directory ");
  322. Serial.flush();
  323. delay(1);
  324. Serial.flush();
  325. }
  326.  
  327. else if(command.equals("*sfi")){
  328. // Serial.println("Sending File");
  329. sendFileToPython4();
  330. //Serial.println("Sent File");
  331. //Serial.println("Sent File2");
  332. }
  333. else if(command.equals("du")){ //do nothing
  334. }
  335. // else if(command[0]=='b'){
  336. else if(command[0]=='"'){
  337. Serial.println("got a double quote ");
  338. Serial.println(command.length());
  339. //strcpy(stringBox,command);
  340. command.toCharArray(stringBox,command.length()+1); //Not sure why +1 needed
  341. Serial.printf("\ncommand=%s stringBox=%s ",command,stringBox);
  342. for(int i = 0;i<20;i++) {
  343. Serial.printf("%d %c ",i,stringBox[i]);
  344. }
  345. //Serial.println(len(command))
  346. }
  347. else{
  348. Serial.println("Invalid command");
  349. Serial.println(command.length());
  350. }
  351. }
  352. }
  353.  
Advertisement
Add Comment
Please, Sign In to add comment