将 arduino 引脚号打印到 MySQL

Printing arduino pin numbers to MySQL

我已经在 Arduino Uno r3 + WiFi Shield 项目上工作了几个月,现在随着我的努力增加了复杂性。我碰壁了。我开始这个项目时,编写任何类型的代码的经验都为零,使用开发板的经验也为零。如果您对我是否理解您的回复有任何疑问,请低调!

我做了一个简单的 arduino + wifi shield 设置,可以将按钮推送报告到 MySql 数据库。后端的所有 .php 东西都可以工作,数据库由 phpmyadmin 管理。我正在尝试添加第二个按钮,但似乎无法正常工作,需要一些帮助。

我选择了一个四字段数据库table作为输出:
第一列:钥匙。每个条目的顺序唯一标识符
第二列:传感器连接的引脚号
第 3 列:传感器值
第四列:日期和时间

请注意,我没有为每个传感器使用专用列。通过使用传感器引脚输入,我想简单地在一列中打印传感器引脚号。这 (a) 在 table 中保存 space 并且 (b) 允许 Arduino 使用尽可能多的传感器,因为它有引脚。

想法是每次按下按钮(即使同时按下两个按钮)都会获得它自己的线路、传感器引脚源、值和时间戳。除非按下按钮,否则不会发送任何值。

我完全不知道如何实现它!求助!

我的问题:
我怎样才能让草图检索信号来源的引脚并将其作为第二列中的数字写入数据库?第 13 行代码将写入 pin 号(我认为)。 "senseval=" 需要是报告源引脚的变量。

我不知道如何设置它,google 只会导致死胡同。我希望这个话题能够回答我确信还有几个人 have/will 有的问题!

我的素描在下面,insert_php_doc 在下面。我正在使用来自 github:

的 Silinas/Benoit "Arduino /Post" 示例
#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();
  }
}

插入 PHP 来自 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.');
  }

?>

对引脚使用定义,运行期间不会更新它们 我选择发送 pin 01 的 id,如果你想要 pin 号码,你只需将调用更改为 postData

使用 static val 存储先前的开关状态并检测按下开关。 使用 100 毫秒的 de delay 去抖动,如果需要会更新。 选择执行 2 posts 以保持逻辑,但根据 http post 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 方面的更新很少 添加间隔以删除 sql 注入 简化如何获取 post 值 处理缺失的 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.'); 
}   

?>