├── README.md
├── V1.0
├── RPi.py
└── wechat.php
└── V2.5
├── downup.php
├── get.ino
└── wechat.php
/README.md:
--------------------------------------------------------------------------------
1 | YouLinkedMe
2 | ---
3 | An IoT system
4 |
--------------------------------------------------------------------------------
/V1.0/RPi.py:
--------------------------------------------------------------------------------
1 | ######################
2 | # RPi to WeChat
3 | # V1.0
4 | ######################
5 |
6 | # 导入GPIO和requests库
7 | import requests
8 | import RPi.GPIO as GPIO
9 |
10 | # 设置GPIO输出方式
11 | GPIO.setmode(GPIO.BCM)
12 | GPIO.setup(18,GPIO.OUT) #green
13 | GPIO.setup(23,GPIO.OUT) #white
14 | GPIO.setup(24,GPIO.OUT) #red
15 | GPIO.setup(25,GPIO.OUT) #blue
16 |
17 | # 利用requests不断读取服务器上的几个文件的内容
18 | # 如果符合就作出某个动作
19 | while True:
20 | r = requests.get('http://example.net/ulink/green.txt')
21 | if r.text == "11":
22 | GPIO.output(18,GPIO.HIGH)
23 | if r.text == "00":
24 | GPIO.output(18,GPIO.LOW)
25 |
26 | r = requests.get('http://example.net/ulink/white.txt')
27 | if r.text == "11":
28 | GPIO.output(23,GPIO.HIGH)
29 | if r.text == "00":
30 | GPIO.output(23,GPIO.LOW)
31 |
32 | r = requests.get('http://example.net/ulink/red.txt')
33 | if r.text == "11":
34 | GPIO.output(24,GPIO.HIGH)
35 | if r.text == "00":
36 | GPIO.output(24,GPIO.LOW)
37 |
38 | r = requests.get('http://example.net/ulink/blue.txt')
39 | if r.text == "11":
40 | GPIO.output(25,GPIO.HIGH)
41 | if r.text == "00":
42 | GPIO.output(25,GPIO.LOW)
43 | '''
44 | r = requests.get('http://example.net/ulink/example.txt')
45 | if r.text == "value1":
46 | code
47 | if r.text == "value2":
48 | code
49 | '''
50 |
--------------------------------------------------------------------------------
/V1.0/wechat.php:
--------------------------------------------------------------------------------
1 | FromUserName;
63 | $toUserName = $xmlObj->ToUserName;
64 | $msgType = $xmlObj->MsgType;
65 |
66 |
67 | if('text' != $msgType) { //初步判断
68 | $retMsg = '只支持文本消息';
69 | }else{
70 | $content = $xmlObj->Content;
71 | if ($content == "开灯") { //比对命令
72 | file_put_contents("store.txt", "11");//更改文件值
73 | $retMsg = "成功";
74 | }else if ($content == "关灯") {
75 | file_put_contents("store.txt", "00");
76 | $retMsg = "成功";
77 | }
78 |
79 | }
80 |
81 | //装备XML
82 | $retTmp = "
83 |
84 |
85 | %s
86 |
87 |
88 | 0
89 | ";
90 | $resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
91 |
92 | //反馈
93 | echo $resultStr;
94 | ?>
95 |
--------------------------------------------------------------------------------
/V2.5/downup.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/V2.5/get.ino:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 |
7 | char state = '0';
8 | char c;
9 | byte mac[] = {
10 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
11 | IPAddress ip(192,168,1,177);
12 |
13 | IPAddress myDns(192,168,1,1);
14 |
15 | EthernetClient client;
16 |
17 | char server[] = "1.ulink42.sinaapp.com";
18 | int sensrdata = 0;
19 |
20 | unsigned long lastConnectionTime = 0;
21 | boolean lastConnected = false;
22 | const unsigned long postingInterval = 200*1000;
23 |
24 | // 定义DS18B20数据口连接arduino的2号IO上
25 | #define ONE_WIRE_BUS 2
26 |
27 | // 初始连接在单总线上的单总线设备
28 | OneWire oneWire(ONE_WIRE_BUS);
29 | DallasTemperature sensors(&oneWire);
30 |
31 | void setup(){
32 | // 设置串口通信波特率
33 | Serial.begin(9600);
34 | delay(1000);
35 | Ethernet.begin(mac, ip, myDns);
36 | Serial.print("My IP address: ");
37 | Serial.println(Ethernet.localIP());
38 | pinMode(7, OUTPUT);
39 | // 初始库
40 | sensors.begin();
41 | }
42 |
43 | void loop(void){
44 | sensors.requestTemperatures();
45 | sensrdata = sensors.getTempCByIndex(0);
46 |
47 | if(state == '0'){
48 | digitalWrite(7, LOW);
49 | }else if(state == '1'){
50 | digitalWrite(7, HIGH);
51 | }
52 |
53 | while(client.available()) {
54 | c = client.read();
55 | if (c == '{'){
56 | state = client.read();
57 | }
58 | }
59 |
60 | if (!client.connected() && lastConnected) {
61 | Serial.println("disconnecting.");
62 | client.stop();
63 | }
64 |
65 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
66 | if (client.connect(server, 80)) {
67 |
68 | // send the HTTP PUT request:
69 | client.print("GET /downup.php?token=doubleq&data=");
70 | client.print(sensrdata);
71 | client.println(" HTTP/1.1");
72 | client.println("Host: 1.ulink42.sinaapp.com");
73 | client.println("User-Agent: arduino-ethernet");
74 | client.println("Connection: close");
75 | client.println();
76 |
77 | lastConnectionTime = millis();
78 | }else {
79 | Serial.println("connection failed");
80 | Serial.println("disconnecting.");
81 | client.stop();
82 | }
83 | }
84 | lastConnected = client.connected();
85 | }
--------------------------------------------------------------------------------
/V2.5/wechat.php:
--------------------------------------------------------------------------------
1 | FromUserName;
64 | $toUserName = $xmlObj->ToUserName;
65 | $msgType = $xmlObj->MsgType;
66 |
67 |
68 | if($msgType == 'voice') {//判断是否为语音
69 | $content = $xmlObj->Recognition;
70 | }elseif($msgType == 'text'){
71 | $content = $xmlObj->Content;
72 | }else{
73 | $retMsg = '只支持文本和语音消息';
74 | }
75 |
76 | if (strstr($content, "温度")) {
77 | $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
78 | mysql_select_db("app_ulink42", $con);//修改数据库名
79 |
80 | $result = mysql_query("SELECT * FROM sensor");
81 | while($arr = mysql_fetch_array($result)){
82 | if ($arr['ID'] == 1) {
83 | $tempr = $arr['data'];
84 | }
85 | }
86 | mysql_close($con);
87 |
88 | $retMsg = "报告大王:"."\n"."主人房间的室温为".$tempr."℃,感谢您对主人的关心";
89 | }else if (strstr($content, "开灯")) {
90 | $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
91 |
92 |
93 | $dati = date("h:i:sa");
94 | mysql_select_db("app_ulink42", $con);//修改数据库名
95 |
96 | $sql ="UPDATE switch SET timestamp='$dati',state = '1'
97 | WHERE ID = '1'";//修改开关状态值
98 |
99 | if(!mysql_query($sql,$con)){
100 | die('Error: ' . mysql_error());
101 | }else{
102 | mysql_close($con);
103 | $retMsg = "好的主人";
104 | }
105 | }else if (strstr($content, "关灯")) {
106 | $con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
107 |
108 |
109 | $dati = date("h:i:sa");
110 | mysql_select_db("app_ulink42", $con);//修改数据库名
111 |
112 | $sql ="UPDATE switch SET timestamp='$dati',state = '0'
113 | WHERE ID = '1'";//修改开关状态值
114 |
115 | if(!mysql_query($sql,$con)){
116 | die('Error: ' . mysql_error());
117 | }else{
118 | mysql_close($con);
119 | $retMsg = "好的主人";
120 | }
121 | }else{
122 | $retMsg = "暂时不支持该命令";
123 | }
124 |
125 | //装备XML
126 | $retTmp = "
127 |
128 |
129 | %s
130 |
131 |
132 | 0
133 | ";
134 | $resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
135 |
136 | //反馈到微信服务器
137 | echo $resultStr;
138 | ?>
--------------------------------------------------------------------------------