-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotones-ESP8266.ino
More file actions
133 lines (105 loc) · 2.36 KB
/
Botones-ESP8266.ino
File metadata and controls
133 lines (105 loc) · 2.36 KB
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
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//char* ssid = "";
//char* password = "";
#define pushbutton1 D0
#define pushbutton2 D1
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println();
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(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(pushbutton1,INPUT);
pinMode(pushbutton2,INPUT);
}
void loop()
{
boton_izquierdo();
boton_derecho();
}
String boton_izquierdo()
{
int estado = 0;
int salida = 0;
int estado_anterior = 0;
String lado = "IZQUIERDO";
estado = digitalRead(pushbutton1);
if((estado == HIGH) && (estado_anterior == LOW))
{
salida = 1 - salida;
}
estado_anterior = estado;
delay(80);
if(salida == 1)
{
Serial.print(lado);
Serial.println(F(" \n "));
Serial.println(F("PETICION ENVIADA"));
Serial.println(F(" \n "));
post(lado);
return lado;
}
else
{
Serial.print("\n .");
}
}
String boton_derecho()
{
int estado2 = 0;
int salida2 = 0;
int estado_anterior2 = 0;
String lado = "DERECHO";
estado2 = digitalRead(pushbutton2);
if((estado2 == HIGH) && (estado_anterior2 == LOW))
{
salida2 = 1 - salida2;
}
estado_anterior2 = estado2;
delay(80);
if(salida2 == 1)
{
Serial.print(lado);
Serial.println(F(" \n "));
Serial.println(F("PETICION ENVIADA"));
Serial.println(F(" \n "));
post(lado);
return lado;
}
else
{
Serial.print("\n . ");
}
}
void post(String lado)
{
HTTPClient http;
String json = "";
String server = "http://192.168.0.4:3000/api/product/";
server.concat(lado);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
Serial.println(server);
http.begin(server);
http.addHeader("Content-Type", "application/json");
Serial.println("");
http.POST(json);
http.writeToStream(&Serial);
http.end();
Serial.println("FIN MyPost");
Serial.println("");
}