Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Thing.h>
- #include <WebThingAdapter.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 0
- const char* ssid = " /* snip */ ";
- const char* password = " /* snip */ ";
- const float threshold = 0.5;
- float last_temperature = 0.0;
- const char* deviceTypes[] = {"TemperatureSensor", "Temperature Sensor", nullptr};
- ThingDevice device("AnalogSensorDevice", "Temperature Sensor", deviceTypes);
- ThingProperty property("temperature", "Analog Input pin", NUMBER, "TemperatureProperty");
- WebThingAdapter* adapter = NULL;
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- void setup(void) {
- sensors.begin();
- Serial.begin(115200);
- #if defined(ESP8266) || defined(ESP32)
- WiFi.mode(WIFI_STA);
- #endif
- 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("analog-sensor", WiFi.localIP());
- device.addProperty(&property);
- adapter->addDevice(&device);
- adapter->begin();
- Serial.println("HTTP server started");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.print("/things/");
- Serial.println(device.id);
- }
- void loop(void)
- {
- sensors.requestTemperatures();
- float temperature = sensors.getTempCByIndex(0);
- float diff = temperature - last_temperature;
- if (diff < 0)
- diff *= -1.0;
- if (diff >= threshold) {
- Serial.print("Recording temperature: ");
- Serial.println(temperature);
- ThingPropertyValue sensorValue;
- sensorValue.number = temperature;
- property.setValue(sensorValue);
- last_temperature = temperature;
- }
- adapter->update();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement