SHARE
TWEET

Untitled

a guest Oct 2nd, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <Thing.h>
  3. #include <WebThingAdapter.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. #define ONE_WIRE_BUS 0
  8.  
  9. const char* ssid = " /* snip */ ";
  10. const char* password = " /* snip */ ";
  11.  
  12. const float threshold = 0.5;
  13. float last_temperature = 0.0;
  14.  
  15. const char* deviceTypes[] = {"TemperatureSensor", "Temperature Sensor", nullptr};
  16. ThingDevice device("AnalogSensorDevice", "Temperature Sensor", deviceTypes);
  17. ThingProperty property("temperature", "Analog Input pin", NUMBER, "TemperatureProperty");
  18. WebThingAdapter* adapter = NULL;
  19.  
  20. OneWire oneWire(ONE_WIRE_BUS);  
  21. DallasTemperature sensors(&oneWire);
  22.  
  23. void setup(void) {
  24.   sensors.begin();
  25.  
  26.   Serial.begin(115200);
  27. #if defined(ESP8266) || defined(ESP32)
  28.   WiFi.mode(WIFI_STA);
  29. #endif
  30.   WiFi.begin(ssid, password);
  31.   Serial.println("");
  32.  
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.   }
  37.  
  38.   Serial.println("");
  39.   Serial.print("Connected to ");
  40.   Serial.println(ssid);
  41.   Serial.print("IP address: ");
  42.   Serial.println(WiFi.localIP());
  43.  
  44.   adapter = new WebThingAdapter("analog-sensor", WiFi.localIP());
  45.   device.addProperty(&property);
  46.   adapter->addDevice(&device);
  47.   adapter->begin();
  48.  
  49.   Serial.println("HTTP server started");
  50.   Serial.print("http://");
  51.   Serial.print(WiFi.localIP());
  52.   Serial.print("/things/");
  53.   Serial.println(device.id);  
  54. }
  55.  
  56. void loop(void)
  57. {
  58.   sensors.requestTemperatures();
  59.   float temperature = sensors.getTempCByIndex(0);
  60.  
  61.   float diff = temperature - last_temperature;
  62.   if (diff < 0)
  63.     diff *= -1.0;
  64.  
  65.   if (diff >= threshold) {
  66.  
  67.     Serial.print("Recording temperature: ");
  68.     Serial.println(temperature);
  69.  
  70.     ThingPropertyValue sensorValue;
  71.     sensorValue.number = temperature;
  72.     property.setValue(sensorValue);
  73.  
  74.     last_temperature = temperature;
  75.   }
  76.  
  77.   adapter->update();
  78.  
  79.   delay(1000);
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top