前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl

话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树。 于是花重金购买了上等的文房四宝。    
一日,饭后突生雅兴,一番磨墨拟纸,  并点上了上好的檀香,颇有王羲之风范,  又具颜真卿气势,定神片刻,泼墨挥毫,    
郑重地写下一行字:libcurl.

由此可知libcurl的刻骨铭心!
官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

刚接触 libcurl的时候, 自然的想到按照常规的思路来连接云端: 先连接Web, 再发送请求, 然后等待接受返回数据. 
这样思路的代码如下

点击(此处)折叠或打开

  1. /*----------------------------------------------------------------------------------------------------
  2. 名称: http_cloud_curl_simple.c
  3. 功能: 利用libcurl的API顺序实现云端通讯, 未利用回调机制
  4. -----------------------------------------------------------------------------------------------------*/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <curl/curl.h>
  9. #include <assert.h>
  10. #include "../http_cloud.h"
  11. #define DBG printf
  12. //-----------------------------------------------------------------------------------------
  13. static void get_local_time(char *pc_str)
  14. {
  15. time_t now;
  16. struct tm *timenow;
  17. assert(pc_str != NULL);
  18. time(&now);
  19. timenow = localtime(&now);
  20. sprintf(pc_str, "%04d-%02d-%02dT%02d:%02d:%02d", timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday,
  21. timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
  22. }
  23. /* Auxiliary function that waits on the socket. */
  24. static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
  25. {
  26. struct timeval tv;
  27. fd_set infd, outfd, errfd;
  28. int res;
  29. tv.tv_sec = timeout_ms / 1000;
  30. tv.tv_usec= (timeout_ms % 1000) * 1000;
  31. FD_ZERO(&infd);
  32. FD_ZERO(&outfd);
  33. FD_ZERO(&errfd);
  34. FD_SET(sockfd, &errfd); /* always check for error */
  35. if(for_recv) {
  36. FD_SET(sockfd, &infd);
  37. }
  38. else {
  39. FD_SET(sockfd, &outfd);
  40. }
  41. /* select() returns the number of signalled sockets or -1 */
  42. res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  43. return res;
  44. }
  45. static char connect_cloud(char *pc_ret, const char *host_addr, const int portno, const char *request)
  46. {
  47. CURL *curl;
  48. CURLcode res;
  49. /* Minimalistic http request */
  50. //const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  51. //char request[1024] = "";
  52. curl_socket_t sockfd; /* socket */
  53. long sockextr;
  54. size_t iolen;
  55. curl_off_t nread;
  56. int iLen = 0;
  57. char cRet = 0;
  58. assert((pc_ret != NULL) && (host_addr != NULL) && (request != NULL));
  59. //curl_global_init(CURL_GLOBAL_DEFAULT);
  60. curl = curl_easy_init();
  61. if (!curl)    return 0;
  62. //curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
  63. curl_easy_setopt(curl, CURLOPT_URL, host_addr);
  64. /* Do not do the transfer - only connect to host */
  65. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  66. res = curl_easy_perform(curl);
  67. if(CURLE_OK != res) {
  68. printf("Error: %s\n", strerror(res));
  69. return -1;
  70. }
  71. /* Extract the socket from the curl handle - we'll need it for waiting.
  72. * Note that this API takes a pointer to a 'long' while we use
  73. * curl_socket_t for sockets otherwise.
  74. */
  75. res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
  76. if(CURLE_OK != res) {
  77. printf("Error: %s\n", curl_easy_strerror(res));
  78. return -2;
  79. }
  80. sockfd = sockextr;
  81. /* wait for the socket to become ready for sending */
  82. if(!wait_on_socket(sockfd, 0, 60000L)) {
  83. printf("Error: timeout.\n");
  84. return -3;
  85. }
  86. /* Send the request. Real applications should check the iolen
  87. * to see if all the request has been sent */
  88. res = curl_easy_send(curl, request, strlen(request), &iolen);
  89. if(CURLE_OK != res) {
  90. printf("Error: %s\n", curl_easy_strerror(res));
  91. return -4;
  92. }
  93. /* read the response */
  94. char buf[1024];
  95. while(1) {
  96. wait_on_socket(sockfd, 1, 60000L);
  97. res = curl_easy_recv(curl, buf, 1024, &iolen);
  98. if(CURLE_OK != res)
  99. break;
  100. nread = (curl_off_t)iolen;
  101. }
  102. buf[nread] = '\0';
  103. DBG("Received (%" CURL_FORMAT_CURL_OFF_T "), %s\n", nread, buf);
  104. /* always cleanup */
  105. curl_easy_cleanup(curl);
  106. pc_ret = strstr(buf, "\r\n\r\n");
  107. if (pc_ret) {
  108. pc_ret += 4;
  109. DBG("ret = %s\n", pc_ret);
  110. }
  111. return 1;
  112. }
  113. #if (YEELINK == 1)
  114. int yeelink_create_data(const int device_id, const int sensor_id, const float device_value)
  115. {
  116. char pc_ret[200], request[1024], pc_json[100], pc_time[30], pc_host_file[100], pc_header[100], ret;
  117. int len;
  118. sprintf(pc_host_file, "v1.0/device/%d/sensor/%d/datapoints", device_id, sensor_id);
  119. sprintf(pc_header, "U-ApiKey: %s", YEELINK_API_KEY);
  120. get_local_time(pc_time);
  121. sprintf(pc_json, "{\"timestamp\":\"%s\",\"value\":%.2f}", pc_time, device_value);
  122. len = strlen(pc_json);
  123. 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",
  124. pc_host_file, YEELINK_HOST, pc_header, len, pc_json);
  125. DBG("request = %s\n", request);
  126. ret = connect_cloud(pc_ret, YEELINK_HOST, YEELINK_PORT, request);
  127. return(ret);
  128. }
  129. #endif
  130. #if (LEWEI50 == 1)
  131. //curl --request POST http://www.lewei50.com/api/V1/Gateway/UpdateSensors/01 --data "[{\"Name\":\"T1\",\"Value\":\"23.08\"}]" --header "userkey:36be8ff22f794f1e8a0bee3336eef237"
  132. int lewei50_create_data(const char *device_id, const float device_value)
  133. {
  134. char pc_ret[200], request[1024], pc_json[100], pc_header[100], ret;
  135. int len;
  136. assert(device_id != NULL);
  137. sprintf(pc_header, "userkey: %s", LEWEI50_USER_KEY);
  138. sprintf(pc_json, "[{\"Name\":\"%s\",\"Value\":\"%.2f\"}]", device_id, device_value);
  139. len = strlen(pc_json);
  140. 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",
  141. LEWEI50_HOST_FILE, LEWEI50_HOST, pc_header, len, pc_json);
  142. DBG("request = %s\n", request);
  143. ret = connect_cloud(pc_ret, LEWEI50_HOST, LEWEI50_PORT, request);
  144. return(ret);
  145. }
  146. #endif
  147. //-------------------------------------------------------------------
  148. int main(void)
  149. {
  150. float f_value = 15.02;
  151. int i_tmp;
  152. time_t t;
  153. srand((unsigned)time(&t));    //初始化随机种子, 否则随机数不随机
  154. i_tmp = rand();
  155. i_tmp -= (i_tmp >> 4 << 4);
  156. f_value += i_tmp;
  157. #if (YEELINK == 1)
  158. yeelink_create_data(YEELINK_DEVICE_ID, YEELINK_SENSOR_ID, f_value);
  159. #endif
  160. #if (LEWEI50 == 1)
  161. lewei50_create_data(LEWEI50_DEVICE_ID, f_value);
  162. #endif
  163. return 1;
  164. }

Makefile: 注意openwrt的连接参数要多加的, 为了这几个字母, 折腾了好长时间

点击(此处)折叠或打开

  1. OPENWRT = 1
  2. ifeq ($(OPENWRT), 1)
  3. 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
  4. CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
  5. LFLAGS += -lcurl -lcrypto -lz -lssl
  6. else
  7. CC = gcc
  8. LFLAGS += -lcurl
  9. endif
  10. CFLAGS += -Wall -O2
  11. #CFLAGS += -g
  12. #可执行文件名和相关的obj文件
  13. APP_BINARY = http_cloud
  14. SRCS += http_cloud_curl_simple.c
  15. OBJS = $(SRCS:.c=.o)
  16. all: APP_FILE
  17. APP_FILE: $(OBJS)
  18. $(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS)
  19. .PHONY: clean
  20. clean:
  21. @echo "cleanning project"
  22. $(RM) *.a $(OBJS) *~ *.so *.lo $(APP_BINARY)
  23. @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最最简单的应用, 本着'师夷长技以制夷'的原则, 继续看官方文档中......

(转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)的更多相关文章

  1. (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)

    一.  HTTP请求的数据流总结#上传数据, yeelink的数据流如下POST /v1.0/device/4420/sensor/9089/datapoints HTTP/1.1Host: api. ...

  2. (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)

    1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...

  3. openwrt下和云端通讯的例程,

    openwrt下和云端通讯的例程 ibcurl 官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 刚接触 libcurl的时候 ...

  4. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  5. Ubuntu 14.04 下搭建SVN服务器 (转载自 http://www.linuxidc.com/Linux/2015-01/111956.htm)-------------我所用到是红色字体

    http://www.linuxidc.com/Linux/2015-01/111956.htm Ubuntu 14.04 下搭建SVN服务器 svn:// 安装软件包: sudo apt-get i ...

  6. Linux 进程间通讯

    一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  7. Ubuntu 13.04下安装WPS for Linux

    [日期:2013-06-03]   有人说Linux下不是有open office 和libre office么?是啊,可是将windows下的doc文档或者ppt放到Libreoffice上打开的时 ...

  8. 【转帖】Linux发行版:CentOS、Ubuntu、RedHat、Android、Tizen、MeeGo

     Linux发行版:CentOS.Ubuntu.RedHat.Android.Tizen.MeeGo作者:阳光岛主 原文在这儿 Linux,最早由Linus Benedict Torvalds在199 ...

  9. Linux 进程间通讯方式 pipe()函数 (转载)

    转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...

随机推荐

  1. java中文乱码解决之道(一)-----认识字符集

    沉寂了许久(大概有三个多月了吧),LZ"按捺不住"开始写博了! java编码中的中文问题是一个老生常谈的问题了,每次遇到中文乱码LZ要么是按照以前的经验修改,要么则是baidu.c ...

  2. JS的内建函数reduce

    @(js) reduce函数,是ECMAScript5规范中出现的数组方法.在平时的工作中,相信大家使用的场景并不多,一般而言,可以通过reduce方法实现的逻辑都可以通过forEach方法来变相的实 ...

  3. org.jboss.deployment.DeploymentException: Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=egmasDS

    17:34:37,235 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080 17:34:37,281 INFO [ ...

  4. Linux基础介绍【第七篇】

    linux用户分类 超级用户:UID=0,root 普通用户:UID 500起,由超级用户或具有超级用户权限的用户创建的用户. 虚拟用户:UID 1-499,为了满足文件或服务启动的需要而存在,一般都 ...

  5. SpringMVC 数据校验

    1.引入jar包 2.配置验证器 <!-- 配置验证器 --> <bean id="myvalidator" class="org.springfram ...

  6. MongoDB基础

    1.概念及特点 说明:由于部分语句中$ 符号无法正常显示,使用¥代表 概念 MongoDB是一个基于文档的分布式的开源的NoSQL数据库,文档的结构为BSON形式,每一个文档都有一个唯一的Object ...

  7. .NET面试题系列[6] - 反射

    反射 - 定义,实例与优化 在面试中,通常会考察反射的定义(操作元数据),可以用反射做什么(获得程序集及其各个部件),反射有什么使用场景(ORM,序列化,反序列化,值类型比较等).如果答得好,还可能会 ...

  8. 分享阿里云推荐码 IC1L2A,购买服务器可以直接打9折,另附阿里云服务器部署ASP.NET MVC5关键教程

    阿里云推荐码为:IC1L2A 阿里云还是不错滴. 以windows server 2008 R2为例,介绍如何从全新的服务器部署MVC5 站点. 新购买的阿里云服务器是没有IIS的,要安装IIS: 控 ...

  9. 探索 Linux 系统的启动过程

    引言 之所以想到写这些东西,那是因为我确实想让大家也和我一样,把 Linux 桌面系统打造成真真正正日常使用的工具,而不是安装之后试用几把再删掉.我是真的在日常生活和工作中都使用 Linux,比如在 ...

  10. C#委托使用详解(Delegates)

    摘要 委托是C#编程一个非常重要的概念,也是一个难点.本文将系统详细讲解委托. 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻.说实话,每个人都委托都有不同的见解,因为看问题的 ...