【发布时间】:2016-08-15 21:50:39
【问题描述】:
因此,在大多数情况下,我最近购买的以太网防护罩真的很幸运。我现在正在尝试将模拟数据从 arduino 上传到本地 mysql 数据库。我的 write_data.php 文件似乎运行良好,每当我在 url 中调用 write_data.php 文件时,我都可以将数据上传到数据库。虽然 arduino 总是无法连接。我正在使用 netgear 路由器,我检查了 netgear genie 网络管理员上的允许设备列表,并且列出了 arduino,这是有道理的,因为它适用于我的所有其他项目。真的会在这里获得一些建议和想法。另外,不确定这是否会有所作为,但我使用 mamp 作为我的本地服务器环境。审查文件如下:
write_data.php:
<?php
// Prepare variables for database connection
$dbusername = "test"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "test"; // enter database password, I used "arduinotest" in step 2.2
$server = "50.135.xxx.xxx"; // 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_pconnect($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);
?>
arduino 草图:
#include SPI.h
#include Ethernet.h
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192,xxx,xx,xx);
int photocellPin = 2; // Analog input pin on Arduino we connected the SIG pin from sensor
int photocellReading; // Here we will place our reading
char server[] = "50.135.xxx.xxx"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
// Initialize the Ethernet server library
EthernetClient client;
void setup() {
// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
// start the Ethernet connection
Ethernet.begin(mac ,ip);
}
void loop() {
photocellReading = analogRead(photocellPin); // Fill the sensorReading with the information from sensor
// Connect to the server (your computer or web page)
if (client.connect(server,8888)) {
client.print("GET /write_data.php?"); // This
client.print("value="); // This
client.print(photocellReading); // 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.println("Host: 50.135.xxx.xxx"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
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
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
// Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(10000);
}
【问题讨论】:
-
警告:如果您只是学习 PHP,请不要使用
mysql_query接口。它是如此可怕和危险,以至于它在 PHP 7 中被删除。像 PDO is not hard to learn 这样的替代品和像 PHP The Right Way 这样的指南解释了最佳实践。你的用户参数不是properly escaped,有SQL injection bugs可以被利用。 -
请注意,如果您打算随着时间的推移使用 Websocket 服务器进行大量阅读,a Websocket module for Arduino 可能会让生活更轻松。这对于将实时数据流向数据库要好得多。
标签: php mysql apache arduino ethernet