开发环境:Virtualbox ubuntu 14.04

使用说明:

1.下载使用安装cJSON
2.将贝壳物联对应设备的ID和APIKEY及数据接口DID替换后,直接make运行#define PASSWORD    "********"    //管理员密码,可以有权限执行关机指令等
3.在"请输入指令"/“输入自定义指令”处,输入“reboot”,即可远程重启电脑
4.增加串口使用,可以与arduino配合使用控制继电器等
5.增加数据上报,可以采集温湿度等传感器数据做条件控制

bigiot.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <cjson/cJSON.h>#include <fcntl.h>
#include <termios.h>#define DOMAIN    "www.bigiot.net"
#define PORT    8181
#define IP      "121.42.180.30"
#define ID      "xxxx"
#define APIKEY  "xxxxxxxx"#define DID     "xxxxx"#define PASSWORD   "xxxxxxxx"#define DEV_NAME    "/dev/ttyUSB0"void *pthread_keepalive(void *);
void *pthread_handler(void *);
void *pthread_serialport(void *);
void *pthread_upload(void *);int fd;
void init_serialport(void);
int len_str_cmd;
unsigned char *str_cmd[1024];
int len;struct task {pthread_t tidp;int micro_seconds;void * (*start_rtn)(void *);
};struct task task_tbl[] = {{ 0, 30000000, pthread_keepalive },{ 0,   100000, pthread_handler },{ 0,    10000, pthread_serialport },{ 0, 60000000, pthread_upload },
};int s;
int ret;
struct sockaddr_in bigiot_addr;
char buf[1024];
cJSON *cjson = NULL;
char *str_cjson = NULL;void show_cjson(void)
{cjson = cJSON_Parse(buf);str_cjson = cJSON_PrintUnformatted(cjson);printf("%s\n", str_cjson);
}struct cmd_oper {char *cmd;void (* fun)();
};void reboot(void)
{system("echo "PASSWORD" | sudo -S reboot");
}struct cmd_oper cmd_oper_tbl[] = {{ "reboot", reboot },
};void cmd_handler(void)
{char *method = NULL;char *cmd = NULL;int i;cjson = cJSON_Parse(buf);
#if 0printf("%s\n", cJSON_GetObjectItem(cjson, "M")->valuestring);printf("%s\n", cJSON_GetObjectItem(cjson, "ID")->valuestring);printf("%s\n", cJSON_GetObjectItem(cjson, "NAME")->valuestring);printf("%s\n", cJSON_GetObjectItem(cjson, "C")->valuestring);printf("%s\n", cJSON_GetObjectItem(cjson, "T")->valuestring);
#elsemethod = cJSON_GetObjectItem(cjson, "M")->valuestring;if (0 == strcmp(method, "say")) {cmd = cJSON_GetObjectItem(cjson, "C")->valuestring;for (i = 0; i < sizeof(cmd_oper_tbl) / sizeof(cmd_oper_tbl[0]); i ++) {if (0 == strcmp(cmd, cmd_oper_tbl[i].cmd)) {cmd_oper_tbl[i].fun();break;}}len_str_cmd = strlen(cmd);memcpy(str_cmd, cmd, len_str_cmd);if (0 == len_str_cmd) {// do_nothing();} else {len = 0;len = write(fd, str_cmd, len_str_cmd);if (len > 0) {printf("cmd send ok!\n");}len_str_cmd = 0;write(fd, '\n', 1);}}
#endif
}int main(int argc, char *argv[])
{cJSON *checkin = NULL;char *str_checkin = NULL;int i;s = socket(AF_INET, SOCK_STREAM, 0);if (s < 0) {exit(-1);}bigiot_addr.sin_family = AF_INET;bigiot_addr.sin_port = htons(PORT);#if 0bigiot_addr.sin_addr.s_addr = inet_addr(IP);
#elsestruct hostent *h;h = gethostbyname(DOMAIN);printf("ip:");for (i = 0; h->h_addr_list[i]; i ++) {printf("%s\t", inet_ntoa(*(struct in_addr*)(h->h_addr_list[i])));}printf("\n");char *ip = inet_ntoa(*((struct in_addr *)h->h_addr_list[0]));bigiot_addr.sin_addr.s_addr = inet_addr(ip);
#endifret = connect(s, (const struct sockaddr *)&bigiot_addr, sizeof(bigiot_addr));if (ret < 0) {exit(-2);} memset(buf, 0, sizeof(buf));ret = recv(s, buf, sizeof(buf), 0);if (ret > 0) {show_cjson();}checkin = cJSON_CreateObject();if (NULL == checkin) {printf("cJSON error!\n");exit(-1);}cJSON_AddStringToObject(checkin, "M", "checkin"); cJSON_AddStringToObject(checkin, "ID", ID);cJSON_AddStringToObject(checkin, "K", APIKEY);str_checkin = cJSON_PrintUnformatted(checkin);if (NULL == str_checkin) {printf("cJSON error!\n");exit(-1);}strcat(str_checkin, "\n");ret = send(s, str_checkin, strlen(str_checkin), 0);if (ret < 0) {printf("send error!\n");exit(-3);}printf("%s", str_checkin);ret = recv(s, buf, sizeof(buf), 0);if (ret > 0) {show_cjson();}init_serialport();for (i = 0; i < sizeof(task_tbl) / sizeof(task_tbl[0]); i ++) {ret = pthread_create(&task_tbl[i].tidp,NULL,task_tbl[i].start_rtn,&task_tbl[i].micro_seconds);if (ret) {printf("Create pthread error:%d\n", i);exit(-1);}}for (i = 0; i < sizeof(task_tbl) / sizeof(task_tbl[0]); i ++) {pthread_join(task_tbl[i].tidp, NULL);}  close(s);return 0;
}void *pthread_keepalive(void *arg)
{cJSON *beat = NULL;char *str_beat = NULL;beat = cJSON_CreateObject();if (NULL == beat) {printf("cJSON error!\n");exit(-1);}cJSON_AddStringToObject(beat, "M", "beat");str_beat = cJSON_PrintUnformatted(beat);if (NULL == str_beat) {printf("cJSON error!\n");exit(-1);}strcat(str_beat, "\n");while (1) {ret = send(s, str_beat, strlen(str_beat), 0);if (ret < 0) {printf("send error!\n");exit(-3);}printf("%s", str_beat);usleep(*(int *)arg);}return NULL;
}void *pthread_handler(void *arg)
{cJSON *cjson = NULL;char *str_cjson = NULL;while (1) {memset(buf, 0, sizeof(buf));ret = recv(s, buf, sizeof(buf), 0);if (ret > 0) {show_cjson();cmd_handler();}usleep(*(int *)arg);}return NULL;
}void *pthread_serialport(void *arg)
{int len;char data;while (1) {len = 0;len = read(fd, &data, 1);if (len > 0) {printf("%c", data);}usleep(*(int *)arg);}return NULL;
}void init_serialport(void)
{struct termios opt;fd = open(DEV_NAME, O_RDWR);if (fd < 0) {perror(DEV_NAME);return;}tcgetattr(fd, &opt);
#if 0     printf("%x\n", opt.c_iflag);printf("%x\n", opt.c_oflag);printf("%x\n", opt.c_cflag);printf("%x\n", opt.c_lflag);
#endif     opt.c_iflag = 0x500;opt.c_oflag = 0x5;opt.c_cflag = 0x1cb2;opt.c_lflag = 0x8a33;opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);opt.c_iflag &= ~(INLCR | ICRNL | IGNCR | IXON);opt.c_oflag &= ~(ONLCR | OCRNL);tcsetattr(fd, TCSANOW, &opt);tcgetattr(fd, &opt);usleep(200000);tcflush(fd, TCIFLUSH);    //清空输入缓存tcflush(fd, TCOFLUSH);    //清空输出缓存tcflush(fd, TCIOFLUSH);   //清空输入输出缓存
}void *pthread_upload(void *arg)
{unsigned char temp = 0;char str_buf[1024];while (1) {cJSON *data = NULL;char *str_data = NULL;data = cJSON_CreateObject();if (NULL == data) {printf("cJSON error!\n");exit(-1);      } sprintf(str_buf, "%d", temp);   cJSON_AddStringToObject(data, DID, str_buf);cJSON *update = NULL;char *str_update = NULL;update = cJSON_CreateObject();if (NULL == update) {printf("cJSON error!\n");exit(-1);}cJSON_AddStringToObject(update, "M", "update");cJSON_AddStringToObject(update, "ID", ID);cJSON_AddItemToObject(update, "V", data);str_update = cJSON_PrintUnformatted(update);if (NULL == str_update) {printf("cJSON error!\n");exit(-1);}strcat(str_update, "\n");ret = send(s, str_update, strlen(str_update), 0);if (ret < 0) {printf("send error!\n");exit(-3);}printf("%s", str_update);temp ++;usleep(*(int *)arg);}return NULL;
}

