嵌入式linux连接onenet平台mqtt简单示例
这里分享一个基于onenet mqtt协议上传系统时间的示例
使用方法:
修改一下mqtt.c里面:
产品id、设备id、apikey
makefile里面:
交叉编译器
main.c
#include "mqtt.h"
#include "tcp.h"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include<time.h> //C语言的头文件
void print(unsigned char *s,int len)
{
int i = 0;
while(i < len)
{
printf("0x%x ",s[i]);
i++;
}
printf("\n");
}
int main()
{
unsigned char buff[100] = {
0},sendbuff[100] = {
0},send_len = 0;
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
MQTT_Init(); //edpÐÒé³õÊŒ»¯
tcp_init(); //tcp³õÊŒ»¯
//Ɯ̚ŒøÈšÉÏÏß
memset(sendbuff,0,100);
send_len = MQTT_conect(sendbuff);
tcp_send(sendbuff, send_len);
print(sendbuff,send_len);
while(1)
{
time(&now);
timenow = localtime(&now);
printf("get time:%s \n",asctime(timenow));
memset(buff,0,100);
sprintf(buff,"time,%s",asctime(timenow));//Ìî³äÐÒé
memset(sendbuff,0,100);
send_len = MQTT_updata( buff, strlen(buff));
print(buff,send_len);
tcp_send(buff, send_len);
sleep(2);
}
}
tcp.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
#include <string.h> //strcmp()µÈ×Ö·ûŽ®²Ù×÷º¯Êý
#include <stdlib.h> //atoi() ×Ö·ûŽ®×ªint
static int socket_fd = -1;
int tcp_init(void)
{
// 1 ŽŽœštcpÍšÐÅsocket
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
perror("socket failed!\n");
return -1;
}
struct sockaddr_in server_addr = {
0};
//·þÎñÆ÷µÄµØÖ·ÐÅÏ¢
server_addr.sin_family = AF_INET;//IPv4ÐÒé
server_addr.sin_port = htons(6002);//·þÎñÆ÷¶Ë¿ÚºÅ
server_addr.sin_addr.s_addr = inet_addr("183.230.40.39");//ÉèÖ÷þÎñÆ÷
//Á¬œÓ·þÎñÆ÷
int ret = connect(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(ret == -1)
{
perror("connect failed!\n");
return -1;
}
else
{
printf("connect server successful!\n");
}
return 0;
}
int tcp_send(char *buf,int len)
{
write(socket_fd, buf, len);//·¢ËÍÏûÏ¢
}
int tcp_rcv(char *buf,int len)
{
int ret = 0;
ret = recv(socket_fd, buf, len, 0);
if(ret < 0)
{
perror("recv\n");
close(socket_fd);
return -1;
}
return ret;
}
void close_tcp(void)
{
close(socket_fd);
socket_fd = -1;
}
volatile unsigned char DeviesID[15] = "585028018";
volatile unsigned char ProductID[15] = "319088";
volatile unsigned char AuthInfo[40] = "iosdugoiasd";
static unsigned char MQTT_CONENCT_BUF[100];
static unsigned char MQTT_SEND_BUF[200];
static unsigned int str_cnt(unsigned char *s)
{
unsigned char len = 0;
while((*s) != 0)
{
s++;
len++;
}
return len;
}
static void str_cpy(unsigned char *s1,unsigned char *s2,unsigned char len)
{
unsigned char i = 0;
while(i < len)
{
s1[i] = s2[i];
i++;
}
}
void MQTT_Init(void)
{
unsigned char DeviesID_len,ProductID_len,AuthInfo_len,tmp;
//Á¬œÓÏûÏ¢buf³õÊŒ»¯
DeviesID_len = str_cnt(DeviesID);
ProductID_len = str_cnt(ProductID);
AuthInfo_len = str_cnt(AuthInfo);
MQTT_CONENCT_BUF[0] = 0X10;//Á¬œÓÔÆÏûÏ¢ÀàÐÍ
MQTT_CONENCT_BUF[1] = DeviesID_len + ProductID_len + AuthInfo_len + 16;// remainedLength
MQTT_CONENCT_BUF[2] = 0X00;
MQTT_CONENCT_BUF[3] = 0X04;// protocolNameLength= 0X0004
MQTT_CONENCT_BUF[4] = 0X4D;
MQTT_CONENCT_BUF[5] = 0X51;
MQTT_CONENCT_BUF[6] = 0X54;
MQTT_CONENCT_BUF[7] = 0X54;//MQTT
MQTT_CONENCT_BUF[8] = 0X04;// protocolLevel=4
MQTT_CONENCT_BUF[9] = 0XC0;// userFlag=1 passwordFlag=1 willFlag=0 willRetainFlag=0 willQosFlag=0 clenSessionFlag=0 clenSessionFlag=0
MQTT_CONENCT_BUF[10] = 0X2C;
MQTT_CONENCT_BUF[11] = 0X01;// keepAlive=0X012C 300
MQTT_CONENCT_BUF[12] = 0x00;
MQTT_CONENCT_BUF[13] = DeviesID_len;
str_cpy(&MQTT_CONENCT_BUF[14], DeviesID, DeviesID_len);
tmp = DeviesID_len+14;
MQTT_CONENCT_BUF[tmp++] = 0x00;
MQTT_CONENCT_BUF[tmp++] = ProductID_len;
str_cpy(&MQTT_CONENCT_BUF[tmp], ProductID, ProductID_len);
tmp = tmp + ProductID_len;
MQTT_CONENCT_BUF[tmp++] = 0x00;
MQTT_CONENCT_BUF[tmp++] = AuthInfo_len;
str_cpy(&MQTT_CONENCT_BUF[tmp], AuthInfo, AuthInfo_len);
//ÉÏŽ«ÊýŸÝÏûÏ¢³õÊŒ»¯
MQTT_SEND_BUF[0] = 0x30;// MQTTÀàÐÍ=3 DupFlag=0 QosLevel=0 Retain=0
MQTT_SEND_BUF[1] = 0x00;//ÏûÏ¢³€¶È ×Ö·ûŽ®ÏûÏ¢³€¶È+5
MQTT_SEND_BUF[2] = 0x00;
MQTT_SEND_BUF[3] = 0x03; // TopicNameLen=0x0003
MQTT_SEND_BUF[4] = 0x24;
MQTT_SEND_BUF[5] = 0x64;
MQTT_SEND_BUF[6] = 0x70;// TopicName=$dp
MQTT_SEND_BUF[7] = 0x05;// DpType=5 TimeSet=0
MQTT_SEND_BUF[8] = 0x00;
MQTT_SEND_BUF[9] = 0x00;
MQTT_SEND_BUF[10] = 0x2C;
MQTT_SEND_BUF[11] = 0x3B;
//ºóÃæÊÇ×Ö·ûŽ®ÏûÏ¢
//---
}
unsigned int MQTT_conect(unsigned char *buf)
{
unsigned int len = 0;
//MQTT_Init();
len = MQTT_CONENCT_BUF[1] + 2;
str_cpy(buf, MQTT_CONENCT_BUF, len);
return len;
}
unsigned int MQTT_updata(unsigned char *buf,unsigned int len)
{
unsigned char i = 0;
unsigned int len1 = 0;
while(i < len)
{
MQTT_SEND_BUF[(i+12)] = buf[i];
i++;
}
MQTT_SEND_BUF[9] = len + 2;
MQTT_SEND_BUF[1] = len + 10;
len1 = (MQTT_SEND_BUF[1] + 2);
str_cpy(buf,MQTT_SEND_BUF,len1);
return len1;
}
unsigned int MQTT_ping(unsigned char *s)
{
s[0] = 0xc0;
s[1] = 0x00;
return 2;
}
tcp.h
#ifndef __TCP_H
#define __TCP_H
int tcp_init(void);
int tcp_send(char *buf,int len);
int tcp_rcv(char *buf,int len);
void close_tcp(void);
#endif
mqtt.h
#ifndef __MQTT_H
#define __MQTT_H
int MQTT_init(void);
unsigned int MQTT_conect(unsigned char *buf);
unsigned int MQTT_updata(unsigned char *buf,unsigned int len);
#endif
makefile
#交叉编译工具链
#CC=arm-linux-gnueabihf-gcc
CC=gcc
#链接库
CFLAGS=-lm -lpthread
#获取.cpp文件
SrcFiles=$(wildcard *.c)
#使用替换函数获取.o文件
OBJS=$(patsubst %.c,%.o,$(SrcFiles))
all:main
main:$(OBJS)
$(CC) -o $@ $^ $(CFLAGS)
%.o:%.c
$(CC) -c $< $(CFLAGS)
.PHONY:clean all
clean:
rm -rf $(OBJS)
rm -rf main