Advertisement
Guest User

ez szar

a guest
Oct 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #define IO_USERNAME "A***********m"
  2. #define IO_KEY "1dc2650223*********338fb****286"
  3.  
  4. #define WIFI_SSID "---------------"
  5. #define WIFI_PASS "---------------"
  6.  
  7. #include "AdafruitIO_WiFi.h"
  8. AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
  9.  
  10. #include <Adafruit_Sensor.h>
  11. #include <DHT.h>
  12.  
  13. #define DHTPIN D6
  14. #define DHTTYPE DHT22
  15. DHT dht(DHTPIN, DHTTYPE);
  16.  
  17. AdafruitIO_Feed *temperature = io.feed("temperature");
  18. AdafruitIO_Feed *humidity = io.feed("humidity");
  19. AdafruitIO_Feed *command = io.feed("onoff");
  20. AdafruitIO_Feed *command01 = io.feed("onoff01");
  21.  
  22. unsigned long startMillis;
  23. unsigned long currentMillis;
  24. const unsigned long period = 10000;
  25. bool current = true;
  26. bool last = true;
  27.  
  28. void setup() {
  29. Serial.begin(115200);
  30. startMillis = millis();
  31. dht.begin();
  32.  
  33. pinMode(D8, OUTPUT);
  34. pinMode(D7, OUTPUT);
  35. pinMode(D5, INPUT);
  36. command->onMessage(handleMessage);
  37. command01->onMessage(handleMessage01);
  38.  
  39. Serial.print("Connecting to Adafruit IO");
  40. io.connect();
  41. Serial.print("Connected to Adafruit IO");
  42.  
  43. while (io.status() < AIO_CONNECTED) {
  44. Serial.print(".");
  45. delay(500);
  46. }
  47.  
  48. Serial.println();
  49. Serial.println(io.statusText());
  50. }
  51.  
  52. void loop() {
  53. io.run();
  54. currentMillis = millis();
  55. if (currentMillis - startMillis >= period)
  56. {
  57. startMillis = currentMillis;
  58. float hum = dht.readHumidity();
  59. float temp = dht.readTemperature();
  60.  
  61. Serial.print("celsius: ");
  62. Serial.print(temp);
  63. Serial.println("C");
  64. Serial.print("Humidity: ");
  65. Serial.print(hum);
  66. Serial.println("%");
  67. Serial.println("...................................");
  68. temperature->save(temp);
  69. humidity->save(hum);
  70. }
  71. if(digitalRead(D5) == HIGH)
  72. current = false;
  73. else
  74. current = true;
  75.  
  76. if(current == last)
  77. return;
  78.  
  79. Serial.print("sending button -> ");
  80. Serial.println(current);
  81. command->save(current);
  82.  
  83. last = current;
  84. }
  85. void handleMessage(AdafruitIO_Data *data) {
  86.  
  87. int command = data->toInt();
  88.  
  89. if (command == 1){ //light up the LED
  90. Serial.print("received <- ");
  91. Serial.println(command);
  92. digitalWrite(D7, LOW);
  93. } else {
  94. Serial.print("received <- ");
  95. Serial.println(command);
  96. digitalWrite(D7, HIGH);
  97. }
  98. }
  99.  
  100. void handleMessage01(AdafruitIO_Data *data) {
  101.  
  102. int command01 = data->toInt();
  103.  
  104. if (command01 == 1){ //light up the LED
  105. Serial.print("received <- ");
  106. Serial.println(command01);
  107. digitalWrite(D8, LOW);
  108. } else {
  109. Serial.print("received <- ");
  110. Serial.println(command01);
  111. digitalWrite(D8, HIGH);
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement