112 lines
2.9 KiB
C++
Executable File
112 lines
2.9 KiB
C++
Executable File
#include <ESP8266WiFi.h>
|
|
#include <WiFiClient.h>
|
|
|
|
int contconexion = 0;
|
|
|
|
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 = "/ESP/conexion.php";
|
|
String chipid = "";
|
|
|
|
//-------Función para Enviar Datos a la Base de Datos SQL--------
|
|
|
|
String enviardatos(String datos) {
|
|
String linea = "error";
|
|
WiFiClient client;
|
|
strhost.toCharArray(host, 49);
|
|
if (!client.connect(host, 80)) {
|
|
Serial.println("Error de conexion");
|
|
return linea;
|
|
}
|
|
|
|
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);
|
|
|
|
Serial.print("Enviando datos a SQL...");
|
|
|
|
unsigned long timeout = millis();
|
|
while (client.available() == 0) {
|
|
if (millis() - timeout > 5000) {
|
|
Serial.println("Cliente fuera de tiempo!");
|
|
client.stop();
|
|
return linea;
|
|
}
|
|
}
|
|
// Lee lo que recibe del servidor e imprime por serial
|
|
while(client.available()){
|
|
linea = client.readStringUntil('\r');
|
|
}
|
|
Serial.println(linea);
|
|
return linea;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
void setup() {
|
|
|
|
// Inicia Serial
|
|
Serial.begin(9600);
|
|
Serial.println("");
|
|
|
|
Serial.print("chipId: ");
|
|
chipid = String(ESP.getChipId());
|
|
Serial.println(chipid);
|
|
|
|
// Conexión WIFI
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED and contconexion <50) { //Cuenta hasta 50 si no se puede conectar lo cancela
|
|
++contconexion;
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
if (contconexion <50) {
|
|
//para 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());
|
|
}
|
|
else {
|
|
Serial.println("");
|
|
Serial.println("Error de conexion");
|
|
}
|
|
}
|
|
|
|
//--------------------------LOOP--------------------------------
|
|
void loop() {
|
|
|
|
unsigned long currentMillis = millis();
|
|
String fecha = ("2018-11-10 11:25");
|
|
if (currentMillis - previousMillis >= 10000) { //envia datos cda 10s
|
|
previousMillis = currentMillis;
|
|
//int analog = analogRead(17);
|
|
float temp = 22.4;//analog*0.322265625;
|
|
byte hum = 35;
|
|
Serial.print("Datos a enviar: ");
|
|
Serial.print(fecha);
|
|
Serial.print(", ");
|
|
Serial.print(temp);
|
|
Serial.print("°C, Humedad ");
|
|
Serial.print(hum);
|
|
Serial.println("%.");
|
|
String respuestaPHP = enviardatos("chipid=" + chipid + "&fecha=" + fecha + "&temp=" + String(temp, 1) + "&hum=" + String(hum));
|
|
Serial.println(respuestaPHP);
|
|
}
|
|
}
|
|
|
|
|
|
|