Introduction: Arduino IDE, ESP8266, Easy IOT, One Relay.
Simple outline for a single internet switch. Using arduino esp8266 and EasyIOT
My style is minimalist. For now.
Don't f* with electricity!
Step 1: Arduino Enviroment
Update the board manager with a custom URL. Open up Arduino, then go to the Preferences (File > Preferences). Copy URL into the “Additional Board Manager URLs” text box:
http://arduino.esp8266.com/stable/package_esp8266c... Go to Tools > Boards > Boards Manager. Look for esp8266. Click on that entry, then select Install.
Install MQTT library, Read this http://iot-playground.com/blog/2-uncategorised/77-...
Go here https://github.com/iot-playground/EasyIoT-Cloud, click clone download.
OPen Zip copy esp-mqtt to documnets/arduino/libraries folder.
In arduino click, include library, manage library. See picture
Now you can add it to the project. #include <mqtt.h>
Step 2: Code
Modified this
https://github.com/iot-playground/EasyIoT-Cloud/bl...
#include ESP8266WiFi.h //filename needs to be in <>
#include MQTT.h //filename needs to be in <>
#define AP_SSID "your ssid"
#define AP_PASSWORD "your pASSWORD"
#define EIOTCLOUD_USERNAME "uSERNAM" //https://easyiot-cloud.com
#define EIOTCLOUD_PASSWORD "PASSWORD"
// create MQTT object
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
#define DO_TOPIC "/Sensor.Parameter1" //Get this from Easy IOT
#define PIN_DO_2 2 // DO pin2
#define MODULE_ID_2 2
MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);
void setup() { Serial.begin(115200);
WiFi.mode(WIFI_STA); WiFi.begin(AP_SSID, AP_PASSWORD); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(AP_SSID); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); };
Serial.println("WiFi connected"); Serial.println("Connecting to MQTT server");
//set client id // Generate client name based on MAC address and last 8 bits of microsecond counter String clientName; uint8_t mac[6]; WiFi.macAddress(mac); clientName += macToStr(mac); clientName += "-"; clientName += String(micros() & 0xff, 16); myMqtt.setClientId((char*) clientName.c_str());
Serial.print("MQTT client id:"); Serial.println(clientName);
// setup callbacks myMqtt.onConnected(myConnectedCb); myMqtt.onDisconnected(myDisconnectedCb); myMqtt.onPublished(myPublishedCb); myMqtt.onData(myDataCb); //////Serial.println("connect mqtt..."); myMqtt.setUserPwd(EIOTCLOUD_USERNAME, EIOTCLOUD_PASSWORD); myMqtt.connect();
delay(500);
pinMode(PIN_DO_2, OUTPUT);
subscribe(); }
void loop() { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } }
String macToStr(const uint8_t* mac) { String result; for (int i = 0; i < 6; ++i) { result += String(mac[i], 16); if (i < 5) result += ':'; } return result; }
void subscribe() { myMqtt.subscribe("/" + String(MODULE_ID_2) + DO_TOPIC); //DO 2 }
void myConnectedCb() { Serial.println("connected to MQTT server"); subscribe(); }
void myDisconnectedCb() { Serial.println("disconnected. try to reconnect..."); delay(500); myMqtt.connect(); }
void myPublishedCb() { Serial.println("published."); }
void myDataCb(String& topic, String& data) { if (topic == String("/"+String(MODULE_ID_2)+ DO_TOPIC)) { if (data == String("1")) digitalWrite(PIN_DO_2, HIGH); else digitalWrite(PIN_DO_2, LOW);
Serial.print("Do 2:"); Serial.println(data); }
}
Step 3: Upload to Chip.
Make schematic. picture.
3.3V MAX!!!! all pins
Use 2x 1.5V batteries for Vcc. USB does not work. I wasted hours trying.
to enter program mode : GPIO0 to ground, Reset to ground, reset floating.GPIO0 floating. I.E. Press reset to ground while GPIO0 is held to ground.
Flash using FTDI232 with 3V output. See picture.
Hook the relay In up to GPIO0
Hope it works.