-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ino
65 lines (60 loc) · 2 KB
/
app.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
TCPClient client;
byte server[] = { 192, 168, 1, 2 }; // Local light server
unsigned int len; // length of val
int inPin = D0; // button // led
//int outPin = D5;
int statusLight = 0;
/* Button and led logics */
int buttonState=0;
void setup() {
Serial.begin(9600);
delay(500);
pinMode(inPin, INPUT); // button pin setup
//pinMode(outPin, OUTPUT);
//digitalWrite(outPin, LOW);
client.connect(server, 80);
if (client.connected()){ //initiate connection
Serial.println("connected");
}else{
Serial.println("failed");
}
}
void loop(){
// read the state of the pushbutton value:
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
buttonState = digitalRead(inPin);
if (buttonState == HIGH && statusLight==0) {
//turn on
statusLight=1;
//digitalWrite(outPin, statusLight);
sendCommand("true");
} else if (buttonState==LOW && statusLight==1){
//turn off
statusLight=0;
//digitalWrite(outPin, statusLight);
sendCommand("false");
}
delay(200);
}
void sendCommand(String state){
if (client.connect(server, 80)) {
if(client.connected()){
unsigned int len = state.length(); //get length
client.println("PUT /api/{{api key here}}/lights/1/state HTTP/1.1");
client.println("Connection: keep-alive"); //
client.println("Host: 192.168.1.2"); //same as server
client.println("Content-Type: text/plain;charset=UTF-8"); //
client.print("Content-Length: "); //param
client.println(10+len); //brightness string + val length
client.println(); // blank line before body
client.print("{\"on\": ");
client.print(state); //value of potentiometer
client.println("}");
Serial.print("sent"); // command executed
delay(100); // slight delay IMPORTANt
}
client.stop();
Serial.println("stopping");
}
}