Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define IO_USERNAME "A***********m"
- #define IO_KEY "1dc2650223*********338fb****286"
- #define WIFI_SSID "---------------"
- #define WIFI_PASS "---------------"
- #include "AdafruitIO_WiFi.h"
- AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #define DHTPIN D6
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- AdafruitIO_Feed *temperature = io.feed("temperature");
- AdafruitIO_Feed *humidity = io.feed("humidity");
- AdafruitIO_Feed *command = io.feed("onoff");
- AdafruitIO_Feed *command01 = io.feed("onoff01");
- unsigned long startMillis;
- unsigned long currentMillis;
- const unsigned long period = 10000;
- bool current = true;
- bool last = true;
- void setup() {
- Serial.begin(115200);
- startMillis = millis();
- dht.begin();
- pinMode(D8, OUTPUT);
- pinMode(D7, OUTPUT);
- pinMode(D5, INPUT);
- command->onMessage(handleMessage);
- command01->onMessage(handleMessage01);
- Serial.print("Connecting to Adafruit IO");
- io.connect();
- Serial.print("Connected to Adafruit IO");
- while (io.status() < AIO_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println();
- Serial.println(io.statusText());
- }
- void loop() {
- io.run();
- currentMillis = millis();
- if (currentMillis - startMillis >= period)
- {
- startMillis = currentMillis;
- float hum = dht.readHumidity();
- float temp = dht.readTemperature();
- Serial.print("celsius: ");
- Serial.print(temp);
- Serial.println("C");
- Serial.print("Humidity: ");
- Serial.print(hum);
- Serial.println("%");
- Serial.println("...................................");
- temperature->save(temp);
- humidity->save(hum);
- }
- if(digitalRead(D5) == HIGH)
- current = false;
- else
- current = true;
- if(current == last)
- return;
- Serial.print("sending button -> ");
- Serial.println(current);
- command->save(current);
- last = current;
- }
- void handleMessage(AdafruitIO_Data *data) {
- int command = data->toInt();
- if (command == 1){ //light up the LED
- Serial.print("received <- ");
- Serial.println(command);
- digitalWrite(D7, LOW);
- } else {
- Serial.print("received <- ");
- Serial.println(command);
- digitalWrite(D7, HIGH);
- }
- }
- void handleMessage01(AdafruitIO_Data *data) {
- int command01 = data->toInt();
- if (command01 == 1){ //light up the LED
- Serial.print("received <- ");
- Serial.println(command01);
- digitalWrite(D8, LOW);
- } else {
- Serial.print("received <- ");
- Serial.println(command01);
- digitalWrite(D8, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement