ESP8266 Internet Connected Switch (EasyIoT Cloud MQTT API V1)
In this tutorial we will show how to build WiFi internet controlled switch connected to EasyIoT Cloud. We'll use an ESP8266, a solid state relay, and the Arduino IDE.
Join the DZone community and get the full member experience.Join For Free
it's project time! today, going to make a switch. this internet-controlled switch uses few components, and it's connected to easyiot cloud. you can control your devices on a computer in the easyiot cloud web interface or with a native android application . the internet-controlled switch is connected via wi-fi connection. our switch can also be controlled locally with the push of a button.
this tutorial is an improved version of the esp8266 internet-connected switch . it works with the latest version of easyiot cloud mqtt broker. two main improvements are that this uses switch information to update the state of the ui interface. if the connection to switch is broken, then the state in ui will not update. the next improvement is that in this switch, you can configure the ap username and password with a mobile phone or computer.
this internet switch is "plug and play" — it will automatically set all easyiot cloud settings, so no configuration in easyiot cloud is needed.
warning!! you will play with live mains!! deadly zone!!
if you don't have any experience and are not qualified for working with mains power, i will not encourage you to play around!
do not use it without proper knowledge about mains circuits!
do not use it without a proper fuse on mains line!
max current for solid state in this tutorial is 2a — suitable for room light only.
finished esp8266 internet-controlled switch
esp8266 wi-fi module:
|esp8266 esp-01 serial wifi wireless transceiver module|
|esp8266 esp-03 serial wifi wireless transceiver module|
|esp8266 esp-12 serial wifi wireless transceiver module|
|esp8266 esp-05 serial wifi wireless transceiver module|
|esp8266 esp-07 serial wifi wireless transceiver module|
|esp8266 lua nodemcu wifi network development board|
solid state relay 2a 240v:
|solid state relay 2a 240v|
3.3v 600ma ac-dc step-down module:
|3.3v 600ma ac-dc step down module|
|10pcs push button switch|
npn transistor to-92 2n2222:
|100pcs npn transistor to-92 2n2222|
|210pcs 25 value 0.1uf-220uf electrolytic capacitors assortment kit set|
|400x 0.25w 1/4w metal film resistor pack kit 1% 20 value each 20 pcs 10 ~ 1m ohm|
see buying guide at the end of tutorial for details.
easyiot cloud configuration
no easyiot cloud configuration is needed, just register to the easyiot cloud service. you will need username and password to access your internet switch via web application or android application. the switch will be automatically added to easyiot cloud and will be visible in the web interface or in the android application after it is powered on.
the program is written in the arduino esp8266 ide. check out the arduino esp8266 ide tutorial to see how to connect a esp8266 module to a computer to upload a program. the program can be downloaded from our github . you will also need the mqtt client library . add this library to the library folder in the arduino ide. our program uses the easyiot cloud mqtt api .
in the program, change the easyiot cloud username and password:
#define eiotcloud_username "xxx"
#define eiotcloud_password "xxx"
you can also change access point name and password, but this is not necessary. you can later change ap username and password in web interface.
at the beginning of the program, it reads eeprom settings. then, it tries to connect to the ap. if the connection to the ap is not successful after 10 seconds, switch to ap mode to change the configuration. if the connection to ap is successful, the switch checks the module id. if the module id is 0, it means the switch is not configured on easyiot cloud. in this case, it adds the module to easyiot cloud and saves the module id to the eeprom settings. after a successful configuration, the module subscribes to a specific topic to listen for switch changes.
the easiest way to test program is to use the esp8266 lua node mcu board. just connect it to the usb port, and you can test program. if you use this board, you can also uncomment #define debug to see debug messages. the flash button on the esp8266 board will work as the button to manually change the switch state. the onboard led displays switch state.
in our case, we use esp8266 01, but you can use any other type of esp8266. gpio2 is connected to the npn transistor to control ssr. the max current for our type of solid state relay is 2a — this is suitable for room light and not for applications that consume more power, like a heater.
for the power supply, we use a 3.3v step-down module. it's very important to add a 1000uf capacitor to the 3.3v power line — when we tried it without the capacitor, the switch didn't work.
gpio0 is connected to the push button for local control of the internet switch.
after power on, the switch will be automatically added to easyiot cloud, and it will be visible in the web interface or the android application.
you can manually rename your switch and group in the easyiot cloud configuration to suit your needs.
configuring the esp8266 internet switch
when the internet switch is turned on it tries to connect to ap. if after 10 sseconds the connection to ap is not successful, it will switch to ap mode. with a mobile phone, search for an ap named thing-xx-xx-xx-xx where xx-xx-xx-xx is random value. connect to this ap and then, in your web browser, go to 192.168.4.1. in the web page displayed, you can configure the ap name and password. after changing the ap name and password, click submit and the internet switch will reboot and connect to ap.
Published at DZone with permission of Igor Jarc, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.