Cannot deserialize JSON object via PHP script on AT commands from Arduino Uno/ESP8266 to connect to mySQL

I've recently started working on a project trying to upload sensor data from my Arduino Uno serially connected to an ESP8266, to a mySQL server. I can't seem to figure out what's incorrect about my post request through AT Commands. Getting the sensor data works great. Also, my php script is probably very buggy as of now, but I am just trying to ensure the $data variable is correctly being assigned as of now (first time working with php and AT commands). Thanks for any help!

I've mainly tried debugging by adjusting the httpRequest string, which I haven't had much luck with. The sensor data is correctly created into a JSON object as I've seen by serially printing the string representation. I'm assuming the error lies in either the AT commands or creating of the http request. $data in the php script receiving the JSON object would be considered a win. Another potential issue is the baud rate, I understand ESP8266 should run at 9600 when serially connected. However, it doesn't seem to connect to the wifi when that is the case. Should I change both baud rates to 9600 (if so how do I still connect to wifi)? Thank you!

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>

SoftwareSerial ESP8266(2, 3); // RX, TX
const int delayTime = 2000;

void setUpESP();
void connect();
String createJSON();
void postRequest();
int sensor1Test();

const int sensor1 = A0;

void setup() {
    Serial.begin(115200);
    ESP8266.begin(115200);

    setUpESP();
    connect();

    pinMode(sensor1, INPUT);
}

void loop() {
    postRequest();
    delay(10000);
}

String createJSON() {
    StaticJsonDocument<256> doc;
    doc["sensor1Val"] = sensor1Test();
    String jsonPayload;
    serializeJson(doc, jsonPayload);
    return jsonPayload;
}

void postRequest() {
    ESP8266.println("AT+CIPSTART=\"TCP\",\"192.168.4.33\",80");
    delay(delayTime);

    String jsonPayload = createJSON();

    ESP8266.print("AT+CIPSEND=");
    ESP8266.println(jsonPayload.length() + 150);
    delay(delayTime);


    String httpRequest = "POST /sensordata/extractData.php HTTP/1.1\r\n";
    // ** = local ipaddress
    httpRequest += "Host: ******\r\n";
    httpRequest += "Connection: keep-alive\r\n";
    httpRequest += "Content-Type: application/json\r\n";
    httpRequest += "Content-Length: ";
    httpRequest += jsonPayload.length();
    httpRequest += "\r\n\r\n";
    httpRequest += jsonPayload;

    Serial.println(jsonPayload);
    ESP8266.println(httpRequest);
    
    delay(delayTime);

    ESP8266.println("AT+CIPCLOSE");
}

int sensor1Test() {
    int sensor1Val = analogRead(sensor1);
    delay(delayTime);
    return sensor1Val;
}



void setUpESP() {
    delay(delayTime);
    ESP8266.println("AT+RST");
    delay(delayTime);

    ESP8266.println("AT+CWMODE=1");
    delay(delayTime);
}

void connect() {
    Serial.println("Connecting to Wifi");
    unsigned char timer = 0;
    unsigned char connection = 0;

    while (connection == 0) {
        Serial.print(". ");
        // ** = ssid and password       
        ESP8266.println("AT+CWJAP=\"**\",\"**\"\r\n");

        ESP8266.setTimeout(5000);

        char expectedResponse[] = "WIFI CONNECTED\r\n";
        if (ESP8266.find(expectedResponse) == 1) {
            Serial.println("WIFI CONNECTED");
            break;
        }

        timer++;
        if (timer > 3) {
            timer = 0;
            Serial.println("Still attempting to connect..");
        }
    }
}

PHP Script currently just tries to get the data. I just want it to output anything on that echo statement, which it currently is not doing.

<?php
$data = json_decode(file_get_contents('php://input'), true);
echo "Received JSON Data: " . $data . "<br>";
?>

one question : why do you need the UNO for ? the ESP8266 could probably read the sensors and handle the http request...

Then, first thing first, have you checked that your set up is working fine and that your Arduino code can actually speak to the ESP8266 using AT commands ? (voltage adapter between UNO Tx to ESP Rx?, GND connected?)

for example which GPIOs are you really using and why ESP8266? sometimes using ESP8266(2, 3); won't do what you expect, for example on the ESP8266 12-E NodeMCU, GPIO2 is labeled D4 on the board.

➜ a simple code where you type commands in the serial monitor and relay them to your ESP module and print the result would go a long way to check things out. Entering AT for example should get you an OK answer. if that does not work, then no need to go further.


side note:

Not 100% sure but you are trying to concatenate an array ($data ) with a string ("Received JSON Data: " ), which won't work.

Try to separate both

<?php
$data = json_decode(file_get_contents('php://input'), true);
echo "Received JSON Data:\n";
print_r($data);
?>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.