HC-SR04 - storing the distance data

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.

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.

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.

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?

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.

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.

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).

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.

developerunify:
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.

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

Good luck with your research.

wvmarle:
If you know your timezone you can just hardcode an offset :slight_smile: 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

joodaa:
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.

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.

developerunify:
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.

wvmarle:
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?

joodaa:
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

joodaa:
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.

DaveEvans:
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?

#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);
}

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.

joodaa:
Thank you, I found a code for SPIFFS below, and I think it writes instead of just reading...

Why do you just "think" it writes? Did you read and understand the code? If you don't trust what you read, then you should try the code, to be sure you understand what it does.

BTW, as a matter of good practice, you should try it even if you think you understand it, just to be sure it works, before you add any code or merge it with some other code (such as your ultrasonic sensor and timestamp code).

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.

Thank you, I put it together and it compiled fine, but when I tried to upload it onto the NodeMCU I got this message:

Sketch uses 291124 bytes (27%) of program storage space. Maximum is 1044464 bytes.
Global variables use 27308 bytes (33%) of dynamic memory, leaving 54612 bytes for local variables. Maximum is 81920 bytes.
Uploading 295264 bytes from C:\Users\jonat\AppData\Local\Temp\arduino_build_221397/write_SPIFFS_sensor.ino.bin to flash at 0x00000000
................................................................................ [ 27% ]
................................................................................ [ 55% ]
................................................................................ [ 83% ]
.....................An error occurred while uploading the sketch

This is the code for writing the sensor data:

#include <ESP8266WiFi.h>
#include <FS.h>   //Include File System Headers
 
const char* filename = "/samplefile.txt";
const int trigPin = 2;  //D4
const int echoPin = 0;  //D3

// defines variables
long duration;
int distance;
 
  
void setup() {
  delay(1000);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  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() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

delay(2500);
}

DaveEvans:
Why do you just "think" it writes? Did you read and understand the code? If you don't trust what you read, then you should try the code, to be sure you understand what it does.

BTW, as a matter of good practice, you should try it even if you think you understand it, just to be sure it works, before you add any code or merge it with some other code (such as your ultrasonic sensor and timestamp code).

The codes, and videos I had looked at previously before I found this one, all had a file in the sketch directory that they wanted to put into the NodeMCU, like image files. I didn't realise I had to distinguish between read and write to get what I was looking for.

joodaa:

An error occurred while uploading the sketch

Just try again.

joodaa:
I didn't realise I had to distinguish between read and write to get what I was looking for.

That's indeed not obvious, just like in the real world it's always hard to distinguish whether you should use "read" and "write", right?

wvmarle:
Just try again.

I have, and I'm getting the same message for other codes now too, even though they're compiling ok.