Go Down

Topic: HC-SR04 - storing the distance data  (Read 968 times) previous topic - next topic

joodaa

Hi all, I hope I am in the right section.

I plan to use the HC-SR04 to measure the level of waste in bins over time.

I was wondering if once I upload the code onto the Arduino, hook up the battery pack, and the sensor; if I was to then leave it for a day, is there a way for me to record what was measured and then see what the level changes over time were, when I hook it back up to my laptop?

Thank you.

wvmarle

Easiest way to do this is to use a NodeMCU or WeMOS (or other board with ESP8266 processor). It's got WiFi connectivity built in, and plenty of Flash (4 MB for the ESP12E modules used on those two boards) to store your waste levels. In deep sleep mode it will run on batteries for a long time.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

joodaa

Easiest way to do this is to use a NodeMCU or WeMOS (or other board with ESP8266 processor). It's got WiFi connectivity built in, and plenty of Flash (4 MB for the ESP12E modules used on those two boards) to store your waste levels. In deep sleep mode it will run on batteries for a long time.
Thank you, would I need to amend the code in some way so it stores the levels on the NodeMCU, or would it do it automatically?

Also, do you know of a way to implement a time stamp of sorts when each level measured?

wvmarle

Of course you have to tell the code to store the data, and how (write a file on SPIFFS).

Timestamp: easy, just make sure you have the current time. Get it over NTP if you have WiFi available or connect a RTC. Then store it as UNIX timestamp for easy processing.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

developerunify

Of course you have to tell the code to store the data, and how (write a file on SPIFFS).

Timestamp: easy, just make sure you have the current time. Get it over NTP if you have WiFi available or connect a RTC. Then store it as UNIX timestamp for easy processing.
Just to add onto this. If you want to store timestamps in your local time instead of UTC (that is what you get from an NTP server) then you can use this github repository code to parse the local timezone file (you can get the local timezone file from this path /usr/share/zoneinfo from any linux machine or from the github itself).

joodaa

Of course you have to tell the code to store the data, and how (write a file on SPIFFS).

Timestamp: easy, just make sure you have the current time. Get it over NTP if you have WiFi available or connect a RTC. Then store it as UNIX timestamp for easy processing.
Just to add onto this. If you want to store timestamps in your local time instead of UTC (that is what you get from an NTP server) then you can use this github repository code to parse the local timezone file (you can get the local timezone file from this path /usr/share/zoneinfo from any linux machine or from the github itself).
Thank you both, I am incredibly new to this, I will try to look into what you bot have talked about, but I may have some follow up questions if that's ok? First, I will purchase a NodeMCU.

wvmarle

If you know your timezone you can just hardcode an offset :-) Good enough for most purposes.

Good luck with your research.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

joodaa

If you know your timezone you can just hardcode an offset :-) Good enough for most purposes.

Good luck with your research.
Hi, a follow-up question:

I found sketches for activating the HC-SR04; NTP server pooling; and simple file creation. I am just not sure how I would put these all together in a way that would give me waste levels with a time stamp, that I could then see at a later date when I plug the NodeMCU back into the laptop?

Thanks

developerunify

#8
Feb 12, 2019, 03:57 am Last Edit: Feb 12, 2019, 04:28 am by developerunify
Hi, a follow-up question:

I found sketches for activating the HC-SR04; NTP server pooling; and simple file creation. I am just not sure how I would put these all together in a way that would give me waste levels with a time stamp, that I could then see at a later date when I plug the NodeMCU back into the laptop?

Thanks
I would suggest going for the Lua firmware instead of the usual arduino based C++ firmware of setup() and loop(). Lua firmware is event based programming and lets you handle events asynchronously. Plus it is easier too!

In the initialization phase: setup the HC-SR04 pins as input and output, create an empty file (if doesnt exist already), initialize the RTC (Real Time Clock) with either UTC time, UTC+hard coded offset OR UTC+timezone file (to handle Daylight Saving Time)

