Skip to content

WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)

WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)

Components

Hardware components

x 1


x 1


x 1


Project description

第一步:准? 1、智能手机微信客?端或微信??版 2、注?微信公?平台 https://mp.weixin.qq.com 3、注?新浪?? http://www.sinacloud.com 4、?有一?Arduino UNO R3 http://item.jd.com/1593883356.html 5、?有一?WS100 https://item.taobao.com/item.htm?spm=a1z09.2.0.0.NJ6mUE&id=528047071612&_u=4p210bab347 第二步:工作流程 WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 第三步:?始工作 步?1:硬件部分 1)、Arduino、WS100、??、路由器。 2)、路由器?置 WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 下?的mac和IP是分配到Arduino??的(非必?,未??) WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 3)、Arduino??程序?? Arduino IDE https://www.arduino.cc/en/Main/Software Eclipse IDE http://eclipse.baeyens.it/download.php(含有自?的arduino的?件,eclipse安?前需要安?JDK和配置?境?量) Arduino代?
  1. #include "Arduino.h"
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. ?
  5. char state = '0';
  6. char c;
  7. byte mac[] = {
  8. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  9. IPAddress ip(192,168,3,2);
  10. ?
  11. IPAddress myDns(192,168,3,1);
  12. ?
  13. EthernetClient client;
  14. ?
  15. char server[] = "?的?接";
  16. int sensrdata = 50;
  17. ?
  18. unsigned long lastConnectionTime = 0;
  19. boolean lastConnected = false;
  20. const unsigned long postingInterval = 200*1000;
  21. ?
  22. //The setup function is called once at startup of the sketch
  23. void setup()
  24. {
  25. // Add your initialization code here
  26. Serial.begin(9600);
  27. delay(1000);
  28. Ethernet.begin(mac, ip, myDns);
  29. Serial.print("My IP address: ");
  30. Serial.println(Ethernet.localIP());
  31. pinMode(13, OUTPUT);
  32. pinMode(7, OUTPUT);
  33. }
  34. ?
  35. // The loop function is called in an endless loop
  36. void loop()
  37. {
  38. //Add your repeated code here
  39. while(client.available()) {
  40. state = client.read();
  41. if(state == '{'){ //?? 0
  42. Serial.println(state);
  43. digitalWrite(13, LOW);
  44. digitalWrite(7, HIGH);
  45. delay(9000);
  46. }else if(state == '}'){ //?? 1
  47. Serial.println(state);
  48. digitalWrite(13, HIGH);
  49. digitalWrite(7, LOW);
  50. delay(9000);
  51. }
  52. }
  53. ?
  54. if (!client.connected() && lastConnected) {
  55. Serial.println("disconnecting 2.");
  56. client.stop();
  57. }
  58. ?
  59. if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  60. if (client.connect(server, 80)) {
  61. ?
  62. Serial.println("connecting");
  63. // send the HTTP PUT request:
  64. client.print("GET /downup.php?token=?的Token&data=");
  65. client.print(sensrdata);
  66. client.println(" HTTP/1.1");
  67. client.println("Host: ?的?接");
  68. client.println("User-Agent: arduino-ethernet");
  69. client.println("Connection: close");
  70. client.println();
  71. ?
  72. lastConnectionTime = millis();
  73. }else {
  74. Serial.println("connection failed");
  75. Serial.println("disconnecting 1.");
  76. client.stop();
  77. }
  78. }
  79. lastConnected = client.connected();
  80. }
步?2:服?器端 1)、新浪云SAE WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) ?据??建 WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) index.php代?
  1. <?php
  2. ?
  3. //??日志
  4. function echo_server_log($log){
  5. file_put_contents("log.txt", $log, FILE_APPEND);
  6. }
  7. ?
  8. //定?TOKEN
  9. define ( "TOKEN", "微信TOKEN" );
  10. ?
  11. //??微信公?平台?名
  12. function checkSignature() {
  13. $signature = $_GET ['signature'];
  14. $nonce = $_GET ['nonce'];
  15. $timestamp = $_GET ['timestamp'];
  16. $tmpArr = array ($nonce, $timestamp, TOKEN );
  17. sort ( $tmpArr );
  18. ?
  19. $tmpStr = implode ( $tmpArr );
  20. $tmpStr = sha1 ( $tmpStr );
  21. if ($tmpStr == $signature) {
  22. return true;
  23. }else{
  24. return false;
  25. }
  26. }
  27. if(false == checkSignature()) {
  28. exit(0);
  29. }
  30. ?
  31. //接入???接口
  32. $echostr = $_GET ['echostr'];
  33. if($echostr) {
  34. echo $echostr;
  35. exit(0);
  36. }
  37. ?
  38. //?取POST?据
  39. function getPostData() {
  40. $data = $GLOBALS['HTTP_RAW_POST_DATA'];
  41. return $data;
  42. }
  43. $PostData = getPostData();
  44. ?
  45. //??
  46. if(!$PostData){
  47. echo_server_log("wrong input! PostData is NULL");
  48. echo "wrong input!";
  49. exit(0);
  50. }
  51. ?
  52. //?入XML
  53. $xmlObj = simplexml_load_string($PostData, 'SimpleXMLElement', LIBXML_NOCDATA);
  54. ?
  55. //??
  56. if(!$xmlObj) {
  57. echo_server_log("wrong input! xmlObj is NULLn");
  58. echo "wrong input!";
  59. exit(0);
  60. }
  61. ?
  62. //准?XML
  63. $fromUserName = $xmlObj->FromUserName;
  64. $toUserName = $xmlObj->ToUserName;
  65. $msgType = $xmlObj->MsgType;
  66. ?
  67. if($msgType == 'voice') {//判?是否??音
  68. $content = $xmlObj->Recognition;
  69. }elseif($msgType == 'text'){
  70. $content = $xmlObj->Content;
  71. }else{
  72. $retMsg = '只支持文本和?音消息';
  73. }
  74. ?
  75. if (strstr($content, "??")) {
  76. $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
  77. mysql_select_db("?的?据?名字", $con);//修改?据?名
  78. ?
  79. $result = mysql_query("SELECT * FROM sensor");
  80. while($arr = mysql_fetch_array($result)){
  81. if ($arr['ID'] == 1) {
  82. $tempr = $arr['data'];
  83. }
  84. }
  85. mysql_close($con);
  86. ?
  87. $retMsg = "?告:"."n"."???".$tempr."!";
  88. }else if (strstr($content, "打?LED")) {
  89. $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
  90. ?
  91. $dati = date("h:i:sa");
  92. mysql_select_db("?的?据?名字", $con);//修改?据?名
  93. ?
  94. $sql ="UPDATE switch SET timestamp='$dati',state = '1' WHERE ID = '1'";//修改?????
  95. ?
  96. if(!mysql_query($sql,$con)){
  97. die('Error: ' . mysql_error());
  98. }else{
  99. mysql_close($con);
  100. $retMsg = "OK";
  101. }
  102. }else if (strstr($content, "??LED")) {
  103. $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
  104. ?
  105. $dati = date("h:i:sa");
  106. mysql_select_db("?的?据?名字", $con);//修改?据?名
  107. ?
  108. $sql ="UPDATE switch SET timestamp='$dati',state = '0' WHERE ID = '1'";//修改?????
  109. ?
  110. if(!mysql_query($sql,$con)){
  111. die('Error: ' . mysql_error());
  112. }else{
  113. mysql_close($con);
  114. $retMsg = "OK";
  115. }
  116. }else{
  117. $retMsg = "命令无??";
  118. }
  119. ?
  120. //??XML
  121. $retTmp = "<xml>
  122. <ToUserName><![CDATA[%s]]></ToUserName>
  123. <FromUserName><![CDATA[%s]]></FromUserName>
  124. <CreateTime>%s</CreateTime>
  125. <MsgType><![CDATA[text]]></MsgType>
  126. <Content><![CDATA[%s]]></Content>
  127. <FuncFlag>0</FuncFlag>
  128. </xml>";
  129. $resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
  130. ?
  131. //反?到微信服?器
  132. echo $resultStr;
  133. ?>
downup.php代?
  1. <?php
  2. error_reporting(E_ALL ^ E_DEPRECATED);//在php程序代?里面?置?警??
  3. ?
  4. if ($_GET['data'] && ($_GET['token'] == "微信TOKEN")) {//可以改token,?相?于密?,在Arduino端改成相?的??可
  5. $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
  6. $data = $_GET['data'];
  7. mysql_select_db("?的?据?名字", $con);//要改成相?的?据?名
  8. ?
  9. $result = mysql_query("SELECT * FROM switch");
  10. while($arr = mysql_fetch_array($result)){//?到需要的?据的??,??出???
  11. if ($arr['ID'] == 1) {
  12. $state = $arr['state'];
  13. }
  14. }
  15. $dati = date("h:i:sa");//?取??
  16. $sql ="UPDATE sensor SET timestamp='$dati',data = '$data' WHERE ID = '1'";//更新相?的?感器的?
  17. if(!mysql_query($sql,$con)){
  18. die('Error: ' . mysql_error());//如果出?,?示??
  19. }
  20. mysql_close($con);
  21. ?
  22. if($state == "0"){ //??
  23. echo "{";
  24. }else if($state == "1"){ //??
  25. echo "}";
  26. }
  27. ?
  28. }else{
  29. echo "Failed!";//?求中?有type或data或token或token???,?示Failed
  30. }
  31. ?
  32. ?>
2)、微信公?平台WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control 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 WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 步?4:??手段 硬件:采用串口?出 WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 服?器端: WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED) 注:本案例涉及多??程,?逐步??。 微信客?端?新浪云?据交互(通??看?据?的??判?交互情?) 硬件?新浪云交互(通??看?据?的??判?交互情?) 第四步: WeChat client + WeChat public platform + Sina cloud SAE + Arduino + W5100S (control LED)

Comments

Similar projects you might like

Comments