(转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)
前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl
话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树。 于是花重金购买了上等的文房四宝。
一日,饭后突生雅兴,一番磨墨拟纸, 并点上了上好的檀香,颇有王羲之风范, 又具颜真卿气势,定神片刻,泼墨挥毫,
郑重地写下一行字:libcurl.
官方文档请参考这里 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最最简单的应用, 本着'师夷长技以制夷'的原则, 继续看官方文档中......
(转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)的更多相关文章
- (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)
一. HTTP请求的数据流总结#上传数据, yeelink的数据流如下POST /v1.0/device/4420/sensor/9089/datapoints HTTP/1.1Host: api. ...
- (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)
1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...
- openwrt下和云端通讯的例程,
openwrt下和云端通讯的例程 ibcurl 官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 刚接触 libcurl的时候 ...
- [笔记]linux下和windows下的 创建线程函数
linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...
- 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 ...
- Linux 进程间通讯
一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Ubuntu 13.04下安装WPS for Linux
[日期:2013-06-03] 有人说Linux下不是有open office 和libre office么?是啊,可是将windows下的doc文档或者ppt放到Libreoffice上打开的时 ...
- 【转帖】Linux发行版:CentOS、Ubuntu、RedHat、Android、Tizen、MeeGo
Linux发行版:CentOS.Ubuntu.RedHat.Android.Tizen.MeeGo作者:阳光岛主 原文在这儿 Linux,最早由Linus Benedict Torvalds在199 ...
- Linux 进程间通讯方式 pipe()函数 (转载)
转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...
随机推荐
- 有朋友问了数据库ID不连续,怎么获取上一篇和下一篇的文章?(不是所有情况都适用)
呃 (⊙o⊙)…,逆天好久没写SQL了,EF用的时间长了,SQL都不怎么熟悉了......[SQL水平比较菜,大牛勿喷] 方法很多种,说个最常见的处理 因为id是自增长的,所以一般情况下下一篇文章的I ...
- ExtJS 4.2 业务开发(二)数据展示和查询
本篇开始模拟一个船舶管理系统,提供查询.添加.修改船舶的功能,这里介绍其中的数据展示和查询功能. 目录 1. 数据展示 2. 数据查询 3. 在线演示 1. 数据展示 在这里我们将模拟一个船舶管理系统 ...
- 步入angularjs directive(指令)--点击按钮加入loading状态
今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...
- ES6的一些常用特性
由于公司的前端业务全部基于ES6开发,于是给自己开个小灶补补ES6的一些常用特性.原来打算花两天学习ES6的,结果花了3天才勉强过了一遍阮老师的ES6标准入门(水好深,ES6没学好ES7又来了...) ...
- .Net Core上也可以使用的二维码组件
我Fork了QRCoder,并且兼容了.Net Core,图形库用的是ZKWeb.System.Drawing Github: https://github.com/zkweb-framework/Q ...
- python中IndentationError: expected an indented block错误的解决方法
IndentationError: expected an indented block 翻译为IndentationError:预期的缩进块 解决方法:有冒号的下一行要缩进,该缩进就缩进
- Android中点击事件的实现方式
在之前博文中多次使用了点击事件的处理实现,有朋友就问了,发现了很多按钮的点击实现,但有很多博文中使用的实现方式有都不一样,到底是怎么回事.今天我们就汇总一下点击事件的实现方式. 点击事件的实现大致分为 ...
- Python处理Excel表格
同事小王今天说他有一个Excel表格,表格如下,一列是姓名,一列是电话号码,总共有大概2000行数据. 有的姓名占了一行,有的占了两行,还有一些占了三行的.如下图: 他问我可不可以全部统一成一行,而且 ...
- Zookeeper常用命令
http://www.cnblogs.com/chengxin1982/p/3997706.html
- 【原】npm 常用命令详解
今年上半年在学习gulp的使用,对npm的掌握是必不可少的,经常到npm官网查询文档让我感到不爽,还不如整理了一些常用的命令到自己博客上,于是根据自己的理解简单翻译过来,终于有点输出,想学习npm这块 ...