-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (122 loc) · 4.36 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <Arduino.h>
#include<ESP8266WiFi.h>
#include<PubSubClient.h>
const char* ssid = "put ssid here"; //Wifi ssid and Password
const char* password = "Put pass here";
const char *mqtt_broker = "mqtt.eclipseprojects.io"; // mqtt broker address and port number
const int mqtt_port = 1883;
const uint8_t Relay1 = 2; // Relay Gpio
const uint8_t Relay2 = 0;
WiFiClient esp;
PubSubClient client(esp);
/* Firmware version -> 0.1 *
* Made by Aman Shaikh *
* contact -> [email protected]*/
/*------------------------------------------------------------------\
| Function roles |
|-------------------------------------------------------------------|
| |
| Connectify --> connects the device to wifi |
| |
| action --> modify this to take action when a |
| a specified message is published |
| on a patricular topic. |
| |
| callback --> Gets the message information from the topic |
| |
| sublist --> Add topics to listen to |
| |
| reconnect --> As name suggests reconnects when disconnected |
| |
| setup --> connect to wifi |
| connect to mqtt broker |
| Set callback function |
| |
| loop --> check for connection status and reconnect if disconnected|
\------------------------------------------------------------------*/
void connectify(const char* network,const char* pass){
int times=0;
Serial.println("connecting to ");
Serial.print(network);
Serial.println("----------------------------------------------------");
WiFi.begin(network,pass);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
times+=1;
}
Serial.println("----------------------------------------------------");
Serial.println("Connecting tries = ");
Serial.println(times);
Serial.println("----------------------------------------------------");
Serial.println("IP address ----> ");
Serial.println(WiFi.localIP());
}
void action(String topic,String message){
if(topic == "Relay"){
if(message == "1on"){
digitalWrite(Relay1,LOW);
}
else if(message == "1off"){
digitalWrite(Relay1,HIGH);
}
if(message == "2on"){
digitalWrite(Relay2,LOW);
}
else if(message == "2off"){
digitalWrite(Relay2,HIGH);
}
}
}
void callback(String topic, byte* message, unsigned int length) {
Serial.println("--------------");
Serial.print(topic);
Serial.println("");
Serial.println("Message: ");
String messageInfo;
for ( int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageInfo += (char)message[i];
}
action(topic,messageInfo);
Serial.println("");
}
void sublist(){
client.subscribe("Relay");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect("esp")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more Relay1s in this example)
sublist();// add here the function to sub
} else {
Serial.print("failed to connect, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
connectify(ssid,password);
client.setServer(mqtt_broker,mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop()){
client.connect("esp");
}
}