前言:生命不息,折腾不止。这是一种对生命的认知及态度。吾本是学软件的,但由于大学期间接触到的社团有搞硬件的,看到他们搞的遥控小车,小四轴。。。觉得他们好geek,于是也对硬件愈发的感兴趣。业余时间研究一下硬件,积累了一点硬件知识。一直期盼着做一个小项目来实战一下。某日在arduino论坛发现了一位大神的作品是用微信控制arduino的,于是在他的作品上演译了一番。
硬件:
arduino uno(¥25/一块,原谅我穷B只能用山寨的了),
enc28j60(¥13/一块,以太网模块),DHT11(温湿度传感器,便宜),MQ-2(烟雾传感器,也不贵)
led(在废弃家电上扒下来的),
杜邦线(若干根,焊工好的话,可以从废弃家电上扒下来再用来焊接)。
帐号准备:微信公众号或测试号,新浪云帐号(新浪云免费的空间足够使用,由于最近学了springmvc,想用java写,但java环境初期都要收费,无奈只好用原文的php)。
硬件接线:arduino uno 与 enc28j60
Enc28j60 | Arduino UNO |
---|---|
VCC | 3.3V |
GND | GND |
SCK | Pin 13 |
SO | Pin 12 |
SI | Pin 11 |
CS | Pin 10 |
arduino uno与DHT11接线说明
arduino uno与MQ-2接线说明
实物:
一切线路okay!下面准备烧录程序:(使用arduino ide将下面代码烧录进uno板,两个库文件需要放进ide的libraries下:文件)
//
// FILE: dht_test.pde
// PURPOSE: DHT library test sketch for Arduino
//
#include
#include
#define DHT11_PIN 6//put the sensor in the digital pin 6
EthernetClient client;
signed long next;
char server[] = "1.smarthousetest.applinzi.com";
char state = '0';
char c;
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 200*1000;
double temperature=0;
double humidity=0;
double smoke=0;
int ledPin=2; //设定控制LED的数字IO脚
dht DHT;
void setup()
{
Serial.begin(9600);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
// 设置串口通信波特率
uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1};
IPAddress myIP(192,168,0,110);
Serial.println("*****");
Ethernet.begin(mac,myIP);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
pinMode(5,OUTPUT);
}
void loop()
{
// READ DATA
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case 0: Serial.print("OK,\t"); break;
case -1: Serial.print("Checksum error,\t"); break;
case -2: Serial.print("Time out error,\t"); break;
default: Serial.print("Unknown error,\t"); break;
}
// DISPLAT DATA
Serial.print(DHT.humidity,1);
Serial.print(",\t");
Serial.println(DHT.temperature,1);
temperature=DHT.temperature;
humidity=DHT.humidity;
temperature=DHT.temperature;
if(state == '0'){
digitalWrite(5, LOW);
}else if(state == '1'){
digitalWrite(5, HIGH);
}
smoke=analogRead(0);
Serial.println(smoke,DEC);
if(smoke>=300){//烟雾浓度值大于300时,自动亮
digitalWrite(ledPin,HIGH); //设定PIN5脚为LOW = 0V
}else{
digitalWrite(ledPin,LOW); //设定PIN5脚为LOW = 0V
}
while(client.available()) {
c = client.read();
if (c == '{'){
state = client.read();
}
Serial.println(state);
}
if (!client.connected() && lastConnected) {
Serial.println("disconnecting**.");
client.stop();
}
if(!client.connected()) {
Serial.println("!client.connected()");
if (client.connect(server, 80)) {
Serial.println(client.connect(server, 80));
// send the HTTP PUT request:
client.print("GET /update.php?token=smarthouse&temperature=");
client.print(temperature);
client.print("&humidity=");
client.print(humidity);
client.print("&smoke=");
client.print(smoke);
client.println(" HTTP/1.1");
client.println("Host: 1.smarthousetest.applinzi.com");//项目url
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}else {
Serial.println("connection failed");
Serial.println("disconnecting.////");
client.stop();
}
}
lastConnected = client.connected();
delay(1000);
}
//
// END OF FILE
//
烧录完成,则将服务器端的项目部署上去。部署步骤省略
index.php:
FromUserName;
$toUserName = $xmlObj->ToUserName;
$msgType = $xmlObj->MsgType;
if($msgType == 'text'){
$content = $xmlObj->Content;
}else{
$retMsg = '只支持文本和语音消息';
}
if (strstr($content, "温度")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
mysql_select_db("app_smarthousetest", $con);
$result = mysql_query("SELECT * FROM sensor_data");
while($arr = mysql_fetch_array($result)){
if ($arr['ID'] == 0) {
$tempr = $arr['temperature'];
}
}
mysql_close($con);
$retMsg = "亲爱的主人,你的房间的温度为".$tempr."℃。";
}else if(strstr($content, "湿度")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
mysql_select_db("app_smarthousetest", $con);
$result = mysql_query("SELECT * FROM sensor_data");
while($arr = mysql_fetch_array($result)){
if ($arr['ID'] == 0) {
$tempr = $arr['humidity'];
}
}
mysql_close($con);
$retMsg = "亲爱的主人,你的房间的湿度为".$tempr."%";
}else if(strstr($content, "烟雾浓度")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
mysql_select_db("app_smarthousetest", $con);
$result = mysql_query("SELECT * FROM sensor_data");
while($arr = mysql_fetch_array($result)){
if ($arr['ID'] == 0) {
$tempr = $arr['smoke'];
}
}
mysql_close($con);
$retMsg = "亲爱的主人,你的房间的烟雾浓度值为".$tempr;
}else if (strstr($content, "开灯")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$dati = date("h:i:sa");
mysql_select_db("app_smarthousetest", $con);
$sql ="UPDATE led_data SET create_time='$dati',status = '1' WHERE ID = '0'";//修改开关状态值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error);
}else{
mysql_close($con);
$retMsg = "好的";
}
}else if (strstr($content, "关灯")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$dati = date("h:i:sa");
mysql_select_db("app_smarthousetest", $con);
$sql ="UPDATE led_data SET create_time='$dati',status = '0'
WHERE ID = '0'";//修改开关状态值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}else{
mysql_close($con);
$retMsg = "好的";
}
}else{
$retMsg = "暂时不支持该命令";
}
//XML信息
$retTmp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
//反馈到微信服务器
echo $resultStr;
?>
update.php
<?php
if (($_GET['temperature'] || $_GET['humidity'] || $_GET['smoke'] )&& ($_GET['token'] == "smarthouse")) {//可以改token,这相当于密码,在Arduino端改成相应的值即可
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$temperature = $_GET['temperature'];
$humidity = $_GET['humidity'];
$smoke = $_GET['smoke'];
mysql_select_db("app_smarthousetest", $con);//要改成相应的数据库名
$result = mysql_query("SELECT * FROM led_data");
while($arr = mysql_fetch_array($result)){//找到需要的数据的记录,并读出状态值
if ($arr['ID'] == 0) {
$status = $arr['status'];
}
}
$dati = date("h:i:sa");//获取时间
$sql ="UPDATE sensor_data SET create_time='$dati',temperature = '$temperature',humidity = '$humidity',smoke = '$smoke'
WHERE ID = '0'";//更新相应的传感器的值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());//如果出错,显示错误
}
mysql_close($con);
echo "{".$status."}";//返回状态值,加“{”是为了帮助Arduino确定数据的位置
}else{
echo "Permission Denied";//请求中没有type或data或token或token错误时,显示Permission Denied
}
?>
数据库:
sensor_data
id | id | int |
温度 | temperature | float |
湿度 | humidity | float |
烟雾浓度 | smoke | float |
创建时间 | create_time | dataTime |
led_data
id | id | int |
状态 | status | int |
创建时间 | create_time | dataTime |
一切基本完成,就差将申请的微信公众号,使用开发者模式,将url填写为项目的url,这里的为:1.smarthousetest.applinzi.com。token则为:smarthouse。
下面就是正式的调用了,关注公众号后,则可实现如下图功能:
折腾多时,终于完成!
来源:oschina
链接:https://my.oschina.net/u/1448523/blog/684536