So using the RTC module NodeMCU would keep a real time clock in the background. You make up a timer that does distance sensing using HC-SR04 in the handler part and inside the handler itself you open up the file and write the distance value along with timestamp (rtctime.get()). You can just unplug the NodeMCU from the setup and then plug it into the computer and use "ESPlorer" to see the values you had put inside the filesystem (if using in built file system it is SPIFFS or else you can use a simple SD Card too).

I had a similar project and I ended up using microSD card with 512 MB size (pretty cheap around 3$ per SD card). Just a caution if you do not use SD card and end up using the in built flash storage (which is around 4 MB) then that storage has limited "write cycles", that means you can write to any file limited number of times. Once the limit is over your NodeMCU is dead and you have to buy a new NodeMCU, therefore it is just easier to swap out the microSD card instead of buying new NodemCU's everytime. Also it is more easier to remove micro SD card (to check the values) than removing the whole NodeMCU from the setup. Plus microSD cards last longer too.

groundFungus

If you use a 3.3V processor you will need to source an HCSR04 that will work at 3.3V.  Most HCSR04 sensors that I have seen are 5V devices.

wvmarle

I had a similar project and I ended up using microSD card with 512 MB size (pretty cheap around 3$ per SD card).
I pay less for 4GB ones - which are the smallest available here.

For most data logging the built-in flash is more than enough, though.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

joodaa

I pay less for 4GB ones - which are the smallest available here.

For most data logging the built-in flash is more than enough, though.
I was looking into writing SPIFF files, but all the examples I saw were of transferring a file on to the NodeMCU instead of transferring data that the NodeMCU generates onto it. How would I do the latter?

Also, If I were to leave the NodeMCU on sensing levels for 5 days, would you say there is enough built-in flash for that?

DaveEvans

I was looking into writing SPIFF files, but all the examples I saw were of transferring a file on to the NodeMCU instead of transferring data that the NodeMCU generates onto it. How would I do the latter?
google esp8266 spiffs write file



If I were to leave the NodeMCU on sensing levels for 5 days, would you say there is enough built-in flash for that?
Most likely (your data) <<<<<< (SPIFFS file system size), but only you would know for sure.

joodaa

google esp8266 spiffs write file



Most likely (your data) <<<<<< (SPIFFS file system size), but only you would know for sure.

Thank you, I found a code for SPIFFS below, and I think it writes instead of just reading, how would I merge this with the ultrasonic sensor code, to then store the data?

Code: [Select]
#include <ESP8266WiFi.h>
#include <FS.h>   //Include File System Headers
 
const char* filename = "/samplefile.txt";
 
  
void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
 
  //Initialize File System
  if(SPIFFS.begin())
  {
    Serial.println("SPIFFS Initialize....ok");
  }
  else
  {
    Serial.println("SPIFFS Initialization...failed");
  }
 
  //Format File System
  if(SPIFFS.format())
  {
    Serial.println("File System Formated");
  }
  else
  {
    Serial.println("File System Formatting Error");
  }
 
  //Create New File And Write Data to It
  //w=Write Open file for writing
  File f = SPIFFS.open(filename, "w");
  
  if (!f) {
    Serial.println("file open failed");
  }
  else
  {
      //Write data to file
      Serial.println("Writing Data to File");
      f.print("This is sample data which is written in file");
      f.close();  //Close file
  }
 
}
void loop() {
  int i;
  
  //Read File data
  File f = SPIFFS.open(filename, "r");
  
  if (!f) {
    Serial.println("file open failed");
  }
  else
  {
      Serial.println("Reading Data from File:");
      //Data from file
      for(i=0;i<f.size();i++) //Read upto complete file size
      {
        Serial.print((char)f.read());
      }
      f.close();  //Close file
      Serial.println("File Closed");
  }
  delay(5000);
}



wvmarle

file.print(), file.println() and file.write() are the functions that write data to file - in your case, your distance data. Write it at a reasonable interval.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up