Guest User

Untitled

a guest
Apr 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include "DHT.h"
  2. #include <readiness_io.h>
  3. #include <Ticker.h>
  4. #include "config.h"
  5.  
  6. const int LED_PIN = 0; // The pin connecting the LED (D3)
  7. const byte INTERRUPT_PIN = 12; // The pin connect the test button (D6)
  8.  
  9. volatile byte interrupt = 0;
  10.  
  11. #define DHTPIN 4 // The Digital Pin the sensor is connected too (D2)
  12. #define DHTTYPE DHT22 // Designated the type of DHT Sensor
  13.  
  14. DHT dht(DHTPIN, DHTTYPE);
  15. readiness_io client(CHANNEL_ID, TOPIC, SENSOR_ID, VERSION, FORMAT);
  16. Ticker timer;
  17.  
  18. void setup() {
  19. pinMode(LED_PIN, OUTPUT);
  20. pinMode(BUILTIN_LED, OUTPUT);
  21. digitalWrite(BUILTIN_LED, HIGH);
  22. pinMode(INTERRUPT_PIN, INPUT_PULLUP);
  23.  
  24. Serial.begin(115200);
  25. Serial.setTimeout(2000);
  26. while(!Serial) { } // Wait for serial to initialize.
  27. Serial.println("Device Started");
  28.  
  29. Serial.print("Connecting to ");
  30. Serial.println(WIFI_SSID);
  31. client.wifiConnection(WIFI_SSID, WIFI_PASS);
  32.  
  33. attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), buttonInterrupt, FALLING);
  34. timer.attach(UPDATE_RATE, writeToServer);
  35.  
  36. client.testConnection();
  37. }
  38.  
  39. void buttonInterrupt() {
  40. interrupt++;
  41. }
  42.  
  43. /* Interrupt timer for sending data to the Readiness.io server */
  44. void writeToServer(){
  45. interrupt++;
  46. }
  47.  
  48. void loop() {
  49. if(interrupt>0){
  50.  
  51. /* Read from the DHT22 Sensor */
  52. float h = dht.readHumidity();
  53. float t = dht.readTemperature();
  54.  
  55. /* Write data to a json string and send to the server. */
  56. String weather = "\"humidity\":" + String(h) + ",";
  57. weather += "\"temperature\":" + String(t) ;
  58.  
  59. /* Publish data to the readiness_io network */
  60. client.publishCustom(weather);
  61. /* Reset the interrupt variable until the timer interrupt or push button sets it off */
  62. interrupt=0;
  63.  
  64. }
  65.  
  66. }
Add Comment
Please, Sign In to add comment