w5100与php通信,arduino UNO R3 + W5100 + PHPServer + 微信公众号 远程监控和控
本帖最后由 rengaoadmin 于 2016-3-21 23:27 編輯
由于時間問題,本篇內容逐步更新,先把主要代碼放出,陸續添加圖片和注意事項.......
一??總體介紹
arduino端實物
IMG_0089.JPG (1.57 MB, 下載次數: 19)
2016-3-21 23:19 上傳
微信端界面,監控菜單包括2個選項溫度和濕度
IMG_6078.png (73.87 KB, 下載次數: 14)
2016-3-21 23:19 上傳
控制菜單包括2個選項:開燈和關燈
IMG_6079.png (71.42 KB, 下載次數: 14)
2016-3-21 23:21 上傳
第一個菜單為按鈕,直接打開官網
IMG_6080.PNG (398.4 KB, 下載次數: 15)
2016-3-21 23:21 上傳
下圖是溫度 /濕度/開燈/關燈所對應的結果顯示
IMG_0082.png (144.25 KB, 下載次數: 17)
2016-3-21 23:26 上傳
LCD_Base_bb_Fritz.png (241.57 KB, 下載次數: 17)
2016-3-21 23:26 上傳
單獨測試LCD時無所謂,LCD與W5100同時使用時,LCD不能再使用12,11腳,否則會出現亂碼,改為9,8端口。
二??所需材料
三??用到的庫文件
四??arduino端
[mw_shl_code=cpp,true]#include
#include
#include
dht11 DHT11;
#define DHT11PIN 6
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
//由于有W5100 pin 12,11 LCD不能使用
char state = '0';
char c;
int dht11state=0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 31, 177);
EthernetClient client;
byte sheng[8] = {
0x04,0x14,0x1f,0x14,0x0e,0x04,0x1f,0x00
};
byte ri[8] = {
0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00
};
byte kuai[8] = {
0x0a,0x0a,0x1f,0x1b,0x1f,0x0a,0x0d,0x00
};
byte le[8] = {
0x1e,0x10,0x14,0x1f,0x04,0x15,0x15,0x00
};
byte tanhao[8] = {
0x02,0x02,0x04,0x04,0x00,0x00,0x08,0x00
};
byte du[8] = {
0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00
};
byte xin[8] = {
0x00,0x0a,0x1f,0x1f,0x1f,0x0e,0x04,0x00
};
char server[] = "plclive.com";//設置為自己的服務器地址
int hum = 0;
int temp = 0;
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
void setup(){
Serial.begin(9600);// 設置串口通信波特率
delay(1000);
Ethernet.begin(mac,ip);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
pinMode(7, OUTPUT);? ?? ?? ???//設置第七個數字io為輸出
pinMode(10, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.createChar(0, sheng);
lcd.createChar(1, ri);
lcd.createChar(2, kuai);
lcd.createChar(3, le);
lcd.createChar(4, tanhao);
lcd.createChar(5, du);
lcd.createChar(6, xin);
}
void loop(void){
int chk = DHT11.read(DHT11PIN);
switch (chk)
{
case DHTLIB_OK:
dht11state=1;
break;
case DHTLIB_ERROR_CHECKSUM:
dht11state=2;
break;
case DHTLIB_ERROR_TIMEOUT:
dht11state=3;
break;
default:
dht11state=10;
break;
}
hum=(int)DHT11.humidity;
temp=(int)DHT11.temperature;
//顯示溫度濕度開始
lcd.setCursor(0, 0);
lcd.print("Temperatue:");
lcd.print(temp);
lcd.write(5);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum:");
lcd.print(hum);
lcd.print("%");
lcd.blink();
//顯示溫度濕度結束
//顯示中文開始
lcd.setCursor(9, 1);
lcd.write(6);
lcd.write(byte(0));
lcd.write(1);
lcd.write(2);
lcd.write(3);
lcd.write(4);delay(100);
lcd.write(6);delay(200);
//顯示中文結束
if(state == '0'){? ?? ?? ?? ? //根據state的狀態設置7腳的電平
digitalWrite(7, HIGH);
digitalWrite(10,LOW);
}else if(state == '1'){
digitalWrite(7, LOW);
digitalWrite(10,HIGH);
}
while(client.available()) {
c = client.read();
if (c == '{'){
state = client.read();
}
}
if (!client.connected() && lastConnected) {
Serial.println("disconnecting...");
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
if (client.connect(server, 80)) {
// send the HTTP PUT request:
client.print("GET http://www.plclive.com/api/downup.php?token=**********&data1=");
//*部分修改為你的token,向自己的服務器發送數據及更新token要與downup.php中設置的token一致,data1\data2\....\dataN根據傳感器數量添加,
//并且要與downup.php里一致,同事數據庫增加相應的記錄值
client.print(temp);
client.print("&data2=");
client.print(hum);
client.println(" HTTP/1.1");
client.println("Host: www.plclive.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}else {
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
lastConnected = client.connected();
}[/mw_shl_code]
五??web端,包括數據庫和PHP頁面兩部分
六??微信端的設置和菜單的生成
總結
以上是生活随笔為你收集整理的w5100与php通信,arduino UNO R3 + W5100 + PHPServer + 微信公众号 远程监控和控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运算放大电路在音频放大电路中的应用研究与
- 下一篇: 微型计算机接口及控制技术,微型计算机及接