Overview
The goal was to be able to control the window fan to turn on when the temperature outside reached higher than the inside temp (for the summer) and turn on when it was cooler outside. Adding a PIR also would enable turning on lights or alerts when it detected motion. All of this would use io.adafruit.com and IFTTT.
Bill of Materials
Description | Price | Link |
---|---|---|
IFTTT complient smart plugs | $6/ea | https://www.amazon.com/gp/product/B07FVST9YN/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 |
Azure Sphere MT Starter Kit | $83 | https://www.newark.com/avnet/aes-ms-mt3620-sk-g/starter-kit-arm-cortex-a7-cortex/dp/02AH9206?gclid=Cj0KCQjwiYL3BRDVARIsA… |
Mikroe-2979 Thermo 7 | $16 | https://www.newark.com/mikroelektronika/mikroe-2979/thermo-7-click-board/dp/77AC3696?COM=eml-AzureSphere-102_94716-MIKRO… |
PIR | $2/ea | https://www.amazon.com/HC-SR501-Pyroelectric-Infrared-Detector-Modules/dp/B07R5Q2QKN/ref=sr_1_7?dchild=1&keywords=pir+se… |
ESP8266 | $5/ea | https://www.amazon.com/gp/product/B010N1SPRK/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 |
Code (Starting)
I started the coding with arduino and was able to log temperatures and trigger events through IFTTT. This is as far as I have gotten for now. Search --- for fields that need to be replaced.
You can subscribe to accuweather to get outdoor temperatures using their api. I also tried to use open weather maps' api, but it was hanging up my code. Hence why it is commented out. This code can be incorporated into the Azure sphere using this helpful tutorials:
One comment I will make on the Azure that took me several days to debug is that to log in and claim the Azure Sphere you must create an account as in the above link, the important part is that it creates a really long username with "onmicrosoft" in it. This is the username you need to log into visual studio and the Azure Sphere.
Adafruit has many helpful tutorials for using their service https://learn.adafruit.com/welcome-to-adafruit-io
#include <ESP8266WiFi.h> #include "Adafruit_Si7021.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include <TridentTD_OpenWeather.h> #include <JsonListener.h> #include "AccuWeatherLibrary.h" /************************* WiFi Access Point *********************************/ #define WLAN_SSID "---" #define WLAN_PASS "---" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 // use 8883 for SSL #define AIO_USERNAME "---" #define AIO_KEY "---" /************************* OpenWeather Setup *********************************/ #define OpenWeather_APIKEY "---" #define lat --- #define lon --- #define timezone ---- TridentTD_OpenWeather myPlace(OpenWeather_APIKEY); /************************* AccuWeather Setup *********************************/ AccuweatherCurrentData dataC; Accuweather aw("3fDpgMJiWzq6YPYKZuP0TCrdWsCMjh5X", ---, "en-us", false); /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'CTem' for publishing. // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> Adafruit_MQTT_Publish CTem = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/CTemp"); Adafruit_MQTT_Publish OTem = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/OTemp"); Adafruit_MQTT_Publish CHu = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/CHum"); /*************************** Sketch Code ************************************/ Adafruit_Si7021 sensor = Adafruit_Si7021(); // Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect(); void setup() { Serial.begin(115200); delay(10); Serial.println(F("Adafruit MQTT demo")); // Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); sensor.begin(); myPlace.setLocation(lat, lon ); myPlace.setUnit("imperial"); } int lastT = 0; uint8_t lastH = 0; uint8_t i = 0; int OutTemp = 255; void loop() { if(i>=3){i=1;} else{i++;} // myPlace.weatherNow(); Serial.println("myPlace"); MQTT_connect(); uint8_t Hum = sensor.readHumidity(); float Tem = sensor.readTemperature() * 9 / 5 + 32; if (i > 1) { // OutTemp = myPlace.readTemperature(); // Serial.println("MyTemp"); } else { int ret = aw.getCurrent(&dataC); if (ret != 0){ Serial.println("ERROR"); Serial.println(ret); return; } while (aw.continueDownload() > 0) { } OutTemp = dataC.Temperature; Serial.println("AccuTemp"); } // this is our 'wait for incoming subscription packets' busy subloop // try to spend your time here Serial.print((int) Tem); Serial.println(Hum); // Now we can publish stuff! if (lastT != (int) (Tem * 5)) { //make it round to a certain value Serial.print(F("\nSending CTem val ")); Serial.print(Tem); Serial.print("..."); if (! CTem.publish(Tem)) { Serial.println(F("Temp Failed ")); } else { Serial.println(F("Temp OK! ")); } } if (! OTem.publish(OutTemp)) { Serial.println(F("Outdoor Temp Failed ")); } else { Serial.println(F("Outdoor Temp OK! ")); } if (Hum != lastH) { if (! CHu.publish(Hum)) { Serial.println(F("Hum Failed")); } else { Serial.println(F("Hum OK!")); } } // ping the server to keep the mqtt connection alive // NOT required if you are publishing once every KEEPALIVE seconds /* if(! mqtt.ping()) { mqtt.disconnect(); } */ lastT = (int) (Tem * 5); lastH = Hum; delay(600000);//make it go to sleep for 10 mins, no need to use up space for free accounts on io.adafruit.com } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); }