Control I2C Realy using Node-Red Dashboard in Smartphone

Now its easy to control your Electronic appliances through IOT application created in Node-Red using Smart Phone.

In this Blogtut, we are going to control the I2C relay Board which is connected with Arduino. I am controlling the relay using Node red dashboard which is working on a smartphone.

Hardware

Software

  • Node-Red
  • Arduino IDE

A newbie can use Arduino IDE setup from here, Not only that for installation of Node-Red and how to use it you can check out the previous BlogTut.

Note: Kindly do not use the I/O and dashboard nodes in the flow page which is used in the previous blog. We have mentioned about nodes which we used in this project in below discussions.

About I2C relay Board and Code

The two-wire — I2C protocol has created a much easier way to connect Relay boards with any Microcontroller like Raspberry Pi, Arduino, ESP8266, ESP32 etc. For this project, we are using the 8 Channel I2C realy board which has the ability to run on various Cross-platform solutions with Extention features to connected the different actuators, sensors, current monitors and PWM drivers using I2C communication. It uses the External power of 12v DC adapter/ AC to DC Power Adapter and by using I2C cable you will be able to connect with different I2C adapters designed to connect the I2C devices with Microcontroller easily and perform the functions by using different I2C libraries available for different programming language on Github.

The I2C relay board is basically working with the help of I/o Extender IC package using MCP23008 mounted on this which is specially used to control the low power Signals of relay you can also check the datasheet for more information

Code

We will fetch the data from I2C devices and will give the commands to the I2C relay using I2C protocol. To add something unique to use this I2C relay board what we did is, we are giving the commands from Laptop Keyboard in serial monitor screen of Arduino IDE.

  • Initialize the Wire.h file called as I2C Library especially use in Arduino IDE
#include <Wire.h>
  • Initialize the I2C registers of sensor module which is specified to work with 2 wire protocol.
#define Addr 0x20
  • Begin the I2C transmission and Initialize the baudrate as per the requirements for serial communication.
  • Initializing the Input/Output for the I2C Relay Board
  • To specify the Pin as an Input or Output, will write to the “0x00” address.
Wire.beginTransmission(Addr);  // Select IODIR register  Wire.write(0x00);  // All pins are configured as outputs  Wire.write(0x00);  // Stop I2C transmission  
Wire.endTransmission();
  • In I2C interface with Arduino, we will be using the GPIO register(0x09).
Wire.beginTransmission(Addr);  // Select GPIO register  Wire.write(0x09);  Wire.write(0x00);  // Stop I2C transmission  Wire.endTransmission();
  • To read the character from Serial Monitor, we will request for 2 bytes of data from Wire.h I2C library using method “Wire.requestFrom()”, which will read the data and send it to I2C relay module
if(Serial.available()>0){
readByte = Serial.read();
char sw = (char) readByte;
Serial.print(sw);
Wire.requestFrom(Addr,2);
data = Wire.read();
  • Define the specific characters from the keyboards and write the switch on or off the relay with the help of characters used in it.
if(sw == ‘1’){
if(data == 0x01)
Wire.write(0x00);
else
Wire.write(0x01);
}
if(sw == ‘2’){
if(data == 0x02)
Wire.write(0x00);
else
Wire.write(0x02);
}

Finally, Upload the Code and open the serial monitor the screen and write the Characters you have mentioned in the code and there will be the change in the Output

Control I2C Relay With Node-Red Dashboard in Smartphone

After working with an above different way to control I2C relay board with Arduino, let’s control the I2C relay with Node red dashboard.

For installation and to install the different nodes in node red in windows operating system please go through the previous Blogtut

In this Project, We will be using much basic way to connect the I2C relay with Node-Red Dashboard via Arduino by

  • Open the node red using the command node-red in command prompt
  • Click on Manage Pallete by clicking top right “hamburger” icon
  • In User Settings click on Install
  • In serach option type — node-red-node-serialport
  • Click on Install button
  • Click on Close Button
  • Select and drag the Serial node(from the left side of node options) in a flow page.
  • Double Click on Serial node
  • Click on Edit Icon
  • Select the COM Port on which Arduino is connected and the baudrate and other settings as per the requirements also.
  • Select and drag Button node in dashboard node and copy/paste or drag around 8 button nodes to the flow.
  • Define the unique name to every button nodes.
  • Most Importantly, mention the characters which we are using on a keyboard in the payload and select the option as the number on the left hand side.

Note: You can also select the String option and other than that, options will be used according to the requirements

  • Connect the Button nodes with serial node
  • Click Deploy button

Finally, the output will be shown as

But the point comes when you want to use node red with your smartphone. Well, we are still working for some modification but till now we can use the Node-Red dashboard in the smartphone, Only if your phone is connected to the same network. Therefore I have connected my laptop as well as my smartphone with the same wifi network, then

  • Open the command prompt
  • Write command Ipconfig
  • Press Enter
  • Copy the Wireless LAN adaptor IPV4 address
  • Use the Wireless LAN IP address with the port number to open the node red in your smartphone web browser like
http://192.168.1.45:1880/ui/#/0 < — — — → http://(Wireless LAN IPV4):1880/ui/#/0
  • Press enter and you will be able to check the node-red dashboard in your smartphone also

Now Using the Node-Red dashboard in the smartphone you will be able to control I2C relay easily.

Limitations

  • This kind of application run on smartphone only when the local server, as well as smartphone, is connected on the same network
  • The node red connection should be running on the gateway end, Only then smartphone will be access to the node red dashboard

Future Implementation

  • This application can also be used with many other nodes and will be able to make the communication with different MCU
  • You can use the Wifi modules to connect with I2C relay via I2C adapter and using MQTT node mentioned in the previous blog, you can easily monitor as well as control the I2C device through a smartphone
  • This kind of application is best in using with home or industrial automation.
  • It creates a secure connection between them by making the user work in the similar network only.

Code

  • I2C code for Arduino and I2C Relay
  • Node-Red Tutorial

Credits