Sketch
Sketch for DS18B20 Connected to ESP8266
/////////////////////////////////////////////////////////////////////
//**** 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;
TemperatureProperty.setValue(value);
}
void setup(void)
{
Serial.begin(115200);
DS18B20.begin();
if (!DS18B20.getAddress(sensorDeviceAddress, 0))
Serial.println("Unable to find address for Device 0");
DS18B20.setResolution(sensorDeviceAddress, 9);
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("");
//
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
//
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//
//
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("ESP_DS18B20", WiFi.localIP());
device.addProperty(&TemperatureProperty);
adapter->addDevice(&device);
//
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(device.id);
DS18B20.requestTemperatures();
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;
TemperatureProperty.setValue(value);
// Serial.println(temperatureInCelsius);
// delay(1000);
//
adapter->update();
//
}