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