Files
gaoguangpu/src/main.cpp
2026-05-07 09:05:57 +08:00

2989 lines
92 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "head.h"
// #define GPS_4G
EventGroupHandle_t Main_EventGroup;
/*
任务名Task0
任务功能:光谱数据采集
*/
TaskHandle_t Task0_Handler;
void Task0(void *pvParameters);
/*
任务名Task1
任务功能:外设控制,加热 、推杆、舵机、温度采集、时间获取
*/
TaskHandle_t Task1_Handler;
void Task1(void *pvParameters);
/*
任务名Task2
任务功能:数据上传
*/
TaskHandle_t Task2_Handler;
void Task2(void *pvParameters);
/*
任务名task_4G_mode
任务功能4G模式
*/
void task_4G_mode(void *pvParameters);
void OTA_task(void *pvParameters);
//work_mode 0:手动 1时间模式 time_mode 2高级模式 advanced_mode 3:low_power_mode 44G_mode
//caiji_mode 0停止 1单次 2连续 3定标 4高级模式 5回传数据 6低功耗模式
//atuo_return 0:不自动返回 1自动返回
//return_mode 设置返回模式 暂时没用
//return_data_type 详情见你懂的O.o
//time_interval 时间间隔不能等于0
//caiji_state 0 :未采集no 1采集完成finish 2采集中working
//dingbiao_time 0自动曝光 非0使用这个时长曝光
//caiji_mode 0停止 1单次 2连续 3定标 4高级模式 5回传数据 6低功耗模式
volatile u_int32_t work_mode = 0,caiji_mode = 0,return_mode = 1,atuo_return = 0,return_data_type = 0x00,time_interval = 60 * 2,caiji_state = 0 ,fengbao_num=0;
volatile float start_time = 1.0,stop_time = 20.30,maximum_temperature = 10,Minimum_temperature = 0,bc_b1,bc_b2,bc_b3,bc_b4,sun_latitude,sun_longitude;
volatile float calculation_bochang1,calculation_bochang2;
// volatile u_int32_t http_flag = 0;
/// @brief 高级模式参数
// advanced_direction 0:up 1:down 2:dark
// advanced_shutter_time 曝光时间
// advanced_collect_times 采集次数
// advanced_remove_dark 0:不去暗 1:去暗
volatile u_int32_t advanced_direction=0,advanced_shutter_time=0,advanced_collect_times=0,advanced_remove_dark=0;
u_int32_t timeOut = 10000;
// SoftwareSerial *wb485port;
SemaphoreHandle_t xMutexInventory = NULL; //创建信号量Handler
//json
DynamicJsonDocument doc(1024);//接收串口json
DynamicJsonDocument sys_sd_doc(1024);//读取系统json
DynamicJsonDocument doc_4G(200);//读取ds18b20 json
void json_command(uint8_t port_type);
void sys_info_init();
//////ds1302时间/////////
Ds1302 rtc(0, 8, 7);
Ds1302::DateTime now;
bool sys_set_time(uint32_t year,uint32_t month,uint32_t day);
// const static char* WeekDays[] =
// {
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday",
// "Saturday",
// "Sunday"
// };
struct IS11_datastruct *IS11_datastructure;
//////is11/////////
SensorIS11 *is11Sensor;
STRSensorInfo STRSensorInfos_structure;
// SoftwareSerial *IS1Serial;
HardwareSerial *IS1Serial;
//原来
// HardwareSerial wb485Serial(0);
#define wb485Serial Serial0
size_t serialwrite(u_char* data,size_t lenth)
{
// return IS1Serial->write(data,lenth);
while (IS1Serial->available() > 0)
{
IS1Serial->read();
}
int rec=IS1Serial->write(data,lenth);
return rec;
}
size_t serialread(u_char *data, size_t lenth)
{
lenth=IS1Serial->available();
if (lenth<=0 )
{
/* code */
return 0;
}
int lenthofread = IS1Serial->readBytes(data,lenth);
return lenthofread;
}
////////////////////////////////AIR780EG///////////////////////////////////////////
// struct gps_struct{
// uint16_t year;
// uint8_t month;
// uint8_t day;
// uint8_t hour;
// uint8_t minute;
// uint8_t second;
// float latitude;
// float longitude;
// };
// String get_GPS(gps_struct *gps);
// String getnetData();
////////////////////////////////////////////////////////////////////////////////////
// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr);
// void sd_read_set_ds18b20_address();
bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff);
void send_dingbiao(String dingbiao_path);
void dingbiao_init(String dingbiao_path);
void rm_dir_or_file(const char * dirname);
void low_power_mode(uint8_t state);
//////////////////////////////////////////////////MQTT//////////////////////////////////
bool UpdateData(String path,uint8_t *data, size_t lenth, String Contenttype = "");
void setup()
{
sun_latitude = 39.0864;
sun_longitude = 117.113055;
// Serial2.begin(115200, SERIAL_8N1, SIMUART_RX, SIMUART_TX);
//关闭光谱仪
pinMode(36,OUTPUT);
digitalWrite(36,LOW);
pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电 低电平断电
digitalWrite(37, LOW);
vTaskDelay(1000);
Serial0.begin(115200);
Serial.begin(115200);
log_init();
IS1Serial =new HardwareSerial(IS1Serial_port);
IS1Serial->begin(115200, SERIAL_8N1, IS1Serial_RX, IS1Serial_TX);
IS1Serial->setTimeout(1);
//初始化SD卡
sdcard::init_sdcard();
sdcard::testwriet();
sdcard::Mkdir("/guangpu_data");
sdcard::Mkdir("/log");
sdcard::Mkdir("/dingbiao");
sdcard::Mkdir("/system");
//检查SD卡是否有log.log文件 没有则创建
// unsigned char log_level = 10;
if(!SD_MMC.exists(log_path))
{
File file;
file = SD_MMC.open(log_path,"ab+");
file.flush();
file.close();
}
if(!SD_MMC.exists(gp_log))
{
//检查SD卡是否有log.log文件 没有则创建
File file;
file = SD_MMC.open(gp_log,"ab+");
file.flush();
file.close();
}
// 初始化DS18b20
uint8_t temp_number = DS18b20_init();
//ADC电压采集初始化
// adc_init();
// write_log(log_path,"adc_read: " + String(adc_read()),10);
//初始化舵机
servo_init(21,0);
// servo_set_angle(0);
// vTaskDelay(1000 * 3);
//初始化蜂鸣器
beep_init(6,2000,12);
//初始化时间
rtc.init();
String time = String(now.year)+String(now.month)+String(now.day)+String(now.hour)+String(now.minute)+String(now.second);
write_log(log_path,time,20);
write_log(log_path,"system start",20);
//初始化74hc595
my74hc595_init();
//开启光谱仪
pinMode(36,OUTPUT);
digitalWrite(36,HIGH);
vTaskDelay(1000 * 2);
InitFunction(serialwrite,serialread);
is11Sensor = new SensorIS11();
STRSensorInfos_structure = is11Sensor->initSensor();
Serial0.println("IS_SN :" +STRSensorInfos_structure.serialnumber);
sys_info_init();
dingbiao_init("/dingbiao/dingbiao_up_gain.bin");
dingbiao_init("/dingbiao/dingbiao_up_offset.bin");
dingbiao_init("/dingbiao/dingbiao_down_gain.bin");
dingbiao_init("/dingbiao/dingbiao_down_offset.bin");
dingbiao_init("/dingbiao/fanshelv_gain.bin");
// gsmm_mqtt_init();
// myusb_init();
// setupmsc();
/* code */
servo_up();
vTaskDelay(1000 * 1.5);
// servo_set_angle(0);
servo_down();
vTaskDelay(1000 * 1.5);
beep_run(2000,1000);
vTaskDelay(1000 * 1.5);
beep_stop();
Main_EventGroup = xEventGroupCreate();
xMutexInventory = xSemaphoreCreateMutex(); //创建MUTEX
if(sys_sd_doc["status_4g"] == "open_4g")
{
gsmm_mqtt_init(STRSensorInfos_structure.serialnumber);
// while (1)
// {
// ////获取GPS
// write_log(log_path,"test gps",10);
// gps_struct gps_structure;
// String gpsbac = get_GPS(&gps_structure);
// write_log(log_path,gpsbac,10);
// Serial.printf("time: %d-%d-%d %d:%d:%d\n",gps_structure.year,gps_structure.month,gps_structure.day,gps_structure.hour,gps_structure.minute,gps_structure.second);
// Serial.printf("latitude: %d ,longitude : %d\n",gps_structure.latitude,gps_structure.longitude);
// vTaskDelay(1000);
// }
xTaskCreatePinnedToCore(task_4G_mode, "task_4G_mode",1024*(20),NULL, 1,NULL, 1);
xTaskCreatePinnedToCore(OTA_task, "OTA_task",1024*(10),NULL, 1,NULL, 1);
}
if (xMutexInventory == NULL)
{
}
else
{
//创建并启动Core1的任务 xTaskCreatePinnedToCore
xTaskCreatePinnedToCore(Task0,"Task0",1024 * 10,NULL,2,&Task0_Handler,0);
xTaskCreatePinnedToCore(Task1,"Task1",1024 * 5, NULL,1,&Task1_Handler, 0);
xTaskCreatePinnedToCore(Task2,"Task2",1024 * 30,NULL,5,&Task2_Handler, 1);
}
// write_log(log_path,"setup ok",10);
}
void loop()
{
vTaskDelay(10000);
// uint32_t cba = 0;
// String stra = getnetData();;
// Serial0.println(stra);
// UpdateData("/HyperspectralV2/DataUpload.php", (uint8_t *)stra.c_str() , stra.length());
}
void Task0(void *pvParameters)
{
vTaskDelay(1);
write_log(log_path,"task 0",10);
uint32_t uint_work_task0 = 100;
while (1)
{
if(work_mode == 4)
{
vTaskDelay(5);
continue;
}
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
if(uint_work_task0 == 100 && caiji_mode != 2)
{
caiji_mode = 0;
uint_work_task0 = 0;
}
if (caiji_state == 2)
{
caiji_state = 1;
}
uint_work_task0 = caiji_mode;
xSemaphoreGive(xMutexInventory);
}
//caiji_mode 0停止 1单次 2连续 3定标 4定标 5回传数据
switch (uint_work_task0)
{
case 0:
break;
case 1:
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_state = 2;
xSemaphoreGive(xMutexInventory);
}
is11Sensor->TakeOneJob();
if (atuo_return == 1)
{
is11Sensor->senddata(&wb485Serial,return_data_type, fengbao_num);
}
uint_work_task0 = 100;
break;
case 2:
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_state = 2;
xSemaphoreGive(xMutexInventory);
}
is11Sensor->TakeOneJob();
if (atuo_return == 1)
{
is11Sensor->senddata(&wb485Serial,return_data_type, fengbao_num);
}
vTaskDelay(1000 * time_interval);
break;
case 3:
is11Sensor->senddata(&wb485Serial,return_data_type, fengbao_num);
uint_work_task0 = 100;
break;
case 4:
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_state = 2;
xSemaphoreGive(xMutexInventory);
}
is11Sensor->advanced_mode(advanced_direction,advanced_shutter_time,advanced_collect_times,advanced_remove_dark);
uint_work_task0 = 100;
break;
case 5:
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_state = 2;
xSemaphoreGive(xMutexInventory);
}
is11Sensor->opt(advanced_direction);
uint_work_task0 = 100;
break;
case 6:
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_state = 2;
xSemaphoreGive(xMutexInventory);
}
is11Sensor->calculation(calculation_bochang1,calculation_bochang2);
uint_work_task0 = 100;
break;
default:
break;
}
vTaskDelay(5);
}
}
//Task1的实现
void Task1(void *pvParameters)
{
vTaskDelay(3);
write_log(log_path,"task 1",10);
uint8_t last_day = 100;
uint32_t count = 10;
while(1)
{
// vTaskDelay(1000);
// //获取时间
// rtc.getDateTime(&now);
// //获取温度
// // float temp[8];
// // getall_temp(temp);
// // //平均温度
// // float temp_ave = 0;
// // for (size_t i = 0; i < 8; i++)
// // {
// // temp_ave += temp[i];
// // }
// // temp_ave = temp_ave / 8;
// // write_log(log_path,"temp_ave: " + String(temp_ave),log_level);
// //获取GPS
// gps_struct gps_structure;
// String gpsbac = get_GPS(&gps_structure);
//计算日出日落
// double transit, sunrise, sunset;
// double utc_offset = 8;
// if (gpsbac != "-1")
// {
// calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset);
// }
// else
// {
// write_log(log_path,"no gps",10);
// }
// //日出日落控制 太阳升起前推杆缩,太阳落下后推杆伸
// write_log(log_path,"now.hour: " + String(now.hour),log_level);
// write_log(log_path,"sunrise: " + String((int)sunrise),log_level);
// write_log(log_path,"sunset: " + String((int)sunset),log_level);
// if (now.hour+1 == (int)sunrise)
// {
// tuigan(suo);
// }
// else if (now.hour+1 >= (int)sunset)
// {
// tuigan(tui);
// }
// if(sys_sd_doc["work_mode"] == "time_mode")
// {
// float now_time = now.hour + now.minute/100;
// if(now_time > start_time && now_time < stop_time)
// {
// }
// else
// {
// if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
// {
// caiji_mode = 0;
// }
// xSemaphoreGive(xMutexInventory);
// }
// }
// write_log(log_path,"temp_ave: " + String(temp_ave),log_level);
// if(temp_ave < Minimum_temperature)
// {
// write_log(log_path,"start jiaresi",10);
// jiaresi(5,start_jiare);
// vTaskDelay(1000 * 10);
// }
// else if (temp_ave > Minimum_temperature + 5)
// {
// write_log(log_path,"stop jiaresi",10);
// jiaresi(5,stop_jiare);
// }
// for(int ab ; ab < 10; ab++)
// {
// gps_struct gps_structure;
// String gpsbac = get_GPS(&gps_structure);
// if(gpsbac == "-1")
// {
// write_log(log_path,"no gps",10);
// }
// else
// {
// sun_latitude = gps_structure.latitude;
// sun_longitude = gps_structure.longitude;
// }
rtc.getDateTime(&now);
write_log(log_path, String(now.year) +"."+ String(now.month) +"."+ String(now.day)+" "+String(now.hour)+":"+String(now.minute)+":"+String(now.second),20);
// String path = "/guangpu_data/"+ String(now.year) + "/" + String(now.month) + "/" + String(now.day);
// if(!SD_MMC.exists(log_path))
//////////////////////////////////////////////////////////////////////////////////////////////////////////
double cardSize = SD_MMC.cardSize() / (1024 * 1024);
double used_size = SD_MMC.usedBytes() / (1024 * 1024);
float used_percent = used_size / cardSize;
// write_log(log_path,"used_percent: " + String(used_percent),10);
//数据清除规则:1、满十年清除第一年数据 2、当数据>90%时删除最前一个月数据
if(used_percent >= 0.8)
{
if (used_percent >= 0.9)
{
//删除文件
String path_b = "/gaugpu_data";
for(int i=24;i<255 ;i++)
{
for (uint8_t j = 1; j < 13; j++)
{
String path_a = path_b + "/" +String(i)+ "/" +String(j);
if(SD_MMC.exists(path_a))
{
rm_dir_or_file(path_a.c_str());
}
}
}
}
//报警
write_log(log_path,"sdcard will full," + String(used_percent * 100) + "%" +"used.",20);
}
if(SD_MMC.exists("/guangpu_data/" + String(now.year - 10) ))
{
String path = "/guangpu_data/" + String(now.year - 10);
rm_dir_or_file(path.c_str());
}
vTaskDelay(1000 * 1);
// write_log(log_path,"delete /guangpu_data/0 1111111111",10);
// if(SD_MMC.exists("/guangpu_data/0"))
// {
// write_log(log_path,"delete /guangpu_data/0 2222222222222222",10);
// // sdcard::deleteFolderOrFile("/gaugpu_data/0");
// rm_dir_or_file("/guangpu_data/0");
// }
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////时间/////////////////////////////////////////
//获取RTC时间
rtc.getDateTime(&now);
#ifdef GPS_4G
if(now.day != last_day)
{
// 获取网络时间
gps_struct net_structure;
String net_date = getnetData();
write_log(log_path,"net_date is :"+net_date,10);
// 2024-07-12 07:12:45
//截取时间
if(net_date != "-1")
{
net_structure.year = net_date.substring(0,4).toInt() - 2000;
net_structure.month = net_date.substring(5,7).toInt();
net_structure.day = net_date.substring(8,10).toInt();
net_structure.hour = net_date.substring(11,13).toInt();
net_structure.minute = net_date.substring(14,16).toInt();
net_structure.second = net_date.substring(17,-1).toInt();
if(now.year != net_structure.year || now.month != net_structure.month || now.day != net_structure.day || now.hour - net_structure.hour || (fabs(now.minute - net_structure.minute)>5 ))
{
now.year = net_structure.year;
now.month = net_structure.month;
now.day = net_structure.day;
now.hour = net_structure.hour;
now.minute = net_structure.minute;
now.second = net_structure.second;
rtc.setDateTime(&now);
}
last_day = now.day;
}
else
{
////获取GPS
gps_struct gps_structure;
String gpsbac = get_GPS(&gps_structure);
if(gpsbac == "-1") write_log(log_path,"no gps",10);
if(net_date != "-1" && gpsbac != "-1")
{
gps_structure.year = gps_structure.year - 2000;
if(now.year != gps_structure.year || now.month != gps_structure.month || now.day != gps_structure.day || now.hour != gps_structure.hour || (fabs(now.minute - net_structure.minute)>5 ))
{
now.year = gps_structure.year;
now.month = gps_structure.month;
now.day = gps_structure.day;
now.hour = gps_structure.hour;
now.minute = gps_structure.minute;
now.second = gps_structure.second;
rtc.setDateTime(&now);
}
last_day = now.day;
}
}
rtc.getDateTime(&now);
}
#endif
//获取时间
if(sys_sd_doc["work_mode"] == "time_mode")
{
float now_time = now.hour + now.minute/100;
if( (now_time > start_time) && (now_time < stop_time ))
{
}
else
{
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_mode = 0;
xSemaphoreGive(xMutexInventory);
}
}
}
vTaskDelay(1000 * 1);
// 推杆
// 日出日落控制 太阳升起前推杆缩,太阳落下后推杆伸
double transit, sunrise, sunset;
double utc_offset = 8;
float tuisuotime = now.hour *100 + now.minute;
tuisuotime = tuisuotime /100;
calcSunriseSunset(now.year+2000,now.month,now.day,sun_latitude,sun_longitude, transit, sunrise, sunset);
sunrise += utc_offset;
sunset += utc_offset;
write_log(log_path,"now.hour: " + String(now.hour),log_level);
write_log(log_path,"sunrise: " + String((int)sunrise),log_level);
write_log(log_path,"sunset: " + String((int)sunset),log_level);
write_log(log_path,"tuisuotime: " + String(tuisuotime),log_level);
if(sunrise<0)
{
sunrise = sunrise + 24;
if(tuisuotime >= (sunset+1) && tuisuotime <= (sunrise-1))
{
write_log(log_path,"tuisuotime: " + String(tuisuotime),20);
write_log(log_path,"sunrise: " + String(sunrise),20);
write_log(log_path,"sunset: " + String(sunset),20);
write_log(log_path,"tui",20);
tuigan(tui);
jiaresi(6,start_jiare);
}
else
{
write_log(log_path,"tuisuotime: " + String(tuisuotime),20);
write_log(log_path,"sunrise: " + String(sunrise),20);
write_log(log_path,"sunset: " + String(sunset),20);
write_log(log_path,"suo",20);
tuigan(suo);
jiaresi(6,stop_jiare);
}
}
else
{
if(tuisuotime >= (sunset+1) || tuisuotime <= (sunrise-1))
{
write_log(log_path,"tuisuotime: " + String(tuisuotime),20);
write_log(log_path,"sunrise: " + String(sunrise),20);
write_log(log_path,"sunset: " + String(sunset),20);
write_log(log_path,"tui",20);
tuigan(tui);
jiaresi(6,start_jiare);
}
else
{
write_log(log_path,"tuisuotime: " + String(tuisuotime),20);
write_log(log_path,"sunrise: " + String(sunrise),20);
write_log(log_path,"sunset: " + String(sunset),20);
write_log(log_path,"suo",20);
tuigan(suo);
jiaresi(6,stop_jiare);
}
}
vTaskDelay(1000 * 1);
float temperature[8];
getall_temp(temperature);
// vTaskDelay(100);
// getall_temp(temperature);
// vTaskDelay(100);
// getall_temp(temperature);
for(uint8_t n=0; n<8 ;n++)
{
if(temperature[n] < -120) write_log(log_path,"temperature " + String(n+1) +":" + String(temperature[n])+"This sensor is broken.",20);
if(count == 10)
{
write_log(log_path,"temperature " + String(n) +":" + String(temperature[n]),20);
count = 0;
}else
write_log(log_path,"temperature " + String(n) +":" + String(temperature[n]),10);
}
if(temperature[2] / 2 > maximum_temperature)
{
write_log(log_path,"stop_jiare",20);
jiaresi(7,stop_jiare);
}
if( temperature[2] < Minimum_temperature)
{
if(temperature[2] < -120)
{
write_log(log_path,"temperature 2 is broken.",20);
jiaresi(7,stop_jiare);
}
else
{
write_log(log_path,"start_jiare",20);
jiaresi(7,start_jiare);
}
}
// vTaskDelay(1000 * 1);
// sdcard::Mkdir(path);
// float temp4 = getone_temp(4);
// float temp6 = getone_temp(6);
// write_log(log_path,"temp4 : ",10);
// write_log(log_path,"temp6 : " + String(temp6),10);
// if((temp4+temp6)/2 < 0)
// {
// write_log(log_path,"start jiaresi",10);
// // jiaresi(5,start_jiare);
// vTaskDelay(1000 * 10);
// }
// else if ((temp4+temp6)/2 > 5)
// {
// write_log(log_path,"stop jiaresi",10);
// // jiaresi(5,stop_jiare);
// }
count++;
vTaskDelay(1000 * 60 * 1);
}
}
// Task2的实现
void Task2(void *pvParameters)
{
vTaskDelay(5);
write_log(log_path,"task 2",10);
unsigned char read_buf[1024*10];
wb485Serial.setTimeout(1);
while(1)
{
vTaskDelay(1);
unsigned short data_length = 0;
//监测接收串口数据
while(wb485Serial.available())
{
data_length += wb485Serial.readBytes(&read_buf[data_length],1024*10);
for(uint32_t i = 0; i < 5000; i++)
{
if(wb485Serial.available() > 0)
{
break;
}
}
}
data_length = IRIS_Cut_Befor_Header(read_buf,data_length);
//如果接收到数据
if (data_length > 0)
{
receive_command_unpack(read_buf,data_length,1);
}
// vTaskDelay(1);
}
}
struct http_data_head
{
char sn[20];
char version[20];
float lat;
float lon;
float is3_a1;
float is3_a2;
float is3_a3;
float is3_a4;
};
void task_4G_mode(void *pvParameters)
{
http_data_head http_head;
uint8_t *data = new u_char[sizeof(IS11_datastruct) + sizeof(http_head)];
while (1)
{
Serial0.println("task_4G_mode 11");
// vTaskDelay(1000 * 10);
bool warn_sta = false;
float temperature[8];
doc_4G["SN"] = STRSensorInfos_structure.serialnumber;
if(check_tuigan() == false )
{
warn_sta = true;
doc_4G["tuigan"] = "warn";
if(get_tuigan_status() == tui)
{
doc_4G["tuigan_status"] = "tui";
}
else
{
doc_4G["tuigan_status"] = "suo";
}
}
float Voltage = adc_read();
Serial0.println("Voltage:" + String(Voltage));
if(Voltage < 10)
{
warn_sta = true;
doc_4G["Voltage"] = "warn";
doc_4G["Voltage_value"] = Voltage;
}
// getall_temp(temperature);
for(uint8_t n=0; n<8 ;n++)
{
temperature[n] = IS11_datastruct_fanshelv.temprature[n];
if((temperature[n] < -10) || (temperature[n] > 50))
{
warn_sta = true;
doc_4G["temperature"] = "warn";
doc_4G["temperature 0 : "] = temperature[0];
doc_4G["temperature 1 : "] = temperature[1];
doc_4G["temperature 2 : "] = temperature[2];
doc_4G["temperature 3 : "] = temperature[3];
doc_4G["temperature 4 : "] = temperature[4];
doc_4G["temperature 5 : "] = temperature[5];
doc_4G["temperature 6 : "] = temperature[6];
doc_4G["temperature 7 : "] = temperature[7];
break;
}
Serial.println("uptodate temperature " + String(n) +":" + String(temperature[n]));
}
if(warn_sta)
{
String data_info;
serializeJson(doc_4G,data_info);
UpdateData("/HyperspectralV2/warning.php",(uint8_t *)data_info.c_str() ,data_info.length() ,HTTP_METHOD_POST);
}
//获取时间
Serial0.println("work_mode = " + String(work_mode) );
if(work_mode == 4)
{
String time_4g = getnetData();
Serial0.println("time_4g " + time_4g);
rtc.getDateTime(&now);
Serial0.println("rtc time :" + String(now.year) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second));
if(time_4g != "-1")
{
uint32_t year = time_4g.substring(0,4).toInt() - 2000;
uint32_t month = time_4g.substring(5,7).toInt();
uint32_t day = time_4g.substring(8,10).toInt();
uint32_t hour = time_4g.substring(11,13).toInt();
uint32_t minute = time_4g.substring(14,16).toInt();
uint32_t second = time_4g.substring(17,-1).toInt();
hour = hour + 8;
if(hour > 23)
{
hour = hour - 24;
day = day + 1;
}
if(now.year != year || now.month != month || now.day != day || now.hour - hour || (fabs(now.minute - minute)>5 ))
{
now.year = year;
now.month = month;
now.day = day;
now.hour = hour;
now.minute = minute;
now.second = second;
rtc.setDateTime(&now);
}
////获取GPS
gps_struct gps_structure;
String gpsbac = get_GPS(&gps_structure);
if(gpsbac == "-1") write_log(log_path,"no gps",10);
else if(gpsbac != "-1")
{
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
sun_latitude = gps_structure.latitude;
sun_longitude = gps_structure.longitude;
xSemaphoreGive(xMutexInventory);
}
Serial.println("gps_structure.latitude " + String(gps_structure.latitude));
Serial.println("gps_structure.longitude " + String(gps_structure.longitude));
// gps_structure.year = gps_structure.year - 2000;
// if(now.year != gps_structure.year || now.month != gps_structure.month || now.day != gps_structure.day || now.hour != gps_structure.hour || (fabs(now.minute - net_structure.minute)>5 ))
// {
// now.year = gps_structure.year;
// now.month = gps_structure.month;
// now.day = gps_structure.day;
// now.hour = gps_structure.hour;
// now.minute = gps_structure.minute;
// now.second = gps_structure.second;
// // rtc.setDateTime(&now);
// }
// last_day = now.day;
}
}
float now_time = now.hour + now.minute/100;
Serial0.println("now_time " + String(now_time));
Serial0.println("start_time " + String(start_time));
Serial0.println("stop_time " + String(stop_time));
// vTaskDelay(1000 * 1);
if((now_time >= start_time) && (now_time < stop_time))
{
is11Sensor->TakeOneJob();
is11Sensor->get_fanshelv();
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
// doc_4G["SN"] = STRSensorInfos_structure.serialnumber;
// doc_4G["latitude"] = sun_latitude;
// doc_4G["longitude"] = sun_longitude;
// doc_4G["Version"] = sys_sd_doc["version"] ;
// doc_4G["a1"] = STRSensorInfos_structure.a1;
// doc_4G["a2"] = STRSensorInfos_structure.a2;
// doc_4G["a3"] = STRSensorInfos_structure.a3;
// doc_4G["a4"] = STRSensorInfos_structure.a4;
// http_head.sn
String sn = STRSensorInfos_structure.serialnumber;
String version = sys_sd_doc["version"] ;
memcpy(http_head.sn,sn.c_str(),sn.length());
memcpy(http_head.version,version.c_str(),version.length());
http_head.lat = sun_latitude;
http_head.lon = sun_longitude;
http_head.is3_a1 = STRSensorInfos_structure.a1;
http_head.is3_a2 = STRSensorInfos_structure.a2;
http_head.is3_a3 = STRSensorInfos_structure.a3;
http_head.is3_a4 = STRSensorInfos_structure.a4;
xSemaphoreGive(xMutexInventory);
}
memcpy(data,&http_head,sizeof(http_head));
memcpy((data + sizeof(http_head)),&IS11_datastruct_fanshelv,sizeof(IS11_datastruct_fanshelv));
vTaskDelay(10);
UpdateData("/HyperspectralV2/DataUpload.php",data , sizeof(http_head) + sizeof(IS11_datastruct_fanshelv),HTTP_METHOD_POST);
}
Serial.println("http_head.lat " + String(http_head.lat));
Serial.println("http_head.lon " + String(http_head.lon));
vTaskDelay(1000 * time_interval);
}
vTaskDelay(10);
}
}
void OTA_task(void *pvParameters)
{
while (1)
{
xEventGroupWaitBits(Main_EventGroup, OTA_BIT, pdTRUE, pdFALSE, portMAX_DELAY);
vTaskDelay(100);
String url = sys_sd_doc["version_url"];
String MD5 = sys_sd_doc["MD5"];
http_OTA(url, MD5);
}
}
void receive_command_unpack(uint8_t * read_buf,uint16_t data_length,uint8_t port_type)
{
unsigned char command_data[1024*10];
unsigned char data_type;
String send_data ;
int send_lenth;
uint8_t low_power_sta = 1;
u_char send_buff[300];
int ret = IRIS_STM32_Protocol_Unpack(read_buf,data_length,&data_type,command_data);
if (ret > 0)
{
// write_log(log_path,"data_type:" +String(data_type),log_level);
// 改写为switch
switch (data_type)
{
case 0x00:
{
fengbao_num = 0;
// String command_str = String((char*)command_data,ret);
// Serial0.println("33333 " + command_str);
deserializeJson(doc,command_data);
json_command(port_type);
doc.clear();
}
break;
// case 0x01:
// break;
case 0x03:
{
String dingbiao_path;
if (command_data[0] == 4 || command_data[0] == 5 || command_data[0] == 6 )
{
// is11Sensor->save_dingbiao(command_data);
//保存到sd卡
if(command_data[0] == 4)
{
if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_up_gain.bin";
else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_up_offset.bin";
}
else if(command_data[0] == 5)
{
if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_down_gain.bin";
else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_down_offset.bin";
}
else if(command_data[0] == 6)
{
if(command_data[1] == 0) dingbiao_path = "/dingbiao/fanshelv_gain.bin";
}
is11Sensor->save_dingbiao(command_data);
File file = SD_MMC.open(dingbiao_path, "wb");
file.write((uint8_t *)&command_data, ret);
file.flush();
file.close();
}
}
break;
case 0x60:
{
uint8_t a[15];
rtc.getDateTime(&now);
a[0] = now.year;
a[1] = now.month;
a[2] = now.day;
a[3] = now.hour;
a[4] = now.minute;
a[5] = now.second;
a[6] = low_power_sta;
float la,lo;
la = sun_latitude;
lo = sun_longitude;
memcpy(&a[7],&la,4);
memcpy(&a[11],&lo,4);
send_lenth = IRIS_Protocol_Pack(0x60,15,a,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x61:
{
float data[4] = {0};
data[0] = STRSensorInfos_structure.a1;
data[1] = STRSensorInfos_structure.a2;
data[2] = STRSensorInfos_structure.a3;
data[3] = STRSensorInfos_structure.a4;
send_lenth = IRIS_Protocol_Pack(0x61,16,(uint8_t *)data,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x62:
{
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
caiji_mode = 1;
xSemaphoreGive(xMutexInventory);
}
uint8_t a = 0x01;
send_lenth = IRIS_Protocol_Pack(0X62,1,&a,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x63:
{
uint8_t sta; //0未开始 1完成 2正在采集
if(caiji_state == 1)
{
doc["caiji_state"] = "finish";
sta = 1;
}
else if(caiji_state == 2 )
{
doc["caiji_state"] = "working";
sta = 2;
}
else sta = 0;
send_lenth = IRIS_Protocol_Pack(0x63,1,&sta,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x64:
{
return_data_type = command_data[0];
fengbao_num = command_data[1];
caiji_mode = 3;
}
break;
case 0x65:
{
uint8_t bb = 0x01;
if(ret != 6)
{
bb = 0x00;
send_lenth = IRIS_Protocol_Pack(0x65,1,&bb,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
break;
}
now.year = command_data[0];
now.month = command_data[1];
now.day = command_data[2];
now.hour = command_data[3];
now.minute = command_data[4];
now.second = command_data[5];
rtc.setDateTime(&now);
rtc.getDateTime(&now);
send_lenth = IRIS_Protocol_Pack(0x65,1,&bb,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x66:
{
low_power_sta = command_data[0];
low_power_mode(low_power_sta);
uint8_t dd = 0x01;
send_lenth = IRIS_Protocol_Pack(0x66,1,&dd,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x67:
{
float a,b;
memcpy(&a,command_data,4);
memcpy(&b,command_data+4,4);
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
calculation_bochang1 = a;
calculation_bochang2 = b;
caiji_mode = 6;
xSemaphoreGive(xMutexInventory);
}
// uint8_t dd = 0x01;
is11Sensor->calculation_value = 0;
uint8_t ddd = 1;
send_lenth = IRIS_Protocol_Pack(0x67,1,&ddd,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x68:
{
float ddd = is11Sensor->calculation_value;
uint8_t aaa[4];
memcpy(aaa,&ddd,4);
send_lenth = IRIS_Protocol_Pack(0x68,4,aaa,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
case 0x69:
{
float ab;
memcpy(&ab,command_data,4);
sun_latitude = ab;
memcpy(&ab,command_data+4,4);
sun_longitude =ab;
uint8_t ac = 1;
send_lenth = IRIS_Protocol_Pack(0x69,1,&ac,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
sys_sd_doc["GPS_mode"]== "manual";
sys_sd_doc["latitude"] = sun_latitude;
sys_sd_doc["longitude"] = sun_longitude;
String send_str = "";
serializeJson(sys_sd_doc, send_str);
File file;
file = SD_MMC.open("/system/system_info.json","w+");
file.println(send_str);
file.flush();
file.close();
}
break;
case 0x6A:
{
uint8_t ddc = get_tuigan_status();
send_lenth = IRIS_Protocol_Pack(0x70,1,&ddc,send_buff);
if(port_type == 1) wb485Serial.write(send_buff,send_lenth);
else if(port_type == 2) mqtt_write(send_buff,send_lenth);
}
break;
default:
break;
}
// send_data = "wb485 success";
// write_log(log_path,send_data,log_level);
// send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff);
// wb485Serial.write(send_buff,send_lenth);
}
else
{
send_data = "send data error";
// write_log(log_path,send_data,log_level);
send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff);
wb485Serial.write(send_buff,send_lenth);
}
memset(read_buf, 0, sizeof(read_buf));
memset(command_data, 0, sizeof(command_data));
}
/*
//处理json命令
command : 1、get_sensor_info
2、set_sensor_info
3、set_work_mode
4、get_work_mode
5、set_time_interval
6、start_work
7、stop_work
8、set_return_mode
9、set_atuo_return
10、set_return_data_type
11、get_9
12、
13、get_data
14、set_bochangxishu
15、get_bochangxishu
16、get_chongcaiyangbochang
18、get_darkcurrent
19、set_servo_offset
20、set_start_time
21、set_stop_time
22、set_maximum_temperature
23、set_Minimum_temperature
24、
*/
/*{"command":"","name":"gaoguangpu","version":"v2.0","SN":"123456","date":"","caiji_mode":"","time_interval":"","return_data_type":"","bochangxishu":""}*/
void json_command(uint8_t port_type)
{
int ret;
bool save = false;
String send_str;
u_char send_buff[1000];
String json_str;
if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS)
{
//command 1
if (doc["command"] == "get_sensor_info")
{
rtc.getDateTime(&now);
sys_sd_doc["date"] = String(now.year+2000) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second);
send_str = "";
serializeJson(sys_sd_doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
serializeJson(sys_sd_doc, send_str);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 2
else if (doc["command"] == "set_sensor_info")
{
write_log(log_path,"set_sensor_info",log_level);
if(doc["name"] == NULL)
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "Name cannot be null";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
save = true ;
sys_sd_doc["name"] = doc["name"].as<String>();
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
if(doc["version"] == NULL)
{
}
else
{
save = true ;
sys_sd_doc["version"] = doc["version"].as<String>();
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 3
else if (doc["command"] == "set_work_mode")
{
if(doc["work_mode"] == "manual_mode")
{
if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1);
save = true ;
sys_sd_doc["work_mode"] = "manual_mode";
sys_sd_doc["caiji_mode"] = "single";
sys_sd_doc["atuo_return"] ="no";
sys_sd_doc["return_data_type"] = 0x00;
work_mode = 0;
caiji_mode = 0;
atuo_return = 0;
return_data_type = 0x00;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if (doc["work_mode"] == "time_mode")
{
if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1);
save = true;
sys_sd_doc["work_mode"] = "time_mode";
sys_sd_doc["caiji_mode"] = "continue";
sys_sd_doc["atuo_return"] = "yes";
sys_sd_doc["return_data_type"] = 0x00;
work_mode = 1;
caiji_mode = 0;
atuo_return = 1;
return_data_type = 0x00;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["work_mode"] == "advanced_mode")
{
if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1);
save = true;
sys_sd_doc["work_mode"] = "advanced_mode";
sys_sd_doc["caiji_mode"] = "advanced";
sys_sd_doc["atuo_return"] = "no";
sys_sd_doc["return_data_type"] = 0x00;
work_mode = 2;
caiji_mode = 0;
atuo_return = 0;
return_data_type = 0x00;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["work_mode"] == "low_power_mode")
{
save = true;
sys_sd_doc["work_mode"] = "low_power_mode";
sys_sd_doc["caiji_mode"] = "low_power";
sys_sd_doc["atuo_return"] = "no";
sys_sd_doc["return_data_type"] = 0x00;
work_mode = 3;
low_power_mode(0);
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["work_mode"] == "4G_mode")
{
save = true;
sys_sd_doc["work_mode"] = "4G_mode";
// sys_sd_doc["caiji_mode"] = "low_power";
// sys_sd_doc["atuo_return"] = "no";
// sys_sd_doc["return_data_type"] = 0x00;
work_mode = 4;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "work mode error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 4.1
else if (doc["command"] == "set_caiji_mode")
{
if(doc["caiji_mode"] == "start")
{
if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["caiji_mode"] == "stop")
{
if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["caiji_mode"] == "single")
{
save = true;
sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as<String>();
caiji_mode = 1;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if(doc["caiji_mode"] == "continue")
{
save = true;
sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as<String>();
caiji_mode = 2;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "work mode error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 4.2
else if (doc["command"] == "take_one_job")
{
if(caiji_mode == 0)
{
caiji_mode = 1;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "Working";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 4.3
else if (doc["command"] == "set_time_interval")
{
if (doc["time_interval"]>0)
{
save = true;
sys_sd_doc["time_interval"] = doc["time_interval"];
time_interval = doc["time_interval"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "time_interval error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 5
else if (doc["command"] == "get_work_mode")
{
doc.clear();
send_str = "";
doc["work_mode"] = sys_sd_doc["work_mode"].as<String>();
doc["caiji_mode"] = sys_sd_doc["caiji_mode"].as<String>();
doc["atuo_return"] = sys_sd_doc["atuo_return"].as<String>();
doc["return_data_type"] = sys_sd_doc["return_data_type"];
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 6
else if(doc["command"] == "set_atuo_return")
{
if(doc["atuo_return"] == "yes")
{
save = true;
sys_sd_doc["atuo_return"] = doc["atuo_return"].as<String>();
atuo_return = 1;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else if (doc["atuo_return"] == "no")
{
save = true;
sys_sd_doc["atuo_return"] = doc["atuo_return"].as<String>();
atuo_return = 0;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "atuo_return error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 7
else if (doc["command"] == "set_return_data_type")
{
u_int32_t b = doc["return_data_type"];
if(b >= 0)
{
save = true;
sys_sd_doc["return_data_type"] = doc["return_data_type"];
return_data_type = doc["return_data_type"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "return_data_type error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 8
//保留,没用
else if (doc["command"] == "set_return_mode")
{
save = true;
sys_sd_doc["return_mode"] = doc["return_mode"].as<String>();
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
/*设置回传模式 */
}
//command 9
else if (doc["command"] == "get_data")
{
u_int32_t b = doc["return_data_type"];
if(b >=0 && caiji_mode !=3)
{
return_data_type = b;
caiji_mode = 3;
}
else
{
send_str = "";
doc["caiji_state"] = "working or return_data_type error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
/* code */
}
//command 10
else if (doc["command"] == "set_bochangxishu")
{
float b1=0,b2=0,b3=0,b4=0;
b1 = doc["bochangxishu"]["b0"];
b2 = doc["bochangxishu"]["b1"];
b3 = doc["bochangxishu"]["b2"];
b4 = doc["bochangxishu"]["b3"];
if(b1!=0 && b2 !=0 && b3 != 0 && b4 != 0)
{
save = true;
bc_b1 = doc["bochangxishu"]["b0"];
bc_b2 = doc["bochangxishu"]["b1"];
bc_b3 = doc["bochangxishu"]["b2"];
bc_b4 = doc["bochangxishu"]["b3"];
sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"];
sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"];
sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"];
sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "bochangxishu error or null";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 11
else if (doc["command"] == "get_bochangxishu")
{
doc.clear();
doc["bochangxishu"]["a0"] = sys_sd_doc["bochangxishu"]["a0"];
doc["bochangxishu"]["a1"] = sys_sd_doc["bochangxishu"]["a1"];
doc["bochangxishu"]["a2"] = sys_sd_doc["bochangxishu"]["a2"];
doc["bochangxishu"]["a3"] = sys_sd_doc["bochangxishu"]["a3"];
doc["bochangxishu"]["b0"] = sys_sd_doc["bochangxishu"]["b0"];
doc["bochangxishu"]["b1"] = sys_sd_doc["bochangxishu"]["b1"];
doc["bochangxishu"]["b2"] = sys_sd_doc["bochangxishu"]["b2"];
doc["bochangxishu"]["b3"] = sys_sd_doc["bochangxishu"]["b3"];
send_str = "";
doc["state"] = "ok";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 12
else if (doc["command"] == "get_chongcaiyangbochang")
{
// String chongcaiyangbochang;
/* code */
}
//command 14
else if(doc["command"] == "set_servo_offset")
{
float angle = doc["servo_offset_angle"];
uint8_t dir;
if(doc["servo_offset_dir"] == "up") dir = 1;
else if(doc["servo_offset_dir"] == "down") dir = 0;
if(dir ==1 || dir ==0)
{
save = true;
if(dir==1) sys_sd_doc["servo_offset_angle_up"] = doc["servo_offset_angle"];
else if(dir==0) sys_sd_doc["servo_offset_angle_down"] = doc["servo_offset_angle"];
servo_offset(angle,dir);
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "servo_offset_dir error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 15
else if(doc["command"] == "set_start_time")
{
float t = doc["start_time"];
if(t>0 && t < 24)
{
save = true;
sys_sd_doc["start_time"] = doc["start_time"];
start_time = doc["start_time"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "start_time error or out of range";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 16
else if(doc["command"] == "set_stop_time")
{
float tt = doc["stop_time"];
if(tt>0 && tt < 24 && tt > start_time)
{
save = true;
sys_sd_doc["stop_time"] = doc["stop_time"];
stop_time = doc["stop_time"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "stop_time error or out of range";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 17
else if(doc["command"] == "set_maximum_temperature")
{
float te = doc["maximum_temperature"];
if(te < 100)
{
save = true;
maximum_temperature = doc["maximum_temperature"];
sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "maximum_temperature error or out of range";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 18
else if(doc["command"] == "set_Minimum_temperature")
{
float tte = doc["Minimum_temperature"];
if(tte < maximum_temperature)
{
save = true;
maximum_temperature = doc["Minimum_temperature"];
sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "Minimum_temperature error or out of range";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 19
else if(doc["command"] == "get_date")
{
send_str = "";
doc.clear();
rtc.getDateTime(&now);
doc["state"] = "ok";
doc["date"]["year"]= now.year;
doc["date"]["month"]= now.month;
doc["date"]["day"]= now.day;
doc["date"]["hour"]= now.hour;
doc["date"]["minute"]= now.minute;
doc["date"]["second"]= now.second;
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 20
else if(doc["command"] == "get_caiji_state")
{
uint32_t percent;
send_str = "";
doc.clear();
if(caiji_state == 1)
{
doc["caiji_state"] = "finish";
percent = is11Sensor->work_progress;
doc["percent"] = percent;
doc["info"] = "end";
}
else if(caiji_state == 2 )
{
doc["caiji_state"] = "working";
percent = is11Sensor->work_progress;
doc["percent"] = percent;
if(percent < 100)
{
doc["info"] = "working";
}
else
{
doc["info"] = "end";
}
}
else doc["caiji_state"] = "no";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 21
else if(doc["command"] == "start_opt")
{
if(sys_sd_doc["work_mode"] == "advanced_mode")
{
caiji_mode = 5;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "The working mode is not advanced_mode";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 22
else if(doc["command"] == "get_opt")
{
if(sys_sd_doc["work_mode"] == "advanced_mode")
{
send_str = "";
uint32_t aa = is11Sensor->dingbiao_shutter;
doc["opt"] = aa;
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "The working mode is not advanced_mode";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
else if(doc["command"] == "start_collect")
{
bool collect = true;
if(sys_sd_doc["work_mode"] == "advanced_mode")
{
// caiji_mode = 3;
if (doc["direction"] == "up")
{
advanced_direction = 0;
}
else if (doc["direction"] == "down")
{
advanced_direction = 1;
}
else if (doc["direction"] == "dark")
{
advanced_direction = 2;
}
else
{
collect = false;
// send_str = "";
// doc["state"] = "failed";
// doc["info"] = "direction error";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
}
if(doc["shutter_time"]>=1)
{
advanced_shutter_time = doc["shutter_time"];
}
else
{
collect = false;
// send_str = "";
// doc["state"] = "failed";
// doc["info"] = "shutter_time error";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
}
if(doc["collect_times"]>0)
{
advanced_collect_times = doc["collect_times"];
}
else
{
collect = false;
// send_str = "";
// doc["state"] = "failed";
// doc["info"] = "collect_times error";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
}
if(doc["remove_dark"] == "yes")
{
advanced_remove_dark = 1;
}
else if(doc["remove_dark"] == "no")
{
advanced_remove_dark = 0;
}
else
{
collect = false;
// send_str = "";
// doc["state"] = "failed";
// doc["info"] = "remove_dark error";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
}
if (collect)
{
caiji_mode = 4;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "parameter error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "The working mode is not advanced_mode";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 25
else if(doc["command"] == "set_sys_time")
{
uint32_t year,month,day,hour,minute,second;
year = doc["year"];
month = doc["month"];
day = doc["day"];
hour = doc["hour"];
minute = doc["minute"];
second = doc["second"];
write_log(log_path,"year" + String(year) + "month" + String(month) + "day" + String(day) + "hour" + String(hour) + "minute" + String(minute) + "second" + String(second),10);
if(year>99 || month>12 || day>31 || hour > 23 || minute >59 || second > 59)
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "date error1";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
if(sys_set_time(year,month,day))
{
now.year = year;
now.month = month;
now.day = day;
now.hour = hour;
now.minute =minute;
now.second =second;
// now.dow = 5;
rtc.setDateTime(&now);
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "date error2";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
}
//command 26
// else if(doc["command"] == "set_ds18b20_address")
// {
// uint8_t num = doc["num"];
// uint8_t addr[8];
// if (num>=0 && num<8)
// {
// addr[0] = doc["addr"]["0"];
// addr[1] = doc["addr"]["1"];
// addr[2] = doc["addr"]["2"];
// addr[3] = doc["addr"]["3"];
// addr[4] = doc["addr"]["4"];
// addr[5] = doc["addr"]["5"];
// addr[6] = doc["addr"]["6"];
// addr[7] = doc["addr"]["7"];
// set_ds18b20_address(num, addr);
// save_sd_ds18b20_address(num,addr);
// send_str = "";
// doc["state"] = "success";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
// }
// else
// {
// send_str = "";
// doc["state"] = "failed";
// doc["info"] = "num error";
// serializeJson(doc, send_str);
// ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
// }
// }
//command 27
// 导出定标文件
else if(doc["command"] == "get_dingbiao")
{
send_dingbiao("/dingbiao/dingbiao_up_gain.bin");
send_dingbiao("/dingbiao/dingbiao_up_offset.bin");
send_dingbiao("/dingbiao/dingbiao_down_gain.bin");
send_dingbiao("/dingbiao/dingbiao_down_offset.bin");
}
//command 28
//获取电源电压
else if(doc["command"] == "get_voltage" )
{
float voltage = adc_read();
doc["voltage"] = voltage;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 29
//设置重采样步长
else if(doc["command"] == "set_chongcaiyang_step")
{
if(doc["step"]>0)
{
save = true;
is11Sensor->chongcaiyang_step = doc["step"];
sys_sd_doc["chongcaiyang_step"] = doc["step"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "step error";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
}
//command 30
//获取重采样步长
else if(doc["command"] == "get_chongcaiyang_step")
{
send_str = "";
doc["state"] = "success";
doc["step"] = is11Sensor->chongcaiyang_step;
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 31
//计算波长
else if(doc["command"] =="calculation")
{
calculation_bochang1 = doc["bochang1"];
calculation_bochang2 = doc["bochang2"];
caiji_mode = 6;
is11Sensor->calculation_value = 0;
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 32
//获取波长计算的结果
else if(doc["command"] =="get_calculation_result")
{
send_str = "";
doc["calculation_result"] = is11Sensor->calculation_value;
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 33
else if(doc["command"] =="set_latitude_longitude")
{
save = true;
sys_sd_doc["latitude"] = doc["latitude"];
sys_sd_doc["longitude"] = doc["longitude"];
sun_latitude = doc["latitude"];
sun_longitude = doc["longitude"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 34
else if(doc["command"] == "get_fanshelv_gain")
{
send_dingbiao("/dingbiao/fanshelv_gain.bin");
}
//command 35
else if(doc["command"] == "set_4g")
{
save = true;
sys_sd_doc["status_4g"] = doc["status_4g"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//command 36
else if(doc["command"] == "OTA")
{
sys_sd_doc["version_url"] = doc["version_url"];
sys_sd_doc["MD5"] = doc["MD5"];
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
xEventGroupSetBits(Main_EventGroup, OTA_BIT);
}
//command 37
else if(doc["command"] == "USB")
{
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
// myusb_init();
setupmsc();
}
//command 38
else if(doc["command"] == "reboot")
{
// setupmsc();
send_str = "";
doc["state"] = "success";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
esp_restart();
}
//command error
else
{
send_str = "";
doc["state"] = "failed";
doc["info"] = "no command";
serializeJson(doc, send_str);
ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff);
// wb485Serial.write(send_buff, ret);
if(port_type == 1) wb485Serial.write(send_buff,ret);
else if(port_type == 2) mqtt_write(send_buff,ret);
}
//将修改后的系统信息写入内存卡
if(save == true)
{
send_str = "";
serializeJson(sys_sd_doc, send_str);
File file;
file = SD_MMC.open("/system/system_info.json","w+");
file.println(send_str);
file.flush();
file.close();
save = false;
}
xSemaphoreGive(xMutexInventory);
}
}
void sys_info_init()
{
gps_struct gps_structure;
String system_info;
String rtcdate_now ="20" + String(now.year) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second);
write_log(log_path,rtcdate_now,10);
if(SD_MMC.exists("/system/system_info.json"))
{
write_log(log_path,"has system_info.json file",log_level);
File file;
file = SD_MMC.open("/system/system_info.json","rb+");
system_info = file.readString();
file.flush();
file.close();
deserializeJson(doc, system_info);
//初始化系统信息
sys_sd_doc["name"] = doc["name"].as<String>();
sys_sd_doc["sensor_type"] = "IRIS-IS11";
sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber;
// sys_sd_doc["serialnumber"] = "is110008";
sys_sd_doc["version"] = doc["version"].as<String>();
sys_sd_doc["date"] = rtcdate_now;
sys_sd_doc["status_4g"] = doc["status_4g"].as<String>();
sys_sd_doc["work_mode"] = doc["work_mode"].as<String>(); //一会
if(doc["work_mode"] == "manual_mode")
{
work_mode = 0;
}
else if (doc["work_mode"] == "time_mode")
{
work_mode = 1;
}
else if (doc["work_mode"] == "4G_mode")
{
work_mode = 4;
}
else
{
work_mode = 0;
}
// if(caiji_mode == 7)
// {
// sys_sd_doc["work_mode"] = "specify";
// }
sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as<String>();//一会
if(doc["caiji_mode"] == "start")
{
if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2;
}
else if(doc["caiji_mode"] == "stop")
{
if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0;
}
else if(doc["caiji_mode"] == "single")
{
sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as<String>();
caiji_mode = 1;
}
else if(doc["caiji_mode"] == "continue")
{
sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as<String>();
caiji_mode = 2;
}
else
{
}
sys_sd_doc["time_interval"] = doc["time_interval"];
time_interval = doc["time_interval"].as<uint32_t>();
sys_sd_doc["start_time"] = doc["start_time"];
start_time = doc["start_time"].as<uint32_t>();
sys_sd_doc["stop_time"] = doc["stop_time"];
stop_time = doc["stop_time"];
sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"];
maximum_temperature = doc["maximum_temperature"];
sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"];
Minimum_temperature = doc["Minimum_temperature"];
// sys_sd_doc["servo_offset_angle"] = doc["servo_offset_angle"];
sys_sd_doc["servo_offset_angle_up"] = doc["servo_offset_angle_up"];
sys_sd_doc["servo_offset_angle_down"] = doc["servo_offset_angle_down"];
float a = doc["servo_offset_angle_up"];
servo_offset(a,1);
write_log(log_path,"servo_offset_angle_up" + String(a),10);
a = doc["servo_offset_angle_down"];
write_log(log_path,"servo_offset_angle_down" + String(a),10);
servo_offset(a,0);
if(doc["GPS_mode"]== "manual")
{
sys_sd_doc["latitude"] = doc["latitude"];
sys_sd_doc["longitude"] = doc["longitude"];
sun_latitude = doc["latitude"];
sun_longitude = doc["longitude"];
write_log(log_path,"latitude" + String(sun_latitude),10);
write_log(log_path,"longitude" + String(sun_longitude),10);
}
sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1;
sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2;
sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3;
sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4;
sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"];
sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"];
sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"];
sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"];
sys_sd_doc["return_data_type"] = doc["return_data_type"];
sys_sd_doc["return_mode"] = doc["return_mode"]; //暂定
sys_sd_doc["atuo_return"] = doc["atuo_return"].as<String>();//一会
if(doc["atuo_return"] == "yes")
{
atuo_return = 1;
}
else
{
atuo_return = 0;
}
sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude;
sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude;
system_info = "";
serializeJson(sys_sd_doc, system_info);
write_log(log_path, system_info,log_level);
doc.clear();
}
else{
write_log(log_path,"do not has system_info.json file" ,log_level);
sys_sd_doc["name"] = "HS";
sys_sd_doc["sensor_type"] = doc["IRIS—IS11"];
sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber;
sys_sd_doc["version"] = "v2.0";
sys_sd_doc["date"] = rtcdate_now;
sys_sd_doc["work_mode"] = "manual_mode";
sys_sd_doc["caiji_mode"] = "stop";
sys_sd_doc["time_interval"] = time_interval;
sys_sd_doc["start_time"] = start_time;
sys_sd_doc["stop_time"] = stop_time;
sys_sd_doc["maximum_temperature"] = maximum_temperature;
sys_sd_doc["Minimum_temperature"] = Minimum_temperature;
sys_sd_doc["servo_offset_angle"] = 0;
sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1;
sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2;
sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3;
sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4;
sys_sd_doc["bochangxishu"]["b0"] = bc_b1;
sys_sd_doc["bochangxishu"]["b1"] = bc_b2;
sys_sd_doc["bochangxishu"]["b2"] = bc_b3;
sys_sd_doc["bochangxishu"]["b3"] = bc_b4;
sys_sd_doc["return_data_type"] = return_data_type;
sys_sd_doc["return_mode"] = ""; //暂定
sys_sd_doc["atuo_return"] = "no";
sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude;
sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude;
serializeJson(sys_sd_doc, system_info);
write_log(log_path, system_info,log_level);
File file;
file = SD_MMC.open("/system/system_info.json","w+");
file.println(system_info);
file.flush();
file.close();
}
}
bool sys_set_time(uint32_t year,uint32_t month,uint32_t day)
{
uint32_t m[4] ={4,6,9,11};
year = year + 2000;
for(int i = 0; i < 4; i++)
{
if(month == m[i])
{
if(day >= 31) return false;
}
}
if(month == 2)
{
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
// write_log(log_path,"is ruannian" + String(day),10);
if(day > 29) return false;
}
else
{
// write_log(log_path,"not is " + String(day),10);
if(day > 28) return false;
}
}
return true;
}
// void save_ds18b20_address(uint8_t *data)
// {
// uint8_t add[8][8]={0};
// uint8_t num = data[0];
// for(uint8_t i = 0; i < 8; i++)
// {
// add[num][i] = data[i+1];
// }
// File file = SD_MMC.open("/ds18b20_address.bin", "wb");
// file.write((uint8_t *)add, 64);
// file.flush();
// file.close();
// }
// void read_ds18b20_address()
// {
// uint8_t add[8][8]={0};
// File file = SD_MMC.open("/ds18b20_address.bin", "rb");
// file.read((uint8_t *)add, 64);
// file.flush();
// file.close();
// for(uint8_t i = 0; i < 8; i++)
// {
// set_ds18b20_address(i,add[i]);
// }
// }
// void sd_read_set_ds18b20_address()
// {
// uint8_t add[8];
// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"};
// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"};
// if(SD_MMC.exists("/temp_sensor.json"))
// {
// File file;
// file = SD_MMC.open("/temp_sensor.json","rb+");
// String temp_sensor = file.readString();
// file.flush();
// file.close();
// deserializeJson(doc, temp_sensor);
// for(uint8_t a = 0; a < 8; a++)
// {
// for(uint8_t b = 0; b < 8; b++)
// {
// add[b] = doc[str1[a]][str2[b]];
// ds18b20_doc[a][b] = doc[str1[a]][str2[b]];
// }
// set_ds18b20_address(a,add);
// }
// temp_sensor = "";
// serializeJson(doc, temp_sensor);
// write_log(log_path, temp_sensor,log_level);
// }
// else
// {
// write_log(log_path,"do not has file",10);
// for(uint8_t a = 0; a < 8; a++)
// {
// for(uint8_t b = 0; b < 8; b++)
// {
// ds18b20_doc[a][b] = "";
// }
// }
// File file;
// file = SD_MMC.open("/temp_sensor.json","w+");
// file.flush();
// file.close();
// }
// doc.clear();
// }
// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr)
// {
// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"};
// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"};
// String temp_sensor;
// for(uint8_t i = 0; i < 8; i++)
// {
// ds18b20_doc[str1[num]][str2[i]] = addr[i];
// }
// temp_sensor = "";
// serializeJson(ds18b20_doc, temp_sensor);
// write_log(log_path, temp_sensor,log_level);
// File file;
// file = SD_MMC.open("/temp_sensor.json","w+");
// file.println(temp_sensor);
// file.flush();
// file.close();
// }
//dingbiao_path
//1、dingbiao/dingbiao_up_gain.txt
//2、dingbiao/dingbiao_up_offset.txt
//3、dingbiao/dingbiao_down_gain.txt
//4、dingbiao/dingbiao_down_offset.txt
bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff)
{
if(SD_MMC.exists(dingbiao_path))
{
File file;
file = SD_MMC.open(dingbiao_path,"rb");
file.read(dianbiao_data_buff,sizeof(IS11_datastruct));
file.flush();
file.close();
}
else
{
return false;
}
return true;
}
void send_dingbiao(String dingbiao_path)
{
uint32_t dingbiao_len = 0;
uint8_t *send_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)+7];
uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)];
if(read_dingbiao(dingbiao_path,read_dingbiao_buff))
{
dingbiao_len = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), read_dingbiao_buff,send_dingbiao_buff);
wb485Serial.write(send_dingbiao_buff,dingbiao_len);
}
delete read_dingbiao_buff;
delete send_dingbiao_buff;
}
void dingbiao_init(String dingbiao_path)
{
uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)];
if(read_dingbiao(dingbiao_path,read_dingbiao_buff))
{
is11Sensor->save_dingbiao(read_dingbiao_buff);
}
delete read_dingbiao_buff;
vTaskDelay(5);
}
void rm_dir_or_file(const char * dirname)
{
File root = SD_MMC.open(dirname);
String dir = String(dirname);
if(root.isDirectory())
{
File file = root.openNextFile();
if(file)
{
if (file.isDirectory())
{
dir = String(dirname) + "/" + String(file.name());
write_log(log_path,dir,10);
rm_dir_or_file(dir.c_str());
}
else
{
do
{
dir = String(dirname) + "/" + String(file.name());
if(SD_MMC.remove(dir))
{
write_log(log_path,"remove file success",10);
}
else
{
write_log(log_path,"remove file failed",10);
break;
}
file = root.openNextFile();
if(!file) break;
} while (!file.isDirectory());
}
}
else
{
SD_MMC.rmdir(dir);
}
}
else
{
SD_MMC.remove(dir);
}
}
// void rm_dir_or_file( const char * dirname)
// {
// File root = SD_MMC.open(dirname);
// String dir = String(dirname);
// if(root.isDirectory())
// {
// File file = root.openNextFile();
// if(file)
// {
// while (file.isDirectory())
// {
// dir = String(dirname) + "/" + String(file.name());
// write_log(log_path,dir,10);
// rm_dir_or_file(dir.c_str());
// file = root.openNextFile();
// }
// if(!file.isDirectory())
// {
// do
// {
// dir = String(dirname) + "/" + String(file.name());
// if(SD_MMC.remove(dir))
// {
// write_log(log_path,"remove file success",10);
// }
// else
// {
// write_log(log_path,"remove file failed",10);
// break;
// }
// file = root.openNextFile();
// if(!file) break;
// } while (!file.isDirectory());
// }
// }
// else
// {
// SD_MMC.rmdir(dir);
// }
// }
// else
// {
// SD_MMC.remove(dir);
// }
// }
//0:关闭 1:开启
void low_power_mode(uint8_t state)
{
if(state == 0)
{
//关闭光谱仪
pinMode(36,INPUT);
//关闭AIR780E
// pinMode(37,OUTPUT);
// digitalWrite(37,LOW);
}
else
{
//开启光谱仪
pinMode(36,OUTPUT);
digitalWrite(36,LOW);
//开启AIR780E
// pinMode(37,OUTPUT);
// digitalWrite(37,HIGH);
}
}