Get text from database and display it in serial monitor

Hello there,
I'm trying to get a line of text from mysql database created in phpmyadmin and display it in serial monitor. It needs to get updated as the database updates and the latest one should be displayed. Below is the code for arduino as well as php code to get the text.

code for arduino:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 10, 101 };
byte gw[] = {192, 168, 10, 1 };
byte subnet[] = { 255, 255, 255, 0 };
IPAddress server(192, 168, 10, 100 );

EthernetClient client;

unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;

void setup() {
Ethernet.begin(mac, ip, gw, subnet); 

void loop() {
  Serial.print(" Connecting to server Database ");
  if (client.connect(server, 80)) {
    client.println("connection ok");
    Serial.println("connection ok");
    client.print("GET /msm.php HTTP/1.1");
    Serial.print("GET /msm.php HTTP/1.1");
    client.println("Host: localhost");
    Serial.println("Host: localhost");
    client.println("User-Agent: Arduino");
    Serial.println("User-Agent: Arduino");
    client.println("Accept: text/html");
    Serial.println("Accept: text/html");

    while (client.available())
    client.println("Connection: close");
    Serial.println("Connection: close");
  else {
    Serial.println("connection failed");

php code to get text, this code is returning a text eg: "this is final one" but its not getting upto serial monitor.

$conn = mysqli_connect("localhost","root","","testboard");
$result = mysqli_query($conn, "SELECT notice FROM class1 ORDER BY id DESC");
$row = mysqli_fetch_array($result, MYSQLI_NUM);
$request = $row[0];
return $request;

In serial monitor, its printing long combination of numbers instead of what i wanted to print. ie: "this is final one"

 Connecting to server Database connection ok
GET /msm.php HTTP/1.1Host: localhost
User-Agent: Arduino
Accept: text/html
72848480474946493252484832669710032821011131171011151161310689711610158327011410544324855327797121325048504932505158525358484932717784131083101114118101114583265112979910410147504652465255324087105110545241327911210111083837647494649464910732807280475546514650561310671111101161011101164576101110103116104583251505413106711111011010199116105111110583299108111115101131067111110116101110116458412111210158321161011201164710411610910859329910497114115101116611051151114556565357454913101310603368796784898069327284777632808566767367323445474773698470474768846832728477763250464847476978346210601041161091086260104101971006210601161051161081016252484832669710032821011131171011151166047116105116108101621060471041019710062609811110012162106010449626697100328210111311710111511660471044962106011262891111171143298114111119115101114321151011101163297321141011131171011151163211610497116321161041051153211510111411810111432991111171081003211011111632117110100101114115116971101004660981143247621060471126210601041146210609710010011410111511562651129799104101475046524652553240871051105452413279112101110838376474946494649107328072804755465146505632831011141181011143297116321081119997108104111115116328011111411632564860479710010011410111511562106047981111001216260471041161091086210Connection: close

try instead


read() returns an int not a char. So print prints the integer value of the char read.

1 Like

Hey what database are you using in this context

Thank you for the correction but I think I have other issues in my code as well. Those long digits were error messages printed in int form.
these were the errors

Connecting to server Database connection ok
GET /msm.php HTTP/1.1Host: localhost
User-Agent: Arduino
Accept: text/html
HTTP/1.1 400 Bad Request
Date: Sat, 08 May 2021 02:14:48 GMT
Server: Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/7.3.28
Content-Length: 326
Connection: close
Content-Type: text/html; charset=iso-8859-1

<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
<address>Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/7.3.28 Server at localhost Port 80</address>
Connection: close

I'm using MySQL database in phpMyAdmin

Ok... are you using the compatible version of mysql and php

yes, they are compatible. Can you point out any errors in my code?

I am looking into it, but although quite complex for me to figure out. Can you put the error message so that I can deal much easier

    client.print("GET /msm.php HTTP/1.1");
    Serial.print("GET /msm.php HTTP/1.1");

Should the above be .println(), not .print() ?

It's causing what should be 2 lines to be a single line in the in the request:

GET /msm.php HTTP/1.1Host: localhost
1 Like

Thank you for you help, I think I have got the solution, now the code is running as it should be.
changed the get request as:

 client.print("GET /msm.php HTTP/1.1");


client.println("GET /msm.php HTTP/1.0");

You got it. Thank you for replying.


Why did you change to 1.0?

You miss a new line here

1 Like

Reason for changing it. Is it the compatability related issue

1 Like

Viewed some examples on the web and tried it, surprisingly it worked in my case. HTTP client was not connecting at 1.1 returning the result as a bad request. It might be some compatibility issue as @vishkas mentioned.

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