Skip to content

Sonoff Sensors

Cory Verellen edited this page Mar 24, 2018 · 13 revisions

This document deals with adding sensors to the tub website. The whole process has a few steps and several files that need to be modified.

You can add as many sensors as you'd like since these networked Sonoff TH sensors use a web API to provide temperature readings. Also, you may add hard wired sensors directly to the Raspberry’s pins. Once configured the new sensor will show readings on the main page of the website if you choose.

I use Sonoff TH10/16 Switch Sensor combo devices flashed with ESPurna firmware. ESPurna was chosen because it is simple, and has the features needed. Sonoff TH sensors with ESPurna don’t need to use MQTT to retrieve temperature. ESPurna has a built-in web API that can be queried to retrieve the reading, this is how we do it here. Other firmware may be used, but the website is expecting temperature reading to be on the second line of a plain text file. Popular Sonoff firmware such as Tasmota format temperature readings in JSON format. If your favorite firmware outputs in JSON, see the following section of cron_min.php to adjust how it handles the data, recode accordingly to your needs.

Recording temp to FILE: /www/html/cron_jobs/cron_min.php line 25

Flashing your device to ESPurna is beyond the scope of this document. Be sure to get your devices up and running with the new firmware before attempting to integrate them into Oasis Spa.

Adding sensors to the web interface

  1. Each DS18B20 sensor wire has a unique identifier that is reported to the ESPurna firmware. Copy the Sensor ID from espurna status screen to the right of the Temperature reading. This will become the Address of the sensor within the Tub website. finding the Sensor ID

  2. Create a unique folder with the Sensor ID. Sensor ID/Address folder should be placed in ‘Sensors’ folder. There are several ways to do it. If your network isn’t configured to find the DNS name OASIS-SPA, then you can substitute the IP address like \\192.168.1.60 or whatever the IP address is. You can rename existing directories to suit if you are just modifying the existing folders. Feel free to delete sensor address folders that you won’t be using, otherwise they will show up in the website.

From SSH: sudo mkdir /var/log/sensors/[SENSOR ID HERE]
UNC path: \\oasis-spa\sensors\[ SENSOR ID HERE]

  1. Once you’ve created the folder, the sensor will show up in the sensor configuration interface. Navigate to Settings > Sensors > click the search button. Then click on the sensor name and fill in details as needed. Don’t change the “Address” field. Repeat as necessary for all sensors you plan on using. If you want the sensor’s readings to show up on the main page, choose Display > Yes. The “+/- Value:” is how you can calibrate the temperature readings of your sensor for accuracy. For example, if you know that your sensor typically reads 1° colder than actual water temp, then insert a value of 1. If it reads 1.5° hotter, use a value of -1.5 to accommodate the difference.

Updating the cron_min.php file

The cron_min file holds the functionality for recording temperature from the sensors. Once a minute it will take a new reading, write it to a file. Later in the cron_min file it will place the reading in the database. By default, I am using 3 sensors. Tub temp, Incoming temp, and ambient air temp. Incoming and Ambient are not necessary for tub operation, but very useful for fine tuning functions.

  1. Navigate to \\oasis-spa\www\html\cron_jobs\ > open cron_min.php. (sudo nano /var/www/html/cron_jobs/cron_min.php)
  2. In the /***** sensor value write*****/ section, place Sensor ID in the two lines. To add additional sensors, copy the three lines and modify accordingly.

/**** [PUT SENSOR NAME/FUNCTION HERE FOR EASY IDENTIFICATION] ****/
echo file_put_contents("/var/log/sensors/28FF55FA83170400/sonoff_th","Current Tub Temperature \n");
exec('curl -s http://192.168.1.63/api/temperature?apikey=9E2CA07C2C799F9C >> /var/log/sensors/28FF55FA83170400/sonoff_th);

  1. In the above lines, replace the new API KEY in the URL. The sensor API key can be found within ESPurna > Admin section.
  2. Change the IP address to the IP of your ESPurna Sensor.
  3. Save the file and go back to the main screen of the website. It may take a minute to update, but you should see sensor values show up if all your hardware is on and functioning.