Arduino UNO:

void setup() {// put your setup code here, to run once:Serial.begin(115200);Serial.println("hello, bigiot!");
}/**offOn play  stopminus up    plusleft  pause rightbackwarddown  forward*/
struct cmd_oper {char *cmd;void (* fun)();
};void fun_offOn(void);
void fun_play(void);
void fun_stop(void);
void fun_minus(void);
void fun_up(void);
void fun_plus(void);
void fun_left(void);
void fun_pause(void);
void fun_right(void);
void fun_backward(void);
void fun_down(void);
void fun_forward(void);struct cmd_oper cmd_oper_tbl[] = {{ "offOn",fun_offOn },{ "play",fun_play },{ "stop",fun_stop },{ "minus",fun_minus },{ "up",fun_up },{ "plus",fun_plus },{ "left",fun_left },{ "pause",fun_pause },{ "right",fun_right },{ "backward",fun_backward },{ "down",fun_down },{ "forward",fun_forward },
}; void loop() {// put your main code here, to run repeatedly:if (Serial.available() > 0) {String rx_buf;rx_buf = Serial.readString();//Serial.print(rx_buf);for (int i = 0; i < sizeof(cmd_oper_tbl) / sizeof(cmd_oper_tbl[0]); i ++) {//Serial.println(cmd_oper_tbl[i].cmd);if (0 == memcmp(rx_buf.c_str(), cmd_oper_tbl[i].cmd, strlen(cmd_oper_tbl[i].cmd))) {cmd_oper_tbl[i].fun();break;}}}
}void fun_offOn(void)
{Serial.println(__FUNCTION__);
}void fun_play(void)
{Serial.println(__FUNCTION__);
}void fun_stop(void)
{Serial.println(__FUNCTION__);
}void fun_minus(void)
{Serial.println(__FUNCTION__);
}void fun_up(void)
{Serial.println(__FUNCTION__);
}void fun_plus(void)
{Serial.println(__FUNCTION__);
}void fun_left(void)
{Serial.println(__FUNCTION__);
}void fun_pause(void)
{Serial.println(__FUNCTION__);
}void fun_right(void)
{Serial.println(__FUNCTION__);
}void fun_backward(void)
{Serial.println(__FUNCTION__);
}void fun_down(void)
{Serial.println(__FUNCTION__);
}void fun_forward(void)
{Serial.println(__FUNCTION__);
}

xxxxxxxx需要替换为root账户密码

Makefile

all:gcc -g bigiot.c -o bigiot -lpthread -lcjson -lmexport LD_LIBRARY_PATH=/usr/local/lib$(shell echo xxxxxxxx | sudo -S chmod 777 /dev/ttyUSB0)./bigiotrm -rf bigiotclean:rm -rf bigiot

gethostbyname的域名解析问题

Linux下c语言模拟贝壳物联设备在线相关推荐

  1. 通过贝壳物联,让小度或天猫精灵语音控制你的8266

    我用的是网上继电器模块上可以插8266-01S那种5V取电,自带5转3.3电源,模块是安装在顶灯里面的.原始开关还在,考虑到单火取电难度太大,主要是8266功率太大了.所以既能在开关处控制灯,也能智能 ...

  2. STM32F103驱动ESP8266连接贝壳物联——②串口助手模拟连接贝壳物联

    所需的AT指令 连接贝壳物联平台 //连接贝壳物联1. AT2. AT+RST3. AT4. AT+CWMODE=15. AT+CWJAP="QQ","qwertyuio ...

  3. Arduino上传数据至贝壳物联并与FPGA进行交互

    本篇实现Arduino与FPGA交互,当然也没有什么新的协议,还是基于串口通讯,现在学一个串口通信基本上可以驱动大多数模块了,而且和各种单片机无缝数据交互,Arduino由于其强大的库函数支持,在实现 ...

  4. 利用贝壳物联远程控制树莓派或windows关机、重启

    最近闲来无事,想起以前在esp8266上对接过贝壳物联,可以远程发送指令控制设备等,可以将这个功能移植到树莓派上实现远程关机.重启等操作,所以使用go语言写了个程序分别实现了在windows和linu ...

  5. Arduino--ESP8266物联网WIFI模块(贝壳物联)--数据上传服务器(单数据接口)

    一.简介 随着移动物联网的发展,各场景下对于物联控制.数据上传.远程控制的诉求也越来越多,基于此乐鑫科技推出了便宜好用性价比极高的wifi物联模块--ESP8266,话不多少我们先来看看这个神奇的模块 ...

  6. MicroPython_ESP8266_IoT——第四回 初入联网(接入了贝壳物联)

    第四回 初入联网(接入了贝壳物联) 本来计划先把所有的硬件介绍完,再介绍如何介入贝壳物联的.但是那样就比较枯燥,还是先尝试接入贝壳物联,来增加ESP8266模块的可玩性. 需要了解[贝壳物联平台通信协 ...

  7. 【esp8266】④esp8266对接贝壳物联平台

    源码github地址:https://github.com/linzhongpaihuai/smartplug ①烧录方法:https://blog.csdn.net/u010177891/artic ...

  8. 简单的单片机链接贝壳物联实时上传数据

    简单的单片机链接贝壳物联实时上传数据 想要实现单片机和贝壳物联云平台的简单的数据上传其实非常简单, 代码如最后一个模块所示 准备 硬件 1.ESP8266芯片(ESP8266-01或ESP8266-0 ...

  9. 贝壳物联平台通讯协议

    贝壳物联平台通讯协议 贝壳物联平台通讯协议TCP.UDP.HTTP.Websocket说明,设备登录服务器,收发命令,发送实时数据,上传图片等协议. 首页 帮助文档 API文档 目录 一.概述 二.通 ...

最新文章

  1. mysql dba工作笔记pdf_社区专家在线:Oracle数据库、MySQL、Db2 等数据库日常运维故障与性能调优在线答疑...
  2. 中文名称:案例编程MOOK系列
  3. python中factor函数_Python入门-函数
  4. MYSQL 5.7 INNODB 表空间
  5. mysql 参数 列 排序_将参数放入MySQL IN()后,按降序对列进行排序?
  6. 自动更改IP地址反爬虫封锁,支持多线程(转)
  7. unity界面按钮的位置
  8. 电子元件-继电器知识汇总
  9. PyCharm自定义背景图片、更换主题
  10. 安徽农业大学计算机考研分数线,安徽农业大学历年考研分数线汇总[2012-2021]
  11. 物理地址是怎么确定的?以8086为例
  12. 学成在线项目(HTML+CSS)
  13. 手机显示服务器迁移中是什么意思,服务器迁移注意什么?什么是服务器迁移?...
  14. BufferedWriter的用法
  15. 力扣股神之路动态规划
  16. ECharts天气预报折线图
  17. 关于Yolov5在测试时,图像大小被调整的问题
  18. R(一): R基础知识
  19. QT+PCL+VS制作点云显示界面(彩色显示xyz点云)
  20. vue百度地图 一进页面加载卫星图

热门文章

  1. js折线图设置y轴刻度_d3.js实现自定义多y轴折线图
  2. linux去除内容重复行,Linux删除文本中的重复行 - 米扑博客
  3. STM8S003F3 使用定时器来计算方波周期的方法
  4. 高德地图可视化2.0封装(飞线,圆点,热力图)
  5. CC2640R2F BLE5.0 CC2640R2F软件架构
  6. 最帅爬虫_豆瓣读书(加密数据获取)
  7. php array的用法,PHP array() 函数用法及示例
  8. 奔骝定位摄影作品之LAS200
  9. for key in file_keys
  10. 星巴克——starbuck