Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //New direction. Add littleFS to HTU_0SantosSensor7.ino .Now SpudSensor Mon Aug 29 14:44:53 NZST 2022
- /*
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor0\SpudSensor0.ino works ok by sending
- good temp and humid data from HDU21 to C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino using Wemos 1
- ..to WemosII
- 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,
- The include file,tryme works. Now try to fill it useful LittleFS stuff
- Tidied up some of below. Put LittleFs into next version.
- Working: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor3\SpudSensor3.ino
- Now using C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor5\SpudSensor5.ino
- Going to add sampling string stuff to fill up struc to espnow send
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor5\SpudSensor5.ino Works ok
- ..above works with boss: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino
- Sun Sep 4 11:47:30 NZST 2022. Now starting on ..C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor6\SpudSensor6.ino
- 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 :
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor7\SpudSensor7.ino and talks to ...master6
- Spud..7 worked out with a lot of cleanup and several new buffer[] helpers
- Nw going to work on: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor8\SpudSensor8.ino
- same Boss: C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosMaster6\SantosMaster6.ino
- Going to add more routines to get sensor readings into litleFs and boss side.
- Some buffer problems in previous version. Have gone away? Now going to tidy up code and try to use ic files
- ...to help witj comprehension. Haven't done much with boss side yet.
- 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.
- 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.
- Decided to time main loop so I can see when sensing of temperature etc starts and stops as well as espNow stuff
- Thu Sep 15 12:29:29 NZST 2022, C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor94\SpudSensor94.ino
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor94C\SpudSensor94C.ino. Playing with files.
- The current date is: Mon 26/09/2022. C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor94D\SpudSensor94D.ino ... finished ...
- ..playing with files. Now, next version, want to get a sendFile() going that sends logfile from sensorWemos to Listener. Now on C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor94E\SpudSensor94E.ino
- Now Wed Sep 28 11:49:02 NZDT 2022. Nearly thru with sending file from sender-Wemos. JUst tidy up with SpudSensor94F.ino
- Now Thu Sep 29 11:10:15 NZDT 2022. Using SpudSensor94G.ino. Nearly sending file from WemosI (sensor) to WemosII(listener)
- Sat Oct 1 13:01:35 NZDT 2022 New sketch folder for C:\Users\Dell\Documents\Arduino\WorkingOctober22\SpudSensor94G\SpudSensor94G.ino .
- Current file going to pastebin is: C:\Users\Dell\Documents\Arduino\WorkingOctober22\SpudSensor94H\SpudSensor94H.ino
- SpudSensor94H.ino now Sender01.ino Sat Oct 15 12:36:04 NZDT 2022
- C:\Users\Dell\Documents\Arduino\WorkingOctober22\Sender02\Sender02.ino Mon Oct 17 12:22:35 NZDT 2022
- Still making code tidy. Take away stop watch stuff and get read sequence right.
- Next, send logfile. Now in Sender03.ino. Going to send main log file.
- */
- //50
- #include <FS.h>
- #include <LittleFS.h>
- #include <time.h>
- #include <ESP8266WiFi.h>
- #include <espnow.h>
- #include <Wire.h>
- #include "SparkFunHTU21D.h"
- #include "helperCode.h"
- #include "globals.h"
- HTU21D myHumidity;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- if (sendStatus == 0){
- Serial.print("\nPacket sent.");
- Serial.printf(" At time %lu",millis());
- }
- else{
- Serial.printf("\nDelivery fail\n");
- }
- }
- // Callback when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- 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<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
- RxFlag =1; //tell loop() we've dgot a packet
- globLen = len; //Need this to output length in loop()
- onDataRecvMillis =millis();
- }
- void printIncomingReadings(){
- // Display Readings in Serial Monitor.
- Serial.println("\n\n---------------INCOMING READINGS from Master ----------------------");
- Serial.print("Temperature: ");
- Serial.print(incomingTemp);
- Serial.println(" ºC");
- Serial.print("Humidity: ");
- Serial.print(incomingHum);
- Serial.println(" %");
- Serial.print("Millies: ");
- Serial.println(incomingMillisA);
- Serial.print("IncomingOrders: ");
- Serial.println(incomingOrders);
- Serial.print("IncomingStackPtr: ");
- Serial.println(incomingStackPtr);
- for(int i =0;i<10;i++) {Serial.print( incomingStac[i]);}
- Serial.println(" <--incomingStac[]");
- for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
- Serial.println(" <--incomingBigBuffer[]");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- myHumidity.begin();
- pinMode(LED_BUILTIN, OUTPUT);
- // 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);
- interval=8000; //slow it down a bit
- startLittleFs();
- delay(500);
- //LittleFS.remove("/log0.txt"); //delete didn't work
- clearBuffer();
- // sendFile("/testFile0.txt"); //initial practice file sensor-->listener
- delay(500);
- // sendFile2("/testFile0.txt"); //initial practice file sensor-->listener
- // sendFile2("/log0.txt"); //test this important routine
- }
- //--------------------------------------------------------------
- void loop() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis; //NB programmable delay.
- doLED2();
- getReadings2(); //read the sensor and note start time
- //Set values to send to Master Wemos2
- sensorReadings.temp = temperature; //these values go into struc
- sensorReadings.hum = humidity;
- sensorReadings.millisA=millis();
- sensorReadings.ord = 52; //'52' = just a normal sensor packet
- saveReadings(); //readings to strings,files save?,fill bigBuf[]?
- esp_now_send(broadcastAddress, (uint8_t *) &sensorReadings, sizeof(sensorReadings));
- if (RxFlag==1) {
- 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);
- RxFlag=0;
- loopCounter=0;
- }
- numReadings++; //Serial.printf("\nNumber of readings is %lu: ",numReadings);
- }
- //loopCounter++;
- }
- void getReadings2(){
- temperature = myHumidity.readTemperature(); //both temperature and humidity are global floats
- humidity= myHumidity.readHumidity();
- Serial.printf("\n\n=======================New Reading of Sensors ==%d========3=====\n",numReadings);
- Serial.print("Time:");
- Serial.print(millis());
- Serial.print(" Temperature:");
- Serial.print(temperature, 1);
- Serial.print("C");
- Serial.print(" Humidity:");
- Serial.print(humidity, 1);
- Serial.print("%");
- }
- 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 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.
- unsigned long myTime;
- myTime=millis();
- int irdTemp= (uint16_t)temperature;
- int irdHum= (uint16_t)humidity; //both from HTU21D read
- sprintf(text, "%d\t%d\t%lu]", irdTemp,irdHum,myTime);
- Serial.printf("\nText buf is: %s In hex: ", text);
- for(int i = 0;i<20;i++) {
- Serial.printf("%x,",text[i]);
- }
- Serial.println();
- displayBufferStatsBanner();
- showFirst10();
- showFirstZero();
- showBufferLength();
- showNumOfBracs();
- showTextLen();
- showNumReadings();
- showLast10() ;
- displayBufferStatsBanner();
- addTextToBuffer2();
- //appendToLogFile2("/log0.txt"); //if log file fills up too fast, delete this line when testing
- }
- void showBufferHex() { //print the intermediate buffer to see all the sensor strings
- for(int i = 0;i<20;i++) {
- Serial.printf("buffer[%u] is : %x ",i,buffer[i]);
- }
- Serial.printf("\nLength of buffer[] is %d ", strlen(buffer));
- }
- void clearBuffer() {
- for (int i=0;i<200;i++) buffer[i]=0; //clear intermediate buffer
- }
- void showFirst10() { //show start of buffer[]
- for(int i = 0;i<15;i++) Serial.printf("%x,",buffer[i]); //Actually 15 not 10
- }
- void showFirstZero() { //check out zeros in buffer.
- int gotAZero = 0;
- int numZeros = 0;
- int firstZeroPos = 0;
- for(int i = 0;i<200;i++) {
- if(buffer[i]==0) {
- if(gotAZero==0) { //must be first zero
- gotAZero=1;
- firstZeroPos=i;
- }
- numZeros++;
- }
- }
- Serial.printf(" fz%d and %dnz",firstZeroPos,numZeros);
- }
- void showBufferLength() { //Buffer is 200 bytes long but what's len of string inside?
- int strLen=0;
- strLen = strlen(buffer); //char,char ..char then \0. Maybe whole buffer
- Serial.printf(" BL%d ",strLen);
- }
- void showNumOfBracs() { //each reading produces one ']'
- int numBrac=0;
- for(int i = 0;i<200;i++) {
- if(buffer[i]==']') {numBrac++; }
- }
- Serial.printf(" [%d] ",numBrac);
- }
- //showTestLen() below: little buffer called text[] takes readings as strings then to buffer along with the other readings
- void showTextLen() {
- int textLen=0;
- textLen= strlen(text);
- Serial.printf(" TL%dTL ",textLen);
- }
- void showLast10() { //show end part of buffer[]
- for(int i = 185;i<200;i++) Serial.printf("%x.",buffer[i]); //Last 15 actually
- }
- void displayBufferStatsBanner() { //Otherwise hard to find
- Serial.printf("\n------------------Stats for buffer[] --------------------\n");
- }
- void appendToLogFile2(const char * path) { //When buffer gets full save it in log file
- //use like this : appendToLogFile2("/log0.txt"); //buffer variable is global. Maybe change later.
- appendFile(path,buffer); //filled buffer goes onto end of log file. Works!
- listDir(path);
- //appendFile("/hello.txt", "----Wor\nld5!\n");
- }
- //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() )
- void addTextToBuffer() {
- if(strlen((buffer) + strlen(text)) < 180) {
- strcat(buffer,text); //buffer = buffer + text
- Serial.printf("\nAdded to buffer.");
- }
- else{
- Serial.printf("Buffer overflow333333333333333333333. Readings num is:%d\n------------------Stats",numReadings);
- }
- }
- void showNumReadings() { //how many times have we asked HTU21Dfor temperature etc?
- Serial.printf(" R%dR ",numReadings);
- }
- void addTextToBuffer2() {
- int slenB=strlen(buffer);
- int slenT = strlen(text);
- int slenTotal = slenB+slenT;
- if((slenB + slenT) < 195) {
- strcat(buffer,text); //buffer = buffer + text
- Serial.printf("\nAdded to buffer.");
- }
- else{
- Serial.printf("\n***Buffer overflow333***.\n Readings num is:%d ",numReadings);
- //*********better send buffer to log file here before buffer gets zerod and started again with next reading.
- //*** appendToLogFile2("/log0.txt"); //try this here
- fixFullBuffer();
- }
- Serial.printf("Lengths B%d, T%d, Sum%d", slenB, slenT, slenTotal);
- }
- void fixFullBuffer() { //come here when buffer overflows if next reading inserted
- clearBuffer(); //200 zeros. Hope old buffer saved.
- strcat(buffer,text); //reading now goes in front.
- Serial.printf("@@@Buffer cleared.\n");
- showBufferHex();
- }
- void getStopWatch(int num) { //insedrt to see time. Used with showStopWatchSettings()
- unsigned long millisNow=millis();
- stopWatchSettings[num] = millisNow;
- }
- void showStopWatchSettings() { //Find timing and duration of parts of code.
- stopWatchSettings[6]=millis(); //So can print time now in loop below.
- for(int i=0;i<6;i++) {
- Serial.printf("sssssss%dsssss%lu;%lu , ",i,stopWatchSettings[i],stopWatchSettings[i+1]-stopWatchSettings[i]); //Time then distance to next time
- }
- }
- void doFile0() { //don't need this now
- createTestFile("/testFile0.txt",1234);
- listDir("/testFile0.txt");
- showFileHeadAndTail("/testFile0.txt") ;
- sendFile("/testFile0.txt");
- }
- void showFileHead2(const char * path){ //show first 10 bytes of file in parameter in serial output
- File f=LittleFS.open(path,"r");
- if(!f){
- Serial.print("Cannot open file in showFileHead2.");
- return;
- }
- if (f.size() <10){
- Serial.printf("File too small.\n");
- }
- else {
- Serial.printf("\nFirst ten bytes of %s are: ",path);
- for (int i=0;i<10;i++) {
- Serial.printf("[%d]%c ",i,f.read());
- }
- f.close();
- }
- }
- void showFileTail2(const char * path) {
- File f=LittleFS.open(path,"r");
- if(!f){
- Serial.print("Cannot open file in showFileHead2.");
- return;
- }
- if (f.size() <10){
- Serial.printf("File too small for showFileTail2 .\n");
- }
- else {
- Serial.printf("\nLast ten bytes of %s are: ",path);
- // for (int i = 1; i < 10 + 1; i++) {
- for (int i = 1; i < 15 + 1; i++) {
- f.seek(i,SeekEnd);
- Serial.printf("[%d]%c ",f.position(),f.read());
- }
- f.close();
- }
- }
- void createTestFile(const char * path, uint16_t sizeOfFile) { //create file of given size. For testing
- // Use like this : createTestFile("/testFile0.txt",1234);
- uint16_t fileCount=0;
- char ascChar=48; //insert into file f
- //File * g;
- File f=LittleFS.open(path,"w");
- //g= &f;
- if(!f){
- Serial.print("Cannot open file in showFileHead2.");
- return;
- } //create new file. Empty at start.
- if((sizeOfFile<2)|(sizeOfFile>65535)){
- Serial.printf("Size of file outside limits");
- return;
- }
- else {
- while(fileCount++ < sizeOfFile) {
- f.write(ascChar++);
- if(ascChar>122) { ascChar=48;} //if we get to 'z' go back 20 '1'
- }
- }
- }
- void showFileHeadAndTail(const char * path) { //peek at both ends of file
- //Use like this: showFileHeadAndTail("/testFile0.txt") ;
- showFileHead2(path);
- showFileTail2(path);
- }
- void sendFile(const char * path) { //send a log file from sensor to listener using espNow
- //Use like this: sendFile("/testFile0.txt");
- uint16_t numOfChunks=0;
- uint16_t extras=0;
- int i,j;
- File f=LittleFS.open(path,"r");
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- if (f.size() > 65535){
- Serial.printf("\nFile too big for sendFile fn .\n");
- }
- numOfChunks= f.size()/200; //integer division, check
- extras= f.size()%200;
- for( j=0;j<numOfChunks;j++) {
- for(i=0;i<200;i++) {
- sensorReadings.bigBuf[i]=f.read(); //fill struct buffer with file chars ready to send
- }
- espNowSendChunk(j,51); //send chunk j with orders that it's just a normal"51" type.
- delay(5); //give Listener time to put data away to file
- }
- Serial.printf("\nAll done. Number of chunks: %d extras: %d i=%d j=%d size=%d", numOfChunks,extras,i,j,f.size());
- doExtras(f,extras); //worked ok. g.fullName() is new for me Suggested by Arduino compiler
- }
- void doExtras( File g,uint16_t numOfExtras) { //sendFile getting too big. Trying tricky parameter passing.
- //Use like this: doExtras(f,extras); f has to be an open file. extras is nuber of leftover bytes in last file buffer send. See sendFile()
- Serial.printf("\nAdding in extras. File name is %s and extras are: %d",g.fullName(),numOfExtras);
- for(int i=0;i<numOfExtras;i++) {
- sensorReadings.bigBuf[i]=g.read(); //fill struct buffer with file chars ready to send
- }
- sensorReadings.stac[2] = numOfExtras; //Nedw marker to show last struc send, fragmented.
- sensorReadings.ord = 59; //59 is code for extras sent, that's all done
- sensorReadings.bigBuf[numOfExtras]=0; //tack 0 onto end of extras to help with strine handling
- esp_now_send(broadcastAddress, (uint8_t *) &sensorReadings, sizeof(sensorReadings)); //send usual structure
- Serial.printf("\nTried to send %d extras with orders %d ", numOfExtras,59);
- }
- void espNowSendChunk(uint16_t chunkNum,uint8_t ordersByte) {
- //use like this: espNowSendChunk(6,51); ie send the 6th chuck from file opened to WemosII with orders=51,ie a normal chunkNum
- sensorReadings.stac[0] = chunkNum/256; //high bytr
- sensorReadings.stac[1] = chunkNum%256; //low byte
- sensorReadings.ord = 51; //51 is orders code for normal chuck from Wemos 1
- sensorReadings.millisA=millis();
- esp_now_send(broadcastAddress, (uint8_t *) &sensorReadings, sizeof(sensorReadings)); //send usual structure
- //delay(100); //give time to receiver to take espNow msg
- delay(200);
- Serial.printf("\nTried to send chunk %d with orders %d ", chunkNum,ordersByte);
- }
- void sendFile2(const char * path) { //send a log file from sensor to listener using espNow
- //Use like this: sendFile("/testFile0.txt");
- //better than sendFile() as it tacks 0 on end of buf to make it a string
- uint16_t numOfChunks=0;
- uint16_t extras=0;
- int i,j;
- File f=LittleFS.open(path,"r");
- if(!f){
- Serial.print("Cannot open file in sendFile fn.");
- return;
- }
- if (f.size() > 65535){
- Serial.printf("\nFile too big for sendFile fn .\n");
- }
- numOfChunks= f.size()/200; //integer division, check
- extras= f.size()%200;
- for( j=0;j<numOfChunks;j++) {
- for(i=0;i<199;i++) {
- sensorReadings.bigBuf[i]=f.read(); //fill struct buffer with file chars ready to send
- sensorReadings.bigBuf[199] = 0;
- }
- espNowSendChunk(j,51); //send chunk j with orders that it's just a normal"51" type.
- delay(5); //give Listener time to put data away to file
- }
- Serial.printf("\nAll done. Number of chunks: %d extras: %d i=%d j=%d size=%d", numOfChunks,extras,i,j,f.size());
- doExtras(f,extras); //worked ok. g.fullName() is new for me Suggested by Arduino compiler
- f.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement