微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)

第一步:准备
1、智能手机微信客户端或微信电脑版
2、注册微信公众平台
3、注册新浪账号
4、拥有一块Arduino UNO R3
5、拥有一块WS100

第二步:工作流程

微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)

第三步:开始工作
步骤1:硬件部分
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
1)、Arduino、WS100、网线、路由器。
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
2)、路由器设置
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
下图的mac和IP是分配到Arduino设备的(非必须,未验证)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
3)、Arduino设备程序烧录
Arduino IDE
Eclipse IDE
http://eclipse.baeyens.it/download.php(含有自带的arduino的插件,eclipse安装前需要安装JDK和配置环境变量)
#include "Arduino.h"
#include <SPI.h>
#include <Ethernet.h> char state = '0';
char c;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,3,2); IPAddress myDns(192,168,3,1); EthernetClient client; char server[] = "你的链接";
int sensrdata = 50; unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 200*1000; //The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
Serial.begin(9600);
delay(1000);
Ethernet.begin(mac, ip, myDns);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
} // The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
while(client.available()) {
state = client.read();
if(state == '{'){ //关灯 0
Serial.println(state);
digitalWrite(13, LOW);
digitalWrite(7, HIGH);
delay(9000);
}else if(state == '}'){ //开灯 1
Serial.println(state);
digitalWrite(13, HIGH);
digitalWrite(7, LOW);
delay(9000);
}
} if (!client.connected() && lastConnected) {
Serial.println("disconnecting 2.");
client.stop();
} if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
if (client.connect(server, 80)) { Serial.println("connecting");
// send the HTTP PUT request:
client.print("GET /downup.php?token=你的Token&data=");
client.print(sensrdata);
client.println(" HTTP/1.1");
client.println("Host: 你的链接");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println(); lastConnectionTime = millis();
}else {
Serial.println("connection failed");
Serial.println("disconnecting 1.");
client.stop();
}
}
lastConnected = client.connected();
}

步骤2:服务器端
1)、新浪云SAE
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
数据库构建
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
index.php代码
<?php

//错误日志
function echo_server_log($log){
file_put_contents("log.txt", $log, FILE_APPEND);
} //定义TOKEN
define ( "TOKEN", "微信TOKEN" ); //验证微信公众平台签名
function checkSignature() {
$signature = $_GET ['signature'];
$nonce = $_GET ['nonce'];
$timestamp = $_GET ['timestamp'];
$tmpArr = array ($nonce, $timestamp, TOKEN );
sort ( $tmpArr ); $tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
}else{
return false;
}
}
if(false == checkSignature()) {
exit(0);
} //接入时验证接口
$echostr = $_GET ['echostr'];
if($echostr) {
echo $echostr;
exit(0);
} //获取POST数据
function getPostData() {
$data = $GLOBALS['HTTP_RAW_POST_DATA'];
return $data;
}
$PostData = getPostData(); //验错
if(!$PostData){
echo_server_log("wrong input! PostData is NULL");
echo "wrong input!";
exit(0);
} //装入XML
$xmlObj = simplexml_load_string($PostData, 'SimpleXMLElement', LIBXML_NOCDATA); //验错
if(!$xmlObj) {
echo_server_log("wrong input! xmlObj is NULL\n");
echo "wrong input!";
exit(0);
} //准备XML
$fromUserName = $xmlObj->FromUserName;
$toUserName = $xmlObj->ToUserName;
$msgType = $xmlObj->MsgType; if($msgType == 'voice') {//判断是否为语音
$content = $xmlObj->Recognition;
}elseif($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("你的数据库名字", $con);//修改数据库名 $result = mysql_query("SELECT * FROM sensor");
while($arr = mysql_fetch_array($result)){
if ($arr['ID'] == 1) {
$tempr = $arr['data'];
}
}
mysql_close($con); $retMsg = "报告:"."\n"."编号为".$tempr."!";
}else if (strstr($content, "打开LED")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); $dati = date("h:i:sa");
mysql_select_db("你的数据库名字", $con);//修改数据库名 $sql ="UPDATE switch SET timestamp='$dati',state = '1' WHERE ID = '1'";//修改开关状态值 if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}else{
mysql_close($con);
$retMsg = "OK";
}
}else if (strstr($content, "关闭LED")) {
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); $dati = date("h:i:sa");
mysql_select_db("你的数据库名字", $con);//修改数据库名 $sql ="UPDATE switch SET timestamp='$dati',state = '0' WHERE ID = '1'";//修改开关状态值 if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}else{
mysql_close($con);
$retMsg = "OK";
}
}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;
?>
downup.php代码
<?php
error_reporting(E_ALL ^ E_DEPRECATED);//在php程序代码里面设置报警级别 if ($_GET['data'] && ($_GET['token'] == "微信TOKEN")) {//可以改token,这相当于密码,在Arduino端改成相应的值即可
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
$data = $_GET['data'];
mysql_select_db("你的数据库名字", $con);//要改成相应的数据库名 $result = mysql_query("SELECT * FROM switch");
while($arr = mysql_fetch_array($result)){//找到需要的数据的记录,并读出状态值
if ($arr['ID'] == 1) {
$state = $arr['state'];
}
}
$dati = date("h:i:sa");//获取时间
$sql ="UPDATE sensor SET timestamp='$dati',data = '$data' WHERE ID = '1'";//更新相应的传感器的值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());//如果出错,显示错误
}
mysql_close($con); if($state == "0"){ //关灯
echo "{";
}else if($state == "1"){ //开灯
echo "}";
} }else{
echo "Failed!";//请求中没有type或data或token或token错误时,显示Failed
} ?>
2)、微信公众平台
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
步骤3:测试网络互通
硬件IP(内网):192.168.3.2
硬件DNS(内网):192.168.3.1
工作电脑IP(内网):192.168.3.100
新浪云地址:http://1.gamesalad.applinzi.com/
新浪云端口:80或443
微信TOKEN:自己定义
微信填写的服务器URL:http://1.gamesalad.applinzi.com/index.php
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)

步骤4:调试手段
硬件:采用串口输出
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
服务器端:
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
注:本案例涉及多个过程,请逐步调试。
微信客户端与新浪云数据交互(通过查看数据库的值来判断交互情况)
硬件与新浪云交互(通过查看数据库的值来判断交互情况)

第四步:
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)

上一篇:Codeforces 1118 F2. Tree Cutting (Hard Version) 优先队列+树形dp


下一篇:Lua语言的介绍和编程语言的归类