Data is going to the web service

Well Depth Sensing: Temperatures are in the database

In my previous post, the ESP8266 Arduino Sketch was reading 12 temperature sensors. In this post, I describe the progress on the web side of things: the PHP web service that stores temperatures in an SQL database.

I’m really happy with the ESP8266 so far: it reliably connects to a given WiFi access point, and now it’s reliably doing an HTTPS POST of data to my PHP web service.

Well Temperature Dataflow
Well Temperature Dataflow

The tricky part of getting an HTTPS request to work from the ESP8266 was finding the Fingerprint of the site I wanted to send data to. I could find instructions on how to copy a site’s SSL Certificate using Chrome, and I could find how generate a Fingerprint from an SSL Certificate, but the file formats didn’t seem right. Eventually I found that the Base-64 encoded X.509 .cer file was exactly what openssl wanted as a .crt file; that is, the one set of data can be stored with (at least) two different file extensions. The details are in Diary.odt in my Github Repostory for the project.

Once I had the Fingerprint, it was easy to add a client.verify() call to the Sketch, to verify that the HTTPS connection was with the host I wanted.

Once I had a connection to the server, it was relatively easy to find how to send HTTPS POST parameters as the Content of the HTTPS request.

On the server side, I wrote a PHP script to receive the HTTPS POST request and its parameters (welldepthpost.php in the Github repo). By the way, I’m using PHP because I haven’t been able to get my (commercially hosted) web site to support Python.  Once I could see – through debug output of the script – that the temperature sensors’ data was getting to the PHP page, all I had left was to add code to do an Insert into an SQL database.

Again, it was pretty straightforward to write the SQL interface code, based on an older PHP script I’d written a few years ago. The big change was the all the mysql_*() calls in PHP have been deprecated and replaced by (mostly!) compatible mysqli_*() calls. I say “mostly” because I found the hard way that the parameters in mysqli_query() were in the opposite order from those in mysql_query().

So now my little ESP8266 periodically reads the temperature from 12 temperature sensors and sends them to my web service, which (after validating the data) puts them in a database. The Sketch has been running for over 4 hours now with no problems – whee!

The diary that shows my wandering path to success, and the code, are in my WellDepthTemperature git repo.

In my next post, I design the 3D printed junction box for the 1-wire bus.