single phase control anywhere in world by IOT
single phase control anywhere in world by IOT
Introduction
This is simple iot application to control all single application control through mobile anywhere from world you need just internet availability
Then we need some components that are given below order wise
1. Nodemcu or ardunio-die
2. 3.3v relay kit module
3. wires male to female pins
4. charger for power supply to Nodemcu
5. usb cable for program upload purpose
program setup explanation
step 1: we need set up pins
step 2: WiFi connection
step 3: server connection
step 4: port set up
step 5: WiFi setup processing
step 6: button setup means (subscribe )
step 7: condition setup means what is you going to doing either on or off
program explanation step-wise
#include <ESP8266WiFi.h>
#include <MQTT.h>
//s
tep 1: setup your wifi user name and password
/
#define AP_SSID "XXX"
#define AP_PASSWORD "XXX"
///s
tep 2: setup your sever user name and password
#define EIOTCLOUD_USERNAME "21111112"
#define EIOTCLOUD_PASSWORD "suresh"
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
#define DO_TOPIC "/Sensor.Parameter1"
///s
tep 3: setup your pins
#define PIN_DO_0 D0
#define MODULE_ID_0 0
#define PIN_DO_1 D1
#define MODULE_ID_1 1
#define PIN_DO_2 D2
#define MODULE_ID_2 2
#define PIN_DO_3 D3
#define MODULE_ID_3 3
#define PIN_DO_4 D4
#define MODULE_ID_4 4
#define PIN_DO_5 D5
#define MODULE_ID_5 5
#define PIN_DO_6 D6
#define MODULE_ID_6 6
#define PIN_DO_7 D7
#define MODULE_ID_7 7
s
tep 4: setup your port
MQTT myMqtt("", EIOT_CLOUD_ADDRESS, 1883);
step 5: WiFi setup processing
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");
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);
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);
myMqtt.setUserPwd(EIOTCLOUD_USERNAME, EIOTCLOUD_PASSWORD);
myMqtt.connect();
delay(500);
///it will set mode of pins or how to work your pins like in put are output
pinMode(PIN_DO_0, OUTPUT);
pinMode(PIN_DO_1, OUTPUT);
pinMode(PIN_DO_2, OUTPUT);
pinMode(PIN_DO_3, OUTPUT);
pinMode(PIN_DO_4, OUTPUT);
pinMode(PIN_DO_5, OUTPUT);
pinMode(PIN_DO_6, OUTPUT);
pinMode(PIN_DO_7, OUTPUT);
subscribe();
////condition setup
}
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;
}
///step 6: button setup means (subscribe )
void subscribe()
{
myMqtt.subscribe("/" + String(MODULE_ID_0) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_1) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_2) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_3) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_4) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_5) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_6) + DO_TOPIC);
myMqtt.subscribe("/" + String(MODULE_ID_7) + DO_TOPIC);
}
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.");
}
////step 7: condition setup means what is you going to doing either on or off
void myDataCb(String& topic, String& data) {
if (topic == String("/"+String(MODULE_ID_1)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_1, HIGH);
else
digitalWrite(PIN_DO_1, LOW);
Serial.print("D1 1:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_0)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_0, HIGH);
else
digitalWrite(PIN_DO_0, LOW);
Serial.print("D0 0:");
Serial.println(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("D2 2:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_3)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_3, HIGH);
else
digitalWrite(PIN_DO_3, LOW);
Serial.print("D3 3:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_4)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_4, HIGH);
else
digitalWrite(PIN_DO_4, LOW);
Serial.print("D4 4:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_5)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_5, HIGH);
else
digitalWrite(PIN_DO_5, LOW);
Serial.print("D5 5:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_6)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_6, HIGH);
else
digitalWrite(PIN_DO_6, LOW);
Serial.print("D6 1:");
Serial.println(data);
}
if (topic == String("/"+String(MODULE_ID_7)+ DO_TOPIC))
{
if (data == String("1"))
digitalWrite(PIN_DO_7, HIGH);
else
digitalWrite(PIN_DO_7, LOW);
Serial.print("D7 1:");
Serial.println(data);
}
}
button setup
server web link click here
module setup
parameter setup
Comments