|
本帖最后由 laoliu1982 于 2013-1-7 13:32 编辑
视频 http://v.youku.com/v_show/id_XNDk4NjA0NzIw.html
感谢 @zcbzjx 在 http://www.geek-workshop.com/for ... thread&tid=2260 提供的教程
采用 修改以后的ethercard 库函数(添加了一个udpreply 方法让手机APP可以自己找到andriod设备)
etercard 修改后的库:
亮点:
1:app自动寻找设备。
2:arduino是server,app 控制arduino 无明显延时。
局限性:
1 现在控制需要在同一个wifi网络下面进行,暂不支持跨网段的控制,手机要用wifi 方式。
2 代码偷懒没做usrkey的验证,所以只要一个wifi下面的设备,哪个用户登陆手机app都可以控制。
3 arduino下面http数据都是明文的。
怎么用:
1 下载手机app http://open.lewei50.com/home/sitecontent/ydkhd,去我们的平台注册一个账号,靠账号登陆app(如果非不愿意注册,用户名:demo 密码:123456 也能登陆,但是这样会影响我们以后更新功能的积极性哈)。
2 把代码找一个arduono+enc28J60下载进去(需要先用 更新 s\arduino-1.0.1\libraries\ethercard 的内容,不放心可以比较着看一下)。
3 找一个灯插在一个enc28J60 没用的引脚上面, 修改const int ledPin = 8;
4 通过手机就可以控制了。
如果你对细节感兴趣,可以看这篇文档:
再说两句废话:欢迎大家使用我们的乐联网开放平台,欢迎提意见,建设性意见会有奖励哈(比如说生成vip用户啥的{:soso_e102:} ),欢迎关注@乐为物联 技术支持群:59162154 我们的平台介绍:open.lewei50.com
代码:代码里面最后一段属于向公网平台提交测试数据,如果wifi网络没有接入公网或者不打算提交数据,可以删除 if (millis() > timer) {} 里面的内容。
- // Simple demo for feeding some random data to Pachube.
- // 2011-07-08 <[email][email protected][/email]> [url]http://opensource.org/licenses/mit-license.php[/url]
- #include <EtherCard.h>
- byte Ethernet::buffer[700];
- // change these settings to match your own setup
- #define FEED "5942"
- #define APIKEY "xxxxxx1ca57a7c8"
- // ethernet interface mac address, must be unique on the LAN
- byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- char website[] PROGMEM = "open.lewei50.com";
- const int ledPin = 8;
- char* on = "ON";
- char* off = "OFF";
- char* statusLabel;
- char* buttonLabel;
- char*udpReply="{"deviceName":"01","username":"laoliu1982","port":80,"api":"api","requestType":"get"}";
- #define gPB ether.buffer
- #define UDP_DP_HIGH 36
- #define UDP_DP_LOW 37
- #define DSTPORT_HIHG 0x26
- #define DSTPORT_LOW 0xe7
- int x=1;
- int sensorValue = 0;
- int sensorPin = A0;
- uint32_t timer;
- Stash stash;
- void setup () {
- Serial.begin(9600);
- Serial.println("\n[webClient]");
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, LOW);
- buttonLabel = "0";
- if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
- Serial.println( "Failed to access Ethernet controller");
- else
- Serial.println("Ethernet controller initialized");
- if (!ether.dhcpSetup())
- Serial.println("DHCP failed");
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
-
- ether.printIp("SRV: ", ether.hisip);
- Serial.println("\n webClient setup end");
- }
- void loop () {
- word len = ether.packetReceive();
- word pos = ether.packetLoop(len);
- if(len)
- {
- if(gPB[IP_PROTO_P]==IP_PROTO_UDP_V)
- {
- if((gPB[UDP_DP_HIGH]==DSTPORT_HIHG)&&(gPB[UDP_DP_LOW]==DSTPORT_LOW))
- {
- Serial.println("---Received an udp 9959 packet---");
- // ether.sendUdp(udpReply,strlen(udpReply),60000,(gPB + IP_SRC_P),9960);
-
- ether.leweiUdpReply(udpReply,strlen(udpReply),9960);
-
- }
-
-
- }
-
- }
- if(pos)
- {
- Serial.print("len:");
- Serial.println(len);
-
- if(strstr((char *)Ethernet::buffer + pos, "&f=getAllSensors") != 0)
- {
- Serial.println("Received GetAllSensor command");
- BufferFiller bfill = ether.tcpOffset();
- bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"
- "{"successful":true,"message":null,"data":[{"id":"1","type":"jdq","name":"s1","value":"$S","status":"ok"}]}"),buttonLabel);
- ether.httpServerReply(bfill.position());
- }
- if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=0") != 0)
- {
- Serial.println("Received OFF command");
- BufferFiller bfill = ether.tcpOffset();
- bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"
- "{"data":{"id":"1","type":"jdq","name":"s1","value":"0","status":"ok"},"successful":true,"message":null}"));
- ether.httpServerReply(bfill.position());
- digitalWrite(ledPin, LOW);
- buttonLabel="0";
- }
- if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=1") != 0)
- {
- Serial.println("Received ON command");
- BufferFiller bfill = ether.tcpOffset();
- bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"
- "{"data":{"id":"1","type":"jdq","name":"s1","value":"1","status":"ok"},"successful":true,"message":null}"));
- ether.httpServerReply(bfill.position());
- digitalWrite(ledPin, HIGH);
- buttonLabel="1";
- }
- }
-
- if (millis() > timer) {
- //timer = millis() + 10000;
- timer = millis() + 3000;
- sensorValue = analogRead(sensorPin);
- //Serial.println(x);
- Serial.println(sensorValue);
- // generate two fake values as payload - by using a separate stash,
- // we can determine the size of the generated message ahead of time
- x=x+1;
- byte sd = stash.create();
- stash.print("[{"Name":"s1","Value":");
- //stash.print((word)x);
- stash.print((word)sensorValue);
- stash.print("},{"Name":"T1","Value":");
- stash.print((word)x);
- stash.println("}]");
- stash.save();
-
- // generate the header with payload - note that the stash size is used,
- // and that a "stash descriptor" is passed in as argument using "$H"
- Stash::prepare(PSTR("POST /api/V1/Gateway/UpdateSensors/01 HTTP/1.1\r\n"
- "Host: open.lewei50.com\r\n"
- "Content-Length: $D\r\n"
- "userkey: $F\r\n"
- "\r\n"
- "$H"),stash.size(), PSTR(APIKEY),sd);
-
- // send the packet - this also releases all stash buffers once done
- ether.tcpSend();
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|