openwrt下和云端通讯的例程,
官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
刚接触 libcurl的时候, 自然的想到按照常规的思路来连接云端: 先连接Web, 再发送请求, 然后等待接受返回数据.
这样思路的代码如下
点击(此处)折叠或打开
- /*----------------------------------------------------------------------------------------------------
- 名称: http_cloud_curl_simple.c
- 功能: 利用libcurl的API顺序实现云端通讯, 未利用回调机制
- -----------------------------------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <curl/curl.h>
- #include <assert.h>
- #include "../http_cloud.h"
- #define DBG printf
- //-----------------------------------------------------------------------------------------
- static void get_local_time(char *pc_str)
- {
- time_t now;
- struct tm *timenow;
- assert(pc_str != NULL);
- time(&now);
- timenow = localtime(&now);
- sprintf(pc_str, "%04d-%02d-%02dT%02d:%02d:%02d", timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday,
- timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
- }
- /* Auxiliary function that waits on the socket. */
- static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
- {
- struct timeval tv;
- fd_set infd, outfd, errfd;
- int res;
- tv.tv_sec = timeout_ms / 1000;
- tv.tv_usec= (timeout_ms % 1000) * 1000;
- FD_ZERO(&infd);
- FD_ZERO(&outfd);
- FD_ZERO(&errfd);
- FD_SET(sockfd, &errfd); /* always check for error */
- if(for_recv) {
- FD_SET(sockfd, &infd);
- }
- else {
- FD_SET(sockfd, &outfd);
- }
- /* select() returns the number of signalled sockets or -1 */
- res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
- return res;
- }
- static char connect_cloud(char *pc_ret, const char *host_addr, const int portno, const char *request)
- {
- CURL *curl;
- CURLcode res;
- /* Minimalistic http request */
- //const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
- //char request[1024] = "";
- curl_socket_t sockfd; /* socket */
- long sockextr;
- size_t iolen;
- curl_off_t nread;
- int iLen = 0;
- char cRet = 0;
- assert((pc_ret != NULL) && (host_addr != NULL) && (request != NULL));
- //curl_global_init(CURL_GLOBAL_DEFAULT);
- curl = curl_easy_init();
- if (!curl) return 0;
- //curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
- curl_easy_setopt(curl, CURLOPT_URL, host_addr);
- /* Do not do the transfer - only connect to host */
- curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
- res = curl_easy_perform(curl);
- if(CURLE_OK != res) {
- printf("Error: %s\n", strerror(res));
- return -1;
- }
- /* Extract the socket from the curl handle - we'll need it for waiting.
- * Note that this API takes a pointer to a 'long' while we use
- * curl_socket_t for sockets otherwise.
- */
- res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
- if(CURLE_OK != res) {
- printf("Error: %s\n", curl_easy_strerror(res));
- return -2;
- }
- sockfd = sockextr;
- /* wait for the socket to become ready for sending */
- if(!wait_on_socket(sockfd, 0, 60000L)) {
- printf("Error: timeout.\n");
- return -3;
- }
- /* Send the request. Real applications should check the iolen
- * to see if all the request has been sent */
- res = curl_easy_send(curl, request, strlen(request), &iolen);
- if(CURLE_OK != res) {
- printf("Error: %s\n", curl_easy_strerror(res));
- return -4;
- }
- /* read the response */
- char buf[1024];
- while(1) {
- wait_on_socket(sockfd, 1, 60000L);
- res = curl_easy_recv(curl, buf, 1024, &iolen);
- if(CURLE_OK != res)
- break;
- nread = (curl_off_t)iolen;
- }
- buf[nread] = '\0';
- DBG("Received (%" CURL_FORMAT_CURL_OFF_T "), %s\n", nread, buf);
- /* always cleanup */
- curl_easy_cleanup(curl);
- pc_ret = strstr(buf, "\r\n\r\n");
- if (pc_ret) {
- pc_ret += 4;
- DBG("ret = %s\n", pc_ret);
- }
- return 1;
- }
- #if (YEELINK == 1)
- int yeelink_create_data(const int device_id, const int sensor_id, const float device_value)
- {
- char pc_ret[200], request[1024], pc_json[100], pc_time[30], pc_host_file[100], pc_header[100], ret;
- int len;
- sprintf(pc_host_file, "v1.0/device/%d/sensor/%d/datapoints", device_id, sensor_id);
- sprintf(pc_header, "U-ApiKey: %s", YEELINK_API_KEY);
- get_local_time(pc_time);
- sprintf(pc_json, "{\"timestamp\":\"%s\",\"value\":%.2f}", pc_time, device_value);
- len = strlen(pc_json);
- sprintf(request, "POST
/%s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n%s\r\nContent-Length:
%d\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection:
Close\r\n\r\n%s\r\n", - pc_host_file, YEELINK_HOST, pc_header, len, pc_json);
- DBG("request = %s\n", request);
- ret = connect_cloud(pc_ret, YEELINK_HOST, YEELINK_PORT, request);
- return(ret);
- }
- #endif
- #if (LEWEI50 == 1)
- //curl --request POST http://www.lewei50.com/api/V1/Gateway/UpdateSensors/01 --data "[{\"Name\":\"T1\",\"Value\":\"23.08\"}]" --header "userkey:36be8ff22f794f1e8a0bee3336eef237"
- int lewei50_create_data(const char *device_id, const float device_value)
- {
- char pc_ret[200], request[1024], pc_json[100], pc_header[100], ret;
- int len;
- assert(device_id != NULL);
- sprintf(pc_header, "userkey: %s", LEWEI50_USER_KEY);
- sprintf(pc_json, "[{\"Name\":\"%s\",\"Value\":\"%.2f\"}]", device_id, device_value);
- len = strlen(pc_json);
- sprintf(request, "POST
/%s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n%s\r\nContent-Length:
%d\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection:
Close\r\n\r\n%s\r\n", - LEWEI50_HOST_FILE, LEWEI50_HOST, pc_header, len, pc_json);
- DBG("request = %s\n", request);
- ret = connect_cloud(pc_ret, LEWEI50_HOST, LEWEI50_PORT, request);
- return(ret);
- }
- #endif
- //-------------------------------------------------------------------
- int main(void)
- {
- float f_value = 15.02;
- int i_tmp;
- time_t t;
- srand((unsigned)time(&t)); //初始化随机种子, 否则随机数不随机
- i_tmp = rand();
- i_tmp -= (i_tmp >> 4 << 4);
- f_value += i_tmp;
- #if (YEELINK == 1)
- yeelink_create_data(YEELINK_DEVICE_ID, YEELINK_SENSOR_ID, f_value);
- #endif
- #if (LEWEI50 == 1)
- lewei50_create_data(LEWEI50_DEVICE_ID, f_value);
- #endif
- return 1;
- }
Makefile: 注意openwrt的连接参数要多加的, 为了这几个字母, 折腾了好长时间
点击(此处)折叠或打开
- OPENWRT = 1
- ifeq ($(OPENWRT), 1)
- CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
- CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
- LFLAGS += -lcurl -lcrypto -lz -lssl
- else
- CC = gcc
- LFLAGS += -lcurl
- endif
- CFLAGS += -Wall -O2
- #CFLAGS += -g
- #可执行文件名和相关的obj文件
- APP_BINARY = http_cloud
- SRCS += http_cloud_curl_simple.c
- OBJS = $(SRCS:.c=.o)
- all: APP_FILE
- APP_FILE: $(OBJS)
- $(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS)
- .PHONY: clean
- clean:
- @echo "cleanning project"
- $(RM) *.a $(OBJS) *~ *.so *.lo $(APP_BINARY)
- @echo "clean completed"
openwrt下的运行结果如下
root@OpenWrt:/xutest# ./http_cloud_libcurl
yeelini ret = HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Thu, 07 Nov 2013 00:56:56 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=dfvacsdpb8ls802dulnt695kk2; expires=Fri, 15-Nov-2013 08:56:56 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
lewei50 ret = HTTP/1.1 200 OK
Date: Thu, 07 Nov 2013 00:56:56 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 44
Connection: close
Cache-Control: private
Set-Cookie: SERVERID=a4a5b2bbca16d8c8b2ba6d5b6e55f36e|1383785816|1383785816;Path=/
{"Successful":true,"Message":"Successful. "}
root@OpenWrt:/xutest#
这仅仅是libcurl最最简单的应用, 本着'师夷长技以制夷'的原则, 继续看官方文档中.....
openwrt下和云端通讯的例程,的更多相关文章
- (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)
一. HTTP请求的数据流总结#上传数据, yeelink的数据流如下POST /v1.0/device/4420/sensor/9089/datapoints HTTP/1.1Host: api. ...
- (转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)
前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl 话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树. 于是花重金购买了上等的文房四宝. 一 ...
- (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)
1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...
- C#利用VUDP.cs开发网络通讯应用例程
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VUDP文档地址:https://github.com/velscode ...
- 第40章 CAN—通讯实验—零死角玩转STM32-F429系列
第40章 CAN—通讯实验—零死角玩转STM32-F429系列 第40章 CAN—通讯实验 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视 ...
- 第41章 RS-485通讯实验—零死角玩转STM32-F429系列
第41章 RS-485通讯实验 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- 关于k8s这项大动作,预示着边缘计算迎来“开源”发展的新周期……
在文章<最近在边缘计算领域,发生了一件足以载入物联网史册的大事…>我曾经提到Kubernetes(简称K8s)将从超大规模云计算环境,被带入到物联网边缘计算场景中. 事情有了新进展,从本周 ...
- 国内物联网平台(5):机智云IoT物联网云服务平台及智能硬件自助开发平台
国内物联网平台(5)——机智云IoT物联网云服务平台及智能硬件自助开发平台 马智 平台定位 机智云平台是致力于物联网.智能硬件云服务的开放平台.平台提供了从定义产品.设备端开发调试.应用开发.产测.运 ...
- 国内物联网平台初探(五) ——机智云IoT物联网云服务平台及智能硬件自助开发平台
平台定位 机智云平台是致力于物联网.智能硬件云服务的开放平台.平台提供了从定义产品.设备端开发调试.应用开发.产测.运营管理等覆盖智能硬件接入到运营管理全生命周期服务的能力. 机智云平台为开发者提供了 ...
随机推荐
- BZOJ1532: [POI2005]Kos-Dicing
1532: [POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1060 Solved: 321[Submit][St ...
- NET分布式缓存Memcached测试体验
原文地址:http://onlyonewt.blog.sohu.com/160168896.html 一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测 ...
- java.sql.SQLException:指定了无效的 Oracle URL
java.sql.SQLException:指定了无效的 Oracle URL 昨天晚上用MyEclipse连接Oracle,出现了" java.sql.SQLException: 指定了无 ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...
- PHP5生成图形验证码(有汉字)
利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor 新建一个真彩色图像 imagecolora ...
- 利用JS实现简单的瀑布流效果
哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...
- 解决linux不能使用chmod更改权限的问题
本人安装的是win10和ubuntu的双系统,发现在ubuntu下挂载windows硬盘不用命令chmod更改文件的权限,解决方法记录如下: 对于使用命令$ chmod 777 dirname更改不了 ...
- LB集群
LB集群 1. LB.LVS介绍LB集群是load balance 集群的简写,翻译成中文就是负载均衡集群LVS是一个实现负载均衡集群的开源软件项目 LVS架构从逻辑上可分为调度层(Directo ...
- FileZilla 安装配置参考
http://www.admin10000.com/document/72.html 解决 NAT issue https://wiki.filezilla-project.org/Network_C ...
- JS高级程序设计学习笔记之RegExp类型
创建正则表达式: 字面量形式定义正则表达式: Var expression = / pattern /flags ;pattern部分可以使任意简单或复杂的正则表达式.每个正则表达式可以带有一个或多个 ...