DS18B20 as webthing

(Petehelm) #1

I am very interested in using the ds18b20 temperature sensor connected to a Raspberry-pi as part of the Mozilla IOT webthings. I am new to Mozilla IOT and a novice with Raspberry-pi and would like to use a ds18b20, which I have connected to the GPIO of the Raspberry-Pi pin 7, as a IOT webthing.
Do any existing add-ons or webthing have this capability?
Thanks Peter

(Dave Hylands) #2

I’m not aware of any existing add-ons. There are 2 ways that this could be done.
1 - Create an addon which talks to the ds18b20
2 - Create a standalone server which advertises itself as a webthing. We have libraries for a variety of languages that does most of the standalone server portion, and then you just need to plug in the code which interfaces with the ds18b20. This server could run on the same raspberry pi as the gateway.

(Galicz Miklós) #3

A lot can depend on the environment as well…

For example: are you planning to hook-up the DS18B20 to the RPI you’re running the gateway from or a totally separate device? If the latter, the RPI would be wasted on it if it doesn’t serve other purposes… If it’s only task would be to read and forward that 1 sensor’s data, you’re better off with and ESP8266 or any other Arduino based, WiFi enabled micro controller…
If you are planning to tie the sensor to the gateway directly, a NodeJS or Python based local service (WebThing implementation) would be your best bet.

You’ve already found my library for the ESP8266/ESP32/Arduino implementation :smiley:

(Petehelm) #4

Hi thanks for the reply and suggestions for a way forward with my project.
I am building a Swimming pool solar heating controller and elected to go with standalone ESP8266 servers which advertises themselves as webthings.
After a lot of reading up on webthing examples and the creation of many test sketches, I eventually created a working solution using DS18B20 sensors.
I have a rooftop temp sensor and a pool water temp sensor communicating with my Pi Zero W which in turn switches a relay to control the pool pump.
Now I wish to create a rule that checks the roof temp is say 5 Degrees above pool water temp before switching on the pump.
Does the rule engine allow for addition and subtraction such that I can create a value for Temp_Roof +5?
Such that If Temp_Roof +5 is > Pool_temp switch relay.
Also in the rules visual display is it possible to show the recorded temperature value rather than just a thing object?
Thanks Peter

My example Sketch -
//**** ESP8266_DS18B20_Moz06
//**** 30/12/2018 ********
//**** P Helm *********
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Thing.h>
#include <WebThingAdapter.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266wifi.h>
#include <ArduinoJson.h>
#define ONE_WIRE_BUS 5 // Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5

OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire sensors
DallasTemperature DS18B20(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress sensorDeviceAddress;

//TODO: Hardcode wifi credentials here (and keep it private)
const char* ssid = “mynetwork”;
const char* password = “password”;
const char *deviceTypes[] = {“TempSensor”, “Sensor”, nullptr};
ThingDevice device(“ESP_DS18B20”,“Roof Temp”, deviceTypes);
// The ThingDevice represents the physical object we want to put on the web;
ThingProperty TemperatureProperty(“Temperature”, “DS18B20 Temp”, NUMBER, “TemperatureProperty”);
// ThingProperty variable_name( id, description, type, @type, label, unit, writable );
WebThingAdapter *adapter;
// The WebThingAdapter knows how to speak to the Web of Things API with our Gateway and handles all the translation necessary for the Gateway to discover and interact with the ThingDevice;
void readData(DeviceAddress deviceAddress)
float temperatureInCelsius(NAN);
temperatureInCelsius = DS18B20.getTempC(sensorDeviceAddress);
ThingPropertyValue value;
value.number = temperatureInCelsius;
void setup(void)


if (!DS18B20.getAddress(sensorDeviceAddress, 0))
Serial.println(“Unable to find address for Device 0”);

DS18B20.setResolution(sensorDeviceAddress, 9);

Serial.print(“Connecting to “);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


Serial.print("Connected to ");
Serial.print(“IP address: “);
adapter = new WebThingAdapter(“ESP_DS18B20”, WiFi.localIP());
Serial.println(“HTTP server started”);

float temperatureInCelsius = DS18B20.getTempC(sensorDeviceAddress);

Serial.print("Temperature: “);
Serial.print(temperatureInCelsius, 4);
Serial.print(” Celsius, ");

/////////////////////////////// output //////////////////////////////////////////
void loop()

DS18B20.requestTemperatures(); //need to update value at each loop
float temperatureInCelsius(NAN);

// TemperatureProperty to add
temperatureInCelsius = DS18B20.getTempC(sensorDeviceAddress);
ThingPropertyValue value;
value.number = temperatureInCelsius;
// Serial.println(temperatureInCelsius);
// delay(1000);

(Petehelm) #5

Thanks Meki, your post on ds18b20-webthing development was very helpful during my steep learning curve on writing sketches for the ESP8266.

(Galicz Miklós) #6

I hope I can get the time to update the my project with comments and images in the coming days… I know it would be helpful, and I’m really sorry for posting a bare bones project…

(Galicz Miklós) #7

Regarding your questions about the rule engine, It can handle only simple rules like temperature ranges, so, no dynamic references (some temp - 5 ) atm.
Same goes for Sensor logs. There’s an ongoing debate on github about how it would be the best.
I hope these features will become available as we’re getting closer to v1.0