Commit a93d07ee authored by 957dd's avatar 957dd

Merge branch 'mnv2' of http://git.yd-ss.com/wenzhongjian/car-controlserver into mnv2

parents 2edf2651 b911f59f
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include "gps.h" #include "gps.h"
#include "delay.h" #include "delay.h"
......
#ifndef __GPS_H__ #ifndef __GPS_H__
#define __GPS_H__ #define __GPS_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
extern char glat[20], glon[20]; extern char glat[20], glon[20];
......
import os
import socket
def get_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception as e:
print(f"获取本地IP地址时出错: {e}")
return None
def write_to_file(data):
try:
# 打开文件,模式为写入模式(会清空已有内容)
with open("/home/kickpi/car/master/Deviceld.txt", "w", encoding="utf-8") as file:
file.write(data)
print("数据已成功写入 Deviceld.txt")
except Exception as e:
print(f"写入文件时出错: {e}")
def read_from_file():
try:
if os.path.exists("/home/kickpi/car/master/Deviceld.txt"):
with open("/home/kickpi/car/master/Deviceld.txt", "r", encoding="utf-8") as file:
return file.read()
return "文件内容为空。"
except Exception as e:
print(f"读取文件时出错: {e}")
return "无法读取文件内容。"
if __name__ == '__main__':
local_ip = get_local_ip()
if local_ip is None:
print("无法获取本地IP地址,程序退出。")
else:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (local_ip, 8080)
server_socket.bind(server_address)
server_socket.listen(5)
print(f'Server is running on http://{server_address[0]}:{server_address[1]}')
while True:
client_socket, client_address = server_socket.accept()
print(f'Accepted connection from {client_address[0]}:{client_address[1]}')
try:
request_data = client_socket.recv(1024).decode('utf-8')
if not request_data:
continue
request_lines = request_data.split('\n')
request_line = request_lines[0]
method, path, _ = request_line.split(' ')
print(f'Received {method} request for {path}')
if method == "POST" and path == "/submit":
# 从请求体中提取数据
body = request_lines[-1] # 请求体在最后一行
data = body.split('=')[1] if '=' in body else body
# 保存数据到文件
write_to_file(data)
# 返回确认响应
response_body = f"""
<html>
<head><title>数据提交成功</title></head>
<body>
<h1>数据已成功提交!</h1>
<a href="/">返回主页</a>
</body>
</html>
"""
status_code = '200 OK'
elif method == "GET" and path == "/":
# 读取文件内容
file_content = read_from_file()
# 返回 HTML 页面
response_body = f'''
<html>
<head>
<title>简易HTTP服务器</title>
<style>
body {{ font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; }}
.container {{ max-width: 600px; margin: 50px auto; padding: 20px;
background: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); }}
h1 {{ text-align: center; }}
textarea {{ width: 100%; height: 150px; margin: 10px 0;
padding: 10px; border: 1px solid #ccc; border-radius: 4px; resize: none; }}
form {{ margin-top: 20px; }}
input[type="text"] {{ width: 90%; padding: 10px;
margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; }}
input[type="submit"] {{ background: #007bff; color: white;
padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }}
input[type="submit"]:hover {{ background: #0056b3; }}
</style>
</head>
<body>
<div class="container">
<h1>欢迎进入设备管理界面!</h1>
<textarea readonly>{file_content}</textarea>
<form action="/submit" method="post">
<input type="text" name="data" placeholder="请输入一些数据" required>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
'''
status_code = '200 OK'
else:
response_body = '<h1>404 Not Found</h1>'
status_code = '404 Not Found'
response_headers = (
f'HTTP/1.1 {status_code}\n'
f'Content-Type: text/html; charset=utf-8\n'
f'Content-Length: {len(response_body.encode("utf-8"))}\n'
f'Connection: close\n\n'
)
response = response_headers + response_body
client_socket.sendall(response.encode('utf-8'))
except Exception as e:
print(f'Error handling request: {e}')
error_response = (
'HTTP/1.1 500 Internal Server Error\n'
'Content-Type: text/html; charset=utf-8\n'
'Connection: close\n\n'
'<h1>500 内部服务器错误</h1>'
)
client_socket.sendall(error_response.encode('utf-8'))
finally:
client_socket.close()
ip.c 100644 → 100755
#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>
#include "ip.h" #include "ip.h"
struct ifaddrs *ifap, *ifa; struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa; struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN]; char ip_address[INET_ADDRSTRLEN];
int ipaddr() { int ipaddr() {
if (getifaddrs(&ifap) == -1) { if (getifaddrs(&ifap) == -1) {
perror("getifaddrs() error"); perror("getifaddrs() error");
return -1; return -1;
...@@ -18,7 +23,6 @@ int ipaddr() { ...@@ -18,7 +23,6 @@ int ipaddr() {
if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET) { if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *) ifa->ifa_addr; sa = (struct sockaddr_in *) ifa->ifa_addr;
inet_ntop(AF_INET, &sa->sin_addr, ip_address, sizeof(ip_address)); inet_ntop(AF_INET, &sa->sin_addr, ip_address, sizeof(ip_address));
//printf("%s: %s ", ifa->ifa_name, ip_address);
} }
} }
......
ip.h 100644 → 100755
#ifndef __IP_H__ #ifndef __IP_H__
#define __IP_H__ #define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <ifaddrs.h> #include <ifaddrs.h>
extern char ip_address[INET_ADDRSTRLEN];//
int ipaddr(); int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#endif #endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "mqtt.h"
char TOPIC[18];
void get_mac_address(const char *iface) {
int sock;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0); // 创建套接字
if (sock < 0) {
perror("Socket creation failed");
return;
}
strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1); // 指定网络接口
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) { // 获取硬件地址
perror("ioctl failed");
close(sock);
//return;
}
unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
printf("MAC address of %s: %02X:%02X:%02X:%02X:%02X:%02X\n",
iface, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(TOPIC,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
close(sock); // 关闭套接字
}
\ No newline at end of file
#ifndef __MAC_H__
#define __MAC_H__
extern char TOPIC[18];
void get_mac_address(const char *iface) ;
#endif
No preview for this file type
#include "main.h" #include <stdio.h>
#include "ip.h" #include <stdlib.h>
#include "mqtt.h" #include <stdint.h>
#include "serial.h" #include <string.h>
#include "delay.h" #include <fcntl.h>
#include "opensh.h" #include <pthread.h>
#include "mac.h" #include <unistd.h>
#include <termios.h>
#include <mosquitto.h>
void *serial_usart1(void *arg) #include <cjson/cJSON.h>
{ #include <time.h>
printf("serial_usart 1 start\n"); #include <sys/socket.h>
#include <netinet/in.h>
serial_Receive_1(); #include <arpa/inet.h>
#include <ifaddrs.h>
return NULL; #include "gps.h"
} #include "main.h"
#include "ip.h"
void *AppExit(void *arg) #include "mqtt.h"
{ #include "serial.h"
#include "delay.h"
while(1) #include "opensh.h"
{
unsigned char bufpwm1[3]={gtypeTemp,1,0}; char buffer[15]; // 用于存储文件内容
unsigned char bufpwm2[3]={gtypeTemp,2,0};
unsigned char bufpwm3[3]={gtypeTemp,3,0}; void *serial_usart1(void *arg)
unsigned char bufpwm4[3]={gtypeTemp,4,0}; {
unsigned char bufpwm5[3]={gtypeTemp,5,15}; printf("serial_usart 1 start\n");
if(gtypeTemp==3)
{ serial_Receive_1();//串口接收1,用于以后读取引脚高低电平
gPwmCount=0;
} return NULL;
Delay_Ms(0,100); }
gPwmCount++;
//printf( "%d\n",gtypeTemp); void *AppExit(void *arg)//2s程序,出现意外2s自动停止
if(gPwmCount>20) {
{
//printf( "%d\n",gtypeTemp); while(1)
gPwmCount=21; {
serial_Write(bufpwm1); unsigned char bufpwm1[3]={gtypeTemp,1,0};
serial_Write(bufpwm2); unsigned char bufpwm2[3]={gtypeTemp,2,0};
serial_Write(bufpwm3); unsigned char bufpwm3[3]={gtypeTemp,3,0};
serial_Write(bufpwm4); unsigned char bufpwm4[3]={gtypeTemp,4,0};
serial_Write(bufpwm5); Delay_Ms(0,100);
} gPwmCount++;
} if(gPwmCount>20)
{
return NULL; gPwmCount=21;
} serial_Write(bufpwm1);
serial_Write(bufpwm2);
void *Mqttbeat(void *arg) serial_Write(bufpwm3);
{ serial_Write(bufpwm4);
printf("Mqttbeat start\n");
}
}
while(1)
{ return NULL;
mqtt_wirte(); }
Delay_Ms(3,0);
} void *Mqttbeat(void *arg)
{
printf("Mqttbeat start\n");
return NULL; Delay_Ms(30,0);
} refresh_cam();
Delay_Ms(5,0);
void *opensh(void *arg) refresh_cam();
{ Delay_Ms(20,0);
Delay_Ms(10,0); refresh_cam();
printf("open cam\n"); Delay_Ms(10,0);
opencamsh(); while(1)
return NULL; {
} ipaddr();//获取ip
Delay_Ms(3,0);
void *serial_usart2(void *arg) mqtt_wirte();//心跳,3s一个
{
}
printf("serial_usart 2 start\n");
serial_Receive_2();
return NULL;
} }
int main(int argc, char *argv[]) { void *opensh(void *arg)
{
Delay_Ms(10,0);
gStart=time(NULL);//开始时间戳 printf("open cam\n");
opencamsh();//10s后打开火狐游览器并且进入网址
return NULL;
get_mac_address("eth0"); }
serial_Init();//串口初始化 void *serial_usart2(void *arg)
{
mqtt_init();//mqtt初始化
printf("serial_usart 2 start\n");
mqtt_create(mosq);//创建mqtt客户端 serial_Receive_2();//启动串口二
return NULL;
ipaddr();//获取ip
mqtt_wirte();//mqtt心跳首次发送 }
//mosquitto_publish(mosq, NULL, "macmqtt", strlen(TOPIC), TOPIC, 0, false);
mqtt_MAC_wirte(); void *Mqtt_onnect(void *arg)
Delay_Ms(1,0); {
mqtt_MAC_wirte(); Delay_Ms(5,0);
Delay_Ms(1,0); mqtt_init();//mqtt初始化
mqtt_MAC_wirte(); mqtt_create(mosq);//创建mqtt客户端
mqtt_wirte();//mqtt心跳首次发送
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh); while(1)
mqtt_cycle(mosq); {
mqtt_create(mosq);
mqtt_cycle(mosq);
mqtt_clean(mosq); }
thread_end();
mqtt_clean(mosq);
return EXIT_SUCCESS; return NULL;
}
char *device_inspect()
{
FILE *file;
int size=0;
while (1) {
file = fopen(filename, "r"); // 以只读模式打开文件
if (file == NULL)
{
printf("文件 %s 打开失败,等待中...\n", filename);
}
else
{
// 尝试读取文件内容
if (fgets(buffer, sizeof(buffer), file) != NULL)
{
// 如果文件内容不为空
fclose(file);
printf("读取到文件内容: %s\n", buffer);
return buffer;
}
else
{
printf("文件为空,等待中...\n");
}
fclose(file);
}
Delay_Ms(1,0); // 等待1秒后再次检查
}
}
int main(int argc, char *argv[]) {
Delay_Ms(60,0);
TOPIC=malloc(21);
TOPIC2=malloc(21);
TOPIC3= malloc(15);
TOPIC3=device_inspect();
//sprintf(TOPIC3,"%s",buffer);
//strcpy(TOPIC3,device_inspect());
sprintf(TOPIC2,"dev2app/%s",TOPIC3);
sprintf(TOPIC,"app2dev/%s",TOPIC3);
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
system("pkill firefox");
//reconnect_4g();
gStart=time(NULL);//开始时间戳
serial_Init();//串口初始化
ipaddr();//获取ip
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh,Mqtt_onnect);
thread_end();
return EXIT_SUCCESS;
} }
\ No newline at end of file
#ifndef __MAIN_H__ #ifndef __MAIN_H__
#define __MAIN_H__ #define __MAIN_H__
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
pthread_t thread[5];
#define filename "/home/kickpi/car/master/Deviceld.txt"
pthread_t thread[5];
int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg)) int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),
void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg))
{ {
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0) if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{ {
perror("Failed to create thread 1"); perror("Failed to create thread 1");
...@@ -35,31 +31,37 @@ int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *M ...@@ -35,31 +31,37 @@ int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *M
if(pthread_create(&thread[3],NULL,serial_usart2,NULL)!=0) if(pthread_create(&thread[3],NULL,serial_usart2,NULL)!=0)
{ {
perror("Failed to create thread 1"); perror("Failed to create thread 4");
return 4; return 4;
} }
if(pthread_create(&thread[4],NULL,opensh,NULL)!=0) if(pthread_create(&thread[4],NULL,opensh,NULL)!=0)
{ {
perror("Failed to create thread 1"); perror("Failed to create thread 5");
return 5; return 5;
} }
if(pthread_create(&thread[5],NULL,Mqtt_onnect,NULL)!=0)
{
perror("Failed to create thread 6");
return 6;
}
} }
// 等待线程结束
void thread_end() void thread_end()
{ {
// 等待线程1结束
pthread_join(thread[0], NULL); pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL); pthread_join(thread[1], NULL);
// 等待线程1结束
pthread_join(thread[2], NULL); pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL); pthread_join(thread[3], NULL);
pthread_join(thread[4], NULL); pthread_join(thread[4], NULL);
pthread_join(thread[5], NULL);
} }
#endif #endif
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#include "mqtt.h" #include "mqtt.h"
#include "gps.h" #include "gps.h"
#include "opensh.h" #include "opensh.h"
time_t gStart;
int gPwmCount = 0; // 计数 int gPwmCount = 0; // 计数
int gmessage_type=10086; int gmessage_type=10086;
int rc=0; int rc=0;
struct mosquitto *mosq;
unsigned char gmodeTemp=0; unsigned char gmodeTemp=0;
unsigned char gtypeTemp=0; unsigned char gtypeTemp=0;
...@@ -16,21 +26,11 @@ unsigned char gpinTemp=0; ...@@ -16,21 +26,11 @@ unsigned char gpinTemp=0;
unsigned char gvalTemp=0; unsigned char gvalTemp=0;
unsigned char gvalt[4]; unsigned char gvalt[4];
struct mosquitto *mosq;
void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数 time_t gStart;
{ char* TOPIC;
if (rc == 0) char* TOPIC2;
{ char* TOPIC3;
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);
}
}
int mqtt_init() int mqtt_init()
{ {
...@@ -45,22 +45,26 @@ int mqtt_init() ...@@ -45,22 +45,26 @@ int mqtt_init()
} }
//int init_mqtt(struct mosquitto *mosq) void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数
void mqtt_MAC_wirte()
{ {
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject(); if (rc == 0)
cJSON_AddStringToObject(body, "TOPIC", TOPIC); {
cJSON_AddItemToObject(root, "body", body); printf("Connected to broker\n");
char *payload = cJSON_PrintUnformatted(root); mosquitto_subscribe(mosq, NULL, TOPIC, 0);
mosquitto_publish(mosq, NULL, "macmqtt", strlen(payload), payload, 0, false); time_t start=time(NULL);
}
else
{
fprintf(stderr, "Connection failed with code %d\n", rc);
}
} }
void mqtt_wirte() void mqtt_wirte()
{ {
int message=1; int message=1;
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject(); cJSON *head = cJSON_CreateObject();
...@@ -81,8 +85,8 @@ void mqtt_wirte() ...@@ -81,8 +85,8 @@ void mqtt_wirte()
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
printf("%s\n",payload); printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC, strlen(payload), payload, 0, false); mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false);
mosquitto_publish(mosq, NULL, TOPIC3, strlen(payload), payload, 0, false);
} }
...@@ -142,9 +146,7 @@ void message_0()//message_type为0时候要改变的值 ...@@ -142,9 +146,7 @@ void message_0()//message_type为0时候要改变的值
void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
{ {
//pwm控制
// 提取 pin_ctrl_req 对象
//cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
if (!cJSON_IsObject(pwm_ctrl)) if (!cJSON_IsObject(pwm_ctrl))
{ {
return; return;
...@@ -173,7 +175,6 @@ void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm ...@@ -173,7 +175,6 @@ void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
void message_4(cJSON *body, cJSON *pin_setctrl) void message_4(cJSON *body, cJSON *pin_setctrl)
{ {
//cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if (!cJSON_IsObject(pin_setctrl)) if (!cJSON_IsObject(pin_setctrl))
{ {
return; return;
...@@ -197,7 +198,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -197,7 +198,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if(gStart+5>end) if(gStart+5>end)
{ {
printf("正在处理过期消息"); printf("正在处理过期消息\n");
return; return;
} }
...@@ -210,7 +211,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -210,7 +211,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
{ {
return; return;
} }
memcpy(payload_str, message->payload, message->payloadlen); memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0'; payload_str[message->payloadlen] = '\0';
...@@ -219,6 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -219,6 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (json == NULL) if (json == NULL)
{ {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
free(payload_str);
} }
else else
{ {
...@@ -226,17 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -226,17 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON *head = cJSON_GetObjectItem(json, "head"); cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head)) if (!cJSON_IsObject(head))
{ {
free(payload_str);
return; return;
} }
// 提取 message_type // 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type"); cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type)) if (!cJSON_IsNumber(message_type))
{ {
free(payload_str);
return; return;
} }
cJSON *body = cJSON_GetObjectItem(json, "body"); cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body)) if (!cJSON_IsObject(body))
{ {
free(payload_str);
return; return;
} }
cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl"); cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
...@@ -280,7 +285,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -280,7 +285,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端 int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
{ {
mosquitto_reconnect_delay_set(mosq, 2, 10, true);
// 设置连接和消息回调 // 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect); mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message); mosquitto_message_callback_set(mosq, on_message);
...@@ -294,23 +299,14 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端 ...@@ -294,23 +299,14 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
if (rc != MOSQ_ERR_SUCCESS) { if (rc != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc)); fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
mosquitto_destroy(mosq); mosquitto_destroy(mosq);
return EXIT_FAILURE; //return EXIT_FAILURE;
} }
//mosquitto_subscribe_opt_set(mosq, TOPIC, strlen(TOPIC), 1, MOSQ_OPT_NO_LOCAL);
} }
int mqtt_cycle(struct mosquitto *mosq) int mqtt_cycle(struct mosquitto *mosq)
{ {
//mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
//mosquitto_subscribe_with_options(mosq, NULL, TOPIC, 0, MQTT_SUB_OPT_NO_LOCAL);
mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量 mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量
//mosquitto_message_retry_set(mosq, 10);
} }
int mqtt_clean(struct mosquitto *mosq) int mqtt_clean(struct mosquitto *mosq)
......
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __MQTT_H__ #define __MQTT_H__
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#include "mac.h"
#define CLIENT_ID "carorship123" #define CLIENT_ID "carorship123"
#define BROKER_ADDRESS "119.45.167.177" #define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883 #define BROKER_PORT 1883
//#define TOPIC "controcar0004" extern char* TOPIC ;//="app2dev/controlcar0004"
extern char* TOPIC2 ;//="dev2app/controlcar0004"
extern char* TOPIC3;//= "controlcar0004"
#define USERNAME "admin" // 替换为你的用户名 #define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码 #define PASSWORD "admin" // 替换为你的密码
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "opensh.h"
#include "mac.h"
#include <string.h> #include <string.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include "ip.h"
#include "serial.h"
#include "mqtt.h"
#include "opensh.h"
char gwebcam[100];
char webcam[100];
int opencamsh() int opencamsh()
{ {
setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(webcam,"sudo firefox --new-window https://jywy.yd-ss.com?dev=%s &",TOPIC); sprintf(gwebcam,"sudo firefox --new-window https://jywy.yd-ss.com?dev=%s &",TOPIC3);
system(webcam); system(gwebcam);
printf("%s\n",webcam); printf("%s\n",gwebcam);
printf("close cam\n"); printf("close cam\n");
} }
void refresh_cam() void refresh_cam()
{ {
system(SHELLFILECLOSE); //system(SHELLFILECLOSE);
const char *search_command = "xdotool search --name \"Mozilla Firefox\""; const char *search_command = "xdotool search --name \"Mozilla Firefox\"";
FILE *fp = popen(search_command, "r"); FILE *fp = popen(search_command, "r");
if (!fp) { if (!fp) {
...@@ -48,3 +60,23 @@ void refresh_cam() ...@@ -48,3 +60,23 @@ void refresh_cam()
pclose(fp); pclose(fp);
printf("recam\n"); printf("recam\n");
} }
int reconnect_4g()
{
FILE *file = fopen("/etc/resolv.conf", "w");
if (file == NULL) {
perror("Failed to open /etc/resolv.conf");
return 1;
}
// 写入 nameserver 8.8.8.8
const char *nameserver = "nameserver 8.8.8.8\n";
if (fputs(nameserver, file) == EOF) {
perror("Failed to write to /etc/resolv.conf");
fclose(file);
return 1;
}
// 关闭文件
fclose(file);
}
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
#define __opensh_H__ #define __opensh_H__
#define SHELLFILE "/home/kickpi/car/master/camopen.sh" //#define SHELLFILE "/home/kickpi/car/master/camopen.sh"
#define SHELLFILECLOSE "/home/kickpi/car/master/recam.sh" //#define SHELLFILECLOSE "/home/kickpi/car/master/recam.sh"
extern char gwebcam[100];
int opencamsh(); int opencamsh();
void refresh_cam(); void refresh_cam();
int reconnect_4g();
#endif #endif
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include "ip.h"
#include "serial.h" #include "serial.h"
#include "mqtt.h" #include "mqtt.h"
#include "delay.h" #include "delay.h"
......
#ifndef __SERIAL_H__ #ifndef __SERIAL_H__
#define __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_PORT1 "/dev/ttyS3" // 根据实际设备修改 #define SERIAL_PORT1 "/dev/ttyS3" // 根据实际设备修改
#define SERIAL_PORT2 "/dev/ttyS0" #define SERIAL_PORT2 "/dev/ttyS0"
......
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