Category: ESP8266


For HOME AUTOMATION project we use a 4 channel Relay board of 5v Relays.

This is LOW enabled , a LOW at input makes the RELAY ON.

So on start up these input pins must be pulled HIGH.

NodeMCU is loaded with ESPEASY FIRMWARE . You can check this post on how to load.

The Relay board requires a separate power source of 5 volt.

The GND of 5v supply to be made common with the GND of Nodemcu.

RELAY BOARD   NODEMCU

IN1     ——- GPIO 16

IN2     ——- GPIO 14

IN3    ——- GPIO 12

IN4    ——- GPIO 13

 

 

hat

 

DOMOTICZ Service is running on Raspberry PI which is on the same LAN as my PC.

The Domoticz server is accessed by typing in the RPI’s IP address followed by port no. 8080

192.168.0.150:8080

Click on Setup – > Hardware and create Type DUMMY for Relay 1 , name it as RL1.Click ADD

Same way create 3 more Type Dummy for other 3 Relays RL2, RL3 & RL4.

Image 10

 

 

Image 12

On top a list of Types created is displayed.

 

Image 8

Click on Create Virtual Sensor for RL1.

Image 15

Select Sensor type as SWITCH and name it as RL1.

Image 13

Similarly create Sensor type Switches for other 3 ,  RL2,RL3 & RL4

Image 14

Under setup –> Devices all the 4 switches are listed out with INDEX (IDX) numbers.

Note these IDX numbers , as we have to set the same IDX number in ESPEASY Setup.

Image 9

Under SWITCHES tab you can see the VIRTUAL SWITCHES created.

Image 3

Click on EDIT button on RL1

Switch ICON is a bulb by default.You can select any one from the drop down list.

Under ON Action feed in the IP address of ESPEASY with HTTP command

http//192.168.0.110/control?cmd=gpio,16,0

Note the usage of 0 for ON , as the relay board is LOW enabled.

Against OFF action

http://192.168.0.110/command?cmd=gpio,16,1

Click SAVE

Image 4

Same way click on other Relay switches EDIT button and feed in the HTTP command including the correct GPIO number.

 

Image 5

 

Image 6

 

 

Image 7

Once the HTTP commands are saved , you can go to ESPEASY Dashboard by typing in the IP address on browser.

Here mine is 192.168.0.110

 

Under CONTROLLERS select the protocol as DOMOTICZ HTTP

Provide the IP address of DOMOTICZ and its port no. 8080

Click ENABLED.

Image 7

 

Under HARDWARE make the GPIO pins used to OUTPUT HIGH on Boot.

As the RELAY Board is LOW enabled , this is necessary.

GPIO16 by default is HIGH on Boot , so there is no settings for GPIO 16.

Image 8

 

Under DEVICES select the DEVICE as SWITCH INPUT – SWITCH

Provide a name RL1

Click ENABLED.

RL1 is connected to GPIO 16 , select it.

Provide the IDX number as 1.

Internal pull up is similar to OUTPUT HIGH , not needed for GPIO 16.

Image 2

 

As done for RL1 repeat for other relays

RL2 –GPIO14 , iNTERNAL PULL UP –Yes, IDX 2

Image 3

 

Image 4

RL3 –GPIO12 , iNTERNAL PULL UP –Yes, IDX 3

Image 5

RL2 –GPIO13 , iNTERNAL PULL UP –Yes, IDX 4

Image 6

 

Now all the settings are done.

Image 1

Go to DOMOTICZ Dashboard and click on SWITCHES tab.

From here you can Switch ON OFF the relays and the load connected to it.

 

Image 11

VIDEO :

 

blog_image

DHT11 module can sense both Temperature and Humidity data.

As a module it has 3 pins .Vcc can be connected to 3.3v , as it can work with 3.3v.

Gnd to Gnd.

Data pin is connected to GPIO 13.

DHT_ESP

DOMOTICZ service is running on Raspberry PI which is on the same WIFI network as my PC.

 

rpi8

 

Domoticz Dashboard can be accessed from a browser by typing in the address followed by port number

192.168.0.150:8080

 

Image 3

Click on Setup –> Hardware

Select Type as DUMMY and provide a name

Image 4

 

Image 5

When you click ADD it appears on the list.

Image 6

Click on Create Virtual Sensors

Select Sensor Type as Temp+Hum

ans provide a name.

Image 7

 

Image 8

 

Click on OK .

The Virtual Sensor is visible under TEMPERATURE tab.

 

Image 11

If you want a description to be added , click on Edit and add description.

 

Image 12

 

Now go to ESP EASY by typing in the IP address 192.168.0.110

Under Controllers select protocol DOMOTICZ HTTP.

Image 13

Provide the IP address of DOMOTICZ Service and the port number.

Click Enabled.

Image 15

 

Under Devies click ADD.

Image 16

Select a device Environment DHT11

Image 17

 

Click Enabled and select the GPIO pin as GPIO 13.

 

Image 18

Interval is the REFRESH rate of DHT11.

DHT11 has a refresh rate of 1 sec , meaning every second it can send out data.

On DOMOTICZ Virtual Sensor you can see the display of Temperature and Humidity.

Image 20

video :

 

blog_image

In the previous post we installed ESPEASY over NODEMCU.

This post is on controlling an LED connected to GPIO 12 of NODEMCU from DOMOTICZ Service.

 

LED_NODE

 

My Raspberry PI and PC both are on same Local Area Network , i.e connected to same WIFI Router .

Domoticz is installed on RPI

 

rpi1

 

 

Image 21

The IP address of RPI connected to the Router over WIfI is 192.168.0.150 & the DOMOTICZ dashboard is accessed by URL

192.168.0.150:8080

 

rpi7

 

On Domoticz dashboard click on Setup –> Hardware

 

Image 28

 

Create a Type  “Dummy “ (select from the drop down list) and provide a name as LED.

Image 29

 

Click on ADD

 

Image 30

Now click on  “ Create Virtual Sensors “

Image 31

Select a Sensor type as SWITCH and provide a name.

 

Image 32

 

The Virtual Switch is visible under SWITCHES tab.

Image 33

Click on EDIT

Against On Action , type in this HTTP command

http ://192.168.0.110/control?cmd=gpio,12,1

192.168.0.110  is the IP address of ESPEASY NODEMCU.

LED is at gpio12 & 1 for switching ON , 0  for Switching Off.

Image 34

Now go to the dashboard of ESPEASY by typing in 192.168.0.110 in the browser.

Under Controllers select a Protocol Domoticz HTTP

Provide the IP address of DOMOTICZ and the port no. as 8080

Click on Enabled & Submit.

Image 35

Under Devices select from the drop down Switch Input –Switch

Provide a Name and click Enabled.

Select the GPIO as GPIO 12.

Image 36

Click on SUBMIT.

Now you can switch On / Off the LED by clicking bulb icon on Domoticz.

 

Image 3

VIDEO:

 

blog_image

ESP EASY IOT OS ON NODEMCU

Video :

 

ESP module can be turned into an IOT MULTIFUNCTION Sensor device by  installing  ESP EASY Firmware on to the module.

For HOME AUTOMATION projects it suits well and can be easily controlled from application servers like DOMOTICZ.

Configuration of the ESP EASY is entirely web based , so once the firmware is loaded , you just need a common web browser to work with.

Older version of ESP EASY is R120. Mega is the newer version.

https://github.com/letscontrolit/ESPEasy/releases

documentation :

https://espeasy.readthedocs.io/en/latest/

 

supported Hardware ESP modules :

https://www.letscontrolit.com/wiki/index.php?title=ESP_Hardware

The NodeMCU or WeMOS D1 mini are recommended , as they have inbuilt USB.

Sensors supported :

https://www.letscontrolit.com/wiki/index.php?title=Devices

Only the list of sensors are supported.You need not write any code separately for sensors.Just connect the Sensor and configure on Dash board. Display or control it from DOMOTICZ over HTTP or MQTT protocols.

Download the MEGA version of ESP EASY.

Under dist folder Flasher application is provided.Double click on that.

Image 1

 

Select the COM port where Nodemcu is connected.

Before selecting a .bin firmware file click on the update button.

Latest .bin files will be downloaded from Github repository.

 

Image 2

Select the relevant .bin file for your hardware.Here I’m selecting ESP8266 4MB version, as my hardware is NODEMCU with 4096 flash memory.

Test beta version supports Nextion display hardware.

Image 3

Setting of WIFI & fixed IP can be done later. For now Flash ESP EASY.

It takes approx 2 minutes to upload.

 

Image 5

Once uploaded , close the flasher and reset Nodemcu.

It starts in AP mode and the WI FI access point name is ESP_EASY.

Connect your PC to this AP using password  configesp

When a fresh ESP Easy module boots up, it has no Wifi config and it will start as an Access Point.

Use a Wifi enabled device (Laptop, Smartphone,..) to search for Wifi access and try to find an access point with the name ESP_Easy.

Connect to this access point with default password:

configesp

 

Image 6

 

Once connected , open your browser and type in ip address   192.168.4.1

You should get a  welcome screen:

Select your SSID and provide password.Click on CONNECT .

Now your NodeMcu connects with your SSID.

If the connection succeeded, you will see a screen, listing it’s new local IP address.

You can now reconnect your laptop or other device back to your private Wifi network.

Then click the “proceed” link to contact the ESP module on it’s private IP address:

Image 7

ESP EASY Nodemcu is offered an IP ADDRESS by your Router.

Use this address on a browser to access the DASHBOARD of ESP EASY.

Under MAIN tab is the SYSTEM INFO.

Image 8

 

Click on CONFIG tab.

Here you can change the AP password from configesp , if you need.

At the bottom is the IP Settings.

You can provide an IP address to make it STATIC.

The IP Address must be in the range of your Gateway IP.

Gateway IP is the IP of your ROUTER.When any client connects with the Router , it provides an IP address by its DHCP method.This IP will be in range of Gateway IP , only the last octet changes.

Finally click SUBMIT to effect changes.

Image 9

Next tab is CONTROLLERS tab.

Under this you select a PROTOCOL .

HTTP,MQTT,UDP all are supported.

In this demo we control an LED from browser commands.So select STANDALONE.

 

Image 10

Under HARDWARE tab you can set any GPIO pin to be High or Low on Boot up.

It is not needed in this demo , so we move on to next tab DEVICES.

Click on ADD from the list of devices.

Select SWITCH INPUT – SWITCH from the drop down.

Provide a name , like LED and then click the ENABLED .

Under GPIO , select GPIO 12 (D6) where we’ve connected an LED.

Click on SUBMIT.

Image 11

Now from your browser use this command to control LED.

Image 12

 

Image 13

Instead of GPIO you can use PWM to control brightness of LED.

gpio, pwm can be in small or capital letters.

Image 14

 

blog_image

 

In the previous post , we worked on a home automation project and Android mobile application & controlled devices locally .ie., over Local Area Network.

In this post we will see how to make the home automation project to control from anywhere in the world. IOT CLOUD Platform BLYNK is used for implementing this.BLYNK is the most popular IoT platform to connect your devices to the cloud,

BLYNK  is a digital dashboard where you can build a graphic interface for your project by simply dragging and dropping widgets.It supports a wide range of Hardware including Arduino,Nodemcu,Raspberry PI & others.

First  install BLYNK on your mobile phone by signing up with your EMAIL ID.

Open BLYNK application.Touch on NEW PROJECT.

2020_01_03_15.07.55

Provide a name to the project & select hardware.Here Im using NODEMCU.

 

2020_01_03_18.39.47

 

Connection type is WIFI .

Touch on CREATE button.

 

2020_01_03_18.39.56

An authorized token ID will be sent to your registered EMAIL.

 

2020_01_03_18.40.02

Now we shall see the WIDGET BOX to add some elements.Each element of WIDGET BOX has got an ENERGY LEVEL.

While starting up you will be provided a free energy level of 2000.According to the usage of widgets this energy level will be deducted.

 

2020_01_03_18.41.52

For e.g , add a BUTTON to your project.

2020_01_03_18.41.59

An energy level of 200 will be deducted.If you want you can BUY Energy level by touching on the ADD button.

 

2020_01_03_18.42.04

 

2020_01_03_18.42.41

Add 2 buttons as shown below.

 

2020_01_03_18.43.16

To set the properties of first button , touch on that button.

 

2020_01_03_18.43.22

 

Provide a name to the button 1.

Touch on PIN to select pin.As we control a dc fan through relay board which is connected to D2 of Nodemcu , select DIGITAL PIN D2.

There are also VIRTUAL PINS Upto 255 , which are specific to BLYNK firmware. These virtual pins are NOT Physical pins , but are handy to send data or message to them and then control physical pins from within code.

2020_01_03_18.43.48

Slide the MODE setting to SWITCH which makes the button at as ON or OFF.

2020_01_03_18.44.29

 

Similarly for the Button 2 set the PIN to digital D4 where we control an AC load through the relay board.

 

2020_01_03_18.44.49

 

2020_01_03_18.45.11

Now touch on the Back arrow to go to the start up page.

On touching the PLAY Icon , a message indication stating Not yet online is displayed.As we are yet to upload code on to Nodemcu , the setting is OFF line for now,

From your PC side , open the Arduino IDE.

For setting NODEMCU to Arduino core ,open File & under Preferences paste this json link

https://arduino.esp8266.com/stable/package_esp8266com_index.json

 

Image 5

Then open Tools –> Boards Manager , search for ESP8266 & install.

Image 29

Also from Sketch – > Manage Libraries –> Library

search fro BLYNK & install the library.

Image 31

Once the library is installed you can see under Examples —> Blynk

Open the NodemCU Example code.

Image40

You need to replace with your Authorized token ID which you received in Email while creating the Blynk project.

Provide your WiFi credentials ,SSID & password of Router to which Nodemcu will be connected.

Also under setup() make the digital pins D2 & D4 HIGH.

This step is to make the loads connected to relays initially OFF.

As the relay board we are using is a LOW ENABLED , a LOW at input energizes the Relay and a HIGH makes it OFF.

Image41

We make use of MB102 PSU board on a MB102 bread board.This MB102 board has got jumper settings to make one rail as 5v & the other rail as 3.3v.

A jumper seen at he relay board can be removed.

Connect Vcc to 3.3v & JD-Vcc to 5v rail of bread board.

Image 32

The loads can be connected as shown below.Always connect the power source  on which the load operates to the POLE pin.Actual load is connected to Normally Open contact.

Image 37

Connect NODEMCU to PC , from ARDUINO IDE select the board as NODEMCU & select the port allotted.For this you must have already installed driver for CP2102.

Upload the code & then disconnect USB cable.

Provide 12v 1amp or 9v 1amp adapter to MB102 PSU board.

Allow some time for NODEMCU to join the SSID mentioned in the code.

While using AC load ,take care not to touch the relay board and insulate properly the relay board inside a plastic box.

Image 38

Now open the BLYNK Application and touch on the project you created.On top right corner touch on the PLAY Icon.

Once you see ONLINE indication , you can touch on the buttons created to control the loads.

video tutorial :

 

 

blog_image

 

 

 

title_iot2

This post is on Home Automation project where we control 2 devices from a mobile App. over Local Area Network.

NodeMCU with Arduino core installed is used with relay board.

You can watch this video on how to install Arduino core on NodeMCU.

https://www.youtube.com/watch?v=BbiLBiFvlsI

Android Mobile Application is developed using MIT APP INVENTOR , a WEB Application IDE.

https://appinventor.mit.edu/

For Arduino code we will use the one from my previous post

https://alselectro.wordpress.com/2017/11/08/nodemcu-arduino-core-2-led-on-off-from-webbrowser/

Download code for web browser LED On/Off HERE

This code is to control an LED from a Web browser on LAN.

We shall modify the code to work for Home Automation.

Arduino Code , Android App. (both .aia , .apk) files can be downloaded HERE

To start with connect a Red LED at D2 (which will be replaced with a DC Fan load)

A green LED at D4  ( will be replaced with CFL lamp AC Load)

 

I41

——————————————

#include <ESP8266WiFi.h>
#define FAN D2
#define LIGHT D4
const char* ssid = “Saravana”;
const char* password = “######”;

WiFiServer server(80);

void setup() {
Serial.begin(115200);
pinMode(FAN, OUTPUT);
pinMode(LIGHT, OUTPUT);
digitalWrite(FAN,HIGH);
digitalWrite(LIGHT,HIGH);

Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
server.begin();
Serial.println(“Server started”);
Serial.println(WiFi.localIP());
}

void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}

Serial.println(“new client”);
while(!client.available())
{
delay(1);
}
String req = client.readStringUntil(‘\r’);
Serial.println(req);
client.flush();

if (req.indexOf(“/fanon”) != -1)
{

digitalWrite(FAN,LOW);
Serial.println(“FAN ON”);
}
else if(req.indexOf(“/fanoff”) != -1)
{
digitalWrite(FAN,HIGH);
Serial.println(“FAN OFF”);
}

if (req.indexOf(“/lighton”) != -1)
{
digitalWrite(LIGHT,LOW);
Serial.println(“LIGHT ON”);
}
else if(req.indexOf(“/lightoff”) != -1)
{
digitalWrite(LIGHT,HIGH);
Serial.println(“LIGHT OFF”);
}
String web = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;

client.print(web);
}

————————————————-

upload the above code and open the serial monitor to note the IP address allotted to NodeMCU.

Note that all devices NODEMCU ,PC & mobile are connected to same Router.

The IP address allotted is DYNAMIC ,meaning , it may change on next power up.

To make it STATIC , you can set Address Reservation setting under Routers settings.

l3

Once the IP is allotted , open up the WEB browser and type in IP/fanon

for e.g  192.168.1.107/fanon

Initially both the LEDs will be ON .As we will use a LOW enabled input Relay board, these LEDs are made ON indicating the devices are OFF initially.

On sending the string fanon from browser , the RED LED will go OFF.

On sending 192.168.1.107/lightoff , the Green LED will go off.

Similarly  fanoff string will make Red LED ON , lightoff string will make Green LED ON.

 

Now the code is working perfectly, we shall replace the LEDs with actual devices to be controlled.

A 4 channel 5 volt Relay module is used to connect the actual devices .We use a DC FAN 5v at relay contact 1 and a CFL bulb AC at RELAY 4

 

I42

For power source a MB102 PSU board is used on MB102 Bread board.This board can be easily plugged on to MB102 Breadboard and has jumpers.One jumper (top)  is set to  5v and the bottom jumper set to 3.3v.

An external adapter of 9v 1amp is used .

I43

Connect IN1 of relay board to D2 of NODEMCU

IN4 to D4,  GND to GND.

I40

While connecting Load to relay board, always connect the power to load at POLE.

Actual Load is connected to NO Normally Open pin of relay contact.

While handling AC loads, take precaution to insulate relay board within a plastic box and do not touch the relay board.

It is safer to connect Neutral to POLE and AC Phase to other end of Load.

I45

Also remove the jumper on relay board.

Connect 3.3v to Vcc and 5v to JD-Vcc

This enables error free operation , as the NODEMCU is a 3.3v device.

jdvcc

 

Now let us develop our Android Mobile Application using on line MIT APP INVENTOR.

 

Image 3

Click on Start a New project and provide a Project name. Space and Hyphen not alloed in name , but underscore allowed.

 

Image 5

You will be presented with development screen which has 4 columns.

1st Column is Palette with all User Interfaces and components to build the App.

2nd Column is the actual Screen viewer.

3rd Column displays all the selected Components.

4th Column shows the Properties of the selected Components.

Image 6

From LAYOUT drag and drop a HORIZONTAL ARRANGEMENT

Image 7

Set the Height under properties to 10 percent , Width TO FILL PARENT

This is to provide space at top of screen.

Image 8

Then drag and drop LABEL from User Interface

Image 9

Set Width TO FILL PARENT , Text to Heading of App. , alignment center ,Fontsize to 25

and background color to yellow.

Image 10

Next drag and drop  TABLE ARRANGEMENT from LAYOUT.

Image 11

Set properties , COLUMNS to 3 and ROWS to 2,  Width to FILL PARENT, Height to Automatic.

2 Rows required for 2 device control.

3 columns  for image , on button and off button.

 

Image 12

Drag and drop an IMAGE component from User Interface.

Image 13

Under Properties set the Height to 15 percent, Width to 30 percent.

Under Picture property click Upload File and select the image to upload.

Image 14

 

Image 15

Now drag and drop 2 buttons next to image 1.

Image 16

Drag and drop another IMAGE component, below the first imge.

Set properties accordingly and upload the second image.

Drag and drop 2 more buttons as shown below.

 

Image 18

Select the properties of Buttons

You need to change the Text and the background COLOR.

Image 19

Next drag and drop a WEB component from CONNECTIVITY.

As this one is a Non Visible component , it will show up at the bottom of the screen.

Image 22

Now click on the BLOCKS button at the right top corner.

Image 23

Select Button1 from the left side Blocks.

You will see a list of options.

Select the When..Do ..Button..Click block.

Image 24

Next click on WEB .

Select the set..web1..url..to block

Image 25

Third block to select is under TEXT

Select an empty Text block for text.

Image 26

Finally from WEB

select  call..web..get   block.

Image 27

Following are the blocks we selected.

Image 28

Assemble the blocks as shown.Just drag and fix at the connecting points.

Image 29

Click on Text block and type in

http://192.168.1.5/fanon

Your IP may be different.Change the IP according to the one shown at serial monitor of

Arduino, after uploading code.

Image 30

The first block is ready.

Right click and select DUPLICATE.

 

Image 31

Change the Text for other blocks as shown.

Image 32

Now click on BUILD App TO SAVE .apk file to computer.

You can transfer this .apk file to mobile using SHAREIT or any application.

Image 34

Also you can export the .aia file.

.apk file is for Android mobile.

.aia file is the one you can import on your MIT APP and do the necessary modifications

and then finally generate your own .apk file.

Also you can install MIT AI2 Companion on mobile to view Real Time development.

By this application you can view the real time changes on mobile , as you develop

appplication on MIT APP Inventor.

Image 36

Now open the Application on mobile.

Touch on the buttons to see the project working.

IMG_20191219_132341

VIDEO TUTORIAL

 

 

blog_image

title_iot1

IOT is Internet Of Things where devices are connected over internet and made SMART.

These devices can be controlled from any where in the World or collect data from devices to process.

In this 1st post of IOT series I will introduce the hardware required to start with and also the IOT platforms used.

Arduino boards are much popular among hobbyists and developers.But these Arduino boards (UNO,MEGA) lack Ethernet or WIFI capability which is essential for IOT.

For wired connection there is ETHERNET Shield which can be plugged on directly to UNO or Mega.

l1

 

For internet access I’m using a Huawei make USB Dongle with a Data SIM inserted.This dongle

can be used in standalone mode , as it has got WIFI capability.But plugging this to a Router

like TP-LINK enhances its capability of WIFI Range, more Clients, Static IP setting,etc..

i3

 

i1

 

The Ethernet shield can be plugged on to Arduino and an Ethernet cable (straight type)

can be used to connect with the Router.

The Router will then allot an IP Address (local IP) to the ethernet shield which is now a client.

DHCP server of Router allots the IP on first come first basis.As this IP is DYNAMIC ,

the address may not be the same if powered off and connected again.’’

To make the IP address STATIC , the ROUTER has ADDRESS RESERVATION settings

under DHCP.

MAC address of the device is used to make the local IP STATIC.

l3

Instead of wired Ethernet connection you can go for WIFI Devices. ESP8266 WIFI modules are

cheap and popular now.

ESP8266 WIFI modules , a wide range available.ESP8266-01 module is basic model .As it is not breadboard friendly , you can make use of a base board to plug on , as shown in picture below.

ESP8266-12E module has more GPIO pins and memory.

These modules can communicate with ARDUINO using AT COMMANDS for ESP.

 

I6

 

To use ESP modules we need a controller like Arduino.

Easier option is to use NODEMCU Modules.These NODEMCU modules are built on ESP12E chip

and has USB to upload code.LUA is the official Firmware that comes with NODEMCU.

Using BOARDS MANAGER in Arduino IDE you can implement ARDUINO CORE on to NODEMCU

and start using NODEMCU individually.

You can check out my video on how to implement Arduino core on to NODEMCU.

https://www.youtube.com/watch?v=BbiLBiFvlsI

Once Arduino core implemented , you can use Arduino IDE and libraries to develop code on NODEMCU.

Latest arrival is ESP32 which is much more powerful than NODEMCU with more Analog pins

and with BLUETOOTH,BLE and inbuilt sensors.

I8

For IOT and HOME AUTOMATION projects RELAY BOARDS are helpful to connect wide

range  of Loads or devices.

5V Relay boards are available with 1 channel , 2, 4,8 Upto 16 channels .

Each relay is driven by an opto coupler and a NPN transistor.The relay contacts are

provided as Terminal connectors.

Each Relay has a  common POLE,  a NC –Normally Connected contact and a

NO – Normally Open contact.

 

I10

These 5V relay boards are LOW ENBLED, meaning , a LOW at input will energize the Relay.

A low at input will make the opto coupler conduct which drives the NPN Transistor.

This in turn will switch ON the relay.

jdvcc

Electronic solid state relay boards are also available which are called SSR.

 

SSR

12v Relay modules also are available.Here the relay is driven by a NPN transistor .12V Modules are HIGH ENABLED ,meaning, a HIGH at input drives the relay ON.

 

I11

 

Any load ,DC or AC can be connected to the RELAY contacts, irrespective of the supply voltage to relay board.

The power supply to load is connected to POLE pin and the actual load is connected to the NO pin.

As the relay is energized the pole pin changes from NC to NO ,thus activating  the Load.

While using AC load take precaution to insulate properly the wires as well as the Relay boar.Do not touch relay board with AC load.

I12

 

You can control the devices over Local Area Network or through Cloud or internet.

While using LAN , make sure that the NODEMCU and your mobile , both are connected to the same Router SSID.

For control over internet you need to PORT FORWARD the local ip device port on the router settings.But nowadays a number of IOT PLATFORMS are available as CLOUD. Though you need a paid subscription , most of them offer free service for hobbyists with some limitations.

BLYNK, CAYENNE, THINGSBOARD are some popular IOT PLATFORMS.

Sign in to any of these platforms and start controlling the devices from anywhere in the world.

iot2

VIDEO support :

 

blog_image

ESP8266 WEMOS D1 With I2C Serial LCD

WemosD1 R1 board is now  the most preferred IOT WIFI Board .As the hardware resembles Arduino , it is most sought out  board for any IOT project , than NODEMCU.

To add a display I prefer to use the SERIAL LCD WITH I2C BACKPACK  .

The serial LCD is built on PCF8574 &  works on I2C Bus ,thus requires only 2 wires SCL & SDA apart from the power pins.

The LCD works on 5V & it can be sourced from D1 board itself.

 

IMG_20180416_130653

For more detailed information on this Serial LCD you can read my previous blog here :

https://alselectro.wordpress.com/2016/05/12/serial-lcd-i2c-module-pcf8574/

 

The connection between D1 board & LCD is simple .

Wemos D1      LCD

5v           —–  Vcc

Gnd        —- Gnd

SCL  D15  (GPIO5)  —    SCL

SDA  D14   (GPIO4)   — SDA

To start with download the latest Library from this link :

https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

From Arduino IDE  Sketch –> Include library –> Add ZIP Library

Browse to the location of the downloaded ZIP file & select it

Image 2

Now the Library for Serial LCD is installed.

Image 3

 

Under Tools selectthe board as WEMOS D1 R1 , upload speed as 115200, port is the one allotted  in Device manager &programmer USBASP.

Image 4

Read this previous blog post of getting started with D1 board, on how to install Arduino core using Boards Manager

https://alselectro.wordpress.com/2018/04/14/wifi-esp8266-development-board-wemos-d1/

 

Now we need to know the I2C Address of the LCD board connected to D1.

For this upload the following Scanner sketch.

http://www.alselectro.com/files/Library_codeexamples.zip

 

Image 5

 

Once uploaded,Open the serial monitor with 9600 baud setting.

The I2C HEX Address of the device is displayed .Here it is 0x27 .

Note that this address varies according to the manufacturer and the jumper solder on A0 A1 A2 pins on the backpack pcb.

 

Image 6

Now open the first example lcd_test.

In this example 2 header files are used

One is the Wire.h ,which is the I2C library that is built in already with IDE.

Second  is the LiquidCrystal_I2C.h which we installed initially.

Then we create an instance  lcd with parameters I2C Address  , EN , RW , RS , D4,D5,D6,D7

I2C address is the ine we got from the scanner code –0x27

EN,RW,RS are the pins of I2C board connected to LCD Enable ,Read/Write & Register Select respectively.

D4 to D7 are Data pins

 

Image 3

lcd,begin(16,2) initializes the 16 x 2 LCD

Then we set the backlight ON for the LCD

lcd.home()moves the cursor to Home position

lcd.print(“,,,,”) prints the message on 1st row.

lcd.setCursor(0,1) moves the cursor to second line.

 

Image 7

 

Upload the code to see the result on LCD.

The Contrast to LCD can be adjusted by the preset on the back.

 

IMG_20180416_164802

 

Here is the second example which prints the message typed on the Serial monitor.

 

Image 8

 

Image 9

VIDEO  :

blog_image

WEMOS D1 is a WIFI development board based on ESP8266 12E. The functioning is similar to that of NODEMCU , except that the hardware is built resembling Arduino UNO.

The D1 board can be configured to work on Arduino environment using BOARDS MANAGER.

So,its not necessary to use a separate Arduino board , the D1 itself can be programmed on Arduino IDE.This is handy in using with IOT projects. Further many Arduino SHIELDs are compatible with the D1 board.

Different versions of D1 boards are available in the market    R1  ,  R2  , MINI

As the name implies D1 Mini is a smaller version

R1 , R2 boards look like Arduino UNO board ,but version is not printed on board. On some boards WEMOS D1is printed , as the ESP12 chip is manufactured by WEMOS.

In some other boards it is printed just D1 ,where the ESP12 chip is that of AI Thinker.

As the ESP12 chip has only one ANALOG pin , on board there is only A0 & other analog pins are dummy .

Image 2

Image 3   Image 4

Use a MicroUSB cable to connect D1 with PC. Make sure that the cable is capable of handling both Data & power (some of the mobile cables can handle  only power & not data) .

Open the DEVICE MANAGER & note the COM PORT allotted to the module.

As the serial chip used on D1 is CH340 , you need to install the corresponding drivers.

Image 1

PIN CONFIGURATION

d1_pinout

Following is the pin configuration of R2 typeD1 Module.Note the changes in GPIO Pin position.

WEMOS_D1_R2

Open the Arduino IDE &  select this COM port.

From TOOLS  —>  BOARDS  —> BOARDS MANAGER install the package for ESP8266.

Prior to this , you need to provide the link from where the Boards manager will look for the package.

To do this copy the following Link

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Go to FILE  –> PREFERENCES on Arduino IDE

Paste the link against the Additional Boards Manager URLs ,seen at the bottom of preferences screen & click OK.

Image 1.1

Now open the TOOLS –> BOARDS –> BOARDS MANAGER

Image8

Search for ESP8266

Now you will find the package from ESP8266 Community.

Select the latest version from drop down & install it.

It takes a while & requires internet connection.

Image 2.1

Once the package is installed ,you can see the list of  boards under TOOLS –> BOARDS

Select the board as WEMOS D1 R1.

Select UPLOAD speed as 115200 &Programmer as USBASP

Image 3.1

Now open the  File –> Examples –> Basics –> BLINK sketch

No need to modify the sketch ,as we’re going to blink the built in LED (on the ESP chip , there is another LED on board which is mapped to pin14)

Click on UPLOAD

error

On some PCs you get a compile error ,stating that the file is being used by another process.

On some forums ,it is mentioned that the board is Duplicate .Its not the case, the reason is that some Antivirus program like McAfee does not allow the compilation process.

Open the Antivirus dashboard & TURN OFF the REAL TIME SCANNING for some time

error2

error3

You can also configure the Antivirus settings for an EXCEPTION to Arduino programs.

Now Upload the sketch.

error4

Once the code is uploaded, you can see the built in LED blinking on the chip

Next code is a WIFI scanner which scans for nearby SSIDs & display the names of WIFInetworks with RSSI signal power &encryption mode.

For this we include the Header file ESP8266WiFi.h

This is installed along with the Boards Manager & is a very powerful library.

You can get more information on this library from here :

https://alselectro.wordpress.com/2016/11/29/esp8266-wifi-library-on-arduino-ide/

Many useful Functions are there for this WiFi library.

WiFi.mode (WIFI_STA); 

The mode function of library is used to set the D1 board in STATION mode.

WiFi.scanNetworks();

scanNetworks function scans for nearby SSIDs & the result is an integer (number of surrounding WiFis) which is stored in a variable n

Then we print the SSID name,RSSI signal strength ( the lower the negative dB the stronger the signal), & Encryption mode (a * indicates SSID is password protected)

—————————————–

// D1 ESP8266 WiFi scanner

# include  “ESP8266WiFi.h”

void  setup () {

Serial . begin ( 9600 );
// set WiFi to station mode and disconnect from previous connection
WiFi.mode (WIFI_STA) ;
WiFi.disconnect ();
delay ( 100 );
}

void  loop () {
Serial . println ( “Start Scanning ..” );
// scan for nearby WiFi SSIDs & save their number into n
int n = WiFi.scanNetworks ();
// in case no Network found , print info
if (n == 0 ) {
Serial . println ( “No SSIDs nearby seen…” );
}

else   {
Serial . print (n);
Serial . println ( “WiFi network SSIDs nearby ….” );
// list all WiFi networks nearby
// name, signal strength and security method
for ( int i = 0 ; i <n; ++ i)
{
Serial . print (i + 1 );
Serial . print ( “:” );
Serial . print ( WiFi.SSID(i));
Serial . print ( “(” );
Serial .print ( WiFi.RSSI(i));
Serial . print ( “)” );
Serial . println (( WiFi.encryptionType (i) == ENC_TYPE_NONE)?”” : “*” );
delay ( 10 );
}
}
// Exit
Serial . println ( “” );
// pause for 15 seconds before new scan
delay ( 15000 );
}

———————————–

scan2

scan1

Support VIDEO:

 

blog_image.jpg

In this post we’ll see how to control an LED connected to NODEMCU from a WebBrowser.

The WebBrowser is a CLIENT & the NodeMCU is the SERVER .

The Nodemcu has to be installed with Boards Manager Arduino core. Refer to the previous post on how to do this.

 

Image 1

 

Connect the long pin of LED to D2 pin of NodeMCU , short pin to GND through a 470E resistor.

Connect the NodeMCU to USB port of PC & note the COM port allotted.Feed in this COM port number in IDE & select the board as NodeMcu v1.0 (ESP12E).

Upload the following code.

 

You can Download the code used in this post HERE

————————————-

 

#include <ESP8266WiFi.h>
#define LED D2
const char* ssid = “SARAVANA-ACT”;
const char* password = “str5412stk4141”;
unsigned char status_led=0;

WiFiServer server(80);

void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED )
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
server.begin();
Serial.println(“Server started”);
Serial.println(WiFi.localIP());
}

void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}

Serial.println(“new client”);
while(!client.available())
{
delay(1);
}
String req = client.readStringUntil(‘\r’);
Serial.println(req);
client.flush();
if (req.indexOf(“/ledoff”) != -1)
{
status_led=0;
digitalWrite(LED,LOW);
Serial.println(“LED OFF”);
}
else if(req.indexOf(“/ledon”) != -1)
{
status_led=1;
digitalWrite(LED,HIGH);
Serial.println(“LED ON”);
}
String web = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;
web += “<html>\r\n”;
web += “<body>\r\n”;
web += “<h1>LED Status</h1>\r\n”;
web += “<p>\r\n”;
if(status_led==1)
web += “LED On\r\n”;
else
web += “LED Off\r\n”;
web += “</p>\r\n”;
web += “</p>\r\n”;
web += “<a href=\”/ledon\”>\r\n”;
web += “<button>LED On</button >\r\n”;
web += “</a>\r\n”;
web += “</p>\r\n”;

web += “<a href=\”/ledoff\”>\r\n”;
web += “<button>LED Off</button >\r\n”;
web += “</a>\r\n”;

web += “</body>\r\n”;
web += “</html>\r\n”;

client.print(web);
}

——————————————-

First of all, we include the ESP8266WiFi library, which will make available the functionality needed for the ESP8266 to connect to a WiFi network.Then we define the D2 pin as LED where an Led is connected.

#include <ESP8266WiFi.h>
#define LED D2

The SSID name & password are stored in char variables.

A WiFiserver object called “server “ is created & we pass the port 80 where the server will be listening ( 80 is the default port of HTTP)

WiFiServer server(80);

In the setup() function serial monitor is started at baud 115200 & the LED pin is declared as Output.

The begin() function of WiFi class is used with parameters ssid & password to start connection to WiFIi

WiFi.begin(ssid, password);

We wait till the WiFi.status() becomes WL_CONNECTED which indicates that connection is established .Until then we print a dot every 500msec.

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}

