Arduino->PHP->SQL

Hi all,

Im trying to send temperature data to my sql server with no luck. For some reason the GET command does not seem to be passed from the Arduino to my PhP code. Can someone have a look:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 192, 168, 42, 58 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
//byte gateway[] = { 192, 168, 42, 5 };                   // internet access via router
byte gw[] = {192,168,42,5};
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
byte server[] = { 192, 168, 42, 21  }; // Server IP

EthernetClient client;

#define DHTPIN 5 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL

int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
String strT;
String strH;

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

 dht.begin(); 
 delay(10000); // GIVE THE SENSOR SOME TIME TO START

 h = (int) dht.readHumidity(); 
 t = (int) dht.readTemperature(); 

 data = "";
}

void loop(){

 currentMillis = millis();
 if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
 previousMillis = currentMillis;
 h = (int) dht.readHumidity();
 t = (int) dht.readTemperature();
 }
  strT = String(t);
  strH = String(h);
 data = "temp1=" + strT + "&hum1=" + strH;

 if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("Connected");
 client.println("GET /writeSql.php"); 
    Serial.println("GET /writeSql.php");
    client.print("temp1=");
    Serial.print("temp1=");
    client.print(t);
    Serial.print(t);
    client.print("&&hum1=");
    Serial.print("&&hum1=");
    client.println(h);
    Serial.println(h);
   
    client.println("HTTP/1.1");
    Serial.println("HTTP/1.1");
    client.println("Host: 192.168.42.21" );
    Serial.println("Host: 192.168.42.21" );
    client.println("Content-Type: application/x-www-form-urlencoded" );
    Serial.println("Content-Type: application/x-www-form-urlencoded" );
    client.println( "Connection: close" );
    Serial.println( "Connection: close" );
    client.println();
    Serial.println();
    client.println();
    Serial.println();
 } 

 if (client.connected()) { 
    client.stop(); // DISCONNECT FROM THE SERVER
  Serial.println("Client Stop");
 }

 delay(300000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
<?php

$val1=$_GET["temp1"];
$val2=$_GET["hum1"];

print "times einai " . $val1;
echo $val2;

echo $val1;

$con = mysqli_connect("localhost","root","41124112","Spiti");

if (!$con) {
        die('Could not Connect'. mysqli_error());
        }

mysqli_query($con,"INSERT INTO tempLog (temperature, humidity) VALUES ($val1,$val2)");

mysqli_close($con);
?>

[/php]

I use the print and the echo command in php to test if GET values reach the php code, but they don't.
However i see the GET values and the print if i use the: Serial.write(client.read()) command on the Arduino.

Can someone help?

P.S

This is the Serial output:

Connected
GET /writeSql.php
temp1=18&&hum1=79
HTTP/1.1
Host: 192.168.42.21
Content-Type: application/x-www-form-urlencoded
Connection: close

Client Stop

Your HTTP request looks like

GET /writeSql.php
temp1=1&&hum1=1
HTTP/1.1
Host: ...

but it should be one line only like this:

GET /writeSql.php?temp1=1&hum1=1 HTTP/1.1
Host: ...

replace the println's with print to send it as one line

I changed it to this output:

Connected
GET /writeSql.php?temp1=18&hum1=79 HTTP/1.1
Host: 192.168.42.21
Content-Type: application/x-www-form-urlencoded
Connection: close

Client Stop

Unfortunately it still failes to print any value on my php!!

If i add this on my Arduino:

while(client.connected()) {
while(client.available()) {
Serial.write(client.read());
}
}

I get :

HTTP/1.1 200 OK
Date: Wed, 22 Mar 2017 10:02:11 GMT
Server: Apache/2.4.10 (Raspbian)
Content-Length: 19
Connection: close
Content-Type: text/html; charset=UTF-8

times einai 187918

which means that the values are passed. Why are they not printed with my print or echo line on my php?

They are printed, but the output goes to your arduino not to your browser. If you want to see the values in the browser you need another php script which retreives + prints the data from the database.

Yup !! it works!!!!! It now stores the values to the database!!

Many thanks SUI!!

How about SQL->PHP->Arduino, what is the best way to make Arduino get data from SQL? UDP packages from Server to Arduino or maybe parsing the Serial.write(client.read()); command? Are there any available projects for me to check that write and read from an SQL server at the same time;

Ty!!

or maybe parsing the Serial.write(client.read()); command?

You're going to have a hard time parsing the output of Serial.write(), and getting anything useful from it.

Storing the data that client.read() returns, instead of just printing it, WOULD give you something to parse.

I completed my code..with the parse:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 192, 168, 42, 58 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
//byte gateway[] = { 192, 168, 42, 5 };                   // internet access via router
byte gw[] = {192,168,42,5};
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
byte server[] = { 192, 168, 42, 21  }; // Server IP
char thaum='!'; //Tha xrisimopoihthei apo ton parser
char miden='0'; //To fws einai kleisto sti basi
char ena='1'; //To fws einai anoixto sti basi
boolean pars = false; //Otan ginei true tote arxizei na katagrafei tous xaraktires sto readString
boolean enableAlarm = false;

#define RELAY1 2 //Fwta
#define RELAY2 3 //Seirina

int valKin = 0;
int aisthKinPin = 4;

EthernetClient client;

#define DHTPIN 5 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 14400000; // READING INTERVAL

long previousMillis2 = 0;
unsigned long currentMillis2 = 0;
long interval2 = 10000; // READING INTERVAL

float t = 0;	// TEMPERATURE VAR
float h = 0;	// HUMIDITY VAR
int alarmBol=0;

void setup() { 



  pinMode (aisthKinPin, INPUT);
  pinMode (RELAY1, OUTPUT);
	pinMode (RELAY2, OUTPUT);

  digitalWrite (RELAY1, LOW);
  digitalWrite (RELAY2, LOW);
  
	Serial.begin(9600);
  Ethernet.begin(mac, ip, gw, gw, subnet);

	dht.begin(); 
	delay(10000); // GIVE THE SENSOR SOME TIME TO START
  
	h = dht.readHumidity(); 
	t = dht.readTemperature(); 
}

void loop(){

  String readString;
  char charBuf[100];

//  memset(readString, 0, sizeof readString);
  //digitalWrite (RELAY1, LOW);
  //digitalWrite (RELAY2, LOW);
  currentMillis = millis();
  currentMillis2 = millis();

  valKin = digitalRead(aisthKinPin);

	if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
		previousMillis = currentMillis;
   //Serial.println(previousMillis);
		h = dht.readHumidity();
		t = dht.readTemperature();
    Serial.println("Arxizei to connect:");
	if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("Connected");
		client.print("GET /writeSql.php?"); 
    Serial.print("GET /writeSql.php?");
    client.print("temp1=");
    Serial.print("temp1=");
    client.print(t);
    Serial.print(t);
    client.print("&hum1=");
    Serial.print("&hum1=");
    client.print(h);
    Serial.print(h);
   
    client.println(" HTTP/1.1");
    Serial.println(" HTTP/1.1");
    client.println("Host: 192.168.42.21" );
    Serial.println("Host: 192.168.42.21" );
    client.println("Content-Type: application/x-www-form-urlencoded" );
    Serial.println("Content-Type: application/x-www-form-urlencoded" );
    client.println( "Connection: close" );
    Serial.println( "Connection: close" );
    client.println();
    Serial.println();
    client.println();
    Serial.println();
	} 
  client.stop();  // DISCONNECT FROM THE SERVER
  Serial.println("Client Stop");

}

if(valKin==1) {
 Serial.println("KINISI");
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
    alarmBol=1;
    Serial.println("Connected");
    client.print("GET /alarmSql.php?"); 
    Serial.print("GET /alarmSql.php?");
    client.print("alarm1=");
    Serial.print("alarm1=");
    client.print(alarmBol);
    Serial.print(alarmBol);
   
    client.println(" HTTP/1.1");
    Serial.println(" HTTP/1.1");
    client.println("Host: 192.168.42.21" );
    Serial.println("Host: 192.168.42.21" );
    client.println("Content-Type: application/x-www-form-urlencoded" );
    Serial.println("Content-Type: application/x-www-form-urlencoded" );
    client.println( "Connection: close" );
    Serial.println( "Connection: close" );
    client.println();
    Serial.println();
    client.println();
    Serial.println();
 // alarmBol=0;
    }


 Serial.println(readString);
//Serial.println(c);
}

if(currentMillis2 - previousMillis2 > interval2) { // READ ONLY ONCE PER INTERVAL
    previousMillis2 = currentMillis2;
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
    //alarmBol=1;
    Serial.println("Connected");
    client.print("GET /alarmSql.php?"); 
    Serial.print("GET /alarmSql.php?");
    client.print("alarm1=");
    Serial.print("alarm1=");
    client.print(alarmBol);
    Serial.print(alarmBol);
   
    client.println(" HTTP/1.1");
    Serial.println(" HTTP/1.1");
    client.println("Host: 192.168.42.21" );
    Serial.println("Host: 192.168.42.21" );
    client.println("Content-Type: application/x-www-form-urlencoded" );
    Serial.println("Content-Type: application/x-www-form-urlencoded" );
    client.println( "Connection: close" );
    Serial.println( "Connection: close" );
    client.println();
    Serial.println();
    client.println();
    Serial.println();
    }

while(client.connected()) {
  if (client.available()) {
    char c = client.read();
         if (c==thaum) {
          pars=true;
         // Serial.println(pars);
               }
       if(pars) {

          readString += c;
            }
       }
    }
    pars=false;
    client.stop();  // DISCONNECT FROM THE SERVER
    Serial.println("Client Stop");
   
readString.toCharArray(charBuf,100);
for(int i=0; i<100; i++) {
//  Serial.println(charBuf[i]);
  
 //Serial.print(charBuf[i]);

   if(charBuf[i]==thaum) {
     if(charBuf[i+3]==ena) {
      enableAlarm=true;
     }
     if(charBuf[i+3]==0) {
      enableAlarm=false;
     }
     if(charBuf[i+1]==miden){
     
      Serial.print("Sbisto");  
        digitalWrite(RELAY1, LOW);
        delay(3000);
       // charBuf[i]=0;
        //digitalWrite(RELAY2, LOW);
                }
   if(charBuf[i+1]==ena){
      Serial.print("Anoixto Fws");
        digitalWrite(RELAY1, HIGH);
        delay(30000);
        //  charBuf[i]=0;
        //digitalWrite(RELAY2, HIGH);
                }
   if((charBuf[i+2]==ena)&&(enableAlarm)){
      Serial.print("Energos Sunagermos");
        digitalWrite(RELAY1, HIGH);
        delay(30000);
        digitalWrite(RELAY1,LOW);
        //  charBuf[i]=0;
        //digitalWrite(RELAY2, HIGH);
                  }
             }
          }
      }  
alarmBol=0;
}

//	delay(300000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN

My arduino uploads humidity and temperature data to my sqlserver(using writesql.php) and also writes to my sqlserver the value one when the PIR sensor catches any movement. At the same time every 10000milliseconds it parses the results of a database query generated by a php page. After analyzing those results the arduino will know if the user has turned on the lights through the html page created (that simply writes data to the sqlserver), or has enabled the alarm && the PIR sensor "saw" movement.

Notes:

  • The parser looks at what the php code returns and searches for char "!" which means that sql querry results will follow (using a simple echo command)
  • An html page allows user to send data to the database in the form of "0" for lights off and "1"for lights on. Those data are stored on charBuf[i+1].
  • On the html page the user can enable or disable the alarm. The results will be stored on the database and on charBuf[i+3].
    -If movement has been detected the value (1 or 0) will be stored on charBuf[i+2].