-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
119 lines (95 loc) · 3.25 KB
/
main.cpp
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
// ESP32 conectado al sensor KY-001
// KY-001
// [s] --> PIN 4
// [+] --> VIN
// [-] --> GROUND
// ESCRIBE INFORMACION A UNA BASE DE DATOS DEL PROYECTO https://github.com/Krim10000/Django-TEMP
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 4 on the Arduino
#define ONE_WIRE_BUS 4
const char* ssid ="TU_RED"; //CAMBIAR
const char* password ="TU_CONTRASEÑA"; //CAMBIAR
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
float temp;
int time1 = 1; // time in minutes
int time2 = time1*60*1000; // delay entre post
void Conect_internet()
{
Serial.println("");
Serial.print("Conectando a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(10);
}
Serial.println("");
Serial.println("Conexion exitosa ");
Serial.print("La IP es :");
Serial.println(WiFi.localIP());
}
///conecta a internet
void setup() {
Serial.println("INICIANDO SETUP");
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
Serial.begin(115200);
delay(10);
Serial.println("SETUP COMPLETO");
}
void loop() {
Serial.println("INICIANDO LOOP");
if (WiFi.status() != WL_CONNECTED) {
Conect_internet();
}
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
//String input = String(("{\"TEMPERATURA\":\") + temp + "(\"}");
// contruye el input para POST
String input = "{\"TEMPERATURA\":";
input+= temp;
input+= "}";
// POST, escribe la Temperatura en el servidor
HTTPClient http;
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
http.begin("http://192.168.0.20:80/snippets/"); //Specify destination for HTTP request
// ojo la URL y el puerto pueden variar
http.addHeader("Content-Type", "application/json"); //Specify content-type header
Serial.print("JSON: ");
Serial.println(input);
int httpResponseCode = http.POST(input); //FUNCIONA
//("{\"name\":\"value\"}")
Serial.println("La temperatura es " + String(temp) +"°C");// mensaje con la temperatura
if(httpResponseCode>0)
{
String response = http.getString(); //Get the response to the request
Serial.print("CODIGO: ");
Serial.println(httpResponseCode); //Print return code
if (httpResponseCode == 201)
{Serial.println("EXITO, valor ingresado a la base de datos.");
}
}
else
{Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
Serial.println("");
Serial.println("Desconectando");
WiFi.disconnect();
Conect_internet();}
http.end(); //Free resources
}else
{Serial.println("Error in WiFi connection");
Serial.println("");
Serial.println("Desconectando");
WiFi.disconnect();
Conect_internet();}
delay(time2); //Send a request every minute
}