Once connection is established , an IP address is allotted to NodeMCU by your Router.We print the IP address using the localIP() function.

Serial.println(WiFi.localIP());

Inside the loop we create an object called client of class WiFiClient & assign the server.available() result (it returns true or false) to it.

WiFiClient client = server.available();

It waits for a client ( here a web browser is the client) to connect with.

Once a request is received  , the client.readStringUntil (‘\r”) will read the request till it encounters a  carriage return & assign it to a String variable req

String req = client.readStringUntil(‘\r’);

indexOf() function of String is used to find out ehether the request is ledoff or ledon.

if (req.indexOf(“/ledoff”) != -1)

The indexOf() function returns a –1 if no match found.

Accordingly the LED is made ON or OFF.

Now let us see how a HTML page is served to webbrowser when a client request is received

ode will be stored in strings that will be returned in the HTTP responses

we can declare it in a string and just return it when a HTTP request is performed on the desired URL.

 

String web = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;
web += “<html>\r\n”;
web += “<body>\r\n”;
web += “<h1>LED Status</h1>\r\n”;

 

First, we  return the 200 HTTP code (OK code). Then, we need to make sure that we specify the return content as text/html, so the browser will know that it should be interpreted and rendered as such

Finally, we pass the string with the HTML/CSS code

The <html> element represents the root of an HTML document. All other elements must be descendants of this element .

The <body> tag specifies where the body of the document starts .

The <h1> tag specifies the Heading of Webpage

The <p> tag specifies a paragraph and thus we use it to indicate our description message.

 

web += “<a href=\”/ledon\”>\r\n”;
web += “<button>LED On</button >\r\n”;

The <a> tag allows us to create a hyperlink by specifying the URL of a page and the message of the hyperlink. So, in the href attribute we specify the URL we want the user to be redirected to, and between the two tags we specify the message of the hyperlink.

In this case, we are specifying a path in the current address, so we don’t need to put the IP and the port. So, when a user clicks the hyperlink, the browser knows it will need to redirect the user to

http://IP Port/[href value]

As we will see later, the paths we are specifying in the href will correspond to the paths where the NodeMcu will be programmed to return the corresponding code.

the <br> tag is just a simple line break between the hyperlinks.

One thing that is very important is that we are specifying the HTML code inside a string, and thus some details need to be taken in consideration.

First, in order to leave the code structured, we should split the declaration of the string in multiple lines. To do so, we need to put the double quotes in each line so the compiler will know that it should be considered as a single string. So, the outermost quotes of the previous code are not from the HTML but from the declaration of a string in multiple lines.

Finally, every quote that is part of the HTML code (for example, the quotes around the URLs in the <a> tag) needs to be escaped with the \ character. So, the \ characters we see before the quotes are also not part of the HTML code but rather the escaping mechanism.

The <button> tag allows for the definition of a clickable button. One of its functionalities allows us to specify a function that is executed when the button is clicked

 

web += “<button>LED Off</button >\r\n”;

Now upload the code & open the serial monitor

Image 5_1

WiFi connection starts & displays IP if connection established.

Open your web browser & type in the IP you got from the serial monitor.

Once you click the Enter button , a CLIENT request is sent to NodeMCU.

Now the HTML page is served & buttons LED ON & LED OFF are displayed on the browser.

Image 6_1

If you click LED ON button , the message string “ledon” is added to URL & sent to Nodemcu.

The string is read and accordingly the LED will be ON or OFF.

Image 7_1

 

Here is another version of code where we use client.print() to serve the Webpage

 

———————————-

 

#include <ESP8266WiFi.h>
#define LED D2  // LED at GPIO4 D2

const char* ssid = “SARAVANA-ACT”;
const char* password = “str5412stk4141”;
unsigned char status_led=0;

WiFiServer server(80);

void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);

// Start the server
server.begin();
Serial.println(“Server started at…”);
Serial.println(WiFi.localIP());

}

void loop() {

// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println(“new client”);
while (! client.available())
{
delay (1);
}

// Read the first line of the request
String req = client.readStringUntil(‘\r’);
Serial.println(req);
client.flush();

// Match the request

if (req.indexOf(“/ledoff”) != -1)  {
status_led=0;
digitalWrite(LED, LOW);
Serial.println(“LED OFF”);
}
else if(req.indexOf(“/ledon”) != -1)
{
status_led=1;
digitalWrite(LED,HIGH);
Serial.println(“LED ON”);
}

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”);
client.println(“”);

client.println(“<!DOCTYPE HTML>”);
client.println(“<HTML>”);
client.println(“<H1> LED CONTROL </H1>”);
client.println(“<br />”);

client.println(“<a href=\”/ledon\”\”> <button style=’FONT-SIZE: 50px; HEIGHT: 200px;WIDTH: 300px; 126px; Z-INDEX: 0; TOP: 200px;’> LED ON </button> </a>”);
client.println(“<a href=\”/ledoff\”\”> <button style=’FONT-SIZE: 50px; HEIGHT: 200px; WIDTH: 300px; 126px; Z-INDEX: 0; TOP: 200px;’> LED OFF </button> </a>”);
client.println(“</html>”);

delay(1);
Serial.println(“Client disconnected”);
Serial.println(“”);

}
———————————–

Inside <button> tag we create a style to make big size buttons.

On clicking the respective buttons the LED is made ON or OFF.

 

Image 9

 

Image 10

 

Image 11

VIDEO DEMO :

cooltext753793315   cooltext753790696