Commit 848760a0 authored by jkingwen's avatar jkingwen

none

parent e0b3877c
#include "ip.h"
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN];
int ipaddr() {
if (getifaddrs(&ifap) == -1) {
perror("getifaddrs() error");
return -1;
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *) ifa->ifa_addr;
inet_ntop(AF_INET, &sa->sin_addr, ip_address, sizeof(ip_address));
//printf("%s: %s ", ifa->ifa_name, ip_address);
}
}
printf("ip:%s\n",ip_address);
freeifaddrs(ifap);
}
#ifndef __IP_H__
#define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#endif
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
struct mosquitto *mosq;
void Delay_Ms()
{
struct timespec ts;
ts.tv_sec = 0; // 秒
ts.tv_nsec = 100000000; // 1毫秒 = 1000000纳秒
nanosleep(&ts,NULL);
}
void *serial_usart1(void *arg)
{
serial_Receive();
return NULL;
}
void *AppExit(void *arg)
{
unsigned char bufpwm1[3]={0x01,0x01,0x00};
unsigned char bufpwm2[3]={0x01,0x02,0x00};
unsigned char bufpwm3[3]={0x01,0x03,0x00};
while(1)
{
Delay_Ms();
gPwmCount++;
if(gPwmCount>20)
{
gPwmCount=21;
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
}
}
return NULL;
}
int main(int argc, char *argv[]) {
//struct mosquitto *mosq;
int rc=0;
serial_Init();
gStart=time(NULL);
// 初始化 mosquitto 库
mosquitto_lib_init();
// 创建 mosquitto 客户端
mosq = mosquitto_new(CLIENT_ID, true, NULL);
if (!mosq) {
fprintf(stderr, "Failed to create mosquitto client\n");
return EXIT_FAILURE;
}
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
// 设置用户名和密码
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// 连接到 MQTT 代理
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
if (rc != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
mosquitto_destroy(mosq);
return EXIT_FAILURE;
}
ipaddr();
mosquitto_publish(mosq, NULL, TOPIC, strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
pthread_t thread[2];
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{
perror("Failed to create thread 1");
return 1;
}
if(pthread_create(&thread[1],NULL,AppExit,NULL)!=0)
{
perror("Failed to create thread 2");
return 1;
}
// 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
mosquitto_loop_forever(mosq, -1, 1000); // -1 表示无限等待,1 表示处理消息的最大数量
// 清理
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
// 等待线程1结束
pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL);
return EXIT_SUCCESS;
}
\ No newline at end of file
#include "mqtt.h"
time_t gStart;
int gPwmCount = 0; // 计数
void on_connect(struct mosquitto *mosq, void *obj, int rc)
{
if (rc == 0)
{
printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0);
time_t start=time(NULL);
}
else
{
fprintf(stderr, "Connection failed with code %d\n", rc);
}
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {
time_t end = time(NULL);
if(gStart+5>end)
{
printf("正在处理过期消息");
return;
}
// 确保消息有效
if (message->payload && message->payloadlen)
{
// 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1);
if (payload_str)
{
memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0';
// 解析 JSON
cJSON *json = cJSON_Parse(payload_str);
if (json == NULL)
{
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
}
else
{
// 提取 head 对象
cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head))
{
return;
}
// 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type))
{
return;
}
cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body))
{
return;
}
printf("message_type: %d\n", message_type->valueint);
if (message_type->valueint==3)
{ //pwm控制
// 提取 pin_ctrl_req 对象
cJSON *pwm_ctrl_req = cJSON_GetObjectItem(body, "p_ctrl_req");
if (!cJSON_IsObject(pwm_ctrl_req))
{
return;
}
// 提取 data 数组
cJSON *data = cJSON_GetObjectItem(pwm_ctrl_req, "data");
if (!cJSON_IsArray(data))
{
return;
}
// 遍历数组
for (int i = 0; i < cJSON_GetArraySize(data); ++i)
{
// 提取数组中的每个对象
cJSON *item = cJSON_GetArrayItem(data, i);
if (!cJSON_IsObject(item))
{
continue;
}
cJSON *mode = cJSON_GetObjectItem(item, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(item, "type");
cJSON *val = cJSON_GetObjectItem(item, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
unsigned char modeTmep= mode->valueint;
unsigned char typeTemp=type->valueint;
unsigned char valtemp= val->valueint;
unsigned char valt[4];
valt[0]=modeTmep;
valt[1]=typeTemp;
valt[2]=valtemp;
gPwmCount = 0;
printf("%d\n",valt[0]);
printf("%d\n",valt[1]);
printf("%d\n",valt[2]);
serial_Write(valt);
}
}
// 释放 payload 字符串
free(payload_str);
}
}
}
}
\ No newline at end of file
#ifndef __MQTT_H__
#define __MQTT_H__
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define CLIENT_ID "device1car/carerw5"
#define TOPIC "devices/controcar05"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
extern time_t gStart;
extern int gPwmCount; // 计数
void on_connect(struct mosquitto *mosq, void *obj, int rc);
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) ;
#endif
\ No newline at end of file
#include "serial.h"
int fd;
int init_serial(const char *port, int baudrate) {
int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open serial port");
return -1;
}
struct termios options;
tcgetattr(fd, &options);
// 设置波特率
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
// 配置串口
options.c_cflag &= ~PARENB; // 无校验
options.c_cflag &= ~CSTOPB; // 1停止位
options.c_cflag &= ~CSIZE; // 清除数据位设置
options.c_cflag |= CS8; // 8数据位
options.c_cflag |= CLOCAL | CREAD; // 忽略调制解调器状态,开启接收器
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 原始模式
options.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用流控
options.c_oflag &= ~OPOST; // 原始输出
tcsetattr(fd, TCSANOW, &options);
return fd;
}
void serial_Receive() {
char buffer[254]="\0";
while (1) {
int bytes_read = read(fd, buffer, sizeof(buffer)-1);
if (bytes_read > 0) {
//printf("Received chatdate: %s\n", buffer);
buffer[bytes_read] = '\0';
printf("Received chatdate:: %s\n", buffer);
//int receivedNumber = atoi(buffer);
//printf("Received intdate: %d\n", receivedNumber);
}
}
close(fd);
//free(buffer);
}
void serial_Write(unsigned char *buf)
{
unsigned char buffers[5];
buffers[0]=0xAA;
//write(fd, buffers[0], sizeof(buffers));
for(int i=1;i<4;i++)
{
buffers[i]=buf[i-1];
//write(fd, buffers[i], sizeof(buffers));
}
buffers[4]=0xFE;
write(fd, buffers, sizeof(buffers));
//printf("%d\n",buffers[0]);
//printf("%d\n",buffers[1]);
//printf("%d\n",buffers[2]);
//printf("%d\n",buffers[3]);
//printf("%d\n",buffers[4]);
}
int serial_Init()
{
fd = init_serial(SERIAL_PORT, 115200);
if (fd == -1) return -1;
printf("Serial communication started...\n");
}
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define SERIAL_PORT "/dev/ttyS3" // 根据实际设备修改
int serial_Init();
void serial_Receive();
void serial_Write(unsigned char *buf);
//extern int fd;
int init_serial(const char *port, int baudrate);
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment