Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sender,Sensor code. Similar to Master, Receiver but not identical.,
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantoSensor_2.ino\SantoSensor_2.ino.ino
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor3\SantosSensor3.ino, Wed Aug 10 13:32:09 NZST 2022.
- //Compiled above and ran but had not plugged in Master Wemos with slightly different code.
- /* SantosSensor4.ino Wed Aug 10 16:38:10 NZST 2022. Making strucs better. Representing Master/Slave idea
- SantosSensor4.ino
- 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
- Now going to next cleaner version. Think Listener need not send espNow pkt edach time it gets one.
- ..Only when it needs to order Sensor (WemosI) to eg. send log file. Now working on SantosSensor8.ino
- Need clearer output from struc coming from Santos sensor Wemos I. Bit cluttered on screen.Wed Oct 5 12:02:23 NZDT 2022
- Why do readings on this Wemos II? It's just the listener and the boss who orders Sensor WemosI.
- Started LittleFs. Sent some code to sa.
- Sat Oct 8 12:24:36 NZDT 2022. SantosSensor9.ino. File tx from WemosI to Wemos II
- 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.
- ..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
- Listener02.ino below. Tidy up file that receives log infro from Sender.
- 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
- rreadings from file from WEmosI to Python in PC.This is Listener04.ino
- 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
- 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
- ..send dir output listing all litlle fs files.Listener04_C.ino
- */
- //35
- #include <ESP8266WiFi.h>
- #include <espnow.h>
- #include <Adafruit_Sensor.h>
- #include <FS.h>
- #include <LittleFS.h>
- #include <time.h>
- #include "helperCode.h"
- uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
- //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
- // Define variables to store Sensor readings to be sent to Master
- float temperature;
- float humidity;
- unsigned long milliSecsA;
- uint8_t orders;
- uint8_t stack[10];
- uint8_t stackPtr;
- uint8_t bufferPtr;
- uint8_t bigBuffer[200];
- // Define variables to store incoming readings from Master
- float incomingTemp;
- float incomingHum;
- unsigned long incomingMillisA;
- uint8_t incomingOrders;
- uint8_t incomingStac[10];
- uint8_t incomingStackPtr;
- uint8_t incomingBufferPtr;
- char incomingBigBuffer[200]; //try this. 'Char' makes it work.
- uint8_t gotAPktflag =1; //flag for packet interrrupt
- uint8_t pktCount=0; //bump 1 for each packet arrival
- //uint32_t loopCounter=0; //bump 1 for each time thru main loop
- uint16_t globLen =0; //to pass rx callback's len to loop
- uint32_t millisODR=0;
- // Variable to store if sending data was successful
- //String success;
- //Structure example to send data
- //Must match the receiver structure
- typedef struct struct_message {
- float temp;
- float hum;
- unsigned long millisA; //for timing
- uint8_t ord;
- uint8_t stac[10];
- uint8_t stacPtr;
- uint8_t bufPtr;
- uint8_t bigBuf[200];
- } struct_message;
- // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
- struct_message SensorReadings;
- // Create a struct_message to hold incoming master readings
- struct_message incomingReadings;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- Serial.print("Last Packet Send Status (From Sensor I: ");
- if (sendStatus == 0){
- Serial.println("Delivery success");
- }
- else{
- Serial.println("Delivery fail");
- }
- }
- // Callback when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- globLen=len;
- incomingTemp = incomingReadings.temp;
- incomingHum = incomingReadings.hum;
- incomingMillisA= incomingReadings.millisA;
- incomingOrders=incomingReadings.ord;
- for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
- incomingStackPtr=incomingReadings.stacPtr;
- incomingBufferPtr=incomingReadings.bufPtr;
- for(int i =0;i<200;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
- gotAPktflag=1; //used by loop
- pktCount++;
- millisODR=millis();
- }
- void printIncomingReadings2(){ //leaner version
- Serial.println("\nNew packet-------");
- Serial.print(" Incoming millis = ");
- Serial.println(incomingMillisA);
- Serial.print(" Millis at ODR ="); //take time when callback recv acutally used.ie when espNow Packet arrives.
- Serial.println(millisODR);
- Serial.print(" IncomingOrders=");
- Serial.println(incomingOrders);
- }
- void setup() {
- // Init Serial Monitor
- delay(1000);
- Serial.printf("\n***Starting WemosII,Listener***\n");
- // Serial.begin(115200);
- Serial.begin(9600);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- // Init ESP-NOW
- if (esp_now_init() != 0) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // Set ESP-NOW Role
- esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
- // Once ESPNow is successfully Init, we will register for Send CB to
- // get the status of Trasnmitted packet
- esp_now_register_send_cb(OnDataSent);
- // Register peer
- esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
- // Register for a callback function that will be called when data is received
- esp_now_register_recv_cb(OnDataRecv);
- Serial.printf("\n***3Starting WemosII,Listener3***\n");
- startLittleFs();
- // sendFileToPython();
- // sendFileToPython2();
- delay(2000);
- Serial.println("Awaiting Serial comands like *sfi ");
- }
- void loop() {
- norwayCommand2();
- delay(500);
- if (gotAPktflag==1){
- gotAPktflag=0;
- Serial.print("Bytes received: ");
- Serial.println(globLen);
- printIncomingReadings2();
- // if(incomingOrders==51) {
- if((incomingOrders==51)||(incomingOrders==59)) {
- doSaveChunk2();
- }
- }
- }
- void startLittleFs() {
- // LittleFS.format(); //take this out after first go!
- LittleFS.setTimeCallback(myTimeCallback); //best I can do without getting date from internet
- Serial.println("Mount LittleFS using *LittleFS.begin()*");
- if (!LittleFS.begin()) {
- Serial.println("LittleFS mount failed");
- return;
- }
- Serial.println("Mount done^*^");
- Serial.println("Now going to use *listDir* from root");
- listDir("/");
- }
- void doSaveChunk2(void) { //Slimmer version, got a '51' packet. Save it to file.
- for(int i =0;i<2;i++) Serial.printf( " %d",incomingStac[i]);
- uint32_t millA,millB;
- millA=millis();
- Serial.printf(" Millis A = %lu ",millis());
- // appendFile("/saveMe0.txt",incomingBigBuffer);
- appendFile("/logSave0.txt",incomingBigBuffer);
- millB=millis();
- Serial.printf(" Millis B = %lu ",millB);
- Serial.printf("Millis diff = %lu ",millB-millA);
- }
- /*
- void sendFileToPython() { //for data from Wemos II
- // File f=LittleFS.open(path,"r");
- File f=LittleFS.open("saveMe0.txt","r");
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- for(int i=0;i<100;i++) {
- Serial.print(f.read()); // first 100 chars
- }
- f.close();
- } */
- void sendFileToPython2() { //for data from Wemos II
- // File f=LittleFS.open(path,"r");
- Serial.println("Going to send file now");
- File f=LittleFS.open("/logSave0.txt","r");
- size_t filesize = f.size(); Serial.println(filesize); //new
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- for(int i=0;i<100;i++) {
- //Serial.printf(f.read()); // first 100 chars
- //Serial.printf(f.read()); // first 100 chars
- Serial.printf("%d %d ",f.read(),i); // first 100 chars
- delay(50);
- }
- f.close();
- Serial.println("File sent.");
- }
- void sendFileToPython3() { //for data from Wemos II
- // File f=LittleFS.open(path,"r");
- //Serial.println("Going to send3 file now");
- File f=LittleFS.open("/logSave0.txt","r");
- size_t filesize = f.size();// Serial.println(filesize); //new
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- for(int i=0;i<filesize;i++) {
- //Serial.printf(f.read()); // first 100 chars
- //Serial.print(f.read());
- Serial.print(f.read());
- //if(i%100==0) { //send every hundredth val
- //Serial.printf("%d %d ",f.read(),i); // first 100 chars
- // delay(1);
- delay(10);
- }
- f.close();
- Serial.println("File sent.");
- }
- void sendFileToPython4() { //for data from Wemos II..experimental Thu Nov 24 12:15:54 NZDT 2022
- File f=LittleFS.open("/logSave0.txt","r");
- size_t filesize = f.size();// Serial.println(filesize); //new
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- //for(int i=0;i<filesize;i++) {
- for(int i=0;i<50;i++) {
- //Serial.printf(f.read()); // first 100 chars
- char ch=(f.read());
- //Serial.print(f.read());
- if (ch=='\t') ch= ',';
- if (ch==']') {Serial.println();}
- else {
- Serial.print(ch);}
- //Serial.print(" ");
- //if(i%100==0) { //send every hundredth val
- //Serial.printf("%d %d ",f.read(),i); // first 100 chars
- // delay(1);
- delay(10);
- }
- f.close();
- //Serial.println("File sent.");
- }
- void sendDir() { //send vial serial LittleFS file dir
- //void listDir(const char * dirname) {
- Serial.println("Now going to use *listDir* from root");
- listDir("/");
- }
- void norwayCommand() {
- Serial.print("N");
- }
- void norwayCommand2(void){ //trying better instruction method from serial https://www.norwegiancreations.com/2017/12/arduino-tutorial-serial-inputs/
- //Having problemswith if ..else if.. Could be OK now
- String command;
- char stringBox[20];
- if(Serial.available()){
- command = Serial.readStringUntil('\n');
- // Serial.printf("len of command is %d and serial available is %d",command.length(),Serial.available());
- if(command.equals("*tst")){ //test string
- Serial.print("0123456789");
- //Serial.println();
- }
- else if(command.equals("*dir")){
- Serial.println("Listing the Little FS directory ");
- sendDir();
- Serial.println(" Did the -->Listing the Little FS directory ");
- Serial.flush();
- delay(1);
- Serial.flush();
- }
- else if(command.equals("*sfi")){
- // Serial.println("Sending File");
- sendFileToPython4();
- //Serial.println("Sent File");
- //Serial.println("Sent File2");
- }
- else if(command.equals("du")){ //do nothing
- }
- // else if(command[0]=='b'){
- else if(command[0]=='"'){
- Serial.println("got a double quote ");
- Serial.println(command.length());
- //strcpy(stringBox,command);
- command.toCharArray(stringBox,command.length()+1); //Not sure why +1 needed
- Serial.printf("\ncommand=%s stringBox=%s ",command,stringBox);
- for(int i = 0;i<20;i++) {
- Serial.printf("%d %c ",i,stringBox[i]);
- }
- //Serial.println(len(command))
- }
- else{
- Serial.println("Invalid command");
- Serial.println(command.length());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment