Go Down

Topic: Arduino do not send data to mysql database (Read 1 time) previous topic - next topic

moonie_elsa

Hello. I have trouble in sending the data to database. The microcontroller board that I used is ESPduino. Firstly, I have successfully connecting the Arduino with Wifi. It should be POST 'URL' to the location of  insert_mysql.php on my web-host. I use POST method to send the data. Did I missed any codes that I should write?


Code: [Select]
//This library allows you to communicate with SPI devices, with the Arduino as the master device.
#include <SPI.h>
#include <ESP8266WiFi.h>

// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "USEGUEST";  // wireless network name
char password[] = ""; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;

// EDIT: 'Server' address to match your domain
char server[] = "10.16.222.85"; // This could also be 192.168.1.18/~me if you are running a server on your computer on a local network.

// This is the data that will be passed into your POST and matches your mysql column
// Global declarations
float sensorTemp = 0.0; // For storing raw data from LM-35
float Temperature = 0.0; // For storing the converted temperature
int analogInputPin = A0; // Data out of LM-35 is connected in Arduino's analog Pin A0
float celcius;//Here will place your reading
 void postData();

void setup() {
  Serial.begin(115200);
 
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, password);
    // Wait 10 seconds for connection
    delay(10000);
  }

 // You're connected now, so print out the status
//printWifiStatus();
}

void loop() {
   sensorTemp = analogRead(analogInputPin); // Reads sensor data from pin A0
  //Temperature = sensorTemp * 0.48875855; //(+Vcc * 1000 / 1023) / 10
  // Line 18-21 prints a formatted output

  float mv = ( sensorTemp/1024.0)*5000;             // Converting equation for voltage to Celsius
  float celsius = mv/10;
  Serial.print("Temperature = ");
  Serial.print(celsius);
  Serial.print(char(176)); //Unicode character degree sign (U+00B0) is 176 in Decimal
  Serial.print("C\n");
  delay(5000); // Temperature is printed in the Serial Monitor every five seconds
    postData();

}

void connectWifi() {
  // Attempt to connect to wifi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, password);
    // Wait 10 seconds for connection
    delay(10000);
  }
}

void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

// This method makes a HTTP connection to the server and POSTs data
void postData() {
  // Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
  // (yourarduinodata) and package them into the String yourdata which is what will be
  // sent in your POST request
  //yourdata = yourdatacolumn + yourarduinodata;

  // If there's a successful connection, send the HTTP POST request
  if (client.connect(server, 8080)) {
    Serial.println("connected");

    // EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
    client.println("POST /temp/insert_mysql.php HTTP/1.1");

    client.println("Host: 10.16.222.85"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("User-Agent: Arduino/1.0");
    // EDIT: 'Host' to match your domain
    //client.print("GET /temp/insert_mysql.php?"); // This
    // client.print("GET /temp/masuk.php?"); // This
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    //client.print("Content-Length: ");
    //client.println(celcius.length());
    client.println();
    client.println(celcius);
   /* client.print("celcius="); // This
    client.print(celcius); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server*/
  }
  else {
    // If you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting.");
    client.stop();
  }
}

PaulS

Code: [Select]
float celcius;//Here will place your reading
But, that is NOT where you put anything.

Code: [Select]
  float celsius = mv/10;
That is where you put your value.

Code: [Select]
    //client.print("Content-Length: ");
    //client.println(celcius.length());

The content length header is mandatory. You can't just comment out code you don't understand.

A float variable does NOT have a length() method.

Why is the temperature so secret that you need to POST it? Making a GET request is far simpler.

josephmars

Hello. I have trouble in sending the data to database. The microcontroller board that I used is ESPduino. Firstly, I have successfully connecting the Arduino with Wifi. It should be POST 'URL' to the location of  insert_mysql.php on my web-host. I use POST method to send the data gmail sign up. Did I missed any codes that I should write?


Code: [Select]
//This library allows you to communicate with SPI devices, with the Arduino as the master device.
#include <SPI.h>
#include <ESP8266WiFi.h>

// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "USEGUEST";  // wireless network name
char password[] = ""; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;

// EDIT: 'Server' address to match your domain
char server[] = "10.16.222.85"; // This could also be 192.168.1.18/~me if you are running a server on your computer on a local network.

// This is the data that will be passed into your POST and matches your mysql column
// Global declarations
float sensorTemp = 0.0; // For storing raw data from LM-35
float Temperature = 0.0; // For storing the converted temperature
int analogInputPin = A0; // Data out of LM-35 is connected in Arduino's analog Pin A0
float celcius;//Here will place your reading
 void postData();

void setup() {
  Serial.begin(115200);
 
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, password);
    // Wait 10 seconds for connection
    delay(10000);
  }

 // You're connected now, so print out the status
//printWifiStatus();
}

void loop() {
   sensorTemp = analogRead(analogInputPin); // Reads sensor data from pin A0
  //Temperature = sensorTemp * 0.48875855; //(+Vcc * 1000 / 1023) / 10
  // Line 18-21 prints a formatted output

  float mv = ( sensorTemp/1024.0)*5000;             // Converting equation for voltage to Celsius
  float celsius = mv/10;
  Serial.print("Temperature = ");
  Serial.print(celsius);
  Serial.print(char(176)); //Unicode character degree sign (U+00B0) is 176 in Decimal
  Serial.print("C\n");
  delay(5000); // Temperature is printed in the Serial Monitor every five seconds
    postData();

}

void connectWifi() {
  // Attempt to connect to wifi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, password);
    // Wait 10 seconds for connection
    delay(10000);
  }
}

void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

// This method makes a HTTP connection to the server and POSTs data
void postData() {
  // Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
  // (yourarduinodata) and package them into the String yourdata which is what will be
  // sent in your POST request
  //yourdata = yourdatacolumn + yourarduinodata;

  // If there's a successful connection, send the HTTP POST request
  if (client.connect(server, 8080)) {
    Serial.println("connected");

    // EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
    client.println("POST /temp/insert_mysql.php HTTP/1.1");

    client.println("Host: 10.16.222.85"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("User-Agent: Arduino/1.0");
    // EDIT: 'Host' to match your domain
    //client.print("GET /temp/insert_mysql.php?"); // This
    // client.print("GET /temp/masuk.php?"); // This
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    //client.print("Content-Length: ");
    //client.println(celcius.length());
    client.println();
    client.println(celcius);
   /* client.print("celcius="); // This
    client.print(celcius); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server*/
  }
  else {
    // If you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting.");
    client.stop();
  }
}

The content length header is mandatory and a float variable does NOT have a length() method.

Go Up