Search code examples
phpmysqlesp8266arduino-esp8266

ESP8266-01 POST Request to MySQL


I am trying to make POST request to a MySQL database that I have set up. I have tried various tutorials to no avail. I can use the PHP POST page I created to input data through a browser like this:

<?php
//post.php
include 'connect.php';
var_dump($_GET);
$id = $_GET['id'];
$value = $_GET['value'];
$id = mysql_real_escape_string($id);
$value = mysql_real_escape_string($value);

mysql_query(
"INSERT INTO iot (value, id) VALUES ('$value', '$id');"
)
?>

I was first trying to reach my own PHP site and send a POST request with temperature and dewpoint like this:

//tempandhumidity2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "SimpleDHT.h"

SimpleDHT11 dht11;
const char* ssid = "nameofssid";
const char* password = "password";

int ledPin = 2; // GPIO2

void setup() {

Serial.begin(115200);                //Serial connection
WiFi.begin(ssid, password);   //WiFi connection

while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
  delay(500);
  Serial.println("Waiting for connection");
  }
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}

void loop() {
HTTPClient http;
http.begin("http://192.168.43.162/iot/post.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("id=12&value=35");
Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
   Serial.print("HTTP response code ");
   Serial.println(httpCode);
   String response = http.getString();
   Serial.println(response);
}
else
{
   Serial.println("Error in HTTP request");
}

http.end();
} 

This returns:

Waiting for connection
Waiting for connection
Waiting for connection
Waiting for connection
Waiting for connection


Connecting to nameofssid
Use this URL to connect: http://192.168.43.176/
200
HTTP response code 200
Connected successfullyarray(0) {
}
<br />
<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\iot\post.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined index: value in <b>C:\xampp\htdocs\iot\post.php</b> on line <b>5</b><br />

Using the webbrowser with http://10.0.0.90/iot/post.php?id=14&value=33 returns:

Connected successfullyarray(2) { ["id"]=> string(2) "14" ["value"]=> string(2) "33" }

I have tried doing a GET request just to see if I can reach out to an external site here:

//tempandhumidity4
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>


const char *SERVER_WIFI_SSID = "nameofssid";
const char *SERVER_WIFI_PASS = "password";

void setup()
{
  Serial.begin(115200); 
  Serial.println("");
  Serial.println("Connecting to WiFi ");
  WiFi.begin(SERVER_WIFI_SSID,SERVER_WIFI_PASS);
  while(WiFi.status() != WL_CONNECTED){
     delay(500);
     Serial.print(".");
   }

   Serial.println("Connected");
}
void loop(){
HTTPClient http;
http.begin("http://www.nfl.com");
int httpCode = http.GET();
Serial.println(httpCode);
if(httpCode == HTTP_CODE_OK)
{
   Serial.print("HTTP response code ");
   Serial.println(httpCode);
   String response = http.getString();
   Serial.println(response);
}
else
{
   Serial.println("Error in HTTP request");
}

http.end();
} 

This returns the following:

Connecting to WiFi 
.........Connected
400
Error in HTTP request
400

Any ideas or suggestions would be much appreciated? The issues seems to be when I tether my ESP8266 to my phone and then try to perform the request I get a "-1" return which is connection refused. I have tried this sveral times and can't figure it out. Could it be the phone is not allowing me to send this manually instead of using a browser? Any ideas?


Solution

  • Sometimes posting your question helps you solve it. There were two issues:

    1. My router kept giving me a different IP on the computer I was using as a webserver. Probablly after each time I reset it.
    2. What was I thinking!! The post.php is using GET. Here is the new code:
    //post.php
    include 'connect.php';
    var_dump($_POST);
    $id = $_POST['id'];
    $value = $_POST['value'];
    $id = mysql_real_escape_string($id);
    $value = mysql_real_escape_string($value);
    
    mysql_query(
        "INSERT INTO iot (value, id) VALUES ('$value', '$id');"
    )
    

    I am now able to post. I am getting one error which I will look for else where:

    Warning: Unexpected character in input: '' (ASCII=18) state=0 in C:\xampp\htdocs\iot\post.php on line 5