The use of the ESP8266 in the world of IoT

User avatar
By Maese Lin
#56626 Hi,
First of all, this is my first question , and thank you very much in advance for your support.
This is a problem regarding sent information from ESP8266 to MySQL by PHP script in a local server. The data is not saved in Mysql database and I don´t know. The rest of code are working correctly. It´s the code not working:
*************************************************************************
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <string.h>
const char* ssid = "ONO1234";
const char* password = "password";
#define LOCALIP "192.168.1.101" // String for the local server configuration
IPAddress ip_addr(192,168,1,101); //Requested static IP address for the ESP
IPAddress gw(192,168,1,1); // IP address for the Wifi router
IPAddress netmask(255,255,255,0);
WiFiClient client;
const char* host="http://192.168.1.200/";
WiFiServer server(80); // need to implemented a server ESP8266 at same time that Client.
void enviarDatosON() {
String nombre=("LuzComedor");
String estado=("ON");
String data = "nombre=" + nombre + "&ip=" + ip_addr + "&estado=" + estado;
String PostData = ("http://192.168.1.200/xxxxx/xxxxx.php?" + data);
if (client.connect(host, 80)) {
client.println("POST /posts HTTP/1.1");
client.println("Host: http://192.168.1.200/");
client.println("Cache-Control: no-cache");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);

long interval = 2000;
unsigned long currentMillis = millis(), previousMillis = millis();

while(!client.available()){

if( (currentMillis - previousMillis) > interval ){
Serial.println("Timeout");
client.stop();
return;
}
currentMillis = millis();
}

while (client.connected())
{
if ( client.available() )
{
char str=client.read();
Serial.println(str);
}
}
}
}

The php file are ok because I tried to insert some information directly by Chrome like :http://192.168.1.101/xxxxx/xxxxx.php?nombre=xx&...etc...
Could you have a look please?
Thank you very much!
MaeseLIn.