-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNodeMCUCode.ino
158 lines (122 loc) · 3.58 KB
/
NodeMCUCode.ino
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
152
153
154
155
156
157
158
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <BlynkSimpleEsp8266.h>
// Update these with values suitable for your network.
const char* auth = "_COnTjjQz48sXCusaUzxUbMWFqEqrVH5"; //Blynk Auth Token
const char* ssid = "mFi_0563C0"; // Wifi Username
const char* password = "1234567890"; //Wifi Password
const char* mqtt_server = "Broker.mqttdashboard.com";
const char* outTopic = "Group02/outT";
const char* inTopic = "Group02/inT";
int motor = D1; // GPIO5
String incomingByte;
WiFiServer server(80);
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void callback(char* topic, byte* payload, unsigned int length) {
if ((char)payload[0]=='2'){
digitalWrite(motor, HIGH);
}else if ((char)payload[0]=='1'){
digitalWrite(motor, LOW);
}
client.loop();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, password);
pinMode(motor, OUTPUT);
digitalWrite(motor, LOW);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
Blynk.run();
if (!client.connected()) {
reconnect();
}
client.loop();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/MOTOR=ON") != -1) {
digitalWrite(motor, HIGH);
value = HIGH;
}
if (request.indexOf("/MOTOR=OFF") != -1) {
digitalWrite(motor, LOW);
value = LOW;
}
// Set motor according to the request
//digitalWrite(motor, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Motor is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("<a href=\"/MOTOR=ON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/MOTOR=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");
delay(1);
}