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

####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失败,就使用这个IP
IPAddress 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值