ESP8266-01 plus 000webhost Myadmin

Hi everyone, I am migrating from ENC28j60 to this ESP8266, and I was doing ok since now. If I send data (temp & hum) to Thingspeaks, its all good, if I send data to my computer working as a server, its all good, but when I send data to 000webhost it just dont get it even though all the responces from arduino are right, it looks like its kicking perfect but it does not (it connects to the LAN and to the web page as well). Check my php file typing http://bamboklaat.hostzi.com/php/habitacion_web.php?t=25&h=53 and the values of t and h goes to the table as it should. Is there any trick about 000webhost? just in case I tried to increase the time beween dumps now its 8 seconds, but nothing happends, suspect its a timming problem that I can not handle. Here its the code:

#include "DHT.h"  //Añadimos la libreria con la cual trabaja nuestro sensor
#define DHTPIN 22     // Indicamos el pin donde conectaremos la patilla data de nuestro sensor
#define DHTTYPE DHT11   // DHT 11 / DHT 22  (AM2302) / DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);  
#include <SoftwareSerial.h>
#define SSID "telecentro"
#define PASS "bamboklat"
#define DST_IP "bamboklaat.hostzi.com" //000Webhost  
SoftwareSerial dbgSerial(10, 11); // RX, TX

String GET = "GET /php/habitacion_web.php?t=";      //php/habitacion_web.php?t="; php/habitacion_web.php?t=25&h=53

//http://bamboklaat.hostzi.com/php/habitacion_web.php?t=25&h=53 

boolean flag_resp;

void setup()
{
  dht.begin();                

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.setTimeout(5000);
  dbgSerial.begin(9600); //can't be faster than 19200 for softserial
  dbgSerial.println("ESP8266 Demo");

  //Ciclara hasta que responde, generalmente 3 o 4 veces

  do
  {
    Serial.println("AT+RST");
    delay(1000);

    if(Serial.find("ready"))
    { 
      flag_resp= true;
      dbgSerial.println("Module is ready");
    }
    else
    { 
      flag_resp= false;
      dbgSerial.println("Module have no response.");
    }

  }
  while(flag_resp== false);

  delay(200);                

  //connect to the wifi
  boolean connected=false;
  for(int i=0;i<5;i++)
  {    
    //Conexion a la red
    if(connectWiFi())
    {
      connected = true;
      break;
    }
  }

  if (!connected){
    dbgSerial.println("No logra coectarse al servidor");
  }            //queda colgado si no se conecta a la pagina

  //delay(1000);

  Serial.println("AT+CIPMUX=0");
  //Serial.println("AT+CIPCLOSE");
}

void loop()
{ 
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";

  Serial.println(cmd);
  dbgSerial.println(cmd);

  if(Serial.find("Error")) return;


  int h = dht.readHumidity();  
  int t = dht.readTemperature(); 


  cmd = GET;
  cmd += String(t, DEC)+"&h="+String(h, DEC);
  cmd += "\r\n";     

  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  //delay(300);    


  if(Serial.find(">"))
  {
    dbgSerial.print(">");
  }
  else
  {
    Serial.println("AT+CIPCLOSE");
    dbgSerial.println("connect timeout");
    delay(2000);
    return;
  }

  Serial.print(cmd);
  //dbgSerial.println(cmd);
  delay(3000);


  //Serial.find("+IPD");
  while (Serial.available())
  {
    char c = Serial.read();
    dbgSerial.write(c);
    if(c=='\r') dbgSerial.print('\n');
  }
  dbgSerial.println("  ====");
  Serial.println("AT+CIPCLOSE");
  delay(8000);

}



boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  dbgSerial.println(cmd);
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("OK"))
  {
    dbgSerial.println("OK, Connected to WiFi.");
    return true;
  }
  else
  {
    dbgSerial.println("Can not connect to the WiFi.");
    return false;
  }
}

Here is also some extra data from 000wbhost that maybe say something:
Apache ver. 2.2.19 (Unix)
PHP version 5.2.*
MySQL ver. 5.1

Any kind of answer will be kindly appreciate, thanks for reading!

(moderator mode)

  1. Please use [code] [/code] tags

  2. Try to improve your coding style - use CTRL-T in the IDE

you might get something as readable as this

#include "DHT.h"                       //Añadimos la libreria con la cual trabaja nuestro sensor
#include "SoftwareSerial.h"

#define DHTPIN 22              // Indicamos el pin donde conectaremos la patilla data de nuestro sensor
#define DHTTYPE DHT11       // DHT 11 / DHT 22  (AM2302) / DHT 21 (AM2301)

#define SSID "telecentro"
#define PASS "bamboklat"
#define DST_IP "bamboklaat.hostzi.com" //000Webhost  

DHT dht(DHTPIN, DHTTYPE);  

SoftwareSerial dbgSerial(10, 11); // RX, TX
   
String GET = "GET /php/habitacion_web.php?t=";      
     
//URL check: http://bamboklaat.hostzi.com/php/habitacion_web.php?t=25&h=53 
 
boolean flag_resp;
   
void setup()
{
  dht.begin();                
     
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.setTimeout(5000);

  dbgSerial.begin(9600); //can't be faster than 19200 for softserial
  dbgSerial.println("ESP8266 Demo");
     
  // Cycle till it connects, usually take 3 o 4 tries
     
  do
  {
    Serial.println("AT+RST");
    delay(1000);
     
    if (Serial.find("ready"))
    { 
      flag_resp = true;
      dbgSerial.println("Module is ready");
    }
    else
    { 
      flag_resp = false;
      dbgSerial.println("Module have no response.");
    }
     
  } while (flag_resp == false);
         
  delay(200);                
     
  //connect to the wifi
     
  boolean connected = false;
  for(int i = 0; i < 5; i++)
  {    
    //Conexion a la red
    if ( connectWiFi() )
   {
     connected = true;
     break;
    }
  }
          
  if ( !connected )
  {
    dbgSerial.println("No logra coectarse al servidor");
  }            

  //delay(1000);
  Serial.println("AT+CIPMUX=0");
  //Serial.println("AT+CIPCLOSE");
}
   
   
void loop()
{ 
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";
     
  Serial.println(cmd);                       //Conects to 000wbhost
  dbgSerial.println(cmd);
     
  if ( Serial.find("Error") ) return;
     
     
  int h = dht.readHumidity();  
  int t = dht.readTemperature(); 
    
           
  cmd = GET;
  cmd += String(t, DEC)+"&h="+String(h, DEC);
  cmd += "\r\n";     
      
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  //delay(300);    
     
     
  if ( Serial.find(">") )
  {
    dbgSerial.print(">");
  }
  else
  {
    Serial.println("AT+CIPCLOSE");
    dbgSerial.println("connect timeout");
    delay(2000);
    return;
  }
       
  Serial.print(cmd);           //sends data
  delay(3000);
       
       
  // Serial.find("+IPD");
  while ( Serial.available() )
  {
    char c = Serial.read();
    dbgSerial.write(c);                          
    
    // on dbgserial I see "GET /php/habitacion_web.php?t=23&h=41"

    if ( c == '\r' ) dbgSerial.print('\n');
  }

  dbgSerial.println("  ====");
  Serial.println("AT+CIPCLOSE");
  delay(8000);
}
     
     
boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");

  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  dbgSerial.println(cmd);
  Serial.println(cmd);

  delay(2000);
  if ( Serial.find("OK") )
  {
    dbgSerial.println("OK, Connected to WiFi.");
    return true;
  }
  else
  {
    dbgSerial.println("Can not connect to the WiFi.");
    return false;
   }
}

Thanks you Rob for the tip, absolutly easier code reading.

The advantage of using a code style is that some bugs are easier to catch.

Thanks Rob, also, any suggestion about the real problem will be kindly appreciate.

The end result of the content you're sending to your web site should look like:

GET /php/habitacion_web.php?t=123&h=123 HTTP/1.0
Host: bamboklaat.hostzi.com
 ---- blank line here ---

Without the "HTTP/1.0" part it defaults to version 0.9 of the HTTP protocol. In that version there is only ever one line sent by the client. That's the limitation of version 0.9 ... and one of the reasons why it was phased out so quickly.

When you send to your 000webhost you need to include your hostname (the Host: line) so that the web server knows what site the request is for. There are probably thousands of sites sitting on any single IP at 000webhost and without the hostname your request won't get anywhere. This is referred to as "name virtualhosting".

Now, comparing this to why your code works on Thingspeak, there's only one "site" on their IP; the hostname never needs to be included in the request and HTTP 0.9 will work just fine there.

robtillaart:
(moderator mode)

  1. Please use tags

  2. Try to improve your coding style - use CTRL-T in the IDE

you might get something as readable as this

Definitely better, however that style is still pretty darn awful. It would be 1000% better if the '{' & '}' touched what they belong to... hate it...

if (Serial.find("ready"))
{
** flag_resp = true;**
** dbgSerial.println("Module is ready");**
}
else
{
** flag_resp = false;**
** dbgSerial.println("Module have no response.");**
}

And below is the same amount of lines, just far easier on the eye.

if(Serial.find("ready")){
** flag_resp = true;**
** dbgSerial.println("Module is ready");**

}else{
** flag_resp = false;**
** dbgSerial.println("Module have no response.");**
}

Thanks so much for the answer Chagrin, it has a great value for me all you said in the last messege, still I am dealing with it, first I tried this one it follows:

Arduino Code modification

     cmd +=" HTTP/1.0";
     cmd += "\r\n";     
     cmd += "Host: bamboklaat.hostzi.com";
     cmd += "\r\n";
     cmd += "\r\n";

Serial Monitor

AT+CIPSTART="TCP","bamboklaat.hostzi.com",80
AT+CIPSEND=79
GET /php/habitacion_web.php?t=24&h=42 HTTP/1.0
Host: bamboklaat.hostzi.com

AT+CIPCLOSE
...
.....

but looking at the debug serial, at the part of the code where it says

while (Serial.available())
       {
         char c = Serial.read();
         dbgSerial.write(c);
         if(c=='\r') dbgSerial.print('\n');
       }

I do not get the whole string, just this part "GET /php/habitacion_web.php?t=24&h=42 HTTP/1.0Host: bamboklaat" am I sending the string in a wrong way? Have arduino Mega so it should not be lack of sram or something.

The string "GET /php/habitacion_web.php?t=24&h=42 HTTP/1.0Host: bamboklaat" is 63 bytes, plus the newline, making that 64 bytes. That's also the buffer size of SoftwareSerial. I'd assume that's why it is being cut off -- or it's not necessarily indicative of your problem at hand.

Running your PHP page manually, I see this output:

root@zippo:/home/creeper# telnet bamboklaat.hostzi.com 80
Trying 31.170.161.36...
Connected to bamboklaat.hostzi.com.
Escape character is '^]'.
GET /php/habitacion_web.php?t=123&h=321 HTTP/1.0
Host: bamboklaat.hostzi.com

HTTP/1.1 200 OK
Date: Tue, 28 Jul 2015 04:44:26 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 255
Connection: close
Content-Type: text/html


INSERT INTO `temp_y_hum` SET humedad = "321", temperatura = "123", fecha_hora = NOW()  

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
Connection closed by foreign host.

Doublecheck your temp_y_hum table; if the humedad and temperatura columns are integer or other numeric type then your SQL is incorrect; you're treating the numbers as strings instead of numbers. Should be:

INSERT INTO `temp_y_hum` SET humedad = 321, temperatura = 123, fecha_hora = NOW()

No quotes around the 321 and 123. Just for sanity's sake you should also check to see if my request left those two values somewhere in your table -- if not then there's a problem with your PHP.

Hi Chagrin, both temperatura and humedad are set as integers in the database, and the record you dumped was saved (humedad=321 & temperatura=123) perfectly in the proper spot of the table, Dont get the point about it, I am not an expert programmer, but we can clearly see that even though the data its sent as a string, later on seems that when temperatura is updated, the value sets in the proper place as an integet in temperatura integer variable. Please, take a look at the code done with the enc28j60 wihch works ok:

    ...
    ....
    client.print("h=");
    client.print(str_h);                        //str_h the vale of h in string format
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
    }

Also tried with HTTP/1.1 as you suggest instead of HTTP/1.0, and also tried exactly like the text above, and also with just one blank line after "Connection: close"... still frustrations...

Hereś the php code just in case or simply someone want to get it:

<?php  
$ipServidor = 'mysql11.000webhost.com';  
$usuario = 'a4224174_root';  
$contraseña = 'ledzeppelin1918';  
$db = 'a4224174_arduino';  
mysql_connect($ipServidor,$usuario,$contraseña);  
mysql_select_db(a4224174_arduino);
unset($ipServidor,$usuario,$contraseña); 
$temperatura = $_GET['t'];  
$humedad = $_GET['h'];  
$sql = 'INSERT INTO `temp_y_hum` SET humedad = "'.mysql_real_escape_string($humedad).'", temperatura = "'.mysql_real_escape_string($temperatura).'", fecha_hora = NOW()';  


echo "
"; 
echo $sql; 
  

mysql_query($sql);  

mysql_close();  

?>

Please, dont get it wrong using expresions like "Just for sanity's sake... ", when it can be lack of knowledge or practice, or even worse, this modules with arduino are not realible like few people claim, and it seems like plenty of dudes deny it just because they dont want to say it. It took me a while to communicate in your lengueage like I do. Thanks for all your shared knowledge though.

Using HTTP/1.1 won't offer any advantage. Stick with HTTP/1.0; I guarantee that will work -- if you can find a solution to whatever bug it is you're experiencing.

Aside from that I don't have any answers. I was simply making suggestions on what you should check, and my "sanity's sake" comment was just to ensure there wasn't a problem with your PHP that was causing your problems. Your English has been excellent so I did not expect a miscommunication there.

Regarding the reliability of the ESP8266 it's my suspicion that it's the default firmwares (which use the AT commands) which are unreliable, but I cannot provide any hard facts to back that up. My solution was to use the NodeMCU firmware so I don't have a lot of experience with the various AT commands.

HOPE SOME ONE EXPERIENCING WITH AI-v0.9.5.0 AT Firmware will read this, I got a happy end at the end, just because i know now what was happening, the command AT+CIPSEND was not able to send the string, even though i set a long delay after it (10 sec)... so I forgot the code and started hitting the commands by hand at the serial monitor and realice that my AT firmware is so unstable, if the string length was 10, it was fine, I got the "SEND" answer back, if it was 11, it doesnt send it, on the other hand, as I said, the code about sending data to Thingspeak works, been the length of the string equal to 44 (and without even getting the "SEND" messege back at the debug serial)... so I said to myself "Enough..."... already load "NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4" and start over, will see how it goes. THANKS to everyone who push me to keep me going, also digging I "was not/will be not" be the only one who suffer this AT firmware. Will update as I keep going. ARDUINO INTERACTING WITH ESP WITH AT COMMANDS IS NOT REALIBLE.

how do I send data from arduinom mega + esp8266-e01 to the 000webhost.com database table ?? please help me