-->
Page 1 of 1

ESP8266 Server and Client problem sent to PHP and MySql

PostPosted: Mon Oct 17, 2016 5:52 am
by Maese Lin
Hello,
First of all, thank you very much for your strong support to comunity.
This is my first question. It´s regarding a data sent problem. The problem was I put a ESP8266 server and client. Server found correctly but I tried to send information from ESP to MySql with PHP code but the result was not good.
This is the code:
-------------------------------------------------------------------
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <string.h>
const char* ssid = "xxxx1";
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);

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);
}
}
}
}

void enviarDatosOFF() {
String nombre=("LuzComedor");
String estado=("OFF");
String data = "nombre=" + nombre + "&ip=" + ip_addr + "&estado=" + estado;
String PostData = ("http://192.168.1.200/zaratrusta/zaratrusta.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);
}
}

}

}
void setup( void) {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
Serial.begin(115200);
WiFi.config(ip_addr,gw,netmask);
Serial.println ( "" );
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
Server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}

void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while(!client.available()){
delay(1);
}

String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}
if (val= 0)
enviarDatosON(); //// use a client to send information to MySQL via PHP
else if (val= 1)
enviarDatosOFF(); //// use a client to send information to MySQL via PHP
else{
}

// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";

// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
}

-------------------------------------------------------------
The Output result are:

WiFi connected
Server started
192.168.1.101
new client
GET /gpio/0 HTTP/1.1
Timeout
Client disonnected
new client
GET /gpio/0 HTTP/1.1
Timeout
Client disonnected
new client
-----------------------------------------
The problem is that not save any information in BBDD Mysql.
Do you know any answer? or any way to follow?

Thank you very much.
MaeseLin.