Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created February 8, 2020 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ShawnHymel/327f9fc4cc57594e4f482b868be02e93 to your computer and use it in GitHub Desktop.
Save ShawnHymel/327f9fc4cc57594e4f482b868be02e93 to your computer and use it in GitHub Desktop.
Mozilla WebThings - ESP8266 Temperature Sensor (Arduino)
/**
* Mozilla WebThings BME280 example
* Date: August 25, 2019
* Author: Shawn Hymel
*
* Notes:
* - Install ArduinoJSON latest v5 (not v6!)
* - Install ESPAsyncTCP
*/
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Thing.h>
#include <WebThingAdapter.h>
const char* ssid = "<YOUR SSID HERE>";
const char* password = "<YOUR WIFI PASSWORD>";
// Hostname used by mDNS
const String mDNSHostname = "weathersensor";
// BME280 sensor (I2C)
Adafruit_BME280 bme280;
// Handle to connection between Thing and Gateway
WebThingAdapter* adapter;
// @type members: Capabilities supported by your Thing
// See schemas: https://iot.mozilla.org/schemas#capabilities
const char* sensorTypes[] = {"TemperatureSensor", nullptr};
// Description of your Thing
// ThingDevice device(id, title, types)
// id: unique identifier for Thing (part of URL: http://<IP>/things/<id>)
// description: string that shows up in Gateway for your Thing
// types: array of @types
ThingDevice sensor("bme280", "BME280 Weather Sensor", sensorTypes);
// Define one or more properties supported by your Thing
// ThingProperty property(id, description, type, atType)
// id: unique identifier for property
// description: user-readable description of property
// type: NO_STATE, BOOLEAN, NUMBER, or STRING
// atType: property @type (https://iot.mozilla.org/schemas#properties)
ThingProperty sensorTemp("temperature", "", NUMBER, "TemperatureProperty");
ThingProperty sensorHumd("humidity", "", NUMBER, nullptr);
ThingProperty sensorPres("pressure", "", NUMBER, nullptr);
void setup() {
// Debug info
Serial.begin(115200);
Serial.print("BME280 Weather Sensor");
// Connect to BME280 sensor
if ( !bme280.begin() ) {
Serial.println("Could not initialize BME280 sensor");
while(1);
}
// Connect to WiFi access point
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println();
// Show connection details
Serial.println("Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Create new WebThings connection handle (default port: 80)
adapter = new WebThingAdapter(mDNSHostname, WiFi.localIP());
// Set units for properties
sensorTemp.unit = "celsius";
sensorHumd.unit = "%";
sensorPres.unit = "hPa";
// Associate properties with device
sensor.addProperty(&sensorTemp);
sensor.addProperty(&sensorHumd);
sensor.addProperty(&sensorPres);
// Associate device with connection
adapter->addDevice(&sensor);
// Start mDNS and HTTP server
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.print(sensor.id);
}
void loop() {
ThingPropertyValue tpVal;
float temp, humd, pres;
// Read sensor values
temp = bme280.readTemperature();
humd = bme280.readHumidity();
pres = bme280.readPressure() / 100.0F;
// Print readings to console
#if 0
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humd);
Serial.println("%");
Serial.print("Pressure: ");
Serial.print(pres);
Serial.println(" hPa");
Serial.println();
#endif
// Update device values
tpVal.number = temp;
sensorTemp.setValue(tpVal);
// UI bug prevents more than one property from being updated
/*tpVal.number = humd;
sensorHumd.setValue(tpVal);
tpVal.number = pres;
sensorPres.setValue(tpVal);*/
// Update all properties and events over the connection
adapter->update();
// Do nothing for a bit
delay(1000);
}
@Catalin84
Copy link

Awesome! Thanks for sharing the code!

I`m not programmer :) I just testet unitl i made it work,maybe with some more exp will manage to check and fix if there are some pb with this type of sensor! Best regards and good work you made for us:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment