Hello, i am working on a project where we created a bin with a Flexi force weight sensor in it and are trying to get the readings from the sensor and send the data to a mysql database using an Arduino uno board and Wifly Shield . But i cant seem to get the wifly shield to connect to the wireless network to even get started.
Find below my Arduino code:
#ifndef __CREDENTIALS_H__
#define __CREDENTIALS_H__
char passphrase[] = "vjz7hmh****"; //password Wi-Fi
char ssid[] = "Ziggo855C2A5"; //name Wi-Fi
#endif
#include <SPI.h>
#include "WiFly.h"
#include <Wire.h>
WiFlyClient client("localhost", 80);
int looped = 1;
float var1 = 19.5; // caliberation factor
int var2 = A0; // FlexiForce sensor is connected analog pin A0 of arduino
int var3 = 0;
float vout;
void setup()
{
Serial.begin(9600);
WiFly.begin();
if( !WiFly.join( ssid, passphrase ) )
{
Serial.println( "Error Connecting to Wireless" );
while( 1 )
{
// Hang on failure.
}
}
else
{
Serial.println("Success");
}
pinMode(var2, INPUT);
}
void loop()
{
if(client.connect())
{
var3 = analogRead(var2);
vout = (var3 * 5.0) / 1023.0;
vout = vout * var1;
Serial.print("Flexi Force sensor: ");
Serial.print(vout,3);
Serial.println("");
client.print("GET/send_data.php?");
client.print("weight=");
client.print(vout);
client.println( " HTTP/1.1");
client.println( "Host: localhost" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
delay(10000);
}
}
Find below my PHP code:
<?php
// Prepare variables for database connection
$dbusername = "root"; // enter database username, I used "arduino" in step 2.2
$dbpassword = ""; // 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"
$dbname = "trashbot";
// Connect to your database
$conn = new mysqli($server, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare the SQL statement
$sql = "INSERT INTO robot (Weight) VALUES ('".$_GET["weight"]."')";
// Execute SQL statement
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>