Arduino YUN IOT Analog input json data insert into mySQL database using PHP REST

Hello Everyone:

A Video on Utube shows how to input json data to mySQL database

But there is a problem, the author left some contents unrevealed.
I have followed the steps to make arduino code, but it didn’t work.

The code stopped at if (client.connect(server, 80)), and it didn’t print anything.
Could someone help me?
Many thanks.

/* Arduino Yun sketch write sensor data to mysql*/

//Include Arduino Yun Lib
#include <Bridge.h>
#include <BridgeClient.h>
#include <SPI.h>

//Wamp server IP adress
const char *server = "192.168.2.108";

//MYSQL table name
const char *table_name = "yundata";

BridgeClient client;
char buffer[64];

/*Send HTTP POST request to the REST PHP/Mysql data API */
void send_request(){
  Serial.println("\n connecting");
  if (client.connect(server, 80)) {
    Serial.println("sending ");
    Serial.println("344");

    //POST URI

    sprintf(buffer, "POST /yundataphp/InsertIotJsonRESTdata.php HTTP/1.1");

    client.println(buffer);
    Serial.println(buffer);   //Debug

    //Host header
    sprintf(buffer, "Host: %s", server);
    client.println(buffer);
    Serial.println(buffer);   //Debug

    //JSON content type
    client.println("Content-Type: application/json");
    Serial.println("Content-Type: application/json");    //Debug

    //POST body
    sprintf(buffer, "{ \"T1\" : \"26\" , \"M1\" : \"57\" , \"date\" : \"2016-02-08 00:25:48\" }", "344");

    //Content Length
    client.println("content-Length: ");
    Serial.println("content-Length: ");    //Debug

    client.println(strlen(buffer));
    Serial.println(strlen(buffer));    //Debug

    //End of Headers
    client.println();
    Serial.println();    //Debug

    //Request body
    client.println(buffer);
    Serial.println(buffer);    //Debug

  } else {
    Serial.println("connection failed");
  }
}
  
void setup() {
  // put your setup code here, to run once:
  
 }

void loop() {
  // put your main code here, to run repeatedly:
send_request();
  }

Any suggestion?

I'm reviewing the video right now.

Also, when leaving comments about the code. It is good to give a line number and not just the code snippet.

Jesse

@tim9510019,

In review of the code, there are several assumptions the author has made and not mentioned them.

1) both the Arduino Yun and the Server are on the same network. Perhaps he mentions that in another video.

2) he assumes that the connection will be successful. As you will notice, he only glazes over the error if the connection fails.

The first thing to do is make sure you are on the same network, then things should work.

Jesse

Hi,

You could try adding the code to wait for the response before sending the next request to the server in a loop.

IE:

/* Wait for a response */
void wait_response()
{
while (!client.available()) {
if (!client.connected()) {
return;
}
}
}

/* Read the response and output to the serial monitor */
void read_response()
{
bool print = true;

Serial.println(“Response”); //debug
while (client.available()) {
char c = client.read();
// Print only until the first carriage return
if (c == ‘\n’)
print = false;
if (print)
Serial.print(c);
}
}

/* Terminate the connection*/
void end_request()
{
client.stop();
}

/* Arduino Yun Setup */
void setup()
{
Serial.begin(9600);
Serial.println(“Starting Bridge”);
Bridge.begin();
}

/* Arduino Yun Loop */
void loop()
{
int val = analogRead(A0);
send_request(val); // sends ADC data to Mobile services
wait_response(); // wait for server response
read_response(); // read server response
end_request(); // free up client resources
delay(10000); // delay for data logging every 10 seconds
}

Also try YunClient, it seems to work

#include <Bridge.h>
#include <YunClient.h>
#include <SPI.h>

const char *server = “192.168.217.3”;

const char *table_name = “YunData”;

YunClient client;

Another issue if using WAMP is to setup WAMP in Apache.conf to accept external connections

All the best Steve