-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.1. I2C_NodeMCU_LEDcontrol.ino
70 lines (70 loc) · 1.6 KB
/
3.1. I2C_NodeMCU_LEDcontrol.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
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char* ssid = "paya";
const char* password = "mishtynaina";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
Wire.begin(D1, D2);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin(); //Capital S???
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();
int val;
if (req.indexOf("/gpio/0") != -1){
val = 0;
Wire.beginTransmission(8);
Wire.write(val);
Wire.endTransmission();
}
else if (req.indexOf("/gpio/1") != -1){
val = 1;
Wire.beginTransmission(8);
Wire.write(val);
Wire.endTransmission();
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(LED_BUILTIN, val);
client.flush();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>";
Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
while(Wire.available()){
char c = Wire.read();
Serial.print(c);
}
Serial.println();
delay(1000);
}