Hello all.
I'm building a project where I want to send some sensor measurements to a database and present them in a site.
I'm using these steps.
I installed and configured XAMPP for the server and SQL database.
I then developed this short PHP code to add values to the DB
<?php
// Prepare variables for database connection
$dbusername = "vicky"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "test"; // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
// Connect to your database
$dbconnect = mysql_connect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);
// Prepare the SQL statement
$sql = "INSERT INTO test.sensor (value) VALUES ('".$_GET["value"]."')";
// Execute SQL statement
mysql_query($sql);
?>
When I paste "http://192.168.1.8:90/write_data.php?value=100" in my browser the DB gets a new value.
Then I edited my sketch to send a value to the DB.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <WiFi.h>
#include <Ethernet.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// LDR
const int analogInPin_LDR = A0; // Analog input - light
int sensor_Value_LDR = 0; // Light sensor value
int output_Value_LDR = 0; // Light output value
// Humidity sensor
const int analogInPin_Humidity = A1; // Analog input - humidity
int sensor_Value_Humidity = 0; // Humidity sensor value
int output_Value_Humidity = 0; // Humidity output value
// Tilt sensor
const int sensorPin_Tilt = 8; // the number of the sensor pin
int output_Value_Tilt = 0; // Tilt status value
const int ledPin = 13; // Led pin
// Wifi Connection
char ssid[] = "HOMENET"; // your network SSID (name)
char pass[] = "86vikydi200619x5"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
//WiFiServer server(80);
char server[] = "192.168.1.8";
//char server[] = "www.google.com";
// Initialize the Wifi server library
WiFiClient client;
//static WiFiClient client;
int hp1 = 1;
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(sensorPin_Tilt, INPUT);
// Wifi connection
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("Connected to network");
}
// print your WiFi shield's IP address:
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
printWifiStatus();
// Connect to the server (your computer or web page)
if (client.connect(server, 90)) {
Serial.println("--> connection ok\n");
client.print("GET /write_data.php?"); // This
client.print("value="); // This
client.print("100"); // 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.print( "Host: " );
client.println(server);
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
Serial.println("--> finished transmission\n");
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
}
void loop() {
// listen for incoming clients
// WiFiClient client = server.available();
//Temperature Sensor Code
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
}
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");
}
It seems that it connects to the server but the Database in SQL is not getting the new value.
From that I infer that the "GET" is not implemented.
Could anyone assist me please as I've looked it in any possible way I could?
In addition, how can I test what is written to the server?