1

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 has not seemed to do much. I also tried adjusting the link to which the extractData.php file exists as I suspected that was incorrect. 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 recieving 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? Thank you!

This is my arduino code:

#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:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "plantSensorReading";

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

// Check if JSON data is properly decoded
if ($data === null) {
    echo "Error: Invalid JSON data";
    exit;
}

if (!isset($data['sensor1Val'])) {
    echo "Error: 'sensor1Val' field is missing in the JSON data";
    exit;
}

$sensor1Val = $data['sensor1Val'];

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed" . $conn->connect_error);
}

$sql = "INSERT INTO sensordatatable (sensor1Val) VALUES ('$sensor1Val')";

if ($conn->query($sql) === TRUE) {
    echo "New record successfully created";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
3
  • Can you show some logs that you end with? Commented Feb 12, 2024 at 19:46
  • Serial output through one loop looks like this: Connecting to Wifi . WIFI CONNECTED {"sensor1Val":446} completed Commented Feb 12, 2024 at 21:12
  • So what did file_get_contents('php://input') actually return? Commented Feb 13, 2024 at 7:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.