【简介】利用Arduino和Coolpy设计网关 —— 开源项目Coolpy

来源:互联网 发布:缺月梧桐 知乎 编辑:程序博客网 时间:2024/05/18 00:12

Coolpy API 官方文档:http://www.icoolpy.com/docs.html

1. Coolpy简介

Coolpy是一个开放的通用物联网服务平台,主要提供传感器数据的接入、存储和展现服务;Coolpy是跨平台的,Coolpy服务端可以运行在Windows、Linux、Mac、Android等主流操作系统,甚至可以运行在openWRT路由器和Arduino Yun中。Coolpy客户端使用HTML5技术开发,既可以运行于桌面设备,也可以运行于移动设备;Coolpy是开放源代码的,因此用户可以基于Coolpy源代码进行二次开发,极大可能地满足用户需求。

1.1 Coolpy API简介

Coolpy对外有一套完整的API,使用这套API通过简单的HTTP请求,即可实现设备、传感器、数据点在服务端的创建、修改、查看、删除等动作。
(1)设备(Devices)相关API
分别对下表中的URL发出HTTP POST、PUT、GET、GET、DELETE请求,将分别创建、编辑、罗列、查看、删除一个设备。
设备URL
(2)传感器(Sensors) 相关API
分别对下表中的URL发出HTTP POST、PUT、GET、GET、DELETE请求,将分别创建、编辑、罗列、查看、删除一个传感器。
传感器URL
(3)数据点(Datapoints) 相关API
分别对下表中的URL发出HTTP POST、PUT、GET、DELETE请求,将分别创建、编辑、查看、删除一个数据点。
数据点URL

2. 开源软件Arduino Ethenet库简介

Arduino Ethenet是一个以AVR单片机和网络接口芯片W5100为硬件核心的软件库,它实现了AVR单片机的SPI接口驱动函数和网络接口芯片W5100的驱动函数,并在此基础上实现了Socket API函数,使开发者可以专注于应用程序的编写,而不需要花太多的精力在底层的驱动函数上。嵌入式HTTP应用开发因此变得简单、高效。
使用Arduino Ethenet库构建HTTP客户端示例:

//设置硬件物理地址byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };//设置服务器地址char server[] = "www.google.com";//设置IP地址,假如DHCP分配IP失败,就使用这个IPIPAddress ip(192,168,1,2);EthernetClient client;void setup() { Serial.begin(9600); if (Ethernet.begin(mac) == 0) {   Ethernet.begin(mac, ip); } //延时1S,要硬件初始化 delay(1000); //请求与服务器连接 if (client.connect(server, 80)) {   Serial.println("connected");   // HTTP GET请求报文:   client.println("GET /search?q=arduino HTTP/1.1");   client.println("Host: www.google.com");   client.println("Connection: close");   client.println(); }  else {   Serial.println("connection failed"); }}void loop(){ // 接收服务器返回的响应报文 if (client.available()) {   char c = client.read();   Serial.print(c); } // 假如与服务器断开连接,那么停止HTTP客户端 if (!client.connected()) {   Serial.println();   Serial.println("disconnecting.");   client.stop(); }}

3. HTTP POST、PUT、GET、DELETE请求的具体实现

(1)POST方法的实现
POST请求报文格式:
POST请求报文
POST请求具体实现:

void Post(String PostData,byte deviceID, byte sensorID){// if you get a connection, report back via serial:  if (client.connect(server, port)) {    Serial.println("connecting.");    // Make a HTTP request:    client.print("POST /v1.0/device/");    client.print(deviceID);    client.print("/sensor/");    client.print(sensorID);    client.println("/datapoints HTTP/1.1");    client.print("Host:");    client.println(server);    client.println("Content-Type:application/json");    client.print("U-ApiKey:");    client.println(ukey);    client.println("Connection: close");    client.print("Content-Length: ");    client.println(PostData.length()+1);    client.println();    client.println(PostData);  }   else {     //If you didn't get a connection to the server:    Serial.println("connection failed.");  }

(2)PUT方法的实现
PUT请求报文格式:
PUT请求报文
PUT方法具体实现:

void Post(String PostData,byte deviceID, byte sensorID){if (client.connect(server, port)) {    // Make a HTTP request:    client.print("PUT /v1.0/device/");    client.print(device);    client.print("/sensor/");    client.print(sensor);    client.print("/datapoint/");    client.print(dpkey);    client.println(" HTTP/1.1");    client.print("Host:");    client.println(server);    client.println("Content-Type:application/json");    client.print("U-ApiKey:");    client.println(ukey);    client.println("Connection: close");    client.print("Content-Length: ");    client.println(PostData.length()+1);    client.println();    client.println(PostData);  }   else {    // kf you didn't get a connection to the server:    Serial.println("connection failed");  }}

(3)GET方法的实现
GET请求报文格式:
GET请求报文
GET方法具体实现:

void Post(String PostData,byte deviceID, byte sensorID){if (client.connect(server, port)) {    // Make a HTTP request:    client.print("GET /v1.0/device/");    client.print(device);    client.print("/sensor/");    client.print(sensor);    client.print("/datapoint/");    client.print(dpkey);    client.println(" HTTP/1.1");    client.print("Host:");    client.println(server);    client.println("Accept:");    client.print("U-ApiKey:");    client.println(ukey);    client.println("Connection: close");    client.println();  }   else {    // kf you didn't get a connection to the server:    Serial.println("connection failed");  }}

(4)DELETE方法的实现
DELETE请求报文格式:
DELETE请求报文
DELETE方法具体实现:

void Post(String PostData,byte deviceID, byte sensorID){if (client.connect(server, port)) {    // Make a HTTP request:    client.print("DELETE /v1.0/device/");    client.print(device);    client.print("/sensor/");    client.print(sensor);    client.print("/datapoint/");    client.print(dpkey);    client.println(" HTTP/1.1");    client.print("Host:");    client.println(server);    client.println("Accept:");    client.print("U-ApiKey:");    client.println(ukey);    client.println("Connection: close");    client.println();  }   else {    // kf you didn't get a connection to the server:    Serial.println("connection failed");  }}
0 0