我已经在Arduino Uno r3 + WiFi Shield项目上工作了几个月,现在我正在努力构建复杂性。我撞墙了。我开始这个项目的经验非常简单,编写任何类型的代码,并且完全没有使用板的经验。如果您对我是否理解您的回答有任何疑问,请将其愚蠢!
我做了一个简单的arduino + wifi屏蔽设置,可以报告按钮推送到MySql数据库。后端的所有.php内容都可以工作,数据库是用phpmyadmin管理的。我试图添加第二个按钮但似乎无法使其正常工作并需要一些帮助。
我已经确定了一个四字段数据库表作为输出:
第一栏:关键。每个条目的顺序唯一标识符
第2列:传感器连接的引脚号
第3列:传感器值
第4栏:日期和时间
请注意,我没有为每个传感器使用专用列。通过使用传感器引脚输入,我只想在列中打印传感器引脚编号。这样(a)节省了表格中的空间,并且(b)允许Arduino使用尽可能多的传感器和它的针脚。
这个想法是按下每个按钮(即使同时按下两个按钮)将获得它自己的线路,传感器引脚源,值和时间戳。除非按下按钮,否则不会发送任何值。
我对如何实现这个最无聊的想法!救命啊!
我的问题 :
如何让草图检索信号源自的引脚,并将其作为第二列中的数字写入数据库?第13行代码是写入引脚号(我认为)。 " senseval ="需要是报告源引脚的变量
我不知道如何设置它,谷歌只会导致死路一条。我希望这个帖子能够回答我确定会有更多人拥有/将要拥有的问题!
我的草图包含在下面,下面是insert_php_doc。我使用的是Silinas / Benoit" Arduino / Post"来自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)
int sensorSense_0 = 0; //variable
int sensorSense_1 = 0; //variable
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);
connectWifi();
}
void loop() {
sensorSense_0=analogRead(inPin_0);
if (sensorSense_0 == LOW){
postData();
delay(5000);
}
sensorSense_1=analogRead(inPin_1);
if (sensorSense_1 == LOW){
postData();
delay(5000);
}
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);
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();
}
}
从https://github.com/ericbenwa/POST-Arduino-Data-Wireless插入PHP:
<?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.');
}
?>
答案 0 :(得分:0)
使用define for pin,它们在运行期间不会更新
我已选择发送引脚0
或1
的ID,如果您需要引脚号,则只需将调用更改为postData
使用静态val存储先前的开关状态并检测按下开关。 使用de延迟100ms进行去抖动,如果需要可以更新。 选择做2个帖子来保持逻辑,但根据http post post的成本,一个帖子中的两个值似乎更好。
#include <SPI.h>
#include <WiFi.h>
#define PIN0 A0; // choose the input pin (sensor #0)
#define PIN1 A1; // choose the input pin (sensor #1)
char ssid[] = "linksys";
int status = WL_IDLE_STATUS;
WiFiClient client;
IPAddress server(xxx,xxx,xxx,xxx);
void setup() {
Serial.begin(9600);
pinMode(PIN0, INPUT);
pinMode(PIN1, INPUT);
connectWifi();
}
void loop() {
static byte prev_0 = HIGH;
static byte prev_1 = HIGH;
byte val;
val = digitalRead(PIN0);
if (val == LOW && prev_0 == HIGH) {
postData(0);
}
prev_0 = val;
val = digitalRead(PIN1);
if (sensorSense_1 == LOW && prev_1 == HIGH) {
postData(1);
}
prev_1 = val;
delay(100); /* switch debouncing */
}
void connectWifi() {
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid);
delay(7000);
}
}
void postData(byte pin) {
char buf[8];
int len = snprintf(buf, sizeof(buf), "senseval=%d", pin);
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(len);//yourdata
client.println();
client.println(buf);//yourdata
client.stop();
} else {
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
connectWifi();
printWifiStatus();
}
}
PHP方面的更新很少 添加intval以删除sql注入 简化如何获得post值 处理缺失的帖子值
<?php
if (empty($_REQUEST['senseval'])) {
echo('Missing data');
exit;
}
$senseval = intval($_REQUEST['senseval']);
// 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.');
}
?>