Below are the two sketches I demo'ed in the first video:
- Turns LED on/off based on "light" feed downloaded from Adafruit.io
- Sends Temperature and Humidity to Adafruit.io
// 3 minute IoT - Download from Cloud
// Hari Wiguna, 2015
// Libraries - Thanks AdaFruit!
#include <ESP8266WiFi.h>
#include "Adafruit_IO_Client.h"
// Configure WiFi access point & AdaFruit.io account.
#define WLAN_SSID "Firefly24"
#define WLAN_PASS "??????????"
#define ADAFRUIT_IO_KEY "??????????"
// Create an ESP8266 WiFiClient, AdaFruit.io client, and our lightFeed
WiFiClient client;
Adafruit_IO_Client aio = Adafruit_IO_Client(client, ADAFRUIT_IO_KEY);
Adafruit_IO_Feed lightFeed = aio.getFeed("light");
byte ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println(); Serial.println();
Serial.println(F("3 minute IoT - Download from Cloud"));
// Connect to WiFi access point.
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());
aio.begin();
Serial.println(F("Ready!"));
}
void loop() {
//--- READ LIGHT FEED STATE ---
FeedData feedData = lightFeed.receive();
if (feedData.isValid()) {
Serial.print(F("Received value from feed: ")); Serial.println(feedData);
digitalWrite(ledPin, strcmp(feedData,"ON"));
}
else {
Serial.print(F("Failed to receive the latest feed value!"));
}
// Don't be a jerk and abuse AdaFruit.io. Wait a second in-between data requests.
delay(1000);
}
Sends Temperature and Humidity to Adafruit.io
// 3 minute IoT - Upload to Cloud
// Hari Wiguna, 2015
// Libraries - Thanks AdaFruit!
#include <ESP8266WiFi.h>
#include "Adafruit_IO_Client.h"
// Configure WiFi access point & AdaFruit.io account.
#define WLAN_SSID "Firefly24"
#define WLAN_PASS "??????????"
#define ADAFRUIT_IO_KEY "??????????"
// Create an ESP8266 WiFiClient class to connect to the AIO server.
WiFiClient client;
Adafruit_IO_Client aio = Adafruit_IO_Client(client, ADAFRUIT_IO_KEY);
Adafruit_IO_Feed temperatureFeed = aio.getFeed("Temperature");
Adafruit_IO_Feed humidityFeed = aio.getFeed("Humidity");
//--------------- SENSOR -----------------------------
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//----------------------------------------------------
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(); Serial.println();
Serial.println(F("3 minute IoT - Upload to Cloud"));
// Connect to WiFi access point.
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());
// Initialize the Adafruit IO client class (not strictly necessary with the
// client class, but good practice).
aio.begin();
//--- SENSOR ---
dht.begin();
Serial.println(F("Ready!"));
}
void loop() {
//--- READ AND SEND TEMPERATURE ---
int f = dht.readTemperature(true);
Serial.print(F("Temperature = ")); Serial.println(f, DEC);
if (!temperatureFeed.send(f)) Serial.println(F("*** Error writing value to temperatureFeed!"));
//--- READ AND SEND HUMIDITY ---
int h = dht.readHumidity();
Serial.print(F("Humidity = ")); Serial.println(h, DEC);
if (!humidityFeed.send(h))Serial.println(F("*** Error writing value to humidityFeed!"));
//delay(1000); // No need for delay, DHT sensor takes 2 seconds to gather data :-(
}
I have several of my lights controlled by X10, it may be ancient but it works okay. Just have to infrequently replace a module but not unexpected since it is switching main voltage and current. I did find that the transceiver module only relays commands for the same house code as it is set on. I added additional feeds in the code for additional modules so I can control more than one x10 device with my echo. Also used 7400 for the step up to 5 volts since I have several of them lying around. Just connect together 2 inputs of the NAND to form an invertor and series connect two for the buffer/level shifter. Thanks Hari for sharing your idea and code.