Retrieving MySQL database information through PHP script to Arduino device

I need help getting data from my database to my Arduino board using a cellular module

I have an Arduino MEGA with a SIM7600CE module and I’m trying to pull information from my MySQL database that’s hosted online. The data should come through a PHP script but my Arduino isn’t getting any response back from the server.

My PHP script grabs the latest humidity reading and converts it to JSON format. When that didn’t work I also tried making a simple JSON test file.

PHP Database Script:

<?php
// Database connection
$connection = new PDO('sqlite:sensorData.sqlite');

if (!$connection) {
    die("Database connection error");
}

// Get latest temperature reading
$query = "SELECT temperature FROM sensors ORDER BY id DESC LIMIT 1";
$results = $connection->query($query);

if ($results) {
    $readings = array();
    
    while($record = $results->fetch(PDO::FETCH_ASSOC)) {
        $readings[] = $record;
    }
    
    $jsonOutput = json_encode($readings);
    echo $jsonOutput;
} else {
    echo "No data found";
}
?>

Test JSON File:

{
  "device": "temperature",
  "timestamp": 1634567890,
  "values": [25.4, 68.7]
}

Arduino Code (shortened version):

#define TINY_GSM_MODEM_SIM7600
#include <TinyGsmClient.h>
#include <ArduinoJson.h>

TinyGsm modem(Serial2);
TinyGsmClient httpClient(modem);

const char* networkAPN = "internet";
const char* serverHost = "mysite.freehosting.com";
const char* dataPath = "/getData.php";

void fetchServerData() {
    httpClient.print("GET ");
    httpClient.print(dataPath);
    httpClient.println(" HTTP/1.0");
    httpClient.print("Host: ");
    httpClient.println(serverHost);
    httpClient.println("Connection: close");
    httpClient.println();
    
    // Parse JSON response
    JsonDocument jsonDoc;
    DeserializationError parseError = deserializeJson(jsonDoc, httpClient);
    
    if (parseError) {
        Serial.println("JSON parsing failed");
        return;
    }
    
    Serial.println(jsonDoc["device"].as<String>());
    httpClient.stop();
}

The cellular connection works fine for sending SMS and uploading data to the server, but I can’t figure out why the data retrieval isn’t working. Has anyone dealt with this kind of issue before?

I’ve hit the same timeout issues with SIM7600 modules. Your main problem is trying to deserialize JSON straight from the HTTP client stream without reading the full response first. TinyGsmClient doesn’t handle HTTP headers automatically, so you’re basically trying to parse headers as JSON.

Read the complete HTTP response first, skip the headers, then grab just the JSON payload. Use httpClient.readString() or read line by line until you hit the empty line between headers and body. Also double-check your connection - make sure httpClient.connect(serverHost, 80) actually connects before sending the GET request. Without that, you’re just firing requests into nothing.

Just because SMS works doesn’t mean HTTP will - they use completely different protocols and network paths.

check ur php error logs first - that sqlite connection might be failing silently. ur arduino code’s missing the actual http connection part b4 sending the GET request. u need httpClient.connect(serverHost, 80) b4 makin the request, otherwise ur just sending data into the void.

Been there with similar setups. You’re mixing direct HTTP requests with JSON parsing, which creates timing issues.

Skip wrestling with Arduino HTTP parsing and cellular problems. Set up a webhook that polls your MySQL database every few minutes and pushes data directly to your Arduino through something more reliable.

I do this for multiple IoT deployments using automation as a buffer layer. It polls the database, formats everything properly, and delivers through whatever protocol works best for your hardware. No more parsing HTTP headers or dealing with cellular timeouts.

This way you can add data transformation, error handling, and retry logic without cramming everything into Arduino code. Plus it’s easy to switch data sources or add multiple endpoints later.

Latenode makes this database-to-device automation really straightforward. You can set up polling, data formatting, and delivery in one workflow that runs reliably in the background.

Had the exact same issue with my SIM7600A a few months ago. It’s probably your HTTP response buffer handling. These modules have tiny receive buffers - if you don’t read fast enough, the data gets chopped up or corrupted.

Add delays between your HTTP request and JSON parsing. Read all the HTTP headers first before you try deserializing anything. Also check if your host blocks cellular IP ranges - some free ones do this to stop spam.

I ditched TinyGsmClient and went straight to AT commands for HTTP requests. Way better control over responses. AT+HTTPREAD on the SIM7600 is way more reliable than the client wrapper for database stuff.