本例程的实验需要9-Ppprouter实验节点;通过边界路由来实现CoAPServer 的节点LED控制;
先烧写一个Ppprouter节点,连接PC,pppd拨号连接后;进行下面的操作;不清楚的朋友大家可以去看看视频部分;
例程目录:
tinyos-main-release_tinyos_2_1_2\apps\cc2538_Test\CoapBlip
Makefile文件;任性的忽略
CoapBlipC.nc文件:
看着这是蛮多的,但是TinyOS的官方例程在Makefile指定了只有LED资源
CoapBlipP.nc文件:
- /********************************************************************************************************************************************************
- *实验8----zigbee CoAP实验(路由为roll)
- *节点需求数 >= 2
- *编译命令make cc2538cb blip coap id.xx (xx为1~65533,注意此实验需要用到实验7,id勿与实验7重复)
- *测试命令:
- *1,先下载实验7的边界路由,sudo /usr/bin/pppd-hack/sbin/pppd debug passive noauth nodetach 115200 /dev/ttyUSB0 nocrtscts nocdtrcts lcp-echo-interval 0 noccp noip ipv6 ::23,::24 连接边界路由
- *2,新打开shell,执行sudo ifconfig ppp0 add fec0::100/64
- *3,烧写实验8的bin到节点
- *4,ping6 ff02::xx(xx为1~65533,且为烧写的测试节点号 (该步骤可省略)
- *5,参考网页测试;http://tinyos.stanford.edu/tinyos-wiki/index.php/CoAP_-17
- * shell进入tinyos-main-release_tinyos_2_1_2\support\sdk\c\coap\examples目录
- * 5.1 GET request测试: ./coap-client coap://[fec0::xx]/l 其中xx为实验8 的编译id
- * 5.2 PUT request:测试: ./coap-client -m put coap://[fec0::xx]/l -e 7 其中xx为实验8 的编译id,其中7为bit操作
- * 对应bit2:bit0分别为led2,led1,led0,置0为灭置1位亮,用户可以put以后再get一次,看看值是否正确,故7为全亮,0为全灭
- *也可以参考\apps\cc2538_Test\PppRouter下的README.blip进行测试,但是注意第一步命令pppd的区别
- *
- *通过例程的学习可以自己注册资源,如传感器,电机控制等,进行应用拓展
- *******************************************************************************************************************************************************/
-
- #include <IPDispatch.h>
- #include <lib6lowpan/lib6lowpan.h>
- #include <lib6lowpan/ip.h>
- #include "blip_printf.h"
-
- #include "net.h"
- #include "resource.h"
-
- #ifdef COAP_CLIENT_ENABLED
- #include "tinyos_net.h"
- #include "option.h"
- #include "address.h"
- #endif
-
- #ifndef COAP_SERVER_PORT
- #define COAP_SERVER_PORT COAP_DEFAULT_PORT
- #endif
-
- module CoapBlipP {
- uses {
- interface Boot;
- interface SplitControl as RadioControl;
- interface Leds;
-
- #ifdef COAP_SERVER_ENABLED
- interface CoAPServer;
- #ifdef COAP_RESOURCE_KEY
- interface Mount;
- #endif
- #endif
-
- #ifdef COAP_CLIENT_ENABLED
- interface CoAPClient;
- interface ForwardingTableEvents;
- #endif
- }
-
- } implementation {
- #ifdef COAP_CLIENT_ENABLED
- uint8_t node_integrate_done = FALSE;
- #endif
-
- event void Boot.booted() {
-
- call RadioControl.start();
- printf("booted %i start\n", TOS_NODE_ID);
- #ifdef COAP_SERVER_ENABLED
- #ifdef COAP_RESOURCE_KEY
- if (call Mount.mount() == SUCCESS) {
- printf("CoapBlipP.Mount successful\n");
- }
- #endif
-
- // needs to be before registerResource to setup context:
- call CoAPServer.setupContext(COAP_SERVER_PORT);
- call CoAPServer.registerResources();
-
- #endif
-
- #ifdef COAP_CLIENT_ENABLED
- // needs to be before registerResource to setup context:
- call CoAPClient.setupContext(COAP_CLIENT_PORT);
- #endif
-
- }
-
- #if defined (COAP_SERVER_ENABLED) && defined (COAP_RESOURCE_KEY)
- event void Mount.mountDone(error_t error) {
- }
- #endif
-
- event void RadioControl.startDone(error_t e) {
- printf("radio startDone: %i\n", TOS_NODE_ID);
- }
-
- event void RadioControl.stopDone(error_t e) {
- }
-
- #ifdef COAP_CLIENT_ENABLED
- event void ForwardingTableEvents.defaultRouteAdded() {
- //struct sockaddr_in6 sa6;
- coap_address_t dest;
- coap_list_t *optlist = NULL;
-
- if (node_integrate_done == FALSE) {
- node_integrate_done = TRUE;
-
- inet_pton6(COAP_CLIENT_DEST, &dest.addr.sin6_addr);
- dest.addr.sin6_port = htons(COAP_CLIENT_PORT);
-
- coap_insert( &optlist, new_option_node(COAP_OPTION_URI_PATH, sizeof("ni") - 1, "ni"), order_opts);
-
- // this stuff should most likely be POST!
- call CoAPClient.request(&dest, COAP_REQUEST_PUT, optlist, 0, NULL);
- }
- }
-
- event void ForwardingTableEvents.defaultRouteRemoved() {
- }
-
- event error_t CoAPClient.streamed_next_block (uint16_t blockno, uint16_t *len, void **data)
- {
- return FAIL;
- }
-
- event void CoAPClient.request_done(uint8_t code, uint16_t len, void *data) {
- //event void CoAPClient.request_done(uint8_t code, uint8_t mediatype, uint16_t len, void *data, bool more) {
- //TODO: handle the request_done
- };
- #endif
-
- }
一般来说我们在2538上都是运行CoAP Server,PC端运行client,通过边界路由来获取资源;get/put方法
tinyos_coap_resources.h文件:
太长了,只是弄出LED部分,让大家知道为什么测试的时候是coap ...l;这个"l"字符;
- #ifdef COAP_RESOURCE_LED
- {
- INDEX_LED,
- "l", sizeof("l"),
- #if defined (COAP_CONTENT_TYPE_JSON) || defined (COAP_CONTENT_TYPE_XML)
- "", "",
- #endif
- {0,0,0,0}, // uri_key will be set later
- COAP_DEFAULT_MAX_AGE,
- (GET_SUPPORTED | PUT_SUPPORTED),
- 1
- },
- #endif
测试部分大家可以参考TinyOS的官网,搜索CoAP部分测试方法,注意编译时候的CoapBlip的节点号(id.xx)
测试我们通过tinyos-main-release_tinyos_2_1_2\support\sdk\c\coap\examples下的coap-client;测试命令参考TinyOS官网;我们也可以按照视频介绍在shell输入命令get方法,通过zigbee获取CoapBlip节点的三个LED的状态;也可以控制三个LED的亮灭(put方法),当然你也可以使用ping6命令来玩,或者路由读取命令查看网络;
tinyos-main-release_tinyos_2_1_2\support\sdk\c\coap是已经编译过的,用户无需再进行make;这是CoAP的C语言版本--libcoap,可以通过他学习使用,注册资源,自己动手注册温度或其他传感器资源;参考哥伦比亚大学的COAP框架(JAVA版本),或者其他你擅长的语言版本,可以去我的那篇帖子找见支持CoAP的其他语言开源版本,自己编写PC端或安卓的浏览器插件,或者浏览器界面,应用软件界面,最近事情有点多,先放着,以后补充!