我有:
上面两个都是工作hunky dory - Arduino连接到WiFi并分配了IP地址。
我现在要做的是编写一些Arduino代码来读取存储在Web服务器数据库中的值。
如果我能弄清楚如何做到这一点,我可以从那里拿走它。有人可以提供一个如何操作的例子吗?
简而言之,只需知道从已连接到网络的arduino查询Web服务器/数据库所需的内容。
#include <WiFi.h>
char ssid[] = "SSID_IS_HERE"; // your network SSID (name)
char pass[] = "MY_PASSWORD"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println();
}
答案 0 :(得分:2)
首先取决于你的arduino板,但如果你有盾牌,我认为你有一个arduino uno,例如。 现在你需要在php中编写一个web服务,例如,在查询后响应存储在mysql中的值。 假设:Arduino web client
您需要在服务器中创建一个名为ledstatus.php的文件,以便调用
www.yourwebserver / ledstatus.php?STAT = LED1 stat是读取led
的统计信息的命令你的php文件,在mysql查询后简单响应一个整数值为0或1的echo ...
现在你的领导已经确定:
set const int ledPin = 13;
pinMode(ledPin, OUTPUT);
并在此部分代码设置Web请求
client.println("GET /ledstatus.php?stat=led1 HTTP/1.1");
client.println("Host: www.yourwebserver");
client.println("Connection: close");
client.println();
并在此代码中读取请求响应
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c); // to see the value
digitalWrite(ledPin, atoi(c));
// atoi is a function to convert ascii to integer
}
while(true);
}
希望这能帮到你