I mistakenly posted this on stack overflow first. The community was kind enough to point me to this forum. I apologize for the double-post.
I've been working on an Arduino Uno r3 + WiFi Shield project for a few months now building up the complexity as I go. I've hit a wall. I started this project with exactly zero experience writing code of any kind and exactly zero experience working with boards. If you have any doubts as to whether or not I'll understand your response, please dumb it down!
I've made a simple arduino + wifi shield setup that can report a button push to a MySql database. All of the .php stuff on the backend works and the database is managed with phpmyadmin. I'm trying to add a second button but can't seem to make it work and need some help.
I have settled on a four-field database table as the output:
- 1st column: the key. A sequential, unique identifier for each entry
- 2nd column: the pin number that the sensor is connected to
- 3rd column: the sensor value
- 4th column: the date and time
Notice that I'm not using a dedicated column for each sensor. By using the sensor pin input, I want to simply print the sensor pin number in a column. This (a) saves space in the table and (b) allows the Arduino to use as many sensors as it has pins.
The idea is that each button press (even if two are pressed simultaneously) will get it's own line, sensor pin source, value, and timestamp. No values will be sent unless a button is pressed.
I haven't the foggiest idea of how to implement this! Help!
My question:
How can I ask the sketch to retrieve the pin from which a signal originates and write it to the db as a number in the second column? The 13th line of code is what will write the pin number (I think). The "senseval=" needs to be a variable that reports the source pin.
I don't know how to set this up and google has led to nothing but dead ends. I'd like this thread to answer the questions I'm sure a few more people have/will have!
My sketch is included below and the insert_php_doc below that. I'm using the Silinas/Benoit "Arduino /Post" example from github:
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "linksys";
int status = WL_IDLE_STATUS;
WiFiClient client;
IPAddress server(xxx,xxx,xxx,xxx);
int inPin_0 = A0; // choose the input pin (sensor #1)
int inPin_1 = A1; // choose the input pin (sensor #2) //ADDED FOR SECOND BUTTON//
int sensorSense_0 = 0; //variable
int sensorSense_1 = 0; //variable //ADDED FOR SECOND BUTTON//
String SensorVal = "senseval=";// "yourdata="
String senseval;//yourdata //MUST KEEP sensval for PHP
void setup() {
Serial.begin(9600);
pinMode(inPin_0,INPUT);
pinMode(inPin_1,INPUT); //ADDED FOR SECOND BUTTON//
connectWifi();
}
void loop() {
sensorSense_0=analogRead(inPin_0);
if (sensorSense_0 == LOW){
postData();
delay(5000);
}
sensorSense_1=analogRead(inPin_1); //ADDED FOR SECOND BUTTON//
if (sensorSense_1 == LOW){ //ADDED FOR SECOND BUTTON//
postData(); //ADDED FOR SECOND BUTTON//
delay(5000); //ADDED FOR SECOND BUTTON//
}
void connectWifi() {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid);
delay(7000);
}
}
}
void postData() {
senseval=SensorVal+String(inPin_0);
senseval=SensorVal+String(inPin_1); //ADDED FOR SECOND BUTTON//
if (client.connect(server, 80)) {
Serial.println("connecting...");
client.println("POST /insert_mysql_doc.php? HTTP/1.1");
client.println("Host: www.<domain>.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(senseval.length());//yourdata
client.println();
client.println(senseval);//yourdata
client.stop();
}
else {
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
connectWifi();
printWifiStatus();
}
}
The insert PHP from https://github.com/ericbenwa/POST-Arduino-Data-Wireless:
<?php
foreach ($_REQUEST as $key => $value)
{
if ($key == "senseval") {
$senseval = $value;
}
}
// EDIT: Your mysql database account information
$username = "test_user";
$password = "test_password";
$database = "test_db_name_here";
$tablename = "test_table_name_here";
$localhost = "localhost";
// Check Connection to Database
if (mysql_connect($localhost, $username, $password))
{
@mysql_select_db($database) or die ("Unable to select database");
// Next two lines will write into your table 'test_table_name_here' with 'yourdata' value from the arduino and will timestamp that data using 'now()'
$query = "INSERT INTO $tablename VALUES ($senseval,now())";
$result = mysql_query($query);
} else {
echo('Unable to connect to database.');
}
?>