143 lines
3.9 KiB
Arduino
143 lines
3.9 KiB
Arduino
|
#include <ESP8266WiFi.h>
|
||
|
#include <WiFiClient.h>
|
||
|
|
||
|
bool enviar, conectado;
|
||
|
String fecha;
|
||
|
float tempSup, tempInf, tempExt;
|
||
|
byte hum1, hum2, hum3, hum4, humSup, humInf, humExt;
|
||
|
|
||
|
const char *ssid = "mi_ssid";
|
||
|
const char *password = "mi_clave";
|
||
|
|
||
|
unsigned long previousMillis = 0;
|
||
|
|
||
|
char host[48];
|
||
|
String strhost = "192.168.0.10";
|
||
|
String strurl = "/Gardener/conexion.php";
|
||
|
int chipid;
|
||
|
|
||
|
//-------Función para Enviar Datos a la Base de Datos SQL--------
|
||
|
|
||
|
void enviardatos(String datos) {
|
||
|
WiFiClient client;
|
||
|
strhost.toCharArray(host, 49);
|
||
|
conectado = client.connect(host, 80);
|
||
|
client.print(String("POST ") + strurl + " HTTP/1.1" + "\r\n" +
|
||
|
"Host: " + strhost + "\r\n" +
|
||
|
"Accept: */*" + "*\r\n" +
|
||
|
"Content-Length: " + datos.length() + "\r\n" +
|
||
|
"Content-Type: application/x-www-form-urlencoded" + "\r\n" +
|
||
|
"\r\n" + datos);
|
||
|
delay(10);
|
||
|
|
||
|
unsigned long timeout = millis();
|
||
|
while(millis()- timeout < 10){
|
||
|
// delay
|
||
|
}
|
||
|
while (client.available() == 0) {
|
||
|
if (millis() - timeout > 5000) {
|
||
|
client.stop();
|
||
|
enviar=false;
|
||
|
}
|
||
|
}
|
||
|
// Agregar respuesta desde PHP, para saber si guardo o no, para repetir con enviado=true
|
||
|
/*
|
||
|
while (client.available()) {
|
||
|
String resp = client.readStringUntil('\r');
|
||
|
}*/
|
||
|
}
|
||
|
|
||
|
//-------------------------------------------------------------------------
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
chipid = ESP.getChipId();
|
||
|
//Serial.println(chipid);
|
||
|
// Conexión WIFI
|
||
|
//Serial.println("Conectando");
|
||
|
WiFi.begin(ssid, password);
|
||
|
byte cont = 0;
|
||
|
/*
|
||
|
unsigned long contador = millis();
|
||
|
while (WiFi.status() != WL_CONNECTED and cont < 50) {
|
||
|
if (millis() - contador > 500) {
|
||
|
++cont;
|
||
|
Serial.print('.');
|
||
|
contador = millis();
|
||
|
}
|
||
|
}
|
||
|
*/
|
||
|
while (WiFi.status() != WL_CONNECTED and cont <50) {
|
||
|
++cont;
|
||
|
delay(500);
|
||
|
//Serial.print(".");
|
||
|
}
|
||
|
//Serial.print("IP :");
|
||
|
//Serial.println(WiFi.localIP());
|
||
|
/*
|
||
|
if (cont < 50) {
|
||
|
//Usar con ip fija
|
||
|
IPAddress ip(192,168,1,156);
|
||
|
IPAddress gateway(192,168,1,1);
|
||
|
IPAddress subnet(255,255,255,0);
|
||
|
WiFi.config(ip, gateway, subnet);
|
||
|
Serial.println("");
|
||
|
Serial.println("WiFi conectado");
|
||
|
Serial.println(WiFi.localIP());
|
||
|
}*/
|
||
|
}
|
||
|
|
||
|
//--------------------------LOOP--------------------------------
|
||
|
void loop() {
|
||
|
//Serial.println("LOOP");
|
||
|
unsigned long currentMillis = millis();
|
||
|
if (currentMillis - previousMillis >= 5000) {
|
||
|
previousMillis = currentMillis;
|
||
|
//Serial.println("Leyendo datos");
|
||
|
leerDatos();
|
||
|
}
|
||
|
if (fecha.endsWith("0") && enviar == false) {
|
||
|
enviardatos("id=" + String(chipid) + "&fecha=" + fecha + "&hum1=" + String(hum1) + "&hum2=" + String(hum2) + "&hum3=" + String(hum3) + "&hum4=" + String(hum4) + "&tempsup=" + String(tempSup, 1) + "&tempinf=" + String(tempInf, 1) + "&humsup=" + String(humSup) + "&huminf=" + String(humInf) + "&tempext=" + String(tempExt, 1) + "&humext=" + String(humExt));
|
||
|
enviar = true;
|
||
|
//Serial.println("If 1 true");
|
||
|
}
|
||
|
if(!fecha.endsWith("0")) {
|
||
|
enviar=false;
|
||
|
//Serial.println("If 2 false");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// validar
|
||
|
void leerDatos() {
|
||
|
/*
|
||
|
if (Serial.available() > 0) {
|
||
|
hum1 = Serial.readStringUntil(',').toInt();
|
||
|
hum2 = Serial.readStringUntil(',').toInt();
|
||
|
hum3 = Serial.readStringUntil(',').toInt();
|
||
|
hum4 = Serial.readStringUntil(',').toInt();
|
||
|
tempSup = Serial.readStringUntil(',').toFloat();
|
||
|
tempInf = Serial.readStringUntil(',').toFloat();
|
||
|
humSup = Serial.readStringUntil(',').toInt();
|
||
|
humInf = Serial.readStringUntil(',').toInt();
|
||
|
tempExt = Serial.readStringUntil(',').toFloat();
|
||
|
humExt = Serial.readStringUntil(',').toFloat();
|
||
|
fecha = Serial.readStringUntil('\r');
|
||
|
while (Serial.available() > 0) {
|
||
|
char temp = Serial.read();
|
||
|
}
|
||
|
}*/
|
||
|
fecha="2018-9-25 23:40";
|
||
|
hum1=10;
|
||
|
hum2=20;
|
||
|
hum3=30;
|
||
|
hum4=40;
|
||
|
tempSup=50.5;
|
||
|
tempInf=60.6;
|
||
|
humSup=70;
|
||
|
humInf=80;
|
||
|
tempExt=90;
|
||
|
humExt=99;
|
||
|
}
|
||
|
|
||
|
|