Commit 277cc3bf authored by 学习的菜鸟's avatar 学习的菜鸟

4

parent 9a95bc90
#define _XOPEN_SOURCE 700
#define _DEFAULT_SOURCE
#include "common.h"
#include "device_id_change.h"
#define BUFFER_SIZE 1024
char buffer_device_change_fail[40]; // 用于回退存储文件内容
// 判断文件是否存在
int file_exists(const char *path) {
return access(path, F_OK) == 0;
}
// 删除文件
int remove_file(const char *path) {
if (remove(path) == 0) {
return 1;
} else {
my_zlog_warn("删除文件失败: %s", path);
return 0;
}
}
// 递归创建目录
int create_directory_if_not_exists(const char *path) {
char temp[1024];
size_t len = strlen(path);
if (len >= sizeof(temp)) {
my_zlog_error("路径过长: %s", path);
return -1;
}
// 拷贝路径并保证以 '/' 结尾
strncpy(temp, path, sizeof(temp));
if (temp[len - 1] != '/') {
temp[len] = '/';
temp[len + 1] = '\0';
len++;
}
for (size_t i = 1; i < len; i++) {
if (temp[i] == '/') {
temp[i] = '\0';
if (access(temp, F_OK) != 0) {
if (mkdir(temp, 0755) != 0) {
my_zlog_error("创建目录失败: %s (%s)", temp, strerror(errno));
return -1;
} else {
my_zlog_debug("已创建目录: %s", temp);
}
}
temp[i] = '/';
}
}
return 0;
}
// 复制文件内容
int copy_file(const char *src, const char *dst) {
FILE *fsrc = fopen(src, "rb");
if (!fsrc) {
my_zlog_warn("无法打开源文件: %s", src);
return -1;
}
FILE *fdst = fopen(dst, "wb");
if (!fdst) {
my_zlog_warn("无法打开目标文件: %s", dst);
fclose(fsrc);
return -1;
}
char buffer[BUFFER_SIZE];
size_t bytes;
while ((bytes = fread(buffer, 1, BUFFER_SIZE, fsrc)) > 0) {
if (fwrite(buffer, 1, bytes, fdst) != bytes) {
my_zlog_warn("写入文件出错: %s", dst);
fclose(fsrc);
fclose(fdst);
return -1;
}
}
fclose(fsrc);
fclose(fdst);
return 0;
}
// 重命名并移动文件
int rename_and_move_file(const char *src, const char *new_name, const char *dst_dir) {
char dst_path[BUFFER_SIZE];
snprintf(dst_path, sizeof(dst_path), "%s/%s", dst_dir, new_name);
my_zlog_debug("尝试移动文件: %s -> %s", src, dst_path);
if (rename(src, dst_path) == 0) {
my_zlog_debug("文件已移动至 %s", dst_path);
return 0;
}
my_zlog_warn("rename()失败: %s -> %s, 错误: %s", src, dst_path, strerror(errno));
if (errno == EXDEV) { // 跨文件系统移动
if (copy_file(src, dst_path) == 0) {
if (remove(src) == 0) {
my_zlog_debug("跨文件系统移动成功: %s", dst_path);
return 0;
} else {
my_zlog_warn("删除源文件失败: %s", src);
return -1;
}
} else {
my_zlog_warn("复制文件失败: %s -> %s", src, dst_path);
return -1;
}
}
my_zlog_warn("移动文件失败: %s", strerror(errno));
return -1;
}
// 提取文件名中的时间戳(例如 Deviceld.txt.202405100930)
time_t extract_timestamp(const char *filename) {
char ts[20];
const char *dot = strrchr(filename, '.');
if (dot) {
strncpy(ts, dot + 1, sizeof(ts) - 1);
ts[sizeof(ts) - 1] = '\0';
struct tm tm = {0};
if (strptime(ts, "%Y%m%d%H%M", &tm)) {
return mktime(&tm);
}
}
return 0;
}
// 查找目录中最
char *find_latest_file(const char *dir_path) {
DIR *dir = opendir(dir_path);
if (!dir) {
my_zlog_error("无法打开目录: %s", dir_path);
return NULL;
}
struct dirent *entry;
time_t latest_time = 0;
char *latest_file = NULL;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
time_t ts = extract_timestamp(entry->d_name);
if (ts > latest_time) {
latest_time = ts;
free(latest_file);
latest_file = strdup(entry->d_name);
}
}
}
closedir(dir);
return latest_file;
}
// 写入设备 ID 到文件
int write_device_id(const char *path, const char *device_id) {
FILE *f = fopen(path, "w");
if (!f) {
my_zlog_error("无法写入文件: %s", path);
return -1;
}
fprintf(f, "%s", device_id);
fclose(f);
my_zlog_debug("成功写入设备ID到 %s", path);
system("sudo reboot");
return 0;
}
// 改名并备份设备文件
int device_changename_back(const char *device_date, const char *device_id) {
// 调试输出:源文件是否存在
if (!file_exists(DEVICE_NAME_FILE)) {
my_zlog_warn("设备文件不存在: %s", DEVICE_NAME_FILE);
return 0;
}
my_zlog_debug("设备文件存在,准备备份");
if (create_directory_if_not_exists(DEVICE_NAME_DIR) != 0) {
my_zlog_error("创建备份目录失败");
return -1;
}
char new_name[256];
snprintf(new_name, sizeof(new_name), "Deviceld.txt.%s", device_date);
my_zlog_debug("新文件名: %s", new_name);
// 移动文件(可能失败)
if (rename_and_move_file(DEVICE_NAME_FILE, new_name, DEVICE_NAME_DIR) != 0) {
my_zlog_warn("设备文件备份失败");
return -1;
}
return write_device_id(DEVICE_NAME_FILE, device_id);
}
// 获取最近的设备备份文件路径(回退使用)
char *device_changename_back_fail(void) {
char *latest_file = find_latest_file(DEVICE_NAME_DIR);
if (!latest_file) {
my_zlog_warn("未找到任何备份文件");
return NULL;
}
size_t size = strlen(DEVICE_NAME_DIR) + strlen(latest_file) + 2;
char *full_path = malloc(size);
if (!full_path) {
free(latest_file);
my_zlog_error("内存分配失败");
return NULL;
}
snprintf(full_path, size, "%s/%s", DEVICE_NAME_DIR, latest_file);
my_zlog_debug("回退路径为: %s", full_path);
free(latest_file);
return full_path;
}
//读取失败,读取备份内容
char *read_device_back_fail() {
FILE *file;
char *device_back_fail =device_changename_back_fail();
if(device_back_fail !=NULL) { //如果在备份文件中找到
file = fopen(device_back_fail, "r"); // 以只读模式打开文件
if (fgets(buffer_device_change_fail, sizeof(buffer_device_change_fail), file) != NULL) {
// 如果文件内容不为空
fclose(file);
my_zlog_debug("读取到备份文件内容: %s", buffer_device_change_fail);
free(device_back_fail);
return buffer_device_change_fail;
} else {
my_zlog_debug("读取备份文件失败");
}
fclose(file);
return NULL;
}
return NULL;
}
#ifndef DEEVICE_ID_CHANGE_H__
#define DEEVICE_ID_CHANGE_H__
#define DEVICE_NAME_DIR "/home/orangepi/car/master/deviceback"
#define DEVICE_NAME_FILE "/home/orangepi/car/master/Deviceld.txt"
int device_changename_back(const char *device_date, const char *device_id);//设备号备份和写入新设备号
int file_exists(const char *path);//文件已经存在
int create_directory_if_not_exists(const char *path);//创建目录
char *read_device_back_fail();//读取失败,读取备份内容
#endif
\ No newline at end of file
#include <cjson/cJSON.h>
#include "wifichange.h"
#include "mqtt.h"
#include "common.h"
#include "judg.h"
char current_ssid[SSID_MAX_LEN] = {0};//用于存现在已连接WiFi
char wifi_Last_ssid[SSID_MAX_LEN]= {0} ;
char wifi_Last_password[SSID_MAX_LEN]= {0} ;
//获取当前连接wifi
void get_current_wifi() {
//显示已连接代码
FILE *fp = popen("nmcli -t -f ACTIVE,SSID dev wifi", "r");
if (!fp) {
my_zlog_error("{\"error\": \"无法执行扫描命令\"}");
return;
}
char line[256];
while (fgets(line, sizeof(line), fp)) {
char *active = strtok(line, ":");
char *ssid = strtok(NULL, "\n");
if (active && ssid && strcmp(active, "yes") == 0 && strlen(ssid) > 0) {
strncpy(current_ssid, ssid, SSID_MAX_LEN - 1);
current_ssid[SSID_MAX_LEN - 1] = '\0';
break;
}
}
pclose(fp);
}
//找重复的WiFi名称
int find_ssid(wifi_info_t list[], int count, const char *ssid) {
for (int i = 0; i < count; i++) {
if (strcmp(list[i].ssid, ssid) == 0) {
return i;
}
}
return -1;
}
//进行qsort前,此未降序操作
int cmp_signal_desc(const void *a, const void *b) {
wifi_info_t *wa = (wifi_info_t *)a;
wifi_info_t *wb = (wifi_info_t *)b;
return wb->signal - wa->signal;
}
//显示以连接WiFi名称和周围已有的WiFi和过滤掉信号强度低于50的WiFi
void scan_wifi_json() {
get_current_wifi();
char line[100];
//显示WiFi名称与信号强度
FILE *fp = popen("nmcli -t -f SSID,SIGNAL dev wifi list", "r");
if (!fp) {
my_zlog_error("{\"error\": \"无法执行扫描命令\"}");
return;
}
wifi_info_t wifi_list[MAX_WIFI_LIST];
int wifi_count = 0;
while (fgets(line, sizeof(line), fp)) {
char *ssid = strtok(line, ":");
char *signal_str = strtok(NULL, "\n");
if (!ssid || !signal_str) continue;
if (strlen(ssid) == 0) continue;
int signal = atoi(signal_str);
int idx = find_ssid(wifi_list, wifi_count, ssid);//去重 + 只保留信号最强的
if (idx == -1) {
if (wifi_count < MAX_WIFI_LIST) {
strncpy(wifi_list[wifi_count].ssid, ssid, SSID_MAX_LEN - 1);
wifi_list[wifi_count].ssid[SSID_MAX_LEN - 1] = '\0';
wifi_list[wifi_count].signal = signal;
wifi_count++;
}
} else {
if (signal > wifi_list[idx].signal) {
wifi_list[idx].signal = signal;
}
}
}
pclose(fp);
qsort(wifi_list, wifi_count, sizeof(wifi_info_t), cmp_signal_desc);//通过信号强度进行排序
//将WiFi信息转化为json格式
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "current_connected", current_ssid[0] ? current_ssid : "");
cJSON *array = cJSON_CreateArray();
for (int i = 0; i < wifi_count; i++) {
if (wifi_list[i].signal < 50) continue; // 过滤信号小于50的
cJSON *item = cJSON_CreateObject();
cJSON_AddStringToObject(item, "ssid", wifi_list[i].ssid);
cJSON_AddNumberToObject(item, "strength", wifi_list[i].signal);
cJSON_AddItemToArray(array, item);
}
cJSON_AddItemToObject(body, "available_wifi", array);
cJSON_AddNumberToObject(head, "message_type", 3003);
cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head);
char *json_str = cJSON_Print(root);
if (json_str) {
my_zlog_debug("%s", json_str);
topic_middle_value();
mosquitto_publish(mosq, NULL, TOPIC3, strlen(json_str), json_str, 0, false);
free(json_str);
}
cJSON_Delete(root);
}
//连接WiFi
int connect_wifi(const char* ssid, const char* password) {
char cmd[CMD_BUFFER_SIZE];
snprintf(cmd, sizeof(cmd), "nmcli dev wifi connect '%s' password '%s' 2>&1", ssid, password);
my_zlog_debug("🔌 正在连接 Wi-Fi:%s...", ssid);
FILE *fp = popen(cmd, "r");
if (!fp) {
my_zlog_warn("❌ 无法执行连接命令。");
return -1;
}
char output[512] = {0};
fread(output, 1, sizeof(output) - 1, fp);
pclose(fp);
if (strstr(output, "successfully activated")) {
my_zlog_debug("✅ 已成功连接到:%s", ssid);
return 0;
} else if (strstr(output, "No network with SSID")) {
my_zlog_warn("❌ 连接失败:Wi-Fi '%s' 不存在。", ssid);
return -2;
} else if (strstr(output, "Secrets were required") || strstr(output, "incorrect password")) {
my_zlog_warn("❌ 连接失败:密码错误。");
return -3;
} else {
my_zlog_error("❌ 连接失败,未知错误:%s", output);
return -4;
}
}
//如果连接失败则回退
int connect_wifi_with_fallback(const char* ssid, const char* password, const char* fallback_ssid) {
int ret = connect_wifi(ssid, password);
if (ret != 0 && fallback_ssid && fallback_ssid[0] != 0) {
my_zlog_warn("⚠️ 连接失败,尝试回退连接原Wi-Fi:%s", fallback_ssid);
char cmd[CMD_BUFFER_SIZE];
snprintf(cmd, sizeof(cmd), "nmcli dev wifi connect '%s' 2>&1", fallback_ssid);
FILE *fp = popen(cmd, "r");
if (fp) {
char output[512] = {0};
fread(output, 1, sizeof(output) - 1, fp);
pclose(fp);
if (strstr(output, "successfully activated")) {
my_zlog_debug("✅ 回退连接成功:%s", fallback_ssid);
} else {
my_zlog_warn("❌ 回退连接失败:%s", output);
ret = -5;
}
} else {
my_zlog_error("❌ 无法执行回退连接命令。");
ret = -5;
}
}
return ret;
}
//改变WiFi连接
int change_wifi_connect(const char *wifi_ssid,const char *wifi_password) {
get_current_wifi();
int ret = connect_wifi_with_fallback(wifi_ssid, wifi_password, current_ssid);
my_zlog_notice("返回代码:%d", ret);
return ret;
}
// 连续尝试 2 次 ping,任意一次成功即认为联网成功
int can_access_internet() {
for (int i = 0; i < 4; ++i) {
int ret = system("ping -c 1 -W 1 baidu.com > /dev/null 2>&1");
if (ret == 0) {
return 0; // 有一次成功就返回 true
}
}
return 1; // 都失败
}
//删除对应wifi
int delete_wifi_by_ssid(const char* ssid) {
if (ssid == NULL || strlen(ssid) == 0) {
fprintf(stderr, "SSID is empty.\n");
return -1;
}
char command[256];
// 直接尝试删除这个名字的连接(假设连接名就是 SSID)
snprintf(command, sizeof(command), "nmcli connection delete \"%s\"", ssid);
int ret = system(command);
if (ret == 0) {
my_zlog_debug("Successfully deleted WiFi connection named: %s", ssid);
} else {
my_zlog_warn("Failed to delete WiFi connection named: %s", ssid);
}
return ret;
}
// 检查文件是否存在,不存在则创建
int check_or_create_wifi_conf() {
struct stat st;
if (stat(WIFI_CONF_PATH, &st) == 0) {
// 文件已存在
return 0;
}
// 创建空配置文件
FILE *fp = fopen(WIFI_CONF_PATH, "w");
if (!fp) {
perror("Failed to create device_wifi.conf");
return -1;
}
fclose(fp);
return 0;
}
// 执行命令并获取结果,通过此函数获取到WiFi密码
char *get_command_output(const char *cmd, char *buffer, size_t size) {
FILE *fp = popen(cmd, "r");
if (!fp) return NULL;
if (fgets(buffer, size, fp)) {
buffer[strcspn(buffer, "\n")] = '\0'; // 去除换行符
}
pclose(fp);
return buffer;
}
// 写入当前 WiFi SSID 和密码到配置文件
int write_wifi_conf() {
char ssid[128] = {0};
char cmd[256];
char password[128] = {0};
// 获取 SSID
if (!get_command_output("iwgetid -r", ssid, sizeof(ssid))) {
fprintf(stderr, "获取当前连接的 SSID 失败\n");
return -1;
}
// 从 NetworkManager 配置中获取密码
snprintf(cmd, sizeof(cmd),
"grep -r '^psk=' /etc/NetworkManager/system-connections/ | grep '%s' | head -n1 | cut -d'=' -f2",
ssid);
if (!get_command_output(cmd, password, sizeof(password))) {
fprintf(stderr, "获取 WiFi 密码失败(可能无权限或未使用 NetworkManager)\n");
return -1;
}
// 写入文件
FILE *fp = fopen(WIFI_CONF_PATH, "w");
if (!fp) {
my_zlog_error("打开配置文件失败");
return -1;
}
fprintf(fp, "SSID=%s\n", ssid);
fprintf(fp, "PASSWORD=%s\n", password);
fclose(fp);
my_zlog_debug("已写入到配置文件: %s\n", WIFI_CONF_PATH);
return 0;
}
//删除对应的存在ssid的conf
void delete_wifi_conf() {
DIR *dir = opendir(WIFI_CONF_DIR);
if (!dir) {
my_zlog_warn("opendir failed");
return;
}
struct dirent *entry;
char full_path[1024];
while ((entry = readdir(dir)) != NULL) {
// 只查找文件名为 "device_wifi.conf"
if (strcmp(entry->d_name, "device_wifi.conf") == 0) {
snprintf(full_path, sizeof(full_path), "%s/%s", WIFI_CONF_DIR, entry->d_name);
if (unlink(full_path) == 0) {
my_zlog_debug("Deleted: %s\n", full_path);
} else {
my_zlog_warn("Failed to delete");
}
break; // 找到并删除就退出
}
}
closedir(dir);
}
//提取WiFi名称和密码
int extract_wifi_file(){
FILE *fp = fopen(WIFI_CONF_PATH, "r");
if (fp == NULL) {
perror("无法打开配置文件");
return -1;
}
char line[256];
while (fgets(line, sizeof(line), fp)) {
// 去除换行符
line[strcspn(line, "\r\n")] = 0;
if (strncmp(line, "SSID=", 5) == 0) {
strncpy(wifi_Last_ssid, line + 5, sizeof(wifi_Last_ssid) - 1);
} else if (strncmp(line, "PASSWORD=", 9) == 0) {
strncpy(wifi_Last_password, line + 9, sizeof(wifi_Last_password) - 1);
}
}
fclose(fp);
my_zlog_debug("SSID: %s", wifi_Last_ssid);
my_zlog_debug("PASSWORD: %s", wifi_Last_password);
return 0;
}
// 从文件读取 SSID,比较当前连接的 WiFi 名称
int compare_ssid_with_file() {
char file_ssid[128] = {0};
get_current_wifi();
FILE *fp = fopen(WIFI_CONF_PATH, "r");
if (!fp) {
perror("Failed to open device_wifi.conf");
return -1;
}
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "SSID=", 5) == 0) {
strncpy(file_ssid, line + 5, sizeof(file_ssid) - 1);
file_ssid[strcspn(file_ssid, "\n")] = 0;
break;
}
}
fclose(fp);
my_zlog_debug("File SSID: %s", file_ssid);
my_zlog_debug("Current SSID: %s", current_ssid);
if (strcmp(file_ssid, current_ssid) == 0) {
my_zlog_debug("wifi没有发生改变或其他问题");
return 0;
} else {
my_zlog_debug("wifi发生改变或其他问题");
return 1;
}
}
/*当wifi_status为0为连接jking,当wifi_status为1为修改成功,2是继续使用正常wifi,3是恢复到默认jking,4为wifi不存在
,5为密码错误,6为未知错误,7为回退失败,8为没网切回默认wifi*/
void wifichange_sendmqtt(int wifi_status) { //改WiFi发送
topic_middle_value();//指针中间函数
get_current_wifi();
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject();
cJSON_AddNumberToObject(body, "wifi_status", wifi_status);//为0成功
cJSON_AddStringToObject(body, "wifi_ssid", current_ssid);//为0成功
cJSON_AddNumberToObject(head, "message_type",3004);
cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head);
char *payload = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, TOPIC3, strlen(payload), payload, 0, false);
cJSON_Delete(root); // 释放 cJSON 对象
}
//默认连接jking
void connect_wifi_jking() {
if(check_or_create_wifi_conf() == 0) my_zlog_debug("文件已创建");
if(write_wifi_conf() == 0) my_zlog_debug("写入成功");
int ret=change_wifi_connect(default_SSID,default_password);
Delay_Ms(15,0);
if(can_access_internet()==0) {
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功");
}
}
//wifi改变初始化,放在main开头,必须要等mqtt直播就绪才行
int wifi_change_sendmqtt_init(){
int wififile_fd = compare_ssid_with_file();
if(wififile_fd==-1){
if(can_access_internet()!=0){
get_current_wifi();
if(strcmp(default_SSID,current_ssid)==0){
my_zlog_debug("没有更改WiFi");
return 0;
}
check_or_create_wifi_conf();
write_wifi_conf();
change_wifi_connect(default_SSID,default_password);
delete_wifi_by_ssid(current_ssid);
}
if(strcmp(default_SSID,current_ssid)==0) wifichange_sendmqtt(0);
}else if(wififile_fd==1){
get_current_wifi();
if(strcmp(default_SSID,current_ssid)==0){
extract_wifi_file();
if(change_wifi_connect(wifi_Last_ssid,wifi_Last_password)==0){
Delay_Ms(15,0);
if(can_access_internet()==0){
delete_wifi_by_ssid(current_ssid);
delete_wifi_conf();
my_zlog_debug("成功回朔到上次WiFi");
}else {
change_wifi_connect(default_SSID,default_password);
my_zlog_debug("成功回朔到上次WiFi但没网回到默认wifi");
}
}else {
change_wifi_connect(default_SSID,default_password);
my_zlog_debug("说明不止一次更换wifi过");
}
}else{
if(can_access_internet()==0){
wifichange_sendmqtt(1);
my_zlog_debug("wifi更改成功");
delete_wifi_conf();
}else {
if(change_wifi_connect(default_SSID,default_password)==0)delete_wifi_conf();
my_zlog_debug("WiFi无网切回");
Delay_Ms(10,0);
}
}
} else if(wififile_fd==0){
if(can_access_internet()==0) {
wifichange_sendmqtt(2);//2是继续使用正常wifi
my_zlog_debug("wifi更改失败,继续使用原WiFi,可使用查询指令查询");
delete_wifi_conf();
} else {//不需要删除文件,不同回删除conf源文件,相同也会删除
wifichange_sendmqtt(3);//3是恢复到默认jking
my_zlog_debug("在WiFi更改失败情况下使用默认连接jking");
delete_wifi_conf();
connect_wifi_jking();
}
}
return 0;
}
//接收mqtt消息
void wifi_change_recmqtt(cJSON *body){
cJSON *wifi_ssid = cJSON_GetObjectItem(body, "wifi_ssid");
cJSON *wifi_password = cJSON_GetObjectItem(body, "wifi_password");
cJSON *wifi_status = cJSON_GetObjectItem(body, "wifi_status");
if(cJSON_IsString(wifi_ssid) && cJSON_IsString(wifi_password)){
char *ssid = wifi_ssid->valuestring,*password=wifi_password->valuestring;
int wifi_statustemp = wifi_status->valueint;
if(check_or_create_wifi_conf() == 0) my_zlog_debug("文件已创建");
if(write_wifi_conf() == 0) my_zlog_debug("写入成功");
int ret = change_wifi_connect(ssid,password);
Delay_Ms(15,0);
if(strcmp(default_SSID,ssid)==0){
delete_wifi_by_ssid(current_ssid);
my_zlog_debug("ssid:%S",current_ssid);
delete_wifi_conf();
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功");
}
if(ret == 0){
if(can_access_internet()==0){
delete_wifi_by_ssid(current_ssid);
my_zlog_debug("ssid:%S",current_ssid);
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功");
}else {
delete_wifi_conf();
if(change_wifi_connect(default_SSID,default_password)==0) delete_wifi_by_ssid(current_ssid);
my_zlog_debug("无网切回默认WiFi");
Delay_Ms(15,0);
wifichange_sendmqtt(8);
}
}
if(ret == -1) {
wifichange_sendmqtt(4);
}else if(ret == -2){
wifichange_sendmqtt(5);
}else if(ret == -3){
wifichange_sendmqtt(6);
}else if(ret == -4 || ret == -5){
wifichange_sendmqtt(7);
system("sudo reboot");//重启香橙派
my_zlog_debug("重启成功");
}
}
}
\ No newline at end of file
#ifndef WIFICHANGE_H__
#define WIFICHANGE_H__
#include <cjson/cJSON.h>
#define SSID_MAX_LEN 128
#define PASS_MAX_LEN 128
#define CMD_BUFFER_SIZE 256
#define MAX_WIFI_LIST 100
#define default_SSID "jking"
#define default_password "12345678"
#define WIFI_CONF_PATH "/home/orangepi/car/master/device_wifi.conf"
#define WIFI_CONF_DIR "/home/orangepi/car/master"
typedef struct {
char ssid[SSID_MAX_LEN];
int signal;
} wifi_info_t;
void scan_wifi_json();//显示以连接WiFi名称和周围已有的WiFi和过滤信号强度低于50的WiFi
int wifi_change_sendmqtt_init();//wifi改变初始化,放在main开头,必须要等mqtt直播就绪才行
void wifi_change_recmqtt(cJSON *body);//接收到修改WiFi指令
#endif
\ No newline at end of file
#include "common.h"
#include "gpio_init.h"
#include "mqtt.h"
#include "http_request.h"
#include "request.h"
#define MIN_DUTY 0 // 最小占空比
#define MAX_DUTY 100 // 最大占空比
......@@ -63,7 +62,7 @@ void pwm_value(int pin,int value) { //软件陪我们控制调速
return ;
}
}
int shot_speed = (int)(shot_speed_value*100);
int shot_speed = (int)(g_shot_speed_value*100);
if(value==1) {
if(pin == 27){
......
#include<curl/curl.h>
#include <cjson/cJSON.h>
#include "http_request.h"
#include "device_identity.h"
const char *post = "http://47.119.190.60/api/v1/device/config";
const char *c_post = "http://47.119.190.60/api/v1/device/config";
int errCodeValue;
char *errMsgptr = NULL;
int g_errCodeValue;
bool enable_buzzer_value=0;
char *force_versionptr = NULL;
double warn_voltage_value;
double err_voltage_value;
double shot_speed_value=0.7;
bool g_enable_buzzer_value=0;
double g_warn_voltage_value;
double g_err_voltage_value;
double g_shot_speed_value=0.7;
// 回调函数,用于处理接收到的数据
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
......@@ -64,36 +63,36 @@ int analysis_request_json(char *payload_str) {
cJSON *err_voltage = cJSON_GetObjectItem(data, "err_voltage");
cJSON *shot_speed= cJSON_GetObjectItem(data, "shot_speed");
if(cJSON_IsNumber(errCode)) {
errCodeValue = errCode->valueint;
if(errCodeValue != 0){
g_errCodeValue = errCode->valueint;
if(g_errCodeValue != 0){
cJSON_Delete(json);
return 6;
}
}
if(cJSON_IsString(errMsg)) {
errMsgptr = errMsg->valuestring;
char *g_errmsgptr = errMsg->valuestring;
}
if(cJSON_IsBool(enable_buzzer)) {
enable_buzzer_value = cJSON_IsTrue(enable_buzzer);
my_zlog_debug("enable_buzzer: %s", enable_buzzer_value ? "true" : "false");
g_enable_buzzer_value = cJSON_IsTrue(enable_buzzer);
my_zlog_debug("enable_buzzer: %s", g_enable_buzzer_value ? "true" : "false");
}
if(cJSON_IsString(force_version)) {
force_versionptr = force_version->valuestring;
my_zlog_debug("force_versionptr: %s",force_versionptr);
char *g_force_versionptr = force_version->valuestring;
my_zlog_debug("g_force_versionptr: %s",g_force_versionptr);
}
if(cJSON_IsNumber(warn_voltage)) {
warn_voltage_value = warn_voltage->valuedouble;
my_zlog_debug("warn_voltage_value: %.2f",warn_voltage_value);
g_warn_voltage_value = warn_voltage->valuedouble;
my_zlog_debug("g_warn_voltage_value: %.2f",g_warn_voltage_value);
}
if(cJSON_IsNumber(err_voltage)) {
err_voltage_value = err_voltage->valuedouble;
my_zlog_debug("err_voltage_value: %.2f",err_voltage_value);
g_err_voltage_value = err_voltage->valuedouble;
my_zlog_debug("g_err_voltage_value: %.2f",g_err_voltage_value);
}
if(cJSON_IsNumber(shot_speed)) {
if(0 <= shot_speed_value&&shot_speed_value<=1) {
shot_speed_value = shot_speed->valuedouble;
if(0 <= g_shot_speed_value&&g_shot_speed_value<=1) {
g_shot_speed_value = shot_speed->valuedouble;
}
my_zlog_debug("shot_speed_value: %.2f",shot_speed_value);
my_zlog_debug("g_shot_speed_value: %.2f",g_shot_speed_value);
}
cJSON_Delete(json);
......@@ -106,7 +105,7 @@ char *wirte_json() {
topic_middle_value();
cJSON_AddStringToObject(body, "device_id", TOPIC3); // 发送设备id
cJSON_AddStringToObject(body, "device_id", mqtt_topic_pure_number()); // 发送设备id
char *payload = cJSON_PrintUnformatted(body);
my_zlog_debug("%s", payload);
......@@ -131,7 +130,7 @@ int request_date() {//请求数据
chunk.size = 0;
// 设置请求URL
curl_easy_setopt(curl, CURLOPT_URL, post);
curl_easy_setopt(curl, CURLOPT_URL, c_post);
// 设置POST请求
curl_easy_setopt(curl, CURLOPT_POST, 1L);
......
......@@ -10,11 +10,11 @@ struct MemoryStruct {
size_t size;
};//初始化拉取接口,接收结构体
extern int errCodeValue;//是否错误,一般不会发生
extern bool enable_buzzer_value;//是否打开蜂鸣器
extern double shot_speed_value;//射击的pwm占空比
extern double warn_voltage_value;//警告电压
extern double err_voltage_value;//错误电压
extern int g_errCodeValue;//是否错误,一般不会发生
extern bool g_enable_buzzer_value;//是否打开蜂鸣器
extern double g_shot_speed_value;//射击的pwm占空比
extern double g_warn_voltage_value;//警告电压
extern double g_err_voltage_value;//错误电压
int request_date();//请求数据
#endif
......@@ -46,7 +46,7 @@ void heartbeat_send() {
cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "ip", g_ip_address);//发送设备ip
cJSON_AddStringToObject(body, "ID", mqtt_topic_pure_number());//发送设备号
cJSON_AddStringToObject(body, "ID", mqtt_topic_app2dev_number());//发送设备号
cJSON_AddStringToObject(body, "V", voltage_str);//心跳发送电压
cJSON_AddStringToObject(body, "I", current_str);//心跳发送电流
cJSON_AddStringToObject(body,"Tem",g_temperature);//发送温度
......
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