This video take a look at the WeMos D1: a Wi-Fi enabled Arduino compatible board based on the ESP8266 chip. The price of it is so tempting, less than 9$.
The board looks like an ordinary Arduino board. The dimensions and the pin layouts are exactly the same. So, this board is compatible with all the existing shields for Arduino. But don’t expect them to work at once, since the libraries available for the ESP8266 chip are few so far. The board, instead of an ATMEGA chip that standard Arduino boards use, use the impressive ESP8266 WiFi chip!
The ESP8266EX chip that the WeMos D1 board uses offers:
• A 32 bit RISC CPU running at 80MHz
• 64Kb of instruction RAM and 96Kb of data RAM
• 4MB flash memory! Yes that’s correct, 4MB!
• Wi-Fi
• 16 GPIO pins
• I2C,SPI
• I2S
• 1 ADC
The on-board LED share with Serial TX pin. So we have to end Serial and set the on-board LED as output after connected to Adafruit IO server.
AdafruitIoTestOnOff.ino
/***************************************************
* Modified from:
* https://github.com/openhomeautomation/adafruit-io-esp8266/tree/master/esp8266_lamp_module
*
* =================================================
Adafruit ESP8266 Lamp Controller Module
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
Adafruit IO example additions by Todd Treece.
MIT license, all text above must be included in any redistribution
****************************************************/
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// WiFi parameters
#define WLAN_SSID "testAP"
#define WLAN_PASS "12345678"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "your AIO username"
#define AIO_KEY "your AIO key"
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
/****************************** Feeds ***************************************/
// Setup a feed called 'OnOff' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/OnOff";
Adafruit_MQTT_Subscribe OnOff = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
/*************************** Sketch Code ************************************/
// connect to adafruit io via MQTT
void connect() {
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}
void setup() {
Serial.begin(115200);
Serial.println(F("Adafruit IO Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// listen for events on the OnOff feed
mqtt.subscribe(&OnOff);
// connect to adafruit io
connect();
delay(500);
//In-order to control the on-board LED,
//have to end Serial
Serial.end();
// Set On-board LED to output
pinMode(BUILTIN_LED, OUTPUT);
}
void loop() {
Adafruit_MQTT_Subscribe *subscription;
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// this is our 'wait for incoming subscription packets' busy subloop
while (subscription = mqtt.readSubscription(1000)) {
// we only care about the OnOff events
if (subscription == &OnOff) {
// convert mqtt ascii payload to int
char *value = (char *)OnOff.lastread;
//Serial.print(F("Received: "));
//Serial.println(value);
// Apply message to OnOff
String message = String(value);
message.trim();
if (message == "ON") {
digitalWrite(BUILTIN_LED, LOW);
//Serial.println("On-board LED ON");
}
if (message == "OFF") {
digitalWrite(BUILTIN_LED, HIGH);
//Serial.println("On-board LED OFF");
}
}
}
}