(转)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)和有名管道( ...
随机推荐
- 01.SQLServer性能优化之---水平分库扩展
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitian/ ...
- nodejs进阶(5)—接收请求参数
1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...
- PC分配盘符的时候发现==》RPC盘符不可用
服务器汇总:http://www.cnblogs.com/dunitian/p/4822808.html#iis 服务器异常: http://www.cnblogs.com/dunitian/p/45 ...
- MyEclipse生成注册码
今天正在使用的MyEclipse出现了使用过期,在网上发现一个可以生成注册码的程序,现在分享给各位. /** * myEclipse生成注册码 * 点击顶部:MyEclipse --> subs ...
- 设计模式之行为类模式大PK
行为类模式大PK 行为类模式包括责任链模式.命令模式.解释器模式.迭代器模式.中介者模式.备忘录模式.观察者模式.状态模式.策略 ...
- WCF基础
初入职场,开始接触C#,开始接触WCF,那么从头开始学习吧,边学边补充. SOA Service-Oriented Architecture,面向服务架构,粗粒度.开放式.松耦合的服务结构,将应用程序 ...
- EC笔记:第4部分:22、所有成员都应该是private的
EC笔记:第4部分:22.所有成员都应该是private的 更简单的访问 用户不用记得什么时候该带上括号,什么时候不用带上括号(因为很确定的就要带上括号) 访问限制 对于public的成员变量,我们可 ...
- android手机登录时遇到“QQ安全登录发现病毒”解决
android手机作为开源系统非常容易感染病毒,有时候我们会经常遇到手机QQ登录时检测到app被感染,一般情况是由手机感染病毒所引起的,安装腾讯管家后只能检测病毒和卸载感染病毒的软件,不能清除病毒.解 ...
- 信息安全-2:python之hill密码算法[原创]
转发注明出处:http://www.cnblogs.com/0zcl/p/6106513.html 前言: hill密码算法我打算简要介绍就好,加密矩阵我用教材上的3*3矩阵,只做了加密,解密没有做, ...
- CentOS上 Mono 3.2.8运行ASP.NET MVC4经验
周一到周三,折腾了两天半的时间,经历几次周折,在小蝶惊鸿的鼎力帮助下,终于在Mono 3.2.8上运行成功MVC4.在此总结经验如下: 系统平台的版本: CentOS 6.5 Mono 3.2.8 J ...