-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogro_API_boton.ino
More file actions
154 lines (107 loc) · 3.04 KB
/
logro_API_boton.ino
File metadata and controls
154 lines (107 loc) · 3.04 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
/* ----------- WiFi ----------- */
#define WIFI_SSID "Tec-IoT"
#define WIFI_PASSWORD "spotless.magnetic.bridge"
HTTPClient httpClient;
WiFiClient wClient;
String URL1 = "http://10.14.255.7:10206/dispensar/insert";
String URL2 = "http://10.14.255.7:10206/rellenar/insert";
const int button = D4;
const int echo = D6;
const int trig = D5;
int duracion;
int distancia;
int limite = 0;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Conectando a red WiFi \"");
Serial.print(WIFI_SSID);
Serial.print("\"");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nConectado! IP: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop(){
getDistancia();
if (distancia <= 15){
Serial.println("No Rellenar");
log_distancia_POST(22206, "No Rellenar");
}else{
Serial.println("Rellenar");
log_distancia_POST(22206, "Rellenar");
}
if(limite == 5){
Serial.println("-----* Boton *-----");
delay(2500);
if(digitalRead(button) == HIGH){
Serial.println("Dispensado");
log_boton_POST(22206, "Dispensar");
}else{
Serial.println("No Dispensado");
log_boton_POST(22206, "No Dispensar");
}
limite = 0;
}
limite++;
delay(1000);
}
void log_boton_POST(int robot_id, String dato){
String string_a;
string_a = robot_id;
// "{\" dispensar\": "si}
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(wClient, URL1);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"id_robot\":" + string_a + ", \"dispensar\": \"" + dato + "\"";
httpRequestData = httpRequestData + "}";
Serial.print(httpRequestData);
Serial.println();
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
return;
}
void getDistancia(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duracion = pulseIn(echo, HIGH);
distancia = duracion * 0.034 / 2;
return;
}
void log_distancia_POST(int robot_id, String dato){
String string_a;
string_a = robot_id;
// "{\" dispensar\": "si}
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(wClient, URL2);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"id_robot\":" + string_a + ", \"rellenar\": \"" + dato + "\"";
httpRequestData = httpRequestData + "}";
Serial.print(httpRequestData);
Serial.println();
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
return;
}