Humidity and temperature data on IoT

It’s been a while since my last post, apologies for that. It must be that the corona restrictions also make your brain run slower.

Anyhow, this time I thought to show you how to take the data from the previous example, a dual quadruple 7 segment display and transfer it to a text file on the Internet. Since you already have the measurement data for humidity and temperature, and the system is connected to get the network time signal for the clock, all we need to do is figure out how to send data to the Internet, and how to have the Internet to write the data into a text file.

To do this in its simplest manifestation, you need a hosting server running PHP. There are a multitude of sites out there that allow you to run PHP scripts for free, though most of them will place ads on your page. Some claim not to do even that, like Infinityfree, and with a server like that you are good to go. So, let’s assume that you have already built the clock I presented in the previous entry. I will then go through just the parts you need to add to the code to send data to your hosted PHP file, and then the PHP file itself.

The INO file changes

In the previous example, there is this bit of code that gets you the humidity and temperature from the DHT11 sensor:

float h = dht.readHumidity(); //Read humidity 
String humiStr = String(h); 
float t = dht.readTemperature();// Read temperature as Celsius (the default) 
String tempStr = String(t); 

Now you have the strings that you want to write to the text file in the strings humiStr and tempStr. If you want to use Fahrenheit degrees, use

float f = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)

To be able to send data to the Internet every five minutes, you need to create a couple of things up in the definitions of the file:

unsigned long timerDelay = 300000;
const char* serverName = "http://[your_server_URL]/[your_server_path/timeandhumidity.php";

The timer delay is for 300 seconds, ie. five minutes in milliseconds. The serverName variable holds the path to the server, folder on it, and file name to refer to in the sending operation.

The next step is to construct a message to be sent to the PHP server via the HTTP POST Method. I need to remind you that what you will see here is totally and utterly without any sense of data security, so to do this professionally, you’d need to add some safeguards. But as this is a basic example, I will leave the security issues to you.

The message looks like this:

//sending data to the server:
if ((millis() - lastTime) > timerDelay) {
Serial.println("Sending data.");
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
// create a HTTP request with a content type: text/plain
http.addHeader("Content-Type", "text/plain");
String httpRequestData = "Temperature: " + tempStr + " " + "Humidity: " + humiStr;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.println("HTTP Response code: ");
// Free resources
else {
Serial.println("WiFi Disconnected");
lastTime = millis();

So, the first line figures out if five minutes have elapsed since the last message. Then it checks to see of you have a valid connection to the WiFi. The object “http” is of the type HTTPclient, and it has a header property and a payload attached to it as httpRequestData. httpBegin makes contact with the server you specified in serverName up in the setup part of the file. As you see, the payload is put together as the label “Temperature: “, a plus sign to add the tempStr string to it, add a space and then the label “Humidity: ” and the plus sign and humiStr.

The actual sending of the data is handled in the line

int httpResponseCode = http.POST(httpRequestData) 

The POST method of the http object returns a number, which you can use to figure out if the transmission worked. After that the http object is removed to save memory, and the variable lastTime updated to take the current moment and count another five minutes.

The PHP end

The PHP file on the web server is really the simplest possible solution. It listens to the standard iput queue for any messages, gets the date and time from the server system clock, opens a text file for appending, and adds a line with the date, time and the string you sent:

    $_POST = file_get_contents('php://input');
    $myYear = date("d.m.Y");
    $myHour = date("H:i");
    $myFile = "your_file_name_here.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $myYear . " " .  $myHour);
    fwrite($fh, "\t");
    fwrite($fh, $_POST);
    fwrite($fh, "\n");    

$_POST is a variable that takes the string you send it. There is no quality control here, anything will work. The next two lines create date and time variables, and the $myFile is a file object to contain the file to write into. $fh is a file handle, and when opened with a ‘a’ parameter, all entries will be appended into the file. If something goes wrong, it would display the message ‘can’t open file’. Then you have four write operations into the file, with /t signifying a tab, and /n signifying a newline character to make the file neat.

The last line merely closes the file. The file looks like this:

22.09.2020 09:18	Temperature: 22.20 Humidity: 60.00
22.09.2020 09:23	Temperature: 22.20 Humidity: 60.00
22.09.2020 09:28	Temperature: 22.20 Humidity: 60.00
22.09.2020 09:34	Temperature: 22.90 Humidity: 59.00
22.09.2020 09:39	Temperature: 23.20 Humidity: 57.00
22.09.2020 09:44	Temperature: 23.40 Humidity: 57.00
22.09.2020 09:49	Temperature: 23.50 Humidity: 56.00
22.09.2020 09:54	Temperature: 23.60 Humidity: 55.00
22.09.2020 09:59	Temperature: 23.60 Humidity: 55.00
22.09.2020 10:04	Temperature: 23.70 Humidity: 55.00

Since the temperature-humidity string was constructed in the sending application, it doesn’t need to be parsed here. If you want to be Excel-friendly, it may be a good idea to send the string with tabs between the labels and the temp and humidity values, but you can just as well use Excel’s text-to-columns functionality to separate the values into columns.

The video below is my implementation of the time, date, temperature and humidity measurement, and this device (still lacking a 3D printed box) sends the data you see above.


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.