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
- */
- //28
- #include <FS.h>
- #include <LittleFS.h>
- #include <time.h>
- #include <ESP8266WiFi.h>
- #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
- #include <Wire.h>
- #include "SparkFunHTU21D.h"
- //#include "C:\Users\Dell\Documents\Arduino\WorkingAugust22\SpudSensor1\tryme.txt" //
- #include "helperCode.h"
- #include "globals.h"
- HTU21D myHumidity;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- //delay(100);
- // Serial.print("\nLast Packet Send Status (From Sensor I: ");
- //delay(110); //above delays make sure something gets serial-written
- if (sendStatus == 0){
- Serial.print("\nPacket sent.");
- Serial.printf(" At time %lu",millis());
- }
- 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));
- 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. Nothing so far
- Serial.println("\n\n---------------INCOMING READINGS from Master --0--------------------");
- 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);
- startLittleFs();
- //stringPlay();
- clearBuffer();
- }
- //--------------------------------------------------------------
- void loop() {
- unsigned long currentMillis = millis();
- getStopWatch(0);
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis; //NB programmable delay.
- doLED2();
- getStopWatch(1);
- getReadings2(); //read the sensor and note start time
- numReadings++; //Serial.printf("\nNumber of readings is %lu: ",numReadings);
- getStopWatch(2);
- //Set values to send to Master Wemos2
- sensorReadings.temp = temperature;
- sensorReadings.hum = humidity;
- sensorReadings.millisA=millis();
- saveReadings(); //readings to strings,files save?,fill bigBuf[]?
- // Send message via ESP-NOW
- getStopWatch(3);
- esp_now_send(broadcastAddress, (uint8_t *) &sensorReadings, sizeof(sensorReadings));
- getStopWatch(4);
- // Serial.print("\nOrders is=> ");
- //Serial.println( incomingReadings.ord); }
- 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;
- getStopWatch(5);
- showStopWatchSettings();
- Serial.printf("eeeeeeeeeeeeeeeeeEnd %lu",millis()); //better value for stop time than getStopWatch(5) above
- }
- loopCounter++;
- }
- void getReadings2(){
- temperature = myHumidity.readTemperature(); //both temperature and humidity are global floats
- humidity= myHumidity.readHumidity();
- Serial.printf("\n=======================New Reading of Sensors ==%d========2=====\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);
- Serial.println("Mount LittleFS using *LittleFS.begin()*");
- if (!LittleFS.begin()) {
- Serial.println("LittleFS mount failed");
- return;
- }Serial.println("Now going to use *listDir* from root");
- listDir("/");
- Serial.println("Mount done^^");
- appendFile("/hello.txt", "----Wor\nld5!\n"); //small change
- listDir("/");
- appendFile("/hello.txt", "123456\n"); //small change
- const char *ssidx = "asdfg1234\n";
- appendFile("/hello.txt", ssidx);
- listDir("/");
- } //string play starts
- 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();
- //char text[20]; //might have to be global
- int irdTemp= (uint16_t)temperature;
- int irdHum= (uint16_t)humidity; //both from HTU21D read
- //Serial.print("555555555555555Millis is ");Serial.println(millis());
- 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();
- //showBufferHex(); //is it filling up?
- displayBufferStatsBanner();
- showFirst10();
- showFirstZero();
- showBufferLength();
- showNumOfBracs();
- showTextLen();
- showNumReadings();
- showLast10() ;
- displayBufferStatsBanner();
- //appendToLogFile();
- addTextToBuffer2(); //filling up too fast, uncomment later
- }
- 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 appendToLogFile() { //When buffer gets full save it in log file
- int temp=2;
- buffer[0]=21;
- appendFile("/log0.txt",buffer); //filled buffer goes onto end of log file. Works!
- listDir("/");
- //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("***Buffer overflow333333333333333333333. Readings num is:%d",numReadings);
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment