From 09256a19723bbd6239646b57387d2a15f3a3ef4d Mon Sep 17 00:00:00 2001 From: lijie Date: Thu, 20 Feb 2025 13:19:16 +0800 Subject: [PATCH] second commit --- extra_script.py | 3 + include/README | 39 + lib/README | 46 + platformio.ini | 65 + src/CastumullSpline.cpp | 57 + src/CastumullSpline.h | 9 + src/Communication_Protocol.h | 58 + src/Communication_struct.h | 33 + src/DS18B20.cpp | 219 +++ src/DS18B20.h | 14 + src/Define.h | 10 + src/GSMMannager.cpp | 166 ++ src/GSMMannager.h | 40 + src/IRIS_Method.cpp | 174 ++ src/IRIS_Method.h | 60 + src/IS11Comon.cpp | 212 +++ src/IS11Comon.h | 143 ++ src/MyEsp8266.cpp | 5 + src/MyEsp8266.h | 14 + src/MyWebServer.cpp | 139 ++ src/MyWebServer.h | 40 + src/SDmanger.cpp | 314 ++++ src/SDmanger.h | 31 + src/SensorIS11.cpp | 1264 ++++++++++++++ src/SensorIS11.h | 80 + src/TCPserver.cpp | 206 +++ src/TCPserver.h | 49 + src/adc.cpp | 22 + src/adc.h | 10 + src/beep.cpp | 38 + src/beep.h | 11 + src/comon.h | 34 + src/ds1302.cpp | 200 +++ src/ds1302.h | 120 ++ src/gaoguangpu.code-workspace | 62 + src/jiaresi.cpp | 22 + src/jiaresi.h | 19 + src/log.cpp | 81 + src/log.h | 28 + src/main.cpp | 3099 +++++++++++++++++++++++++++++++++ src/my74hc595.cpp | 158 ++ src/my74hc595.h | 13 + src/servo.cpp | 80 + src/servo.h | 16 + src/slave.cpp | 123 ++ src/slave.h | 30 + src/tuigan.cpp | 30 + src/tuigan.h | 17 + src/updatebyme.cpp | 233 +++ src/updatebyme.h | 23 + src/wifidebug.cpp | 41 + src/wifidebug.h | 16 + 52 files changed, 8016 insertions(+) create mode 100644 extra_script.py create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/CastumullSpline.cpp create mode 100644 src/CastumullSpline.h create mode 100644 src/Communication_Protocol.h create mode 100644 src/Communication_struct.h create mode 100644 src/DS18B20.cpp create mode 100644 src/DS18B20.h create mode 100644 src/Define.h create mode 100644 src/GSMMannager.cpp create mode 100644 src/GSMMannager.h create mode 100644 src/IRIS_Method.cpp create mode 100644 src/IRIS_Method.h create mode 100644 src/IS11Comon.cpp create mode 100644 src/IS11Comon.h create mode 100644 src/MyEsp8266.cpp create mode 100644 src/MyEsp8266.h create mode 100644 src/MyWebServer.cpp create mode 100644 src/MyWebServer.h create mode 100644 src/SDmanger.cpp create mode 100644 src/SDmanger.h create mode 100644 src/SensorIS11.cpp create mode 100644 src/SensorIS11.h create mode 100644 src/TCPserver.cpp create mode 100644 src/TCPserver.h create mode 100644 src/adc.cpp create mode 100644 src/adc.h create mode 100644 src/beep.cpp create mode 100644 src/beep.h create mode 100644 src/comon.h create mode 100644 src/ds1302.cpp create mode 100644 src/ds1302.h create mode 100644 src/gaoguangpu.code-workspace create mode 100644 src/jiaresi.cpp create mode 100644 src/jiaresi.h create mode 100644 src/log.cpp create mode 100644 src/log.h create mode 100644 src/main.cpp create mode 100644 src/my74hc595.cpp create mode 100644 src/my74hc595.h create mode 100644 src/servo.cpp create mode 100644 src/servo.h create mode 100644 src/slave.cpp create mode 100644 src/slave.h create mode 100644 src/tuigan.cpp create mode 100644 src/tuigan.h create mode 100644 src/updatebyme.cpp create mode 100644 src/updatebyme.h create mode 100644 src/wifidebug.cpp create mode 100644 src/wifidebug.h diff --git a/extra_script.py b/extra_script.py new file mode 100644 index 0000000..1eafbb4 --- /dev/null +++ b/extra_script.py @@ -0,0 +1,3 @@ +Import("env") + +env.Replace(PROGNAME="firmware_%s" % env.GetProjectOption("custom_prog_version")) \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d719497 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,65 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +; [env:esp32dev] +; platform = espressif32 +; board = esp32dev +; framework = arduino +; monitor_speed = 115200 +; ;upload_speed=115200 +; ; upload_port = COM31 +; ;monitor_port = com5 +; lib_deps = +; vshymanskyy/TinyGSM@^0.11.5 +; knolleary/PubSubClient@^2.8 +; plerup/EspSoftwareSerial@^6.15.2 +; arduino-libraries/ArduinoHttpClient@^0.4.0 +; janelia-arduino/Vector@^1.2.2 +; build_flags: +; -DVERSION=${this.custom_prog_version} +; extra_scripts = pre:extra_script.py +; custom_prog_version = V2.6 + + +[env:esp32-s3-devkitc-1] +; platformio/espressif32@^6.4.0 +platform = espressif32 @ ^4.4.0 +board = esp32-s3-devkitc-1 +framework = arduino +platform_packages = + toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 + ; framework-arduinoespressif32 @ 2.0.6+sha.099b432 +board_upload.flash_size=4MB +board_upload.psram_size =2MB +lib_deps = + vshymanskyy/TinyGSM@^0.11.5 + knolleary/PubSubClient@^2.8 + plerup/EspSoftwareSerial@^6.15.2 + arduino-libraries/ArduinoHttpClient@^0.4.0 + janelia-arduino/Vector@^1.2.2 + paulstoffregen/OneWire@^2.3.7 + milesburton/DallasTemperature@^3.11.0 + adafruit/Adafruit MLX90614 Library@^2.1.3 + sparkfun/SparkFun u-blox Arduino Library@^1.8.11 + bblanchon/ArduinoJson@^6.20.1 + paulstoffregen/OneWire@^2.3.7 + jpb10/SolarCalculator @ 2.0.1 + luisllamasbinaburo/InterpolationLib @ ^1.0.2 + ; simsso/ShiftRegister74HC595 @ 1.3.1 + ; makuna/RTC @ 2.4.2 + +; build_flags: +; -g + +build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 ; Enable USB CDC + -DCORE_DEBUG_LEVEL=1 ; Set debug level + + + diff --git a/src/CastumullSpline.cpp b/src/CastumullSpline.cpp new file mode 100644 index 0000000..874ecd1 --- /dev/null +++ b/src/CastumullSpline.cpp @@ -0,0 +1,57 @@ +float catmullSlope(float x[], float y[], int n, int i) +{ + if (x[i + 1] == x[i - 1]) return 0; + return (y[i + 1] - y[i - 1]) / (x[i + 1] - x[i - 1]); +} + + +float CatmullSpline(float xValues[], float yValues[], int numValues, float pointX, bool trim) +{ + if (trim) + { + if (pointX <= xValues[0]) return yValues[0]; + if (pointX >= xValues[numValues - 1]) return yValues[numValues - 1]; + } + + auto i = 0; + if (pointX <= xValues[0]) i = 0; + else if (pointX >= xValues[numValues - 1]) i = numValues - 1; + else while (pointX >= xValues[i + 1]) i++; + if (pointX == xValues[i + 1]) return yValues[i + 1]; + + auto t = (pointX - xValues[i]) / (xValues[i + 1] - xValues[i]); + auto t_2 = t * t; + auto t_3 = t_2 * t; + + auto h00 = 2 * t_3 - 3 * t_2 + 1; + auto h10 = t_3 - 2 * t_2 + t; + auto h01 = 3 * t_2 - 2 * t_3; + auto h11 = t_3 - t_2; + + auto x0 = xValues[i]; + auto x1 = xValues[i + 1]; + auto y0 = yValues[i]; + auto y1 = yValues[i + 1]; + + float m0; + float m1; + if (i == 0) + { + m0 = (yValues[1] - yValues[0]) / (xValues[1] - xValues[0]); + m1 = (yValues[2] - yValues[0]) / (xValues[2] - xValues[0]); + } + else if (i == numValues - 2) + { + m0 = (yValues[numValues - 1] - yValues[numValues - 3]) / (xValues[numValues - 1] - xValues[numValues - 3]); + m1 = (yValues[numValues - 1] - yValues[numValues - 2]) / (xValues[numValues - 1] - xValues[numValues - 2]); + } + else + { + m0 = catmullSlope(xValues, yValues, numValues, i); + m1 = catmullSlope(xValues, yValues, numValues, i + 1); + } + + auto rst = h00 * y0 + h01 * y1 + h10 * (x1 - x0) * m0 + h11 * (x1 - x0) * m1; + return rst; +} + diff --git a/src/CastumullSpline.h b/src/CastumullSpline.h new file mode 100644 index 0000000..4a38bea --- /dev/null +++ b/src/CastumullSpline.h @@ -0,0 +1,9 @@ +#ifndef CASTUMULLSPLINE_H +#define CASTUMULLSPLINE_H + +#include +float CatmullSpline(float xValues[], float yValues[], int numValues, float pointX, bool trim = true); + + + +#endif diff --git a/src/Communication_Protocol.h b/src/Communication_Protocol.h new file mode 100644 index 0000000..b3a0569 --- /dev/null +++ b/src/Communication_Protocol.h @@ -0,0 +1,58 @@ + +#ifndef COMMUNICATION_PROTOCOL_H +#define COMMUNICATION_PROTOCOL_H + + +#include +#include "IRIS_Method.h" +#include +// DEFINE The protocol Here + //Forexample +// #define MYCOMAN 0x02 //命令全大写 +// todo : define your protocol here +/*-------------------------------------------------------------------------------------------------------------*/ +// #define Digital_number 0 +#define DIGITALNUMBER 0 +#define RADIANCE 1 +#define REFLECTANCE 2 +#define DARKNOISE 3 +#define DIRECTIO_UP 0 +#define DIRECTION_DOWN 1 +#define BANDNUMBER 2048 + +/// 加时间戳 +struct IS11_datastruct +{ + u_int8_t type = 0; // 0 DN 1 Radiance 2 重采样 3 Reflectance 4 gain 5 offset 6 fanshelv + u_int8_t direction = 0; //0 up 1 down 2 dark_up 3 dark_down + u_int8_t tuigan_stat =0;//0:伸 1:缩 + u_int8_t year= 0; + u_int8_t month= 0; + u_int8_t day= 0; + u_int8_t hour= 0; + u_int8_t minute= 0; + u_int8_t second= 0; + u_int8_t NCa = 0; //手动对齐,方便解析。 + u_int8_t NCb = 0; + u_int8_t NCc = 0; + u_int32_t shutter_time = 0; + u_int64_t index = 0; //在获取重采样值时这个表示有多少位数据 + float temprature[8]; + float data[BANDNUMBER]; +}; + + +struct clalidata_struct +{ + uint32_t shutter; + float gain[BANDNUMBER]; + float offset[BANDNUMBER]; +}; + +/*-------------------------------------------------------------------------------------------------------------*/ +/* +在此之下开始定义一些函数 + +*/ +#endif + diff --git a/src/Communication_struct.h b/src/Communication_struct.h new file mode 100644 index 0000000..6568700 --- /dev/null +++ b/src/Communication_struct.h @@ -0,0 +1,33 @@ +#ifndef COMMUNICATION_STRUCT_H +#define COMMUNICATION_STRUCT_H + +// Define your communication structures here +// For example: +/* +如果发送和接受的数据结构一样,那么就定义一个结构体 +struct MyComan_struct //对应的命令结构体 小驼峰命名法+struct +{ + uint8_t Command; + uint16_t LenthofIn; +}; + + + +如果发送和接受的数据结构不一样,那么就分开定义 +struct MyComan_Send_struct //对应的命令结构体 小驼峰命名法+Send_struct +{ + uint8_t Command; + uint16_t LenthofIn; + +}; + +struct MyComan_Recv_struct //对应的命令结构体 小驼峰命名法+Recv_struct +{ + uint8_t Command; + uint16_t LenthofOut; + +}; +*/ + + +#endif // COMMUNICATION_STRUCT_H diff --git a/src/DS18B20.cpp b/src/DS18B20.cpp new file mode 100644 index 0000000..a418979 --- /dev/null +++ b/src/DS18B20.cpp @@ -0,0 +1,219 @@ +#include "DS18B20.h" +#include + +DeviceAddress sensor0 = { 0x28, 0x40, 0xB9, 0x2C ,0xF, 0x0, 0x0 ,0xEA }; +DeviceAddress sensor1 = { 0x28, 0xB0, 0xBA, 0x2D ,0xF, 0x0, 0x0 ,0x59 }; +DeviceAddress sensor2 = { 0x28, 0x8C ,0xDA ,0x2D, 0xF, 0x0 ,0x0, 0xD8 }; +DeviceAddress sensor3 = { 0x28 ,0x4A ,0xD7, 0x2D, 0xF ,0x0 ,0x0 ,0x19 }; +DeviceAddress sensor4 = { 0x28, 0x9E ,0xDA, 0x2D ,0xF, 0x0, 0x0, 0xED }; +DeviceAddress sensor5 = { 0x28, 0xA9 ,0x0A ,0xFB, 0xD, 0x0, 0x0, 0x9C }; +DeviceAddress sensor6 = { 0x28, 0x17 ,0xF6, 0x2C, 0xF ,0x0 ,0x0 ,0xC7 }; +DeviceAddress sensor7 = { 0x28, 0xBF, 0x92, 0x2D ,0xF, 0x0, 0x0 ,0xBB }; + +DeviceAddress sensoraddr[8]; + +OneWire oneWire(DS18b20_pin); +DallasTemperature DS18b20(&oneWire); +int temp_number; + + +uint8_t *p[8] = {sensoraddr[0],sensoraddr[1],sensoraddr[2],sensoraddr[3],sensoraddr[4],sensoraddr[5],sensoraddr[6],sensoraddr[7]}; + +//冒泡排序 将温度地址 DeviceAddress sensoraddr[30]; 从小到大排序 +void bubble_sort(DeviceAddress *addr,int n) +{ + uint64_t a1 ,a2; + for (int i = 0; i < n; i++) + { + for(uint8_t b = 0 ; b<8;b++) + { + a1 += addr[i][b]; + } + + for (int j = 0; j < n-i-1; j++) + { + for(uint8_t c = 0 ; c<8;c++) + { + a2 += addr[j][c]; + } + if (a1 > a2) + { + uint8_t *temp = p[i]; + p[i] = p[j]; + p[j] = temp; + } + } + } +} + + +uint8_t DS18b20_init() +{ + //温度模块初始化 + // DS18b20.setOneWire(&oneWire); + DS18b20.begin(); + temp_number = DS18b20.getDeviceCount(); + int i = 0; + while(temp_number <= 0 ) + { + temp_number = DS18b20.getDeviceCount(); + vTaskDelay(500); + if (i>=5) + { + write_log(log_path,"ds18b2 init failed,no ds18b20",10); + return 0; + } + i++; + } + write_log(log_path,"ds18b20 has" + String(temp_number),10); + for(size_t i = 0; i < temp_number; i++) + { + DS18b20.getAddress(sensoraddr[i],i); + } + bubble_sort(sensoraddr,temp_number); + return temp_number; +} + + +//温度监测 +void getall_temp(float *temp) +{ + DS18b20.requestTemperatures(); // Send the command to get temperatures + for(int8_t i ;i +#include +#define DS18b20_pin 4 +uint8_t DS18b20_init(); +void getall_temp(float *temp); +float getone_temp(uint8_t address); +uint8_t get_ds18b20_addr(); +void set_ds18b20_address(uint8_t num,uint8_t * addr); + +#endif \ No newline at end of file diff --git a/src/Define.h b/src/Define.h new file mode 100644 index 0000000..3d8c5fa --- /dev/null +++ b/src/Define.h @@ -0,0 +1,10 @@ +#ifndef Define_h +#define Define_h +//#define DebugCalibration +//#define DINBIAO +//#define Curentvsion "V2.4b"\ +#define Curentvsion "V2.5b" +#define Curentvsion "V2.3" +#define TINY_GSM_DEBUG Serial + +#endif \ No newline at end of file diff --git a/src/GSMMannager.cpp b/src/GSMMannager.cpp new file mode 100644 index 0000000..03a2a72 --- /dev/null +++ b/src/GSMMannager.cpp @@ -0,0 +1,166 @@ +#include "GSMMannager.h" +// #include "SoftwareSerial.h" +// uint32_t AIR_AutoBaud(); + +const char apn[] = "CMNET"; +const char gprsUser[] = ""; +const char gprsPass[] = ""; + +HardwareSerial *MySerial; +GSMMannger::GSMMannger(int serialnumber, int rx, int tx) +{ + pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电 低电平断电 + digitalWrite(37, HIGH); + vTaskDelay(1000*10); + // digitalWrite(37, LOW); + + MySerial=new HardwareSerial(serialnumber); + + modem = new TinyGsm(*MySerial); + MySerial->begin(115200, SERIAL_8N1, rx, tx); + client = new TinyGsmClient(*modem); + // !!!!!!!!!!! �ϵ� + // Serial.println("Wait..."); + write_log(log_path,"Wait...",10); + + // Serial.println("Wait...1"); + write_log(log_path,"Wait...1",10); + vTaskDelay(1800); + // Set your reset, enable, power pins here + // !!!!!!!!!!! + + vTaskDelay(5000); +// Set GSM module baud rate +// MySerial->readString(); + int banddd=TinyGsmAutoBaud(*MySerial, rx, tx, 9600,460800); + if (banddd==0) + { + // Serial.println("fail to connect sim800 reboot"); + write_log(log_path,"fail to connect sim800 reboot",10); + esp_restart(); + /* code */ + } + + // Serial.println("band is" + String(banddd)); + write_log(log_path,"band is" + String(banddd),10); + vTaskDelay(6000); + modem->init(); + + String modemInfo = modem->getModemInfo(); + if (modemInfo=="") + { + // Serial.println("fail to getmode reboot"); + write_log(log_path,"fail to getmode reboot",10); + esp_restart(); + /* code */ + } + // Serial.print("Modem Info: "); + write_log(log_path,"Modem Info: ",10); + + //以后用4G再 + // SerialMon.println(modemInfo); + write_log(log_path,modemInfo,10); + if (GSM_PIN && modem->getSimStatus() != 3) + { + modem->simUnlock(GSM_PIN); + } + // SerialMon.print("Waiting for network..."); + write_log(log_path,"Waiting for network...",10); + if (!modem->waitForNetwork()) + { + // SerialMon.println(" fail"); + write_log(log_path,"fail",10); + vTaskDelay(10000); + return; + } + // SerialMon.println(" success"); + write_log(log_path,"success",10); + + if (modem->isNetworkConnected()) + { + // SerialMon.println("Network connected"); + write_log(log_path,"Network connected",10); + } + // SerialMon.print(F("Connecting to ")); + write_log(log_path,"Connecting to ",10); + // SerialMon.print(apn); + write_log(log_path,apn,10); + if (!modem->gprsConnect(apn, gprsUser, gprsPass)) + { + // SerialMon.println(" fail"); + write_log(log_path,"fail",10); + vTaskDelay(10000); + return; + } + // SerialMon.println(" success"); + write_log(log_path,"success",10); + + if (modem->isGprsConnected()) + { + // SerialMon.println("GPRS connected"); + write_log(log_path,"GPRS connected",10); + } + modem->NTPServerSync(); +} + + + + +String GSMMannger::I2toS(int n) +{ + if (n>=10) + { + return String(n); + } else{ + return "0"+ String(n); + } +} +String GSMMannger::GetDataAndTime() { + //String str=modem->getGSMDateTime(DATE_FULL); + if(modem->getNetworkTime(&year,&month,&day,&hour,&minute, &second,&timezone)) + { + String date=String(year)+"-"+I2toS(month)+"-"+I2toS(day)+" "+I2toS(hour)+":"+I2toS(minute)+":"+I2toS(second); + return date;} + return "-1"; +} + +void GSMMannger::loop() +{ + + if (!modem->isNetworkConnected()) + { + + SerialMon.println("Network disconnected"); + if (!modem->waitForNetwork(20000L, true)) + { + SerialMon.println(" re wait For Network fail"); + esp_restart(); + vTaskDelay(10000); + return; + } + if (modem->isNetworkConnected()) + { + SerialMon.println("Network re-connected"); + } + + // and make sure GPRS/EPS is still connected + if (!modem->isGprsConnected()) + { + SerialMon.println("GPRS disconnected!"); + SerialMon.print(F("Connecting to ")); + SerialMon.print(apn); + if (!modem->gprsConnect(apn, gprsUser, gprsPass)) + { + SerialMon.println(" set apn gprsuser gprspass fail"); + esp_restart(); + vTaskDelay(10000); + return; + } + if (modem->isGprsConnected()) + { + SerialMon.println("GPRS reconnected"); + } + } + } +} + diff --git a/src/GSMMannager.h b/src/GSMMannager.h new file mode 100644 index 0000000..0900032 --- /dev/null +++ b/src/GSMMannager.h @@ -0,0 +1,40 @@ +#ifndef _GSMMannager_H_ +#define _GSMMannager_H_ + + +// #define TINY_GSM_MODEM_SIM800 +#define TINY_GSM_MODEM_BG96 +#define SerialMon Serial +#include +#include +#include "log.h" + + +//#define TINY_GSM_DEBUG SerialMon +#define GSM_AUTOBAUD_MIN 9600 +#define GSM_AUTOBAUD_MAX 115200 +#define TINY_GSM_USE_GPRS true +#define TINY_GSM_USE_WIFI false + +#define GSM_PIN "" + + + +class GSMMannger +{ + public: + GSMMannger(int serialnumber,int rx,int tx); // ���ǹ��캯�� + // GSMMannger(); + TinyGsm *modem; + TinyGsmClient *client; + String GetDataAndTime(); + void loop(); + String I2toS(int n); + int year,month, day, hour,minute, second; + float timezone; + +}; + +#endif + + \ No newline at end of file diff --git a/src/IRIS_Method.cpp b/src/IRIS_Method.cpp new file mode 100644 index 0000000..20e853f --- /dev/null +++ b/src/IRIS_Method.cpp @@ -0,0 +1,174 @@ +/** + ****************************************************************************** + * @file : IRIS_Method.c + * @author : xin + * @brief : None + * @attention : None + * @date : 2024/2/1 + ****************************************************************************** + */ + +// +// Created by xin on 2024/2/1. +// +#include "IRIS_Method.h" + +int32_t IRIS_Protocol_Pack(uint8_t Command, uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData) { + if (PackData == NULL || (LenthofIn != 0 && BufferIn == NULL)) { + return -1; + } + + PackData[0] = 0x55; + PackData[1] = 0xAA; + PackData[2] = Command; + uint16_t datalenth = LenthofIn; + PackData[3] = (datalenth >> 8) & 0xFF; + PackData[4] = datalenth & 0xFF; + + if (LenthofIn != 0) { + memcpy(&PackData[5], BufferIn, LenthofIn); + } + uint16_t crcbytelenth = LenthofIn; + uint16_t CRC = IRIS_calcCRC(PackData + 5, crcbytelenth); + PackData[LenthofIn + 5] = (CRC >> 8) & 0xFF; + PackData[LenthofIn + 6] = CRC & 0xFF; + return LenthofIn + 7; + + +} + +int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut) { + + if (PackData == NULL || BufferOut == NULL) { + return ERROR_INPUT; + } + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); //减去CRC的两个字节 + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + + + if (LenthofOut == 0) { + return 0; + } + *Command = PackData[2]; + + memcpy(BufferOut, &PackData[5], LenthofOut); + return LenthofOut; +} + + +int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut) { + if (PackData == NULL || BufferOut == NULL) { + return ERROR_INPUT; + } + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + if (PackData[2] != Command) { + return ERROR_COMMAND; + } + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + if (LenthofOut == 0) { + return 0; + } + memcpy(BufferOut, &PackData[5], LenthofOut); + return LenthofOut; + +} + + +int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn) { + if (PackData == NULL) { + return ERROR_INPUT; + } + uint16_t i = 0; + for (i = 0; i < LenthofIn; i++) { + if (PackData[i] == 0x55 && PackData[i + 1] == 0xAA) { + break; + } + } + if (i == LenthofIn) { + //清空数据 + memset(PackData, 0, LenthofIn); + return 0; + } + + uint16_t LenthofOut = LenthofIn - i; + memcpy(PackData, &PackData[i], LenthofOut); + return LenthofOut; +} + +int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn) { + if (PackData == NULL) { + return ERROR_INPUT; + } + if (LenthofIn < 7) { + return ERROR_NOT_ENOUGH_DATA; + /* code */ + } + + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + return 1; + +} + + +uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize) { + const uint8_t *pBytesArray = (const uint8_t *) pBuffer; + uint16_t poly = 0x8408; + uint16_t crc = 0; + uint8_t carry; + uint8_t i_bits; + uint16_t j; + for (j = 0; j < bufferSize; j++) { + crc = crc ^ pBytesArray[j]; + for (i_bits = 0; i_bits < 8; i_bits++) { + carry = crc & 1; + crc = crc / 2; + if (carry) { + crc = crc ^ poly; + } + } + } + return crc; +} + diff --git a/src/IRIS_Method.h b/src/IRIS_Method.h new file mode 100644 index 0000000..99a3582 --- /dev/null +++ b/src/IRIS_Method.h @@ -0,0 +1,60 @@ +/** + ****************************************************************************** + * @file : IRIS_Method.h + * @author : xin + * @brief : None + * @attention : None + * @date : 2024/2/1 + ****************************************************************************** + */ + +// +// Created by xin on 2024/2/1. +// + +#ifndef IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H +#define IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H + +#define ERROR_NOT_ENOUGH_DATA -200 +#define ERROR_HEADER -300 +#define ERROR_COMMAND -400 +#define ERROR_INPUT -500 +#define ERROR_CRC -600 +#include +#include + +// 成功返回打包后的数据长度 +// -1: Error +// 成功返回打包后的数据长度 +int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData); + +// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令 BufferOut 是输出 +// 下位机使用的打包函数 Command 是输出 +// 成功返回解包后的数据长度 +// 0: 该命令返回无参数 +// 错误返回ERRor +// 成功返回解包后的数据长度 +int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut); + +// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令输入 BufferOut 是输出 上位机使用 +// 成功返回解包后的数据长度 +// 0: 该命令返回无参数 +// 错误返回ERRor +// 成功返回解包后的数据长度 +int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut); + +// 定义裁切命令 +// 成功返回裁切后的数据长度 +// -1: Error +int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn ); + +// 检查数据是否有效 +// 有效返回值1 +// 错误返回ERRor +int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn ); + + +// 返回CRC校验值 +uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize); + +#endif //IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H diff --git a/src/IS11Comon.cpp b/src/IS11Comon.cpp new file mode 100644 index 0000000..725a796 --- /dev/null +++ b/src/IS11Comon.cpp @@ -0,0 +1,212 @@ +#include "IS11Comon.h" +u_char BufferForRead[10000]; +int TotalIndexNow = 0; +u_char BufferFortempWrite[1000]; +// MySerialWrite=nullptr; +SERIALWRITE MySerialWrite = nullptr; +SERIALREAD MySerialRead = nullptr; +bool ISIS11Init = false; +uint16_t crc16(const uint8_t *data, size_t len, uint16_t polynomial) +{ + uint16_t i, j, tmp, CRC16; + + CRC16 = 0xFFFF; // CRC寄存器初始值 + for (i = 0; i < len; i++) + { + CRC16 ^= data[i]; + for (j = 0; j < 8; j++) + { + tmp = (uint16_t)(CRC16 & 0x0001); + CRC16 >>= 1; + if (tmp == 1) + { + CRC16 ^= polynomial; // 异或多项式 + } + } + } + return CRC16; +} + +size_t SendSettingCommand(u_char *Command, size_t CommandLenth, u_char *Value, size_t ValueLenth) +{ + memcpy(BufferFortempWrite, Command, CommandLenth); + memcpy(BufferFortempWrite + CommandLenth, Value, ValueLenth); + uint16_t crc = crc16(BufferFortempWrite, CommandLenth + ValueLenth); + memcpy(BufferFortempWrite + CommandLenth + ValueLenth, &crc, 2); + + SerialWrite(BufferFortempWrite, CommandLenth + ValueLenth + 2); + + return GetSetBackFromSensor(); +} + +size_t SendGetData(int shutter) +{ + int lenth = sizeof(GET_ALL_DN); + uint16_t crc = crc16(GET_ALL_DN, lenth); + memcpy(BufferFortempWrite, GET_ALL_DN, lenth); + memcpy(BufferFortempWrite + lenth, &crc, 2); + SerialWrite(BufferFortempWrite, lenth + 2); + + vTaskDelay(shutter); + + return GetInfoBackFromSensor(true); +} + +size_t SendGetSensorInfo(u_char *Command, size_t lenth) +{ + + uint16_t crc = crc16(Command, lenth); + memcpy(BufferFortempWrite, Command, lenth); + memcpy(BufferFortempWrite + lenth, &crc, 2); + + SerialWrite(BufferFortempWrite, lenth + 2); + + String datastr; + for (size_t i = 0; i < lenth + 2; i++) + { + datastr += String(BufferFortempWrite[i]); + } + // write_log(gp_log, "send data:" + datastr, 20); + + size_t retunnumber = GetInfoBackFromSensor(); + + return retunnumber; +} + +size_t GetSetBackFromSensor() +{ + TotalIndexNow = 0; + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + + while (TotalIndexNow < 8) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + + } + return TotalIndexNow; +} + +bool panduanHeader() +{ + if (TotalIndexNow < 2) + { + return false; + /* code */ + } + int temp = 0; + while (BufferForRead[temp] != 0x01 && + !(BufferForRead[temp + 1] == 0x03 || BufferForRead[temp + 1] == 0x06 || BufferForRead[temp + 1] == 0x10)) + { + if (temp >= TotalIndexNow - 2) + { + break; + /* code */ + } + temp++; + + /* code */ + } + memcpy(BufferForRead, BufferForRead + temp, TotalIndexNow - temp); + TotalIndexNow = TotalIndexNow - temp; + temp = 0; + if (BufferForRead[temp] != 0x01 && + !(BufferForRead[temp + 1] == 0x03 || BufferForRead[temp + 1] == 0x06 || BufferForRead[temp + 1] == 0x10)) + { + return false; + } + return true; +} + +size_t GetInfoBackFromSensor(bool isbig) // big 是指用几个字节表示数据长度 暂时只发现采集数据时用两个字节 +{ + + if (isbig) + { + + TotalIndexNow = 0; + memset(BufferForRead, 0, sizeof(BufferForRead)); + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 2); + while (!panduanHeader()) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + } + while (TotalIndexNow < 4) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + } + + int lenth = BufferForRead[2] * 256 + BufferForRead[3]; + while (TotalIndexNow < lenth + 4) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1024); + } + return TotalIndexNow; + } + else + { + // 长度用一个字节表示 + TotalIndexNow = 0; + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + + while (TotalIndexNow < 3) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + } + int lenth = BufferForRead[2]; + while (TotalIndexNow < lenth + 5) + { + TotalIndexNow += SerailRead(BufferForRead + TotalIndexNow, 1); + } + return TotalIndexNow; + } +} + +size_t SerialWrite(u_char *data, size_t lenth) +{ + if (MySerialWrite != nullptr) + { + return MySerialWrite(data, lenth); + } + + + return 0; +} + +size_t SerailRead(u_char *data, size_t lenth) +{ + if (MySerialRead != nullptr) + { + return MySerialRead(data, lenth); + } + return 0; +} + +void InitFunction(SERIALWRITE a, SERIALREAD readfunc) +{ + MySerialWrite = a; + MySerialRead = readfunc; + ISIS11Init = true; +} + +u_char *GetDataBufferPTR() +{ + return BufferForRead; +} +bool isSensorInit() +{ + return ISIS11Init; +} + +void CoverLittleAndBig(char *data, int lenth) +{ + char *tempdata = new char[lenth]; + memcpy(tempdata, data, lenth); + + for (size_t i = 0; i < lenth / 2; i++) + { + data[2 * i] = tempdata[2 * i + 1]; + data[2 * i + 1] = tempdata[2 * i]; + /* code */ + } + delete[] tempdata; +} \ No newline at end of file diff --git a/src/IS11Comon.h b/src/IS11Comon.h new file mode 100644 index 0000000..7061143 --- /dev/null +++ b/src/IS11Comon.h @@ -0,0 +1,143 @@ + +/** + * @brief is11相关底层函数 + * + */ +#ifndef __IS11COMON_H__ +#define __IS11COMON_H__ + +#ifndef IS11COMMON_H +#define IS11COMMON_H +#include +#include +#include +#include +#include "log.h" +#define COMMAND_GET 0x03 +#define COMMAND_SET 0x06 +#define COMMAND_MULTSET 0x10 +#define POLYNOMIAL 0xa001 //modbus crc + +typedef unsigned char u_char; +/** + * @brief 串口写函数类型 + * + */ +typedef size_t (*SERIALWRITE)(u_char* data,size_t lenth); +/** + * @brief 串口读函数原型 + * + */ +typedef size_t (*SERIALREAD)(u_char* data,size_t lenth); + +const u_char GET_ADDRESS[] ={0x01,0x03,0x00,0x01,0x00,0x01}; +const u_char GET_BANDRATE[] ={0x01,0x03,0x00,0x02,0x00,0x01}; +const u_char GET_INTEGRAL_TIME[] ={0x01,0x03,0x00,0x06,0x00,0x01}; +const u_char GET_AVERAGE_NUMBER[] ={0x01,0x03,0x00,0x07,0x00,0x01}; +const u_char GET_WAVELENTH_AT_BAND[]={0x01,0x03,0x00,0x10,0x00,0x02}; +const u_char GET_VALUE_AT_BAND[] ={0x01,0x03,0x00,0x30,0x00,0x02}; +const u_char GET_SETTING_OF_LAMP[] ={0x01,0x03,0x00,0x04,0x00,0x01}; +const u_char GET_WAVELENTH_COEFF[] ={0x01,0x03,0x00,0x20,0x00,0x08}; +const u_char GET_ALL_DN[] ={0x01,0x03,0x01,0x00,0x10,0x00}; +const u_char GET_SERIAL_NUMBER[] ={0x01,0x03,0x00,0x40,0x00,0x00}; +const u_char GET_PRODUCT_NAME[] ={0x01,0x03,0x00,0x50,0x00,0x00}; + +const u_char SET_ADDRESS[] ={0x01,0x06,0x00,0x01}; +// const u_char SET_BandRATE[] ={0x01,0x06,0x00,0x02}; +const u_char SET_INTEGRAL_TIME[] ={0x01,0x06,0x00,0x06}; +const u_char SET_AVERAGE_NUMBER[] ={0x01,0x06,0x00,0x07}; +const u_char SET_WORK_MODE[] ={0x01,0x06,0x00,0x01}; +const u_char SET_WAVELENTH_COEFF[] ={0x01,0x10,0x00,0x20,0x00,0x08,0x16}; + +/** + * @brief 判断传感器是否初始化完成 + * + * @return true + * @return false + */ +bool isSensorInit(); +/** + * @brief 初始化传感器 + * + * @param writefunc 写函数 + * @param readfunc 读函数 + */ +void InitFunction(SERIALWRITE writefunc,SERIALREAD readfunc); + +/** + * @brief 获取 BufferForRead 的指针 用于读取返回的数据 + * + */ +u_char * GetDataBufferPTR(); + +/** + * @brief 发送获取设备信息的指令 适用于Get指令 + * + * @param Command 指令 预定的数组 + * @param lenth 指令长度 可以用sizeof来求算 + * @return size_t 返回数据的长度 输出存放于 BufferForRead; + */ +size_t SendGetSensorInfo(u_char * Command,size_t lenth);//此过程不适合获取数据 + +/** + * @brief + * + * @param shutter + * @return size_t + */ +size_t SendGetData(int shutter); +/** + * @brief 发送设置指令 + * + * @param Command 指令内容 + * @param CommandLenth 指令长度 + * @param Value 设置值 + * @param ValueLenth 值长度 默认是两个字节 + * @return size_t 返回值长度 + */ +size_t SendSettingCommand(u_char * Command,size_t CommandLenth,u_char *Value,size_t ValueLenth=2); + +//big +/** + * @brief 用来获取信息的返回的数据 + * + * @param isbig 是指用几个字节表示数据长度 暂时只发现采集数据时用两个字节 + * @return size_t 接收到的数据大小 + */ +size_t GetInfoBackFromSensor(bool isbig=false); +/** + * @brief 获取设置后返回数据 + * + * @return size_t 返回数据长度; + */ +size_t GetSetBackFromSensor(); + + + +/** + * @brief + * + * @param data + * @param lenth + * @return size_t + */ + +size_t SerialWrite(u_char* data,size_t lenth); +size_t SerailRead(u_char* data,size_t lenth=0); +/** + * @brief 计算crc16 + * + * @param data 数据 + * @param len 数据长度 + * @param polynomial crc16多项式 默认是A001H + * @return uint16_t crc16的值 + */ +uint16_t crc16(const uint8_t *data, size_t len, uint16_t polynomial=POLYNOMIAL) ; + +bool panduanHeader(); + + +void CoverLittleAndBig(char *data,int lenth); + +#endif +#endif // __IS11COMON_H__ \ No newline at end of file diff --git a/src/MyEsp8266.cpp b/src/MyEsp8266.cpp new file mode 100644 index 0000000..728b40f --- /dev/null +++ b/src/MyEsp8266.cpp @@ -0,0 +1,5 @@ +// +// Created by xin on 2022/4/28. +// + +#include "MyEsp8266.h" diff --git a/src/MyEsp8266.h b/src/MyEsp8266.h new file mode 100644 index 0000000..f735a1a --- /dev/null +++ b/src/MyEsp8266.h @@ -0,0 +1,14 @@ +// +// Created by xin on 2022/4/28. +// + +#ifndef ESP32MAINBOARD_MYESP8266_H +#define ESP32MAINBOARD_MYESP8266_H + + +class MyEsp8266 { + +}; + + +#endif //ESP32MAINBOARD_MYESP8266_H diff --git a/src/MyWebServer.cpp b/src/MyWebServer.cpp new file mode 100644 index 0000000..48e77ed --- /dev/null +++ b/src/MyWebServer.cpp @@ -0,0 +1,139 @@ +#include "MyWebServer.h" + + +ESPWebServer *ESPStaticServer; +void webserver::printn(String str) +{ + if (serialmy != nullptr) + { + serialmy->print(str); + } +} +void webserver::initme() +{ + + printn("webserver begain\n"); + + // wifiMulti.addAP("IRIS", "irishk*******"); // 将需要连接的一系列WiFi ID和密码输入这里 + // wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); // ESP8266-NodeMCU再启动后会扫描当前网络 + // wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); // 环境查找是否有这里列出的WiFi ID。如果有 + // printn("Connecting ..."); // 则尝试使用此处存储的密码进行连接。 + + // int i = 0; + // while (wifiMulti.run() != WL_CONNECTED) { // 尝试进行wifi连接。 + // vTaskDelay(1000); + // printn("retry to connect wifi"); + // } + + // WiFi连接成功后将通过串口监视器输出连接成功信息 + + printn("Connected to \n"); + printn(WiFi.SSID()); // 通过串口监视器输出连接的WiFi名称 + printn("IP address:\t"); + printn(WiFi.localIP().toString()); // 通过串口监视器输出ESP8266-NodeMCU的IP + printn("\n"); + if (SPIFFS.begin()) + { // 启动闪存文件系统 + printn("SPIFFS Started.\n"); + } + else + { + printn("SPIFFS Failed to Start.\n"); + } + + server->onNotFound(handleUserRequet); // 告知系统如何处理用户请求 + + server->begin(); // 启动网站服务 + printn("HTTP server started\n"); +} +webserver::webserver(HardwareSerial *serial) +{ + serialmy = serial; + server=new ESPWebServer(80); + ESPStaticServer=server; + initme(); +} +webserver::webserver() +{ + serialmy = nullptr; + server=new ESPWebServer(80); + ESPStaticServer=server; + initme(); +} + +webserver::~webserver() +{ +} + +void webserver::handleUserRequet() +{ + // Serial.println("some one come\n"); + write_log(log_path,"some one come\n",10); + // 获取用户请求网址信息 + String webAddress = ESPStaticServer->uri(); + + // 通过handleFileRead函数处处理用户访问 + bool fileReadOK = handleFileRead(webAddress); + + // 如果在SPIFFS无法找到用户访问的资源,则回复404 (Not Found) + if (!fileReadOK) + { + ESPStaticServer->send(404, "text/plain", "404 Not Found"); + } +} + +bool webserver::handleFileRead(String path) +{ //处理浏览器HTTP访问 + + if (path.endsWith("/")) + { // 如果访问地址以"/"为结尾 + path = "/index.html"; // 则将访问地址修改为/index.html便于SPIFFS访问 + } + + String contentType = getContentType(path); // 获取文件类型 + + if (SPIFFS.exists(path)) + { // 如果访问的文件可以在SPIFFS中找到 + File file = SPIFFS.open(path, "r"); // 则尝试打开该文件 + ESPStaticServer->streamFile(file, contentType); // 并且将该文件返回给浏览器 + file.close(); // 并且关闭文件 + return true; // 返回true + } + return false; // 如果文件未找到,则返回false +} + +// 获取文件类型 +String webserver::getContentType(String filename) +{ + if (filename.endsWith(".htm")) + return "text/html"; + else if (filename.endsWith(".html")) + return "text/html"; + else if (filename.endsWith(".css")) + return "text/css"; + else if (filename.endsWith(".js")) + return "application/javascript"; + else if (filename.endsWith(".png")) + return "image/png"; + else if (filename.endsWith(".gif")) + return "image/gif"; + else if (filename.endsWith(".jpg")) + return "image/jpeg"; + else if (filename.endsWith(".ico")) + return "image/x-icon"; + else if (filename.endsWith(".xml")) + return "text/xml"; + else if (filename.endsWith(".pdf")) + return "application/x-pdf"; + else if (filename.endsWith(".zip")) + return "application/x-zip"; + else if (filename.endsWith(".gz")) + return "application/x-gzip"; + return "text/plain"; +} + +void webserver::loop() +{ + ESPStaticServer->handleClient(); + // printn("..."); +} diff --git a/src/MyWebServer.h b/src/MyWebServer.h new file mode 100644 index 0000000..376c0ea --- /dev/null +++ b/src/MyWebServer.h @@ -0,0 +1,40 @@ +#ifndef wenserver_H +#define wenserver_H +#define ESP32 +#ifdef ESP32 + +#include "WebServer.h" +#include "WiFi.h" +#include "Arduino.h" +#include "SPIFFS.h" +#define ESPWebServer WebServer +#elif +#include +#include +#define ESPWebServer ESP8266WebServer +#endif +#include + +#include "log.h" + +class webserver +{ +private: + +public: + webserver(HardwareSerial *serial); + webserver(); + void initme(); + HardwareSerial *serialmy; + ~webserver(); + static void handleUserRequet(); + static bool handleFileRead(String path); + static String getContentType(String filename); + void loop(); + void printn(String str); + ESPWebServer *server; +}; + +#endif + + diff --git a/src/SDmanger.cpp b/src/SDmanger.cpp new file mode 100644 index 0000000..6e94006 --- /dev/null +++ b/src/SDmanger.cpp @@ -0,0 +1,314 @@ +#include"SDmanger.h" +File Comenfile; + +int sdcard::init_sdcard() +{ + SD_MMC.setPins(9,10,11,12,13,14); + int succ = SD_MMC.begin("/sdcard", true,true,20000); + if (succ) { + // Serial.printf("SD_MMC Begin: %d\n", succ); + write_log(log_path,"SD_MMC Begin: "+String(succ),10); + uint8_t cardType = SD_MMC.cardType(); + // Serial.print("SD_MMC Card Type: ");uj + write_log(log_path,"SD_MMC Card Type: ",10); + if (cardType == CARD_MMC) { + // Serial.println("MMC"); + write_log(log_path,"MMC",10); + } else if (cardType == CARD_SD) { + // Serial.println("SDSC"); + write_log(log_path,"SDSC",10); + } else if (cardType == CARD_SDHC) { + // Serial.println("SDHC"); + write_log(log_path,"SDHC",10); + } else { + // Serial.println("UNKNOWN"); + write_log(log_path,"UNKNOWN",10); + } + + uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024); + uint32_t c = cardSize; + // Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize); + write_log(log_path,"SD_MMC Card Size: "+String(c)+"MB",10); + + // Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024)); + float a = SD_MMC.totalBytes() / (1024 * 1024); + write_log(log_path,"Total space: "+String(a)+"MB",10); + // Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024)); + a = SD_MMC.usedBytes() / (1024 * 1024); + write_log(log_path,"Used space: "+String(a)+"MB",10); + + mylistDir( "/", 0); + // File file; + + // file.listFiles("/"); + // file.flush(); + // file.close(); + + } else { + // Serial.printf("Failed to mount SD card VFAT filesystem. \n"); + write_log(log_path,"Failed to mount SD card VFAT filesystem. ",10); + // Serial.println("Do you have an SD Card installed?"); + write_log(log_path,"Do you have an SD Card installed?",10); + // Serial.println("Check pin 12 and 13, not grounded, or grounded with 10k resistors!\n\n"); + write_log(log_path,"Check pin 12 and 13, not grounded, or grounded with 10k resistors!\n\n",10); + // major_fail(); + } + + return ESP_OK; +} + + void sdcard::mylistDir( const char * dirname, uint8_t levels) { + write_log(log_path,"Listing directory: "+String(dirname),10); + String dir(dirname); + File root = SD_MMC.open(dirname); + if (!root) { + write_log(log_path,"Failed to open directory",10); + return; + } + if (!root.isDirectory()) { + write_log(log_path,"Not a directory",10); + return; + } + + File filex = root.openNextFile(); + while (filex) { + if (filex.isDirectory()) + { + write_log(log_path," DIR : " + String(filex.name()),10); + if (levels) { + String filename=dir+"/"+String(filex.name()); + mylistDir( filename.c_str(), levels - 1); + } + } + else + { + write_log(log_path," FILE: " + String(filex.name()) + " size :"+ String(filex.size()) + "B",10); + } + filex = root.openNextFile(); + } +} +bool sdcard::ListDir(const char * dirname,Vector &stringlist) +{ + stringlist.clear(); + // Serial.printf("Listing directory: %s\n", dirname); + write_log(log_path,"Listing directory: "+String(dirname),10); + String dir(dirname); + File root = SD_MMC.open(dirname); + if (!root) { + // Serial.println("Failed to open directory"); + write_log(log_path,"Failed to open directory",10); + return true; + } + if (!root.isDirectory()) { + // Serial.println("Not a directory"); + write_log(log_path,"Not a directory",10); + return true; + } + + File filex = root.openNextFile(); + while (filex) + { + if (filex.isDirectory()) { + + } else { + String filename=dir+"/"+String(filex.name()); + stringlist.push_back(filename); + + if (stringlist.size()==stringlist.max_size()) + { + return false; + /* code */ + } + + } + filex = root.openNextFile(); + } + + return true; +} + + + void sdcard::delete_old_stuff() { + + // Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024)); + float a = SD_MMC.totalBytes() / (1024 * 1024); + write_log(log_path,"Total space: "+String(a)+"MB",10); + // Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024)); + a = SD_MMC.usedBytes() / (1024 * 1024); + write_log(log_path,"Used space: "+String(a)+"MB",10); + //listDir( "/", 0); + + float full = 1.0 * SD_MMC.usedBytes() / SD_MMC.totalBytes();; + if (full < 0.8) { + // Serial.printf("Nothing deleted, %.1f%% disk full\n", 100.0 * full); + write_log(log_path,"Nothing deleted, "+String(100.0 * full)+"% disk full",10); + } else { + // Serial.printf("Disk is %.1f%% full ... deleting oldest file\n", 100.0 * full); + write_log(log_path,"Disk is "+String(100.0 * full)+"% full ... deleting oldest file",10); + while (full > 0.8) { + + double del_number = 999999999; + char del_numbername[50]; + + File f = SD_MMC.open("/"); + + File file = f.openNextFile(); + + while (file) { + //Serial.println(file.name()); + if (!file.isDirectory()) { + + char foldname[50]; + strcpy(foldname, file.name()); + for ( int x = 0; x < 50; x++) { + if ( (foldname[x] >= 0x30 && foldname[x] <= 0x39) || foldname[x] == 0x2E) { + } else { + if (foldname[x] != 0) foldname[x] = 0x20; + } + } + + double i = atof(foldname); + if ( i > 0 && i < del_number) { + strcpy (del_numbername, file.name()); + del_number = i; + } + //Serial.printf("Name is %s, number is %f\n", foldname, i); + } + file = f.openNextFile(); + + } + // Serial.printf("lowest is Name is %s, number is %f\n", del_numbername, del_number); + write_log(log_path,"lowest is Name is "+String(del_numbername)+", number is "+String(del_number),10); + if (del_number < 999999999) { + deleteFolderOrFile(del_numbername); + } + full = 1.0 * SD_MMC.usedBytes() / SD_MMC.totalBytes(); + // Serial.printf("Disk is %.1f%% full ... \n", 100.0 * full); + write_log(log_path,"Disk is "+String(100.0 * full)+"% full ... ",10); + f.close(); + } + } +} + + void sdcard::deleteFolderOrFile(const char * val) { + write_log(log_path,"Deleting : "+String(val),10); + File f = SD_MMC.open(val); + if (!f) { + write_log(log_path,"Failed to open "+String(val),10); + return; + } + + if (f.isDirectory()) { + File file = f.openNextFile(); + while (file) { + if (file.isDirectory()) { + write_log(log_path," DIR : ",10); + write_log(log_path,file.name(),10); + } else { + write_log(log_path," FILE: ",10); + write_log(log_path,file.name(),10); + write_log(log_path," SIZE: ",10); + write_log(log_path,String(file.size()),10); + if (SD_MMC.remove(file.name())) { + write_log(log_path," deleted.",10); + } else { + write_log(log_path," FAILED.",10); + } + } + file = f.openNextFile(); + } + f.close(); + if (SD_MMC.rmdir(val)) { + write_log(log_path,"Dir "+String(val)+" removed",10); + } else { + write_log(log_path,"Remove dir failed",10); + } + + } else { + //Remove the file + if (SD_MMC.remove(val)) { + write_log(log_path,"File "+String(val)+" deleted",10); + } else { + write_log(log_path,"Delete failed",10); + } + } +} + + void sdcard:: testwriet() +{ + File file; + file = SD_MMC.open("/try.txt", "wb"); + file.println("hello world"); + file.flush(); + file.close(); +} + + + + void sdcard::WriteStringToFile(String name,String Str) + { + File file; + + file = SD_MMC.open(name, "w+"); + file.println(Str); + file.flush(); + file.close(); + } + void sdcard::WriteStringToFile(String name,char *data,size_t lenth) + { + File file; + + file = SD_MMC.open(name, "wb"); + file.write((const byte *)data,lenth); + file.flush(); + file.close(); + } + bool sdcard::Mkdir(String path) + { + return SD_MMC.mkdir(path); + } + +namespace sdcard +{ + void openFileformWirte(String Path) + { + if (Comenfile.available()) + { + Comenfile.close(); + /* code */ + } + Comenfile = SD_MMC.open(Path, "w+"); + + } + + void WritetoFileCommen(String Str) + { + if (Comenfile.available()) + { + Comenfile.print(Str); + Comenfile.flush(); + /* code */ + } + } + + void WritetoFileCommen(char *data,size_t lenth) + { + if (Comenfile.available()) + { + Comenfile.write((const byte *)data,lenth); + Comenfile.flush(); + /* code */ + } + + } + + void closeCommenFile() + { + if (Comenfile.available()) + { + Comenfile.close(); + /* code */ + } + + } +} \ No newline at end of file diff --git a/src/SDmanger.h b/src/SDmanger.h new file mode 100644 index 0000000..cdde818 --- /dev/null +++ b/src/SDmanger.h @@ -0,0 +1,31 @@ +#ifndef SDMANGER_H +#define SDMANGER_H +#include "driver/sdmmc_host.h" +#include "driver/sdmmc_defs.h" +#include "sdmmc_cmd.h" +#include "esp_vfs_fat.h" +#include "FS.h" +#include +#include "Vector.h" +#include "log.h" +#include "Arduino.h" +#include "string.h" +namespace sdcard{ + int init_sdcard(); + + void mylistDir( const char * dirname, uint8_t levels); + bool ListDir(const char * dirname,Vector &stringlist); + void delete_old_stuff(); + void deleteFolderOrFile(const char * val) ; + void testwriet(); + void WriteStringToFile(String name,String Str); + void WriteStringToFile(String name,char *data,size_t lenth); + bool Mkdir(String path); + void openFileformWirte(String Path); + void WritetoFileCommen(String Str); + void WritetoFileCommen(char *data,size_t lenth); +void closeCommenFile(); + +} +#endif + diff --git a/src/SensorIS11.cpp b/src/SensorIS11.cpp new file mode 100644 index 0000000..1b30a62 --- /dev/null +++ b/src/SensorIS11.cpp @@ -0,0 +1,1264 @@ +#include +#include"servo.h" +Ds1302 ds1302_date(0, 8, 7); +Ds1302::DateTime sys_time; + +struct IS11_datastruct IS11_datastruct_fanshelv; + +STRSensorInfo SensorIS11::initSensor() +{ + // IS1Sensor.SetPortName(id); + pinMode(5,OUTPUT); + pinMode(6,OUTPUT); + radiance_struct_up.shutter = 1; + radiance_struct_down.shutter = 1; + + for (uint32_t i = 0; i < 2048; i++) + { + radiance_struct_up.gain[i] = 1; + radiance_struct_down.gain[i] = 1; + radiance_struct_up.offset[i] = 0; + radiance_struct_down.offset[i] = 0; + } + SensorInfo.BandNum = 2048; + uint8_t buff[4]; + String path2 = "/system/index.bin"; + File file; + if(SD_MMC.exists(path2)) + { + + file = SD_MMC.open(path2,"rb"); + file.read(buff,4); + file.flush(); + file.close(); + memcpy(&index,buff,4); + + write_log(log_path,"index is " + String(index),10); + } + + return SensorInfo=GetSensorInfo(); + // return SensorInfo; + // return true; +} + +STRSensorInfo SensorIS11::GetSensorInfo() +{ + // Serial.println("init ok"); + // write_log(log_path,"init isSensorInit",10); + STRSensorInfo setem; + if (!isSensorInit()) + { + // Serial.println("sensor is 123123"); + // write_log(log_path,"sensor is 123123",10); + return setem; + } + + + // write_log(log_path,"init ok",10); + int retlenth= SendGetSensorInfo((u_char *)GET_SERIAL_NUMBER,sizeof(GET_SERIAL_NUMBER)); + vTaskDelay(100); + // write_log(log_path,String(retlenth),10); + + char * result=new char[retlenth]; + memcpy(result,DataRetrun+3,retlenth); + //result[4]='\0'; + // std::string str11(result); + // setem.serialnumber = str11; + setem.serialnumber = String(result); + delete[] result; + // Serial.println(String(setem.serialnumber.c_str())); + + // write_log(log_path,"serialnumber :"+String(setem.serialnumber.c_str()),10); + // write_log(log_path,"serialnumber :"+setem.serialnumber,10); + retlenth= SendGetSensorInfo((u_char *)GET_PRODUCT_NAME,sizeof(GET_PRODUCT_NAME)); + vTaskDelay(100); + result=new char[retlenth]; + memcpy(result,DataRetrun+3,retlenth); + // vTaskDelay(500); + + // result[4]='\0'; + // std::string str1(result); + // write_log(log_path,"productname :"+String(setem.SensorName.c_str()),10); + + setem.SensorName = String(result); + // write_log(log_path,"productname :"+setem.SensorName,10); + + delete result; + setem.BandNum=2048; + // Serial.println(String(setem.SensorName.c_str())+"_"+String(setem.serialnumber.c_str())); + // write_log(log_path,String(setem.SensorName.c_str())+"_"+String(setem.serialnumber.c_str()),10); + + setem.maxValue=65535; + setem.wavelenthlist = new float[setem.BandNum](); + retlenth= SendGetSensorInfo((u_char *)GET_WAVELENTH_COEFF,sizeof(GET_WAVELENTH_COEFF)); + vTaskDelay(100); + float a[4]; + memcpy(a,DataRetrun+3,16); + + // write_log(log_path,"CoverLittleAndBig",10); + CoverLittleAndBig((char *)a,4*sizeof(float)); + int bandsss= setem.BandNum; + #ifdef ARDUINO + + + // Serial.print("a0:"); + // Serial.print(a[0]); + // Serial.print(" a1:"); + // Serial.print(a[1]); + // Serial.print(" a2:"); + // Serial.print(a[2]); + // Serial.print(" a3:"); + // Serial.println(a[3]); + // write_log(log_path,"ARDUINO",10); + // write_log(log_path,"a0:"+String(a[0])+" a1:"+String(a[1])+" a2:"+String(a[2])+" a3:"+String(a[3]),10); + + #endif + setem.a1=a[0]; + setem.a2=a[1]; + setem.a3=a[2]; + setem.a4=a[3]; + for ( int i = 1; i <=bandsss ; i++) + { + setem.wavelenthlist[i-1] = a[0] * i*i*i + a[1] * i*i + a[2] * i + a[3]; + setem.WavelenthStr=setem.WavelenthStr+String( setem.wavelenthlist[i-1]).c_str()+","; + } + // vTaskDelay(500); + // Serial.write(dataretrun,retlenth); + // memcpy(a,dataretrun+) + u_char temp[2]={0x00,0x01}; + retlenth=SendSettingCommand((u_char*)SET_AVERAGE_NUMBER,sizeof(SET_AVERAGE_NUMBER),temp,sizeof(temp)); + // Serial.println("init ok"); + + return setem; +} + + +//shutter1 1关 2开 +void SensorIS11::SetShutter(int id) +{ + switch (id) { + case 1:{ //关 + digitalWrite(5,LOW); + vTaskDelay(200); + break; + } + case 2:{ //开 + digitalWrite(5,HIGH); + vTaskDelay(200); + break; + } + } +} + + +//0下 1上 +void SensorIS11::servo_direction(int direction) +{ + switch (direction) + { + case 0:{ + // servo_set_angle(12); + servo_down(); + vTaskDelay(1500); + break; + } + case 1:{ + // servo_set_angle(200); + servo_up(); + vTaskDelay(1500); + break; + } + } +} + + +///这获取到的是4096的那个数组 +void SensorIS11::GetOneDate(int msc) +{ + if (msc!=shutternow) + { + u_char shutter[2]; + shutter[0]=msc/256; + shutter[1]=msc%256; + SendSettingCommand((u_char *) SET_INTEGRAL_TIME,sizeof(SET_INTEGRAL_TIME),shutter,sizeof(shutter)); + } + shutternow=msc; + + size_t retsize= SendGetData(shutternow); + //DATABUFF 4096 需要输出的数据 + memcpy(DATABUFF,DataRetrun+4,4096); + shortLittletoBiG(DATABUFF, SensorInfo.BandNum*2); +} + + +//获取SHUTTER时间,返回的是最大值 +int SensorIS11::OptSnenser(int persent) +{ + long maxtime = 20000; + int maxvalue=SensorInfo.maxValue*1.0*persent / 100; + int maxvaluenow = 0; + static float shutternow = 100; + GetOneDate(100); + maxvaluenow= Getmaxvalue(DATABUFF, SensorInfo.BandNum); + // Serial.print("now Shutter is :"); + // Serial.print(shutternow); + // Serial.print(" maxvalue is :"); + // Serial.println(maxvaluenow); + // write_log(log_path,"now Shutter is :"+String(shutternow)+" maxvalue is :"+String(maxvaluenow),10); + + static int numberoftry; + write_log(log_path,"numberoftry is :"+String(numberoftry),10); + while (maxvaluenowmaxvalue) { + if(numberoftry == 0) shutternow = 100; + if (maxvaluenow > maxvalue) + { + shutternow = shutternow *0.7; + } + else + { + shutternow = maxvalue * 0.98 / (maxvaluenow * 1.0)*shutternow + 1; + } + if (shutternow > maxtime) + { + shutternow = maxtime; + break; + } + GetOneDate(shutternow); + maxvaluenow= Getmaxvalue(DATABUFF, SensorInfo.BandNum); +#ifdef ARDUINO + // Serial.print("now Shutter is :"); + // Serial.print(shutternow); + // Serial.print(" maxvalue is :"); + // Serial.println(maxvaluenow); + // write_log(log_path,"now Shutter is :"+String(shutternow)+" maxvalue is :"+String(maxvaluenow),10); +#endif + numberoftry++; + if (numberoftry > 200) + { + numberoftry = 1; + return maxtime; + } + if (shutternow == maxtime) + { + + return maxtime; + } + vTaskDelay(1); + } +#ifdef ARDUINO + // Serial.print("zi dong value:"); + // Serial.println(shutternow); + write_log(log_path,"zi dong value:"+String(shutternow),10); +#endif + if (shutternow<0) + { + shutternow=maxtime; + /* code */ + } + write_log(log_path,"Finish OptSnenser",10); + shutternow = shutternow; + return shutternow; +} + +void SensorIS11::shortLittletoBiG(unsigned short *data,int lenth) +{ + CoverLittleAndBig((char *)data,lenth); +} + + +int SensorIS11::Getmaxvalue(unsigned short *data,int lenth) +{ + int ret=-1; + for (int i = 0; i < lenth; ++i) { + if (data[i]>ret){ + ret=data[i]; + } + } + return ret; +} + + +struct IS11_datastruct IS11_datastruct_up; +struct IS11_datastruct IS11_datastruct_down; +struct IS11_datastruct IS11_datastruct_dark_up; +struct IS11_datastruct IS11_datastruct_dark_down; + +void SensorIS11::TakeOneJob1(bool dingbing) +{ + ds1302_date.getDateTime(&sys_time); + // String path = "/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)+ "/"+ String(sys_time.hour) + "-" + String(sys_time.minute) + "-" + String(sys_time.second) + ".bin"; + String time = String(sys_time.year) +"."+ String(sys_time.month) +"."+ String(sys_time.day)+" "+String(sys_time.hour)+":"+String(sys_time.minute)+":"+String(sys_time.second); + write_log(log_path,time,20); + write_log(log_path,"start take one job.",20); + work_progress = 0 ; + float temp[8]; + getall_temp(temp); + //opt_up opt_down dark_up dn_up dn_down dark_down + + //1 opt_up + servo_direction(1); + SetShutter(2); + shutterup=OptSnenser(70); + SetShutter(1); + work_progress = 15; + + //2 opt_down + servo_direction(0); + SetShutter(2); + shutterdown=OptSnenser(70); + SetShutter(1); + work_progress = 30; + + //3 dark_up + SetShutter(1); + GetOneDate(shutterup); + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastruct_dark_up.data[i] = DATABUFF[i]; + } + + IS11_datastruct_dark_up.type = 0; + IS11_datastruct_dark_up.direction = 2; + IS11_datastruct_dark_up.tuigan_stat = get_tuigan_status(); + IS11_datastruct_dark_up.year = sys_time.year; + IS11_datastruct_dark_up.month = sys_time.month; + IS11_datastruct_dark_up.day = sys_time.day; + IS11_datastruct_dark_up.hour = sys_time.hour; + IS11_datastruct_dark_up.minute = sys_time.minute; + IS11_datastruct_dark_up.second = sys_time.second; + IS11_datastruct_dark_up.shutter_time = shutterup; + IS11_datastruct_dark_up.index = index; + memcpy(IS11_datastruct_dark_up.temprature,temp,8*sizeof(float)); + work_progress = 45; + + //4 dn_up + servo_direction(1); + SetShutter(2); + GetOneDate(shutterup); + SetShutter(1); + SensorInfo.BandNum = 2048; + + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastruct_up.data[i] = DATABUFF[i]; + } + IS11_datastruct_up.type = 0; + IS11_datastruct_up.direction = 0; + IS11_datastruct_up.tuigan_stat = get_tuigan_status(); + IS11_datastruct_up.year = sys_time.year; + IS11_datastruct_up.month = sys_time.month; + IS11_datastruct_up.day = sys_time.day; + IS11_datastruct_up.hour = sys_time.hour; + IS11_datastruct_up.minute = sys_time.minute; + IS11_datastruct_up.second = sys_time.second; + IS11_datastruct_up.shutter_time = shutterup; + IS11_datastruct_up.index = index; + memcpy(IS11_datastruct_up.temprature,temp,8*sizeof(float)); + work_progress = 60; + + //5 dn_down + servo_direction(0); + SetShutter(2); + GetOneDate(shutterdown); + SetShutter(1); + + for (int i = 0; i < SensorInfo.BandNum;i++) { + IS11_datastruct_down.data[i] = DATABUFF[i]; + } + IS11_datastruct_down.type = 0; + IS11_datastruct_down.direction = 1; + IS11_datastruct_down.tuigan_stat = get_tuigan_status(); + IS11_datastruct_down.year = sys_time.year; + IS11_datastruct_down.month = sys_time.month; + IS11_datastruct_down.day = sys_time.day; + IS11_datastruct_down.hour = sys_time.hour; + IS11_datastruct_down.minute = sys_time.minute; + IS11_datastruct_down.second = sys_time.second; + IS11_datastruct_down.shutter_time = shutterdown; + IS11_datastruct_down.index = index; + memcpy(IS11_datastruct_down.temprature,temp,8*sizeof(float)); + work_progress = 75; + + //6 dark_down + SetShutter(1); + GetOneDate(shutterdown); + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastruct_dark_down.data[i] = DATABUFF[i]; + } + IS11_datastruct_dark_down.type = 0; + IS11_datastruct_dark_down.direction = 3; + IS11_datastruct_dark_down.tuigan_stat = get_tuigan_status(); + IS11_datastruct_dark_down.year = sys_time.year; + IS11_datastruct_dark_down.month = sys_time.month; + IS11_datastruct_dark_down.day = sys_time.day; + IS11_datastruct_dark_down.hour = sys_time.hour; + IS11_datastruct_dark_down.minute = sys_time.minute; + IS11_datastruct_dark_down.second = sys_time.second; + IS11_datastruct_dark_down.shutter_time = shutterdown; + IS11_datastruct_dark_down.index = index; + memcpy(IS11_datastruct_dark_down.temprature,temp,8*sizeof(float)); + servo_off(); + work_progress = 90; + //save data to file + + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year)); + } + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month)); + } + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)); + } + String path1 = "/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)+ "/"+ String(sys_time.hour) + "-" + String(sys_time.minute) + "-" + String(sys_time.second); + sdcard::Mkdir(path1); + + String path2 = path1 + "/up.bin"; + File file; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *)(&IS11_datastruct_up),sizeof(IS11_datastruct)); + file.flush(); + file.close(); + work_progress = 92; + + path2 = path1 + "/dark_up.bin"; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *)(&IS11_datastruct_dark_up),sizeof(IS11_datastruct)); + file.flush(); + file.close(); + work_progress = 94; + + path2 = path1 + "/down.bin"; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *)(&IS11_datastruct_down),sizeof(IS11_datastruct)); + file.flush(); + file.close(); + work_progress = 96; + + path2 = path1 + "/dark_down.bin"; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *)(&IS11_datastruct_dark_down),sizeof(IS11_datastruct)); + file.flush(); + file.close(); + + index++; + work_progress = 100 ; + IS11_datastruct_fanshelv.NCa == 0; + write_log(log_path,"take one job end.",20); +} + +void SensorIS11::TakeOneJob() +{ + index++; + ds1302_date.getDateTime(&sys_time); + String time = String(sys_time.year) +"."+ String(sys_time.month) +"."+ String(sys_time.day)+" "+String(sys_time.hour)+":"+String(sys_time.minute)+":"+String(sys_time.second); + write_log(log_path,time + " start take one job",10); + + work_progress = 0 ; + float temp[8]; + getall_temp(temp); + //opt_up opt_down dark_up dn_up dn_down dark_down + // write_log(log_path,"start take one jon",20); + // write_log(log_path,"start",10); + //1 opt_up + servo_direction(1); + SetShutter(2); + shutterup=OptSnenser(70); + SetShutter(1); + work_progress = 30; + + + //3 dn_up + SensorInfo.BandNum = 2048; + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastruct_up.data[i] = DATABUFF[i]; + } + IS11_datastruct_up.type = 0; + IS11_datastruct_up.direction = 0; + IS11_datastruct_up.tuigan_stat = get_tuigan_status(); + IS11_datastruct_up.year = sys_time.year; + IS11_datastruct_up.month = sys_time.month; + IS11_datastruct_up.day = sys_time.day; + IS11_datastruct_up.hour = sys_time.hour; + IS11_datastruct_up.minute = sys_time.minute; + IS11_datastruct_up.second = sys_time.second; + IS11_datastruct_up.shutter_time = shutterup; + IS11_datastruct_up.index = index; + memcpy(IS11_datastruct_up.temprature,temp,8*sizeof(float)); + work_progress = 45; + + //2 dark + //dark_up + SetShutter(1); + GetOneDate(shutterup); + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastruct_dark_up.data[i] = DATABUFF[i]; + IS11_datastruct_dark_down.data[i] = DATABUFF[i]; + } + + IS11_datastruct_dark_up.type = 0; + IS11_datastruct_dark_up.direction = 2; + IS11_datastruct_dark_up.tuigan_stat = get_tuigan_status(); + IS11_datastruct_dark_up.year = sys_time.year; + IS11_datastruct_dark_up.month = sys_time.month; + IS11_datastruct_dark_up.day = sys_time.day; + IS11_datastruct_dark_up.hour = sys_time.hour; + IS11_datastruct_dark_up.minute = sys_time.minute; + IS11_datastruct_dark_up.second = sys_time.second; + IS11_datastruct_dark_up.shutter_time = shutterup; + IS11_datastruct_dark_up.index = index; + memcpy(IS11_datastruct_dark_up.temprature,temp,8*sizeof(float)); + + + //dark_down + IS11_datastruct_dark_down.type = 0; + IS11_datastruct_dark_down.direction = 3; + IS11_datastruct_dark_down.tuigan_stat = get_tuigan_status(); + IS11_datastruct_dark_down.year = sys_time.year; + IS11_datastruct_dark_down.month = sys_time.month; + IS11_datastruct_dark_down.day = sys_time.day; + IS11_datastruct_dark_down.hour = sys_time.hour; + IS11_datastruct_dark_down.minute = sys_time.minute; + IS11_datastruct_dark_down.second = sys_time.second; + IS11_datastruct_dark_down.shutter_time = shutterdown; + IS11_datastruct_dark_down.index = index; + memcpy(IS11_datastruct_dark_down.temprature,temp,8*sizeof(float)); + + work_progress = 60; + + // //3 dn_up + // servo_direction(1); + // SetShutter(2); + // shutterdown = shutterup; + // GetOneDate(shutterdown); + // SetShutter(1); + // SensorInfo.BandNum = 2048; + + // for (int i = 0; i < SensorInfo.BandNum;i++) + // { + // IS11_datastruct_up.data[i] = DATABUFF[i]; + // } + // IS11_datastruct_up.type = 0; + // IS11_datastruct_up.direction = 0; + // IS11_datastruct_up.tuigan_stat = get_tuigan_status(); + // IS11_datastruct_up.year = sys_time.year; + // IS11_datastruct_up.month = sys_time.month; + // IS11_datastruct_up.day = sys_time.day; + // IS11_datastruct_up.hour = sys_time.hour; + // IS11_datastruct_up.minute = sys_time.minute; + // IS11_datastruct_up.second = sys_time.second; + // IS11_datastruct_up.shutter_time = shutterup; + // IS11_datastruct_up.index = index; + // memcpy(IS11_datastruct_up.temprature,temp,8*sizeof(float)); + // work_progress = 60; + + //4 dn_down + servo_direction(0); + SetShutter(2); + GetOneDate(shutterup); + SetShutter(1); + + for (int i = 0; i < SensorInfo.BandNum;i++) { + IS11_datastruct_down.data[i] = DATABUFF[i]; + } + IS11_datastruct_down.type = 0; + IS11_datastruct_down.direction = 1; + IS11_datastruct_down.tuigan_stat = get_tuigan_status(); + IS11_datastruct_down.year = sys_time.year; + IS11_datastruct_down.month = sys_time.month; + IS11_datastruct_down.day = sys_time.day; + IS11_datastruct_down.hour = sys_time.hour; + IS11_datastruct_down.minute = sys_time.minute; + IS11_datastruct_down.second = sys_time.second; + IS11_datastruct_down.shutter_time = shutterdown; + IS11_datastruct_down.index = index; + memcpy(IS11_datastruct_down.temprature,temp,8*sizeof(float)); + servo_off(); + work_progress = 75; + + + // work_progress = 90; + //save data to file + + // if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year))) + // { + // sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year)); + // } + // if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month))) + // { + // sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month)); + // } + // if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day))) + // { + // sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)); + // } + // String path1 = "/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)+"/"+ String(sys_time.hour) + "-" + String(sys_time.minute) + "-" + String(sys_time.second); + // // sdcard::Mkdir(path1); + + // String path2 = path1 + "/up.bin"; + // File file; + // file = SD_MMC.open(path2,"wb"); + // file.write((uint8_t *)(&IS11_datastruct_up),sizeof(IS11_datastruct)); + // file.flush(); + // file.close(); + // work_progress = 92; + + // path2 = path1 + "/dark_up.bin"; + // file = SD_MMC.open(path2,"wb"); + // file.write((uint8_t *)(&IS11_datastruct_dark_up),sizeof(IS11_datastruct)); + // file.flush(); + // file.close(); + // work_progress = 94; + + // path2 = path1 + "/down.bin"; + // file = SD_MMC.open(path2,"wb"); + // file.write((uint8_t *)(&IS11_datastruct_down),sizeof(IS11_datastruct)); + // file.flush(); + // file.close(); + // work_progress = 96; + + // path2 = path1 + "/dark_down.bin"; + // file = SD_MMC.open(path2,"wb"); + // file.write((uint8_t *)(&IS11_datastruct_dark_down),sizeof(IS11_datastruct)); + // file.flush(); + // file.close(); + + // path2 = "/system/index.bin"; + // file = SD_MMC.open(path2,"wb"); + // file.write((uint8_t *) &index,4); + // file.flush(); + // file.close(); + write_log(log_path,"start SD.",10); + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year)); + } + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month)); + } + if(!SD_MMC.exists("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day))) + { + sdcard::Mkdir("/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day)); + } + String path1 = "/guangpu_data/"+ String(sys_time.year) + "/" + String(sys_time.month) + "/" + String(sys_time.day); + + // sdcard::Mkdir(path1); + + String path2 = path1 + "/" + String(sys_time.hour) + "-" + String(sys_time.minute) + "-" + String(sys_time.second) + ".bin"; + File file; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *)(&IS11_datastruct_up),sizeof(IS11_datastruct)); + file.write((uint8_t *)(&IS11_datastruct_dark_up),sizeof(IS11_datastruct)); + file.write((uint8_t *)(&IS11_datastruct_down),sizeof(IS11_datastruct)); + file.write((uint8_t *)(&IS11_datastruct_dark_down),sizeof(IS11_datastruct)); + file.flush(); + file.close(); + work_progress = 92; + + + path2 = "/system/index.bin"; + file = SD_MMC.open(path2,"wb"); + file.write((uint8_t *) &index,4); + file.flush(); + file.close(); + + + work_progress = 100; + IS11_datastruct_fanshelv.NCa == 0; + write_log(log_path,"take one job end.",10); +} + + +SensorIS11::SensorIS11() +{ + shutternow = 0; + DataRetrun = GetDataBufferPTR(); +} + + +/////////////////////////////////////////////////////////// +void SensorIS11::send_dn(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num) +{ + int32_t send_lenth; + u_char *send_buff = new u_char[sizeof(IS11_datastruct) + 7]; + + if(fengbao_num >= 1) + { + u_char *data_buff = new u_char[960+1]; + data_buff[0] = fengbao_num; + uint32_t x = sizeof(IS11_datastruct) / 960; + uint32_t n = 960; + if(fengbao_num == x+1) + { + data_buff[0] = fengbao_num | 0x80; + n = sizeof(IS11_datastruct) % 960; + } + memcpy(data_buff+1,(uint8_t *)IS11_datastructure+960*(fengbao_num-1),n); + send_lenth = IRIS_Protocol_Pack(0x02,n+1,data_buff,send_buff); + wb485Serial->write(send_buff,send_lenth); + delete data_buff; + } + else + { + send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)IS11_datastructure,send_buff); + wb485Serial->write(send_buff,send_lenth); + } + + // send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)IS11_datastructure,send_buff); + // wb485Serial->write(send_buff,send_lenth); + delete send_buff; + vTaskDelay(50); +} + + +void SensorIS11::send_radiance(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num) +{ + struct IS11_datastruct IS11_datastruct_send = *IS11_datastructure; + int32_t send_lenth; + u_char *send_buff = new u_char[sizeof(IS11_datastruct) + 7]; + IS11_datastruct_send.type = 0x01; + if(fengbao_num <= 1 ) + { + if(IS11_datastructure == &IS11_datastruct_up) + { + for (int i = 0; i < 2048;i++) + { + IS11_datastruct_send.data[i] = IS11_datastruct_send.data[i] / IS11_datastruct_send.shutter_time * radiance_struct_up.shutter * radiance_struct_up.gain[i] - radiance_struct_up.offset[i]; + } + } + else if(IS11_datastructure == &IS11_datastruct_down) + { + for (int i = 0; i < 2048;i++) + { + IS11_datastruct_send.data[i] = IS11_datastruct_send.data[i] / IS11_datastruct_send.shutter_time * radiance_struct_down.shutter * radiance_struct_down.gain[i] - radiance_struct_down.offset[i]; + } + } + } + + if(fengbao_num >= 1) + { + // uint32_t n= 960; + // if(fengbao_num == 8) n = 568; + // send_lenth = IRIS_Protocol_Pack(0x02,n,(uint8_t *)(&IS11_datastruct_send) + 960*(fengbao_num-1),send_buff); + // wb485Serial->write(send_buff,send_lenth); + + u_char *data_buff = new u_char[960+1]; + data_buff[0] = fengbao_num; + uint32_t x = sizeof(IS11_datastruct) / 960; + uint32_t n = 960; + if(fengbao_num == x+1) + { + data_buff[0] = fengbao_num | 0x80; + n = sizeof(IS11_datastruct) % 960; + } + memcpy(data_buff+1,(uint8_t *)(&IS11_datastruct_send)+960*(fengbao_num-1),n); + send_lenth = IRIS_Protocol_Pack(0x02,n+1,data_buff,send_buff); + wb485Serial->write(send_buff,send_lenth); + delete data_buff; + } + else + { + send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)(&IS11_datastruct_send),send_buff); + wb485Serial->write(send_buff,send_lenth); + } + // send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)(&IS11_datastruct_send),send_buff); + // wb485Serial->write(send_buff,send_lenth); + delete send_buff; + vTaskDelay(50); +} + + +void SensorIS11::send_chongcaiyang(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num) +{ + struct IS11_datastruct IS11_datastruct_send = *IS11_datastructure; + int32_t send_lenth; + u_char *send_buff = new u_char[sizeof(IS11_datastruct) + 7]; + IS11_datastruct_send.type = 0x02; + + if(fengbao_num <= 1 ) + { + const int numValues = 2048; + float xValues[2048]; + float *yValues = IS11_datastructure->data; + for (uint32_t i = 0; i < 2048; i++) + { + xValues[i] = SensorInfo.a1 * i * i * i + SensorInfo.a2 * i * i + SensorInfo.a3 * i+ SensorInfo.a4; + } + + if(chongcaiyang_step <= 0.5) + { + chongcaiyang_step = 0.5; + } + + float xValue_target = ceil(SensorInfo.a4); + float xValue_stop = SensorInfo.a1 * 2047 *2047*2047 + SensorInfo.a2 *2047*2047 + SensorInfo.a3 *2047 + SensorInfo.a4; + + + uint32_t b = 0; + while(xValue_target < xValue_stop) + { + IS11_datastruct_send.data[b] = CatmullSpline(xValues, yValues, numValues, xValue_target); + b++; + xValue_target += chongcaiyang_step; + } + + for(uint32_t c = b; c < 2048; c++) + { + IS11_datastruct_send.data[c] = 0; + } + IS11_datastruct_send.index = b+1; + } + + if(fengbao_num >= 1) + { + // uint32_t n= 960; + // if(fengbao_num == 8) n = 568; + // send_lenth = IRIS_Protocol_Pack(0x02,n,(uint8_t *)(&IS11_datastruct_send) + 960*(fengbao_num-1),send_buff); + // wb485Serial->write(send_buff,send_lenth); + + u_char *data_buff = new u_char[960+1]; + data_buff[0] = fengbao_num; + uint32_t x = sizeof(IS11_datastruct) / 960; + uint32_t n = 960; + if(fengbao_num == x+1) + { + data_buff[0] = fengbao_num | 0x80; + n = sizeof(IS11_datastruct) % 960; + } + memcpy(data_buff+1,(uint8_t *)(&IS11_datastruct_send)+960*(fengbao_num-1),n); + send_lenth = IRIS_Protocol_Pack(0x02,n+1,data_buff,send_buff); + wb485Serial->write(send_buff,send_lenth); + delete data_buff; + } + else + { + send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)(&IS11_datastruct_send),send_buff); + wb485Serial->write(send_buff,send_lenth); + } + // send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)(&IS11_datastruct_send),send_buff); + // wb485Serial->write(send_buff,send_lenth); + + delete send_buff; + vTaskDelay(50); +} + + + + +void SensorIS11::get_fanshelv() +{ + IS11_datastruct_fanshelv.type = 0x06; + + for (int i = 0; i < 2048;i++) + { + IS11_datastruct_fanshelv.data[i] =IS11_datastruct_up.data[i] / IS11_datastruct_down.data[i]; + } + + IS11_datastruct_fanshelv.NCa == 1; +} + +void SensorIS11::send_fanshelv(HardwareSerial *wb485Serial) +{ + if(IS11_datastruct_fanshelv.NCa == 0) + { + get_fanshelv(); + } + + int32_t send_lenth; + u_char *send_buff = new u_char[sizeof(IS11_datastruct) + 7]; + + send_lenth = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), (uint8_t *)(&IS11_datastruct_fanshelv),send_buff); + wb485Serial->write(send_buff,send_lenth); + delete send_buff; + vTaskDelay(50); +} + +void SensorIS11::calculation(float bochang1,float bochang2) +{ + if(IS11_datastruct_fanshelv.NCa == 0) + { + get_fanshelv(); + } + + float xValues[2048]; + float *yValues = IS11_datastruct_fanshelv.data; + const int numValues = 2048; + for(uint32_t i = 0; i < 2048; i++) + { + xValues[i] = SensorInfo.a1 * i * i * i + SensorInfo.a2 * i * i + SensorInfo.a3 * i+ SensorInfo.a4; + } + float R1 = CatmullSpline(xValues, yValues, numValues, bochang1); + float R2 = CatmullSpline(xValues, yValues, numValues, bochang2); + + calculation_value = (R1 - R2) / (R1 + R2); +} + + + + +//send_data_type 0x00 DN类型 只回传up、down、up_darknoise、down_darknoise +//send_data_type 0x01 DN类型 只回传up、down +//send_data_type 0x02 DN类型 只回传up +//send_data_type 0x03 DN类型 只回传down +//send_data_type 0x04 DN类型 只回传up_darknosie +//send_data_type 0x05 DN类型 只回传down_darknoise +//send_data_type 0x06 DN类型 只回传dark (这个dark主要是在高级模式下才有,高级模式下dark不用区分方向) + + +//send_data_type 0x10 radiance类型 只回传up、down和DN类型的 up_darknoise、down_darknoise +//send_data_type 0x11 radiance类型 只回传up、down +//send_data_type 0x12 radiance类型 只回传up +//send_data_type 0x13 radiance类型 只回传down +//send_data_type 0x14 radiance类型 反射率 + +//send_data_type 0x20 重采样值 只回传up、down和DN类型的 up_darknoise、down_darknoise +//send_data_type 0x21 重采样值 只回传up、down +//send_data_type 0x22 重采样值 只回传up +//send_data_type 0x23 重采样值 只回传down +//send_data_type 0x24 重采样值 只回传up_darknosie +//send_data_type 0x25 重采样值 只回传down_darknoise +//send_data_type 0x26 重采样值 只回传dark (这个dark主要是在高级模式下才有,高级模式下dark不用区分方向) + +void SensorIS11::senddata(HardwareSerial *wb485Serial,u_int32_t send_data_type,uint32_t fengbao_num) +{ + switch (send_data_type) + { + case 0X00: + send_dn(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_down, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_dark_down, fengbao_num); + break; + case 0X01: + send_dn(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X02: + send_dn(wb485Serial,&IS11_datastruct_up, fengbao_num); + break; + case 0X03: + send_dn(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X04: + send_dn(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + break; + case 0X05: + send_dn(wb485Serial,&IS11_datastruct_dark_down, fengbao_num); + break; + + case 0X10: + send_radiance(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_radiance(wb485Serial,&IS11_datastruct_down, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + send_dn(wb485Serial,&IS11_datastruct_dark_down, fengbao_num); + break; + case 0X11: + send_radiance(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_radiance(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X12: + send_radiance(wb485Serial,&IS11_datastruct_up, fengbao_num); + break; + case 0X13: + send_radiance(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X14: + send_fanshelv(wb485Serial); + break; + + case 0X20: + send_chongcaiyang(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_chongcaiyang(wb485Serial,&IS11_datastruct_down, fengbao_num); + send_chongcaiyang(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + send_chongcaiyang(wb485Serial,&IS11_datastruct_dark_down, fengbao_num); + break; + case 0X21: + send_chongcaiyang(wb485Serial,&IS11_datastruct_up, fengbao_num); + send_chongcaiyang(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X22: + send_chongcaiyang(wb485Serial,&IS11_datastruct_up, fengbao_num); + break; + case 0X23: + send_chongcaiyang(wb485Serial,&IS11_datastruct_down, fengbao_num); + break; + case 0X24: + send_chongcaiyang(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + break; + case 0X25: + send_chongcaiyang(wb485Serial,&IS11_datastruct_dark_down, fengbao_num); + break; + case 0X26: + send_chongcaiyang(wb485Serial,&IS11_datastruct_dark_up, fengbao_num); + break; + default: + break; + } +} + +/// + + + +/* +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +高级模式 +支持选择采集的方向,up、down、dark +是否自动扣除暗电流 +不支持自动曝光, +平均次数 + + +direction 0:up 1:down 2:dark +shutter_time 使用指定时间曝光 单位毫秒 最大时长30秒 必须大于0 +remove_dark 0:不扣除 1:扣除 +collect_times 采集次数 +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +*/ +void SensorIS11::advanced_mode(u_int32_t direction,u_int32_t shutter_time,u_int32_t collect_times,u_int32_t remove_dark) +{ + + work_progress = 0; + struct IS11_datastruct *IS11_datastructure_avn = nullptr; + switch (direction) + { + case 0: + servo_direction(1); + SetShutter(2); + IS11_datastructure_avn = &IS11_datastruct_up; + break; + case 1: + servo_direction(0); + SetShutter(2); + IS11_datastructure_avn = &IS11_datastruct_down; + break; + case 2: + SetShutter(1); + IS11_datastructure_avn = &IS11_datastruct_dark_up; + break; + default: + break; + } + + int aa = collect_times; + uint32_t caiji_times = 0; + memset(IS11_datastructure_avn->data,0,8192); + getall_temp(IS11_datastructure_avn->temprature); + ds1302_date.getDateTime(&sys_time); + IS11_datastructure_avn->year = sys_time.year; + IS11_datastructure_avn->month = sys_time.month; + IS11_datastructure_avn->day = sys_time.day; + IS11_datastructure_avn->hour = sys_time.hour; + IS11_datastructure_avn->minute = sys_time.minute; + IS11_datastructure_avn->second = sys_time.second; + + while(aa > 0) + { + work_progress = (caiji_times * 100) / (collect_times * 2) ; + + GetOneDate(shutter_time); + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastructure_avn->data[i] += DATABUFF[i]; + } + IS11_datastructure_avn->type = 0; + IS11_datastructure_avn->direction = direction; + IS11_datastructure_avn->tuigan_stat = get_tuigan_status(); + IS11_datastructure_avn->shutter_time = shutter_time; + IS11_datastructure_avn->index = index; + + if(remove_dark == 1 && direction != 2) + { + + // write_log(log_path,"remove_dark",10); + SetShutter(1); + GetOneDate(shutter_time); + SetShutter(2); + for (int i = 0; i < SensorInfo.BandNum;i++) + { + // if (i==1000) + // { + // write_log(log_path,"or data:"+String(IS11_datastructure_avn->data[i])+ " darkdata:"+String(DATABUFF[i]),10); + + // /* code */ + // } + IS11_datastructure_avn->data[i] -= DATABUFF[i]; + IS11_datastruct_dark_up.data[i] = DATABUFF[i]; + } + + IS11_datastruct_dark_up.type = 0; + IS11_datastruct_dark_up.direction = 2; + IS11_datastruct_dark_up.tuigan_stat = get_tuigan_status(); + IS11_datastruct_dark_up.shutter_time = shutter_time; + IS11_datastruct_dark_up.index = index; + // getall_temp(IS11_datastruct_dark_up.temprature); + } + aa--; + caiji_times++; + } + for (int i = 0; i < SensorInfo.BandNum;i++) + { + IS11_datastructure_avn->data[i] = IS11_datastructure_avn->data[i] / collect_times; + } + SetShutter(1); + servo_off(); + IS11_datastruct_fanshelv.NCa == 0; + work_progress = 100; +} + +/* +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +opt +方向有up、down、dark +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +*/ +void SensorIS11::opt(u_int32_t direction) +{ + switch (direction) + { + case 0: + // servo_direction(1); + SetShutter(2); + break; + case 1: + // servo_direction(0); + SetShutter(2); + break; + case 2: + SetShutter(1); + break; + default: + break; + } + work_progress = 50; + dingbiao_shutter = OptSnenser(70); + work_progress = 100; + SetShutter(1); +} + + + +////////////////////////////////////////////////////////////////////////////////////////////// +void SensorIS11::save_dingbiao(uint8_t * data) +{ + IS11_datastruct *IS11_datastructure = (IS11_datastruct *)data; + float ab = 0; + write_log(log_path,"save_dingbiao",10); + write_log(log_path,"direction:"+String(IS11_datastructure->direction),10); + write_log(log_path,"type:"+String(IS11_datastructure->type),10); + if(IS11_datastructure->direction == 0) + { + if (IS11_datastructure->type == 4) + { + radiance_struct_up.shutter = IS11_datastructure->shutter_time; + write_log(log_path,"shutter_time:"+String(IS11_datastructure->shutter_time),10); + + for (int i = 0; i < BANDNUMBER;i++) + { + radiance_struct_up.gain[i] = IS11_datastructure->data[i]; + } + ab = IS11_datastructure->data[0] * 1000000; + write_log(log_path,"gain:"+String(ab),10); + } + else if (IS11_datastructure->type == 5) + { + for (int i = 0; i < BANDNUMBER;i++) + { + radiance_struct_up.offset[i] = IS11_datastructure->data[i]; + } + write_log(log_path,"offset:"+String(IS11_datastructure->data[0]),10); + } + else + { + return; + } + } + else if(IS11_datastructure->direction == 1) + { + if (IS11_datastructure->type == 4) + { + radiance_struct_down.shutter = IS11_datastructure->shutter_time; + write_log(log_path,"shutter_time:"+String(IS11_datastructure->shutter_time),10); + for (int i = 0; i < BANDNUMBER;i++) + { + radiance_struct_down.gain[i] = IS11_datastructure->data[i]; + } + ab = IS11_datastructure->data[0] * 1000000; + write_log(log_path,"gain:"+String(ab),10); + } + else if (IS11_datastructure->type == 5) + { + for (int i = 0; i < BANDNUMBER;i++) + { + radiance_struct_down.offset[i] = IS11_datastructure->data[i]; + } + write_log(log_path,"offset:"+String(IS11_datastructure->data[0]),10); + } + else + { + return; + } + } + else + { + return; + } +} + + + + + + + +////////////////////////////////////////////////////////////////////////////////////////////// +/* +//////////////////////////// +波长 序列号x 能量值 +=a1*x^3+a2*x^2+a3*x+a4 0 1111 + + +DC +暗电流测量的目的 设备本身的干扰 主要受到温度的影响 +测试不同的波长对应的能量 + +DN +通过光栅将光分解为不同的波长 然后照射到CCD上 +CCD的将不同波长的能量采集出来转换为DN值 + + +opt 最佳曝光的时长 +采集到的能量和曝光时间有关 +CCD采集能量的最好范围是 70%到90%之间是线性的 可信度最高 准确率最好 +*/ + + +/* + Radiance = DN / shuttertime * shutter * a -b; + + + +*/ + + + + + +//send_data_type 0x02 DN类型 只回传up +//send_data_type 0x03 DN类型 只回传down +//send_data_type 0x04 DN类型 只回传up_darknosie +//send_data_type 0x05 DN类型 只回传down_darknoise + +//send_data_type 0x12 radiance类型 只回传up +//send_data_type 0x13 radiance类型 只回传down +//send_data_type 0x14 radiance类型 反射率 + +//send_data_type 0x22 重采样值 只回传up +//send_data_type 0x23 重采样值 只回传down \ No newline at end of file diff --git a/src/SensorIS11.h b/src/SensorIS11.h new file mode 100644 index 0000000..c7580eb --- /dev/null +++ b/src/SensorIS11.h @@ -0,0 +1,80 @@ +#ifndef SENSORIS11_H +#define SENSORIS11_H +#include +#include "comon.h" +#include "IS11Comon.h" +#include "log.h" +#include "Communication_Protocol.h" +#include "tuigan.h" +#include "servo.h" +#include "DS18B20.h" +#include "SoftwareSerial.h" +#include "SDmanger.h" +#include "ds1302.h" +#include "InterpolationLib.h" +#include "CastumullSpline.h" + +class SensorIS11 { +public: + SensorIS11(); + STRSensorInfo SensorInfo; + STRSensorInfo initSensor(); + STRSensorInfo GetSensorInfo(); + void SetShutter(int id);// 0 dark 1 green 2 blue + void GetOneDate(int msc); + int OptSnenser(int percent); + unsigned short DATABUFF[2048]; + int shutterup,shutterdown; + unsigned short *DownData=nullptr; + unsigned short *UpData=nullptr; + void shortLittletoBiG( unsigned short *data,int lenth); + + int Getmaxvalue(unsigned short *data,int lenth); + // void TakeOneJob(); + + void TakeOneJob1(bool dingbing); + void TakeOneJob(); + void servo_direction(int direction); + + void send_dn(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num); + void send_radiance(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num); + void senddata(HardwareSerial *wb485Serial,u_int32_t send_data_type,uint32_t fengbao_num); + void save_dingbiao(uint8_t * data); + void save_guangpu_data(IS11_datastruct * data); + // int dingbiao(u_int32_t step,u_int32_t shutter_timer); + + void advanced_mode(u_int32_t direction,u_int32_t shutter_time,u_int32_t collect_times,u_int32_t remove_dark); + void opt(u_int32_t direction); + PRINTFUNC PrintFunc; + void send_chongcaiyang(HardwareSerial *wb485Serial,IS11_datastruct *IS11_datastructure,uint32_t fengbao_num); + + + void get_fanshelv(); + void send_fanshelv(HardwareSerial *wb485Serial); + void calculation(float bochang1,float bochang2); + // Ds1302 ds1302; + + volatile float calculation_value; + + int shutternow; + u_int32_t index = 0; + volatile u_int32_t work_progress; + volatile u_int32_t dingbiao_shutter; + //uint16_t result[2048]; + + + float chongcaiyang_step = 0.2; + clalidata_struct radiance_struct_up; + clalidata_struct radiance_struct_down; + + + u_char *DataRetrun; + + +}; + + + + + +#endif \ No newline at end of file diff --git a/src/TCPserver.cpp b/src/TCPserver.cpp new file mode 100644 index 0000000..84b16b3 --- /dev/null +++ b/src/TCPserver.cpp @@ -0,0 +1,206 @@ +#include "TCPserver.h" +bool fenge(String &zifuchuan, String &outstring, String fengefu) +{ + int weizhi = 0; //找查的位置 + + if (zifuchuan == "") + { + // Serial.print("commanesdafasdf"); + return false; + + /* code */ + } + + weizhi = zifuchuan.indexOf(fengefu); //找到位置 + if (weizhi != -1) //如果位置不为空 + { + // Serial.println(zifuchuan); + outstring = zifuchuan.substring(0, weizhi); //打印取第一个字符 + zifuchuan = zifuchuan.substring(weizhi + fengefu.length(), zifuchuan.length()); + return true; + //分隔后只取后面一段内容 以方便后面找查 + } + else + { + // Serial.print(zifuchuan); + outstring = zifuchuan; + zifuchuan = ""; + return true; + } +} +TCPserver::TCPserver(int port) +{ + server = new WiFiServer(port); + myserial = nullptr; + isPortOutInit = false; + +} +#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE +TCPserver::TCPserver(USBCDC *serial,int port) +{ + server = new WiFiServer(port); + myserial = serial; + isPortOutInit = true; + +} +#elif ARDUINO_USB_CDC_ON_BOOT && ARDUINO_USB_MODE +TCPserver::TCPserver(HWCDC *serial,int port) +{ + server = new WiFiServer(port); + myserial = serial; + isPortOutInit = true; + +} +#else +TCPserver::TCPserver(HardwareSerial *serial, int port /* args */) +{ + server = new WiFiServer(port); + myserial = serial; + isPortOutInit = true; + +} +#endif +void TCPserver::initme() +{ + server->begin(); + //关闭小包合并包功能,不会延时发送数据 + server->setNoDelay(true); +} +void TCPserver::loop() +{ + uint8_t i; + //检测是否有新的client请求进来 + // myserial->print("asdffsd"); + if (server->hasClient()) + { + for (i = 0; i < MAX_SRV_CLIENTS; i++) + { + //释放旧无效或者断开的client + if (!serverClients[i] || !serverClients[i].connected()) + { + if (serverClients[i]) + { + serverClients[i].stop(); + } + //分配最新的client + serverClients[i] = server->available(); + if (isPortOutInit) + { + myserial->print("New client: "); + myserial->print(i); + /* code */ + } + break; + } + } + //当达到最大连接数 无法释放无效的client,需要拒绝连接 + if (i == MAX_SRV_CLIENTS) + { + WiFiClient serverClient = server->available(); + serverClient.stop(); + if (isPortOutInit) + { + myserial->print("Connection rejected "); + } + } + } + //检测client发过来的数据 + for (i = 0; i < MAX_SRV_CLIENTS; i++) + { + if (serverClients[i] && serverClients[i].connected()) + { + if (serverClients[i].available()) + { + // get data from the telnet client and push it to the UART + String str = ""; + while (serverClients[i].available()) + { + //发送到串口调试器 + if (isPortOutInit) + { + str = str + String((char)serverClients[i].read()); + // myserial->write(serverClients[i].read()); + /* code */ + } + } + + } + } + } + if (isPortOutInit) + { + + if (myserial->available()) + { + //把串口调试器发过来的数据 发送给client + size_t len = myserial->available(); + uint8_t sbuf[len]; + myserial->readBytes(sbuf, len); + // push UART data to all connected telnet clients + for (i = 0; i < MAX_SRV_CLIENTS; i++) + { + if (serverClients[i] && serverClients[i].connected()) + { + serverClients[i].write(sbuf, len); + vTaskDelay(1); + } + } + } + } +} +void TCPserver::SendDataToClinet(String str, int clinetid) +{ + + // myserial->print("Connection rejected "); + if (clinetid == -1) + { + int i; + for (i = 0; i < MAX_SRV_CLIENTS; i++) + { + if (serverClients[i] && serverClients[i].connected()) + { + serverClients[i].println(str); + vTaskDelay(1); + } + } + /* code */ + } + else + { + if (serverClients[clinetid] && serverClients[clinetid].connected()) + { + serverClients[clinetid].println(str); + vTaskDelay(1); + } + } +} +void TCPserver::SendDataToClinet(char *str, int clinetid) +{ + // push UART data to all connected telnet clients + myserial->print("Connection rejected "); + if (clinetid == -1) + { + int i; + for (i = 0; i < MAX_SRV_CLIENTS; i++) + { + if (serverClients[i] && serverClients[i].connected()) + { + serverClients[i].write(str); + vTaskDelay(1); + } + } + /* code */ + } + else + { + if (serverClients[clinetid] && serverClients[clinetid].connected()) + { + serverClients[clinetid].write(str); + vTaskDelay(1); + } + } +} +TCPserver::~TCPserver() +{ + delete server; +} \ No newline at end of file diff --git a/src/TCPserver.h b/src/TCPserver.h new file mode 100644 index 0000000..a1f9f84 --- /dev/null +++ b/src/TCPserver.h @@ -0,0 +1,49 @@ +#ifndef TCPSERVER_H +#define TCPSERVER_H + +#include +#ifdef ESP8266 +#include +#else +#include +#endif + +#define MAX_SRV_CLIENTS 2 +class TCPserver +{ +private: + /* data */ + //创建server 端口号是23 + WiFiServer *server; +//管理clients + WiFiClient serverClients[MAX_SRV_CLIENTS]; + + bool isPortOutInit; + +public: + + +#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE //Serial used for USB CDC + USBCDC *myserial; + TCPserver(USBCDC *serial,int port); +#elif ARDUINO_USB_CDC_ON_BOOT && ARDUINO_USB_MODE + HWCDC *myserial; + TCPserver(HWCDC *serial,int port); +#else + HardwareSerial *myserial; + TCPserver(HardwareSerial *serial,int port); +#endif + + + + TCPserver(int port); + void initme(); + ~TCPserver(); + void SendDataToClinet(String str,int clinetid=-1); + void SendDataToClinet(char *str,int clinetid=-1); + void loop(); + +}; + +#endif + diff --git a/src/adc.cpp b/src/adc.cpp new file mode 100644 index 0000000..005cd3b --- /dev/null +++ b/src/adc.cpp @@ -0,0 +1,22 @@ +#include "adc.h" + +//电源电压测量 + +void adc_init() +{ + pinMode(ADC_PIN,INPUT); +} + +float adc_read() +{ + float Value =0; + float power =0; + for(uint8_t i = 0; i < 10; i++) + { + Value = analogRead(ADC_PIN); + power += (Value * 3.3 / 4095.0) * 11; + } + power = power / 10.f + 0.51; + // write_log(log_path,"power: " + String(power),10); + return power; +} \ No newline at end of file diff --git a/src/adc.h b/src/adc.h new file mode 100644 index 0000000..52040f7 --- /dev/null +++ b/src/adc.h @@ -0,0 +1,10 @@ +#ifndef ADC_H +#define ADC_H +#include +#include "log.h" +#define ADC_PIN 35 +void adc_init(); +float adc_read(); + +#endif + diff --git a/src/beep.cpp b/src/beep.cpp new file mode 100644 index 0000000..8fac487 --- /dev/null +++ b/src/beep.cpp @@ -0,0 +1,38 @@ +#include "beep.h" +uint8_t chan; +uint8_t bit_num; + +void beep_init(uint8_t beep_chan, uint32_t beep_freq, uint8_t beep_bit_num) +{ + chan = beep_chan; + bit_num = beep_bit_num; + ledcSetup(chan, beep_freq , bit_num); + ledcAttachPin(BEEP_PIN, chan); + ledcWrite(chan, 0); +} + +void beep_run( uint32_t freq,uint32_t duty) +{ + ledcSetup(chan, freq , bit_num); + + ledcWrite(chan, duty); +} +void beep_stop() +{ + ledcWrite(chan, 0); +} + + +// beep_init(39,0,2000,10); +// while (1) +// { +// beep_run(2000,512); +// vTaskDelay(1000); +// beep_stop(); +// vTaskDelay(1000); +// } +//开机成功长响3秒 +//光谱仪故障一直滴滴声 +//温度异常报警快速滴滴声 +//其他功能异常慢速滴滴声 + diff --git a/src/beep.h b/src/beep.h new file mode 100644 index 0000000..b5bbbc1 --- /dev/null +++ b/src/beep.h @@ -0,0 +1,11 @@ +#ifndef _BEEP_H_ +#define _BEEP_H_ + +#include + +#define BEEP_PIN 39 + +void beep_stop(); +void beep_run( uint32_t freq,uint32_t duty); +void beep_init(uint8_t beep_chan, uint32_t beep_freq, uint8_t beep_bit_num); +#endif \ No newline at end of file diff --git a/src/comon.h b/src/comon.h new file mode 100644 index 0000000..1702a8e --- /dev/null +++ b/src/comon.h @@ -0,0 +1,34 @@ +#include + +// struct STRSensorInfo +// { +// std::string SensorName; +// long maxValue; +// long BandNum; +// std::string WavelenthStr; +// float *wavelenthlist; +// //double *wavelenth; +// bool isSensorInit; +// std::string serialnumber; +// float a1,a2,a3,a4; + +// }; + +struct STRSensorInfo +{ + String SensorName; + long maxValue; + long BandNum; + String WavelenthStr; + float *wavelenthlist; + //double *wavelenth; + bool isSensorInit; + String serialnumber; + float a1,a2,a3,a4; + +}; + + +typedef void (*PRINTFUNC)(std::string ); + + diff --git a/src/ds1302.cpp b/src/ds1302.cpp new file mode 100644 index 0000000..dc6a08a --- /dev/null +++ b/src/ds1302.cpp @@ -0,0 +1,200 @@ +/** Ds1302.cpp + * + * Ds1302 class. + * + * @version 1.0.3 + * @author Rafa Couto + * @license GNU Affero General Public License v3.0 + * @see https://github.com/Treboada/Ds1302 + * + */ + +#include "ds1302.h" + +#include + +#define REG_SECONDS 0x80 +#define REG_MINUTES 0x82 +#define REG_HOUR 0x84 +#define REG_DATE 0x86 +#define REG_MONTH 0x88 +#define REG_DAY 0x8A +#define REG_YEAR 0x8C +#define REG_WP 0x8E +#define REG_BURST 0xBE + + +Ds1302::Ds1302(uint8_t pin_ena, uint8_t pin_clk, uint8_t pin_dat) +{ + _pin_ena = pin_ena; + _pin_clk = pin_clk; + _pin_dat = pin_dat; + _dat_direction = INPUT; +} + + +void Ds1302::init() +{ + pinMode(_pin_ena, OUTPUT); + pinMode(_pin_clk, OUTPUT); + pinMode(_pin_dat, _dat_direction); + + digitalWrite(_pin_ena, LOW); + digitalWrite(_pin_clk, LOW); +} + + +bool Ds1302::isHalted() +{ + _prepareRead(REG_SECONDS); + uint8_t seconds = _readByte(); + _end(); + return (seconds & 0b10000000); +} + + +void Ds1302::getDateTime(DateTime* dt) +{ + _prepareRead(REG_BURST); + dt->second = _bcd2dec(_readByte() & 0b01111111); + dt->minute = _bcd2dec(_readByte() & 0b01111111); + dt->hour = _bcd2dec(_readByte() & 0b00111111); + dt->day = _bcd2dec(_readByte() & 0b00111111); + dt->month = _bcd2dec(_readByte() & 0b00011111); + dt->dow = _bcd2dec(_readByte() & 0b00000111); + dt->year = _bcd2dec(_readByte() & 0b11111111); + _end(); +} + + +void Ds1302::setDateTime(DateTime* dt) +{ + _prepareWrite(REG_WP); + _writeByte(0b00000000); + _end(); + + _prepareWrite(REG_BURST); + _writeByte(_dec2bcd(dt->second % 60 )); + _writeByte(_dec2bcd(dt->minute % 60 )); + _writeByte(_dec2bcd(dt->hour % 24 )); + _writeByte(_dec2bcd(dt->day % 32 )); + _writeByte(_dec2bcd(dt->month % 13 )); + _writeByte(_dec2bcd(dt->dow % 8 )); + _writeByte(_dec2bcd(dt->year % 100)); + _writeByte(0b10000000); + _end(); +} + + +void Ds1302::halt() +{ + _setHaltFlag(true); +} + + +void Ds1302::start() +{ + _setHaltFlag(false); +} + + +void Ds1302::_setHaltFlag(bool stopped) +{ + uint8_t regs[8]; + _prepareRead(REG_BURST); + for (int b = 0; b < 8; b++) regs[b] = _readByte(); + _end(); + + if (stopped) regs[0] |= 0b10000000; else regs[0] &= ~0b10000000; + regs[7] = 0b10000000; + + _prepareWrite(REG_WP); + _writeByte(0b00000000); + _end(); + + _prepareWrite(REG_BURST); + for (int b = 0; b < 8; b++) _writeByte(regs[b]); + _end(); +} + + +void Ds1302::_prepareRead(uint8_t address) +{ + _setDirection(OUTPUT); + digitalWrite(_pin_ena, HIGH); + uint8_t command = 0b10000001 | address; + _writeByte(command); + _setDirection(INPUT); +} + + +void Ds1302::_prepareWrite(uint8_t address) +{ + _setDirection(OUTPUT); + digitalWrite(_pin_ena, HIGH); + uint8_t command = 0b10000000 | address; + _writeByte(command); +} + + +void Ds1302::_end() +{ + digitalWrite(_pin_ena, LOW); +} + + +uint8_t Ds1302::_readByte() +{ + uint8_t byte = 0; + + for(uint8_t b = 0; b < 8; b++) + { + if (digitalRead(_pin_dat) == HIGH) byte |= 0x01 << b; + _nextBit(); + } + + return byte; +} + + +void Ds1302::_writeByte(uint8_t value) +{ + for(uint8_t b = 0; b < 8; b++) + { + digitalWrite(_pin_dat, (value & 0x01) ? HIGH : LOW); + _nextBit(); + value >>= 1; + } +} + +void Ds1302::_nextBit() +{ + digitalWrite(_pin_clk, HIGH); + delayMicroseconds(1); + + digitalWrite(_pin_clk, LOW); + delayMicroseconds(1); +} + + +void Ds1302::_setDirection(int direction) +{ + if (_dat_direction != direction) + { + _dat_direction = direction; + pinMode(_pin_dat, direction); + } +} + + +uint8_t Ds1302::_dec2bcd(uint8_t dec) +{ + return ((dec / 10 * 16) + (dec % 10)); +} + + +uint8_t Ds1302::_bcd2dec(uint8_t bcd) +{ + return ((bcd / 16 * 10) + (bcd % 16)); +} + diff --git a/src/ds1302.h b/src/ds1302.h new file mode 100644 index 0000000..84eba0d --- /dev/null +++ b/src/ds1302.h @@ -0,0 +1,120 @@ +/** Ds1302.h + * + * Ds1302 class. + * + * @version 1.0.3 + * @author Rafa Couto + * @license GNU Affero General Public License v3.0 + * @see https://github.com/Treboada/Ds1302 + * + */ + +#ifndef _DS_1302_H +#define _DS_1302_H + +#include + +class Ds1302 +{ + public: + + typedef struct { + uint8_t year; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t dow; + } DateTime; + + /** + * Months of year + */ + enum MONTH : uint8_t { + MONTH_JAN = 1, + MONTH_FEB = 2, + MONTH_MAR = 3, + MONTH_APR = 4, + MONTH_MAY = 5, + MONTH_JUN = 6, + MONTH_JUL = 7, + MONTH_AUG = 8, + MONTH_SEP = 9, + MONTH_OCT = 10, + MONTH_NOV = 11, + MONTH_DEC = 12 + }; + + /** + * Days of week + */ + enum DOW : uint8_t { + DOW_MON = 1, + DOW_TUE = 2, + DOW_WED = 3, + DOW_THU = 4, + DOW_FRI = 5, + DOW_SAT = 6, + DOW_SUN = 7 + }; + + /** + * Constructor (pin configuration). + */ + Ds1302(uint8_t pin_ena, uint8_t pin_clk, uint8_t pin_dat); + + /** + * Initializes the DW1302 chip. + */ + void init(); + + /** + * Returns when the oscillator is disabled. + */ + bool isHalted(); + + /** + * Stops the oscillator. + */ + void halt(); + + /** + * Starts the oscillator. + */ + void start(); + + /** + * Returns the current date and time. + */ + void getDateTime(DateTime* dt); + + /** + * Sets the current date and time. + */ + void setDateTime(DateTime* dt); + + private: + + uint8_t _pin_ena; + uint8_t _pin_clk; + uint8_t _pin_dat; + + void _prepareRead(uint8_t address); + void _prepareWrite(uint8_t address); + void _end(); + + int _dat_direction; + void _setDirection(int direction); + + uint8_t _readByte(); + void _writeByte(uint8_t value); + void _nextBit(); + + uint8_t _dec2bcd(uint8_t dec); + uint8_t _bcd2dec(uint8_t bcd); + + void _setHaltFlag(bool stopped); +}; + +#endif // _DS_1302_H diff --git a/src/gaoguangpu.code-workspace b/src/gaoguangpu.code-workspace new file mode 100644 index 0000000..6b1f9cc --- /dev/null +++ b/src/gaoguangpu.code-workspace @@ -0,0 +1,62 @@ +{ + "folders": [ + { + "path": ".." + } + ], + "settings": { + "files.associations": { + "*.tpp": "cpp", + "sstream": "cpp", + "typeinfo": "cpp", + "array": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "*.tcc": "cpp", + "fstream": "cpp", + "utility": "cpp", + "ostream": "cpp", + "random": "cpp", + "atomic": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "ratio": "cpp", + "thread": "cpp" + } + } +} \ No newline at end of file diff --git a/src/jiaresi.cpp b/src/jiaresi.cpp new file mode 100644 index 0000000..2013526 --- /dev/null +++ b/src/jiaresi.cpp @@ -0,0 +1,22 @@ + +#include "jiaresi.h" +#include "log.h" + +// 控制4组加热丝加热 +// status 1:开始加热 0:停止加热 +void jiaresi(unsigned char address ,enum jiare_status status) +{ + + if(status == start_jiare) + { + my74hc595_setpin(address , 1); + updatepin(); + } + else + { + my74hc595_setpin(address, 0); + updatepin(); + } +} + + diff --git a/src/jiaresi.h b/src/jiaresi.h new file mode 100644 index 0000000..c3963c3 --- /dev/null +++ b/src/jiaresi.h @@ -0,0 +1,19 @@ +#ifndef _jiaresi_H_ +#define _jiaresi_H_ +#include "Arduino.h" + + +#include "my74hc595.h" + + +enum jiare_status +{ + stop_jiare, + start_jiare +}; + + +void jiaresi(unsigned char address ,enum jiare_status status); +#endif + + diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..b582612 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,81 @@ +#include "log.h" +/* + +w+以纯文本方式读写,而wb+是以二进制方式进行读写。 + +mode说明: + +w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。 + +w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 + +wb 只写方式打开或新建一个二进制文件,只允许写数据。 + +wb+ 读写方式打开或建立一个二进制文件,允许读和写。 + +r 打开只读文件,该文件必须存在,否则报错。 + +r+ 打开可读写的文件,该文件必须存在,否则报错。 + +rb+ 读写方式打开一个二进制文件,只允许读写数据。 + +a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留) + +a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留) + +ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。 + +加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。 +*/ + +// SoftwareSerial wb485Serial(wb485PORT_RX,wb485PORT_TX,false); +// void log_init() +// { + +// } + +#ifdef ARDUINO_USB_CDC_ON_BOOT + #define U0_Serial Serial +#else + SoftwareSerial U0_Serial(19,20,false); +#endif + + + + +void log_init() +{ + U0_Serial.begin(115200); +} + +void write_log(String path,String write_data,unsigned char level) +{ + if(level <15) { + U0_Serial.println(write_data); + } + if (level == 20) + { + U0_Serial.println(write_data); + + File file; + file = SD_MMC.open(path,"ab+"); + file.println(write_data); + file.flush(); + file.close(); + } +} + +void write_log(uint8_t *data,uint32_t size) +{ + U0_Serial.write(data,size); +} + +void save_data(String path,String write_data) +{ + File file; + file = SD_MMC.open(path,"w+"); + file.println(write_data); + file.flush(); + file.close(); +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..840c66e --- /dev/null +++ b/src/log.h @@ -0,0 +1,28 @@ +#ifndef _LOG_H_ +#define _LOG_H_ + + + +#include "SDmanger.h" +#include + + + +#define log_path "/log/log.log" +#define gp_log "/gp.log" +#define log_level 10 +#define log_level_1 0 //打印到串口 +#define log_level_2 5 //写入文件 +#define log_level_3 10 //打印到串口和写入文件 + + + +void log_init(); +void write_log(String path,String write_data,unsigned char level); +void save_data(String path,String write_data); +void write_log(uint8_t *data,uint32_t size); +// void my_println(String data,int level); + + +#endif + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..9f67010 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,3099 @@ +#include"Define.h" +#include +#include +#include +#include +#include +#include +#include +//#include "MyWebServer.h" +#include "SDmanger.h" +//#include "HttpsOTAUpdate.h" +#include "slave.h" +#include "log.h" + +#include +#include +#include + +#include "IRIS_Method.h" +#include "DS18B20.h" +#include "Servo.h" +#include "jiaresi.h" +#include +#include "servo.h" +#include "SensorIS11.h" +#include "tuigan.h" +#include "beep.h" +#include "My74HC595.h" +#include "SolarCalculator.h" +#include "adc.h" +#define LOGGING + + + +#define IS1Serial_port 1 +#define IS1Serial_RX 18 +#define IS1Serial_TX 17 + +#define SIMUARTNUMER 2 + +#define wb485PORTNUMER 0 + +#ifdef ARDUINO_USB_CDC_ON_BOOT + #define SIMUART_RX 2 + #define SIMUART_TX 3 + + #define wb485PORT_RX 44 + #define wb485PORT_TX 43 + +#else + #define SIMUART_RX 16 + #define SIMUART_TX 17 + + #define wb485PORT_RX 2 + #define wb485PORT_TX 3 +#endif + +// #define GPS_4G + + + + + +// u_char ret[17]; + +// Ticker ticker; + +/* + 任务名: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); + +//work_mode 0:手动 1:时间模式 time_mode 2:高级模式 advanced_mode 3:low_power_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 = 9.0,stop_time = 15.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; + + +/// @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 ds18b20_doc(1024);//读取ds18b20 json + + +void json_command(); +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); + +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/////////////////////////////////////////// +GSMMannger *gsmmanger; +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(); +HttpClient *http; +//////////////////////////////////////////////////////////////////////////////////// + +// 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); +void setup() +{ + //关闭光谱仪 + pinMode(36,OUTPUT); + digitalWrite(36,LOW); + + pinMode(37, OUTPUT); //pin32控制sim800C电源 高电平上电 低电平断电 + digitalWrite(37, LOW); + vTaskDelay(1000); + Serial.begin(115200); + // while (1) + // { + // Serial.println("LOW"); + // digitalWrite(36,LOW); + // vTaskDelay(1000); + // Serial.println("HIGH"); + // digitalWrite(36,HIGH); + // vTaskDelay(1000); + // } + + + + + log_init(); + + + // wb485Serial.begin(115200); + // wb485Serial.begin(115200, SERIAL_8N1, wb485PORT_RX, wb485PORT_TX, false); + wb485Serial.begin(115200); + + vTaskDelay(1000); + // while(1) + // { + // Serial.println("start"); + // vTaskDelay(1000); + // } + + vTaskDelay(5000); + 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"); + + + /// + + // while(1) + // { + // write_log(log_path,"hello world",10); + // if(SD_MMC.exists("/guangpu_data/0")) + // { + // rm_dir_or_file("/guangpu_data/0"); + // } + // } + + // 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"); + //检查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(); + // float temp[temp_number]; + // getall_temp(temp); + // while (1) + // { + // getall_temp(temp); + // for (size_t i = 0; i < temp_number; i++) + // { + // write_log(log_path,"temp " + String(i + 1) + ": " + String(temp[i]),10); + // } + // vTaskDelay(10); + // } + + // sd_read_set_ds18b20_address(); + //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); + // while (1) + // { + // servo_set_angle(120); + // vTaskDelay(1000 * 1.5); + // servo_set_angle(0); + + // beep_run(2000,1000); + // vTaskDelay(1000 * 3); + // beep_stop(); + // } + + + // while(1) + // { + // beep_run(2000,1000); + // vTaskDelay(1000 * 3); + // beep_stop(); + // vTaskDelay(1000 * 3); + // } + + + + //初始化时间 + rtc.init(); + // now.year = 24; + // now.month = 5; + // now.day = 9; + // now.hour = 6; + // now.minute = 41; + // now.second = 0; + // now.dow = Ds1302::DOW_TUE; + // rtc.setDateTime(&now); + // vTaskDelay(1000); + // while(1) + // { + // rtc.getDateTime(&now); + // String time = String(now.year)+String(now.month)+String(now.day)+String(now.hour)+String(now.minute)+String(now.second); + // Serial.println(time); + // vTaskDelay(1000 * 5); + // } + 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(); +/////////////////////////加热测试//////////////////////////////////// +// while(1) +// { +// write_log(log_path,"start jiaresi",10); + + +// tuigan(tui); + +// vTaskDelay(1000 * 10); + +// write_log(log_path,"stop jiaresi",10); + +// tuigan(suo); +// vTaskDelay(1000 * 10); + +// } + + +///////////////////////////加热测试//////////////////////////////////// + +///////////////////////////推杆测试///////////////////////////////////// + // uint32_t x = 0; + // float n = 20; + // uint32_t data_length; + // uint8_t read_buf[100]; + // while (1) + // { + // while(wb485Serial.available()) + // { + // data_length += wb485Serial.readBytes(&read_buf[data_length],100); + // for (uint32_t i = 0; i < 5000; i++) + // { + // if(wb485Serial.available() > 0) + // { + // break; + // } + // } + // } + // //如果接收到数据 + // if (data_length > 0) + // { + // if(read_buf[0] == 'n') + // { + // //将转换为数字 + // n = atof((const char *) (read_buf+1)); + // if(n<5) n = 5; + // } + // if(read_buf[0] == 'H') + // { + // wb485Serial.println("n = " + String(n)); + // } + // memset(read_buf, 0, sizeof(read_buf)); + // data_length = 0; + // } + + + // if(x == 1) + // { + // wb485Serial.println("start tuigan tui"); + // tuigan(tui); + // } + // else if(x == 1000 *n) + // { + // wb485Serial.println("start tuigan suo"); + // tuigan(suo); + // } + // else if(x == 1000 * 2 * n) + // { + // x = 0; + // } + + // x++; + // vTaskDelay(1); + // } +///////////////////////////推杆测试///////////////////////////////////// + + +#ifdef GPS_4G + // //初始化AIR780EG + gsmmanger = new GSMMannger(SIMUARTNUMER, SIMUART_RX, SIMUART_TX); + http = new HttpClient(*gsmmanger->client, "82.156.1.111"); + String Date = getnetData(); + write_log(log_path,"date is :"+Date,10); + + gps_struct gps_structure; + + String gpsbac = get_GPS(&gps_structure); + + 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); + } +#endif + + + // while(1) + // { + // String Date = getnetData(); + // write_log(log_path,"date is :"+Date,10); + // vTaskDelay(1000); + // } + // //初始化AIR780EG + // gsmmanger = new GSMMannger(); + // //获取GPS + + + //计算日出日落 + // double transit, sunrise, sunset; + // double utc_offset = 8; + + + // while (1) + // { + // String Date = getnetData(); + // write_log(log_path,"date is :"+Date,10); + + // gpsbac = get_GPS(&gps_structure); + // write_log(log_path,"gpsbac: " + gpsbac,10); + // vTaskDelay(1000); + // } + + // int i=0; + // while(1) + // { + // gpsbac = get_GPS(&gps_structure); + // write_log(log_path,String(i)+"Getting GPS " + gpsbac,10); + // vTaskDelay(1000); + // i++; + // if (gpsbac != "-1") + // { + // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); + // write_log(log_path,"sunrise: " + String(sunrise),10); + // write_log(log_path,"sunset: " + String(sunset),10); + // write_log(log_path,"transit: " + String(transit),10); + // write_log(log_path,"latitude: " + String(gps_structure.latitude),10); + // write_log(log_path,"longitude: " + String(gps_structure.longitude),10); + // write_log(log_path,"gps_date: " + gpsbac,10); + // } + // } + + + //开启光谱仪 + // pinMode(36,OUTPUT); + digitalWrite(36,HIGH); + vTaskDelay(1000 * 2); + InitFunction(serialwrite,serialread); + is11Sensor = new SensorIS11(); + STRSensorInfos_structure = is11Sensor->initSensor(); + + + // while(1) + // { + // is11Sensor->SetShutter(2); + // vTaskDelay(1000); + // is11Sensor->SetShutter(1); + // vTaskDelay(1000); + // } + // 删除原来SD卡的系统信息 + + + sys_info_init(); + + // servo_set_angle(120); + 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(); + + // sys_sd_doc["is_spacial"] = "yes"; + + // deserializeJson(sys_sd_doc,system_info); + // while (1) + // { + // } + + xMutexInventory = xSemaphoreCreateMutex(); //创建MUTEX + if (xMutexInventory == NULL) { + + } else{ + //创建并启动Core1的任务 xTaskCreatePinnedToCore + xTaskCreatePinnedToCore( + Task0, /* 任务函数 */ + "Task0", /* 任务名称 */ + 1024 * 100, /* 栈大小 */ + NULL, /* 任务参数 */ + 2, /* 任务优先级 */ + &Task0_Handler, /* 任务句柄 */ + 0); /* Core 0 */ + + xTaskCreatePinnedToCore( + Task1, /* 任务函数 */ + "Task1", /* 任务名称 */ + 1024 * 5, /* 栈大小 */ + NULL, /* 任务参数 */ + 1, /* 任务优先级 */ + &Task1_Handler, /* 任务句柄 */ + 0); + + //创建并启动Core0的任务 + xTaskCreatePinnedToCore( + Task2, /* 任务函数 */ + "Task2", /* 任务名称 */ + 1024 * 60, /* 栈大小 */ + NULL, /* 任务参数 */ + 20, /* 任务优先级 */ + &Task2_Handler, /* 任务句柄 */ + 1); + } + // write_log(log_path,"setup ok",log_level); + // beep_run(2000,512); + // vTaskDelay(1000 * 3); + // beep_stop(); +} + +void loop() +{ + + +} +void Task0(void *pvParameters) +{ + vTaskDelay(1); + write_log(log_path,"task 0",10); + uint32_t uint_work_task0 = 100; + while (1) + { + 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, fengbao_num) + { + 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(10); + 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++) + // { + + 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; i++) + { + 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(10 * 1); + getall_temp(temperature); + vTaskDelay(10 * 1); + 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[1] + temperature[2]) / 2 > maximum_temperature) + { + write_log(log_path,"stop_jiare",20); + jiaresi(7,stop_jiare); + } + + if((temperature[1]+ temperature[2]) / 2 < Minimum_temperature) + { + if(temperature[1] <-120 || temperature[2] < -120) + { + write_log(log_path,"temperature 2,3 is broken.",20); + } + 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 * 10); + } + +} + +// Task2的实现 +void Task2(void *pvParameters) +{ + vTaskDelay(5); + // write_log(log_path,"task 2",10); + unsigned char read_buf[1024*10]; + 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]; + wb485Serial.setTimeout(1); + // u_char data_buff[300]; + + 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"); + 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) + { + // 测试数据 + // write_log(log_path,"data_length: " + String(data_length),log_level); + + int ret = IRIS_STM32_Protocol_Unpack(read_buf,data_length,&data_type,command_data); + // write_log(log_path,"ret:" +String(ret),log_level); + if (ret > 0) + { + // write_log(log_path,"data_type:" +String(data_type),log_level); + + // 改写为switch + switch (data_type) + { + case 0x00: + { + fengbao_num = 0; + deserializeJson(doc,command_data); + json_command(); + doc.clear(); + } + break; + // case 0x01: + // break; + case 0x03: + { + String dingbiao_path; + if (command_data[0] == 4 || command_data[0] == 5) + { + // 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"; + } + 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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.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); + wb485Serial.write(send_buff,send_lenth); + 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); + wb485Serial.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)); + } + // vTaskDelay(1); + } +} +/* +//处理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() +{ + int ret; + bool save = false; + String send_str; + u_char send_buff[1000]; + + 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); + } + //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); + } + else + { + save = true ; + sys_sd_doc["name"] = doc["name"].as(); + 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(doc["version"] == NULL) + { + + } + else + { + save = true ; + sys_sd_doc["version"] = doc["version"].as(); + + 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); + } + } + //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); + } + 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); + } + 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); + } + 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); + } + 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); + } + } + //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); + } + 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); + } + else if(doc["caiji_mode"] == "single") + { + save = true; + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + 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); + } + else if(doc["caiji_mode"] == "continue") + { + save = true; + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + 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); + } + 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); + } + } + //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); + } + 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); + } + } + //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); + } + 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); + } + } + //command 5 + else if (doc["command"] == "get_work_mode") + { + doc.clear(); + send_str = ""; + doc["work_mode"] = sys_sd_doc["work_mode"].as(); + doc["caiji_mode"] = sys_sd_doc["caiji_mode"].as(); + doc["atuo_return"] = sys_sd_doc["atuo_return"].as(); + 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); + } + //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(); + 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); + } + else if (doc["atuo_return"] == "no") + { + save = true; + sys_sd_doc["atuo_return"] = doc["atuo_return"].as(); + 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); + } + 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); + } + } + //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); + } + 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); + } + } + //command 8 + //保留,没用 + else if (doc["command"] == "set_return_mode") + { + save = true; + sys_sd_doc["return_mode"] = doc["return_mode"].as(); + + 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); + /*设置回传模式 */ + } + //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); + } + /* 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); + } + 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); + } + } + + //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); + } + + //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); + } + 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); + } + } + + //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); + } + 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); + } + } + //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); + } + 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); + } + } + //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); + } + 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); + } + } + + //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); + } + 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); + } + } + //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); + } + //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); + } + //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); + } + 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); + } + } + //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); + } + 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); + } + } + 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); + } + 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); + } + } + 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); + } + } + //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); + } + 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); + } + 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); + } + } + } + //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); + } + //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); + } + 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); + } + } + //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); + } + //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); + + } + //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); + } + //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); + } + //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(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); +} + + +String get_GPS(gps_struct *gps) +{ + +// write_log(log_path,"start get GPS ",10); +// String gpsbac,Date,temp,Latitude,Longitude; +// gsmmanger->modem->sendAT(GF("+CGNSPWR?")); +// gsmmanger->modem->waitResponse("OK"); + +// gsmmanger->modem->sendAT(GF("+CGNSPWR=1")); +// gsmmanger->modem->waitResponse("OK"); + +// gsmmanger->modem->sendAT(GF("+CGNSAID=31,1,1,1")); +// gsmmanger->modem->waitResponse("OK"); + +// gsmmanger->modem->sendAT(GF("+CGNSINF")); + +// //获取gps信息 +// gsmmanger->modem->waitResponse(5000,gpsbac); +// write_log(log_path,"gpsbac is " + gpsbac,10); + +// //检测是否有效 +// temp = gpsbac.substring(16,17); +// // write_log(log_path,"hahaha :" + temp,10); + +// if(temp != "1") +// { +// // write_log(log_path,"gps error,hahahaha",10); +// return "-1"; +// } +// //截取时间 +// Date = gpsbac.substring(18,32); +// temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1); + +// gps->year = Date.substring(0,4).toInt(); +// gps->month = Date.substring(4,6).toInt(); +// gps->day = Date.substring(6,8).toInt(); +// gps->hour = Date.substring(8,10).toInt(); +// gps->minute = Date.substring(10,12).toInt(); +// gps->second = Date.substring(12,-1).toInt(); + +// Date = temp; +// // +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,, + +// // OK +// ////////获取经纬度////////// +// temp = gpsbac.substring(33); +// // write_log(log_path,"Longitude is :" + temp,10); +// int pos = temp.indexOf(","); +// Latitude = temp.substring(0,pos); +// gps->latitude = Latitude.toFloat(); +// // write_log(log_path,"Latitude ::::" + Latitude,10); + +// temp = temp.substring(pos+1); +// // write_log(log_path,"Longitude is :" + temp,10); +// pos = temp.indexOf(","); +// Longitude = temp.substring(0,pos); +// gps->longitude = Longitude.toFloat(); +// // write_log(log_path,"Longitude ::::" + Longitude,10); + +// if(Date.indexOf(",") != -1) +// { +// return "-1"; +// } + +// return Date; + +////////////////////////////////////////////////////////////////////////////// + String gpsbac; + write_log(log_path,"start get GPS ",10); + + gsmmanger->modem->sendAT(GF("+QGPS=1")); + gsmmanger->modem->waitResponse("OK"); + + gsmmanger->modem->sendAT(GF("+QGPSLOC=0")); + // gsmmanger->modem->waitResponse("OK"); + + + //获取gps信息 + gsmmanger->modem->waitResponse(5000,gpsbac); + write_log(log_path,"gpsbac is " + gpsbac,10); + +// //检测是否有效 +// temp = gpsbac.substring(16,17); +// // write_log(log_path,"hahaha :" + temp,10); + +// if(temp != "1") +// { +// // write_log(log_path,"gps error,hahahaha",10); +// return "-1"; +// } +// //截取时间 +// Date = gpsbac.substring(18,32); +// temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1); + +// gps->year = Date.substring(0,4).toInt(); +// gps->month = Date.substring(4,6).toInt(); +// gps->day = Date.substring(6,8).toInt(); +// gps->hour = Date.substring(8,10).toInt(); +// gps->minute = Date.substring(10,12).toInt(); +// gps->second = Date.substring(12,-1).toInt(); + +// Date = temp; +// // +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,, + +// // OK +// ////////获取经纬度////////// +// temp = gpsbac.substring(33); +// // write_log(log_path,"Longitude is :" + temp,10); +// int pos = temp.indexOf(","); +// Latitude = temp.substring(0,pos); +// gps->latitude = Latitude.toFloat(); +// // write_log(log_path,"Latitude ::::" + Latitude,10); + +// temp = temp.substring(pos+1); +// // write_log(log_path,"Longitude is :" + temp,10); +// pos = temp.indexOf(","); +// Longitude = temp.substring(0,pos); +// gps->longitude = Longitude.toFloat(); +// // write_log(log_path,"Longitude ::::" + Longitude,10); + +// if(Date.indexOf(",") != -1) +// { +// return "-1"; +// } + + return gpsbac; +} + + + +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(); + 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(); + sys_sd_doc["date"] = rtcdate_now; + + sys_sd_doc["work_mode"] = doc["work_mode"].as(); //一会 + if(doc["work_mode"] == "manual_mode") + { + work_mode = 0; + } + else if (doc["work_mode"] == "time_mode") + { + work_mode = 1; + } + else + { + work_mode = 0; + } + + // if(caiji_mode == 7) + // { + // sys_sd_doc["work_mode"] = "specify"; + // } + + + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as();//一会 + 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(); + caiji_mode = 1; + } + else if(doc["caiji_mode"] == "continue") + { + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + caiji_mode = 2; + } + else + { + + } + + sys_sd_doc["time_interval"] = doc["time_interval"]; + time_interval = doc["time_interval"].as(); + sys_sd_doc["start_time"] = doc["start_time"]; + start_time = doc["start_time"].as(); + 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); + + 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();//一会 + 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); +} + + + + +// /////////////////////////////////上传数据///////////////////////////////////// +// bool UpdateData(String path, char *str, size_t lenth, String Contenttype = "") +// { +// http->beginRequest(); +// http->post(path); +// if (Contenttype != "") +// { +// http->sendHeader(HTTP_HEADER_CONTENT_TYPE, Contenttype); + +// /* code */ +// } + +// http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, lenth); +// http->endRequest(); +// int err = http->write((const byte *)str, lenth); +// delay(1000); +// Serial.print("send date size"); +// Serial.println(err); + +// /* +// for (size_t i = 0; i < lenth; i++) +// { +// Serial.print("0x"); +// Serial.print(str[i],HEX); +// Serial.print(" "); +// if (i%20==0) +// { +// Serial.println(" "); +// } +// } +// */ +// // http->write((const byte*)IS1Sensor.UpData, IS1Sensor.SensorInfo.BandNum*2); +// // http->stop(); +// Serial.println("hear1"); +// if (err == 0) +// { +// http->stop(); +// return false; +// } +// int status = http->responseStatusCode(); +// Serial.println("hear3"); +// if (!status) +// { +// http->stop(); +// return false; +// } + +// int length = http->contentLength(); +// Serial.println("hear4"); +// if (length == 0) +// { +// http->stop(); +// return false; +// } + +// String body = http->responseBody(); +// Serial.println("body:"+body); +// if (body != "ok") +// { +// http->stop(); +// return false; +// } + +// http->stop(); +// return true; +// } + + +// void ReuploadData(String path, String webpath, String content = "") +// { +// vTaskDelay(1); +// Vector files; +// String vec[20]; //每次处理20个 +// files.setStorage(vec); +// while (!sdcard::ListDir(path.c_str(), files)) +// { +// if (files.size() != 0) +// { +// Serial.println("find " + String(files.size()) + "file"); +// for (size_t i = 0; i < files.size(); i++) +// { +// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); +// size_t size = nowfile.size(); +// char *arr = new char[size]; +// nowfile.readBytes(arr, size); +// bool flagsucc = UpdateData(webpath, arr, size, content); +// if (!flagsucc) +// { +// return; +// /* code */ +// } +// delete[] arr; +// sdcard::deleteFolderOrFile(files.at(i).c_str()); +// vTaskDelay(1); +// /* code */ +// } +// } +// vTaskDelay(1); +// } +// if (files.size() != 0) +// { +// Serial.println("find " + String(files.size()) + "file not enough 20"); +// for (size_t i = 0; i < files.size(); i++) +// { +// vTaskDelay(1); +// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); +// size_t size = nowfile.size(); +// char *arr = new char[size]; +// nowfile.readBytes(arr, size); +// Serial.println("run here now"); +// bool flagsucc = UpdateData(webpath, arr, size, content); +// if (!flagsucc) +// { +// return; +// /* code */ +// } + +// delete[] arr; +// sdcard::deleteFolderOrFile(files.at(i).c_str()); +// /* code */ +// } +// } +// vTaskDelay(1); +// } +// /////////////////////////////////////////////////////////////////////////////////////////////////// + + + + + +String getnetData() +{ + write_log(log_path,"start get net data",10); + int err = http->get("/weather/php/Date.php"); + if (err != 0) + { + return "-1"; + } + write_log(log_path,"responseStatusCode",10); + int status = http->responseStatusCode(); + if (!status) + { + return "-1"; + } + + int times_try = 0; + write_log(log_path,"headerAvailable",10); + while (http->headerAvailable()) + { + if(times_try > 100) + { + http->stop(); + return "-1"; + } + write_log(log_path,"readHeaderName",10); + String headerName = http->readHeaderName(); + write_log(log_path,"readHeaderValue",10); + String headerValue = http->readHeaderValue(); + + vTaskDelay(100); + times_try++; + } + write_log(log_path,"isResponseChunked",10); + if (http->isResponseChunked()) + { + } + write_log(log_path,"contentLength",10); + String body = http->responseBody(); + http->stop(); + return body; +} + + +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); + } +} + + diff --git a/src/my74hc595.cpp b/src/my74hc595.cpp new file mode 100644 index 0000000..1e362ac --- /dev/null +++ b/src/my74hc595.cpp @@ -0,0 +1,158 @@ + +#include "my74hc595.h" +// // #define SHCP 34 +// // #define STCP 33 +// // #define DS 35 +// // int data=100; + + +// // uint8_t value[8] = {1, 1, 1, 1, 1, 1, 0, 0}; +// // void writeto74() +// // { +// // digitalWrite(STCP, LOW); +// // // delay(1); +// // // vTaskDelay(1); + +// // for (int i = 0; i < 8; i++) +// // { +// // digitalWrite(SHCP, LOW); +// // // delay(1); +// // // vTaskDelay(1); +// // for(int j = 0; j < 1500; j++) +// // { + +// // } +// // digitalWrite(DS, value[i]); +// // // delay(1); +// // // vTaskDelay(1); +// // for(int i = 0; i < 1500; i++) +// // { + +// // } +// // digitalWrite(SHCP, HIGH); +// // } +// // // delay(1); +// // // vTaskDelay(1); +// // digitalWrite(STCP, HIGH); +// // } + + +// // #define SHCP 33 +// // #define STCP 34 +// // #define DS 35 +// // int data=100; + + +// // uint8_t value[8] = {0, 0, 0, 0, 0, 0, 0, 0}; +// // void writeto74() +// // { +// // digitalWrite(STCP, LOW); +// // // delay(1); +// // // vTaskDelay(1); + +// // for (int i = 0; i < 8; i++) +// // { +// // digitalWrite(SHCP, LOW); +// // digitalWrite(DS, value[i]); +// // // for(int j = 0; j < 1500; j++) +// // // { + +// // // } + + +// // digitalWrite(SHCP, HIGH); +// // // for(int i = 0; i < 1500; i++) +// // // { + +// // // } +// // // digitalWrite(DS, 0); +// // } + +// // // delay(1); +// // // vTaskDelay(1); +// // digitalWrite(STCP, HIGH); +// // } + + +// void updatepin() +// { +// writeto74(); +// } + +// bool my74hc595_setpin(int index, bool val) +// { +// if (index < 0 || index > 7) +// { +// return false; +// } +// value[index] = val; + +// updatepin(); +// return true; +// } + + +// void my74hc595_init(){ +// pinMode(SHCP, OUTPUT); +// pinMode(STCP, OUTPUT); +// pinMode(DS, OUTPUT); +// digitalWrite(SHCP, LOW); +// digitalWrite(STCP, LOW); +// digitalWrite(DS, LOW); +// updatepin(); +// tuigan(suo); +// } + +// void my74hc595_setallpin(bool val) +// { +// for (size_t i = 0; i < 8; i++) +// { +// value[i] = val; +// // my74hc595_setpin(i, value); +// /* code */ +// } +// updatepin(); +// } + + +#define SHCP 33 +#define STCP 34 +#define DS 35 + +uint8_t data=0x00; + +void my74hc595_init(){ + pinMode(SHCP, OUTPUT); + pinMode(STCP, OUTPUT); + pinMode(DS, OUTPUT); + digitalWrite(SHCP, LOW); + digitalWrite(STCP, LOW); + digitalWrite(DS, LOW); + updatepin(); + tuigan(suo); + + vTaskDelay(1000 * 10); +} +bool my74hc595_setpin(int index, bool val) +{ + if (index < 0 || index > 7) + { + return false; + } + if(val) + { + data |= (1 << index); + } + else + { + data &= ~(1 << index); + } + return true; +} + +void updatepin() +{ + digitalWrite(STCP, LOW); + shiftOut(DS, SHCP, MSBFIRST, data); + digitalWrite(STCP, HIGH); +} diff --git a/src/my74hc595.h b/src/my74hc595.h new file mode 100644 index 0000000..40ed00b --- /dev/null +++ b/src/my74hc595.h @@ -0,0 +1,13 @@ +#ifndef _MY74HC595_H +#define _MY74HC595_H +#include "Arduino.h" +#include "jiaresi.h" +#include "log.h" +#include "tuigan.h" + +void my74hc595_init(); +// void my74hc595_setallpin(bool value); +bool my74hc595_setpin(int index, bool val); +void updatepin(); + +#endif \ No newline at end of file diff --git a/src/servo.cpp b/src/servo.cpp new file mode 100644 index 0000000..8b39b8f --- /dev/null +++ b/src/servo.cpp @@ -0,0 +1,80 @@ +#include "servo.h" +uint8_t channel; +float now_angle,servo_offset_angle_up = 0,servo_offset_angle_down = 0; + +void servo_init(uint8_t servo_pin,uint8_t servo_channel) +{ + int servo_freq = 50; // 频率(20ms周期) + int servo_resolution = 12; // 分辨率(8 ~ 16) + channel = servo_channel; + ledcSetup(servo_channel, servo_freq, servo_resolution); + ledcAttachPin(servo_pin, servo_channel); + servo_set_angle(0); +} + +int calculatePWM(float degree) +{ + if (degree < 0) + degree = 0; + if (degree > 360) + degree = 360; + return (degree / 360.0) * 410 + 103.0; + +} + +void servo_off() +{ + ledcWrite(channel,0); +} + +void servo_set_angle(float angle) +{ + now_angle = angle; + ledcWrite(channel, calculatePWM(angle)); +} +//获取舵机的角度 +float servo_get_angle() +{ + return now_angle; +} + +void servo_up() +{ + float a = 180 + servo_offset_angle_up; + if(a < 0 || a > 270) a = 180; + // servo_set_angle(200); + // servo_set_angle(213); + servo_set_angle(a); +} + +void servo_down() +{ + float a = 0 + servo_offset_angle_down; + if(a < 0 || a > 270) a = 0; + // servo_set_angle(12); + // servo_set_angle(28); + servo_set_angle(a); +} +/// @brief +/// @param dir 0 down 1 up +/// @param angle +void servo_offset(float angle,uint8_t dir) +{ + + switch (dir) + { + case 0: + servo_offset_angle_down = angle; + servo_down(); + break; + case 1: + servo_offset_angle_up = angle; + servo_up(); + break; + default: + break; + } + +} + + diff --git a/src/servo.h b/src/servo.h new file mode 100644 index 0000000..85e4f33 --- /dev/null +++ b/src/servo.h @@ -0,0 +1,16 @@ +#ifndef _SERVO_H_ +#define _SERVO_H_ + + +#include + +void servo_init(uint8_t servo_pin,uint8_t servo_channel); +int calculatePWM(float degree); +void servo_set_angle( float angle ); +float servo_get_angle(); +void servo_off(); +void servo_up(); +void servo_down(); +void servo_offset(float angle,uint8_t dir); + +#endif \ No newline at end of file diff --git a/src/slave.cpp b/src/slave.cpp new file mode 100644 index 0000000..e66e535 --- /dev/null +++ b/src/slave.cpp @@ -0,0 +1,123 @@ +#include "slave.h" + +double new_emissivity =0.95; + + +Adafruit_MLX90614 mlx = Adafruit_MLX90614(); +TwoWire wireme(0); + +u_char a[]={0x01,0x04,0x00,0x00,0x00 ,0x06,0x70,0x08}; +u_char yuliang[]={0x02, 0x03, 0x00, 0x00 ,0x00, 0x01, 0x84, 0x39}; +u_char ClearYL[]={0x02,0x06,0x00, 0x00 ,0x00 ,0x5A ,0X09 ,0Xc2}; +u_char yuliangfuliangdu[]={0x02,0x03,0x00,0x02,0x00,0x02,0x65,0xf8}; + +u_char openGNRMC[]={0xF1,0xD9,0x06,0x01,0x03,0x00,0xF0,0x05,0x01,0x00,0x1A}; +u_char gpsSaveConfig[]={0xF1,0xD9,0x06,0x09,0x08,0x00,0x00,0x00,0x00,0x00,0x2F,0x00,0x00,0x00,0x46,0xB7}; + + + +SoftwareSerial *my485Port; +void slave::init(int8_t MY485PORT_RX,int8_t MY485PORT_TX){ + // put your setup code here, to run once: + //Wire.begin(4,5); + + // Serial.println("slave init ..."); + write_log(log_path,"slave init ...",10); + my485Port = new SoftwareSerial(MY485PORT_RX ,MY485PORT_TX,false); + my485Port->begin(9600, SWSERIAL_8N1, MY485PORT_RX, MY485PORT_TX, false); + + ///////////// 光谱电源控制,重启 + pinMode(36,OUTPUT); + digitalWrite(36,LOW); + vTaskDelay(2000); + digitalWrite(36,HIGH); + + + // GPSSeiral.begin(115200, SWSERIAL_8N1, 2, 12, false); + wireme.setPins(7,8); + + if (!mlx.begin(MLX90614_I2CADDR,&wireme)) { + // Serial.println("Error connecting to MLX sensor. Check wiring."); + write_log(log_path,"Error connecting to MLX sensor. Check wiring.",10); + //while (1); + }; + mlx.writeEmissivity(new_emissivity); // this does the 0x0000 erase write + // Serial.println(sizeof(openGNRMC)); + write_log(log_path,String(sizeof(openGNRMC)),10); + +} + +double slave::getMLX(){ + double a = mlx.readObjectTempC(); + return a; + // Serial.write((char*) &a,8); +} + +void slave::getWehter(){ + while (my485Port->read()!=-1); + my485Port->write(a,8); + vTaskDelay(90); + for (int j = 0; j <17 ; ++j) { + ret[j]=my485Port->read(); + } + float VV=(ret[3]*256+ret[4])*1.0/100; + float DD=(ret[5]*256+ret[6])*1.0/10; + float TT=(ret[7]*256+ret[8])*1.0/100; + float HH=(ret[9]*256+ret[10])*1.0/100; + long PP=(ret[13]*256+ret[14])*256*256+(ret[11]*256+ret[12]); +#ifdef DEBG + String str_getWehter = "VV:"+String(VV)+" DD:"+String(DD)+" TT:"+String(TT)+" HH:"+String(HH)+" PP:"+String(PP); + // return str; + // Serial.println("VV:"+String(VV)+" DD:"+String(DD)+" TT:"+String(TT)+" HH:"+String(HH)+" PP:"+String(PP)); +#endif + // Serial.write(ret,17); +} + +float slave::getYuliang(){ + while (my485Port->read()!=-1); + my485Port->write(yuliang,8); + vTaskDelay(90); + + for (int j = 0; j <7; ++j) { + ret[j]=my485Port->read(); + } + float Value=ret[3]*256+ret[4]; + Value=Value/10; + // Serial.write(ret,7); + // Serial.print(Value); + // Serial.print("#"); + + return Value; + +} + +int32_t slave::getYuliangfuliang(){ + while (my485Port->read()!=-1); + my485Port->write(yuliangfuliangdu,8); + vTaskDelay(90); + + for (int j = 0; j <7; ++j) { + ret[j]=my485Port->read(); + } + int32_t Value=(ret[3]*256+ret[4])*256*256+ret[5]*256+ret[6]; + // Value=Value/10; + // Serial.write(ret,7); + // Serial.print(Value); + // Serial.print("#"); + + return Value; +} + +/// 返回1,表示ok +uint8_t slave::claeryuliang(){ + while (my485Port->read()!=-1); + my485Port->write(ClearYL,8); + vTaskDelay(90); + for (int j = 0; j <8; ++j) { + ret[j]=my485Port->read(); + } + // Serial.print("OK"); + return 1; +} + + diff --git a/src/slave.h b/src/slave.h new file mode 100644 index 0000000..0742eb4 --- /dev/null +++ b/src/slave.h @@ -0,0 +1,30 @@ +#ifndef _SLAVE_H_ +#define _SLAVE_H_ + +#include +#include "SoftwareSerial.h" +#include "Wire.h" +#include +//#define DEBUG_SERIAL Serial +#include +#include "SparkFun_Ublox_Arduino_Library.h" +#include "log.h" + +class slave +{ + public: + u_char ret[17]; + String str_getWehter; + + void init(int8_t MYPORT_RX,int8_t MYPORT_TX); + double getMLX(); + void getWehter(); + float getYuliang(); + int32_t getYuliangfuliang(); + uint8_t claeryuliang(); +}; + + + + +#endif \ No newline at end of file diff --git a/src/tuigan.cpp b/src/tuigan.cpp new file mode 100644 index 0000000..07cda9f --- /dev/null +++ b/src/tuigan.cpp @@ -0,0 +1,30 @@ +#include "tuigan.h" +#include "log.h" + +// //status 0:伸 1:缩 +uint8_t tuigan_sta; + +void tuigan(enum tuigan_status status) +{ + if(status == tui) + { + my74hc595_setpin(0, 0); + my74hc595_setpin(1, 1); + updatepin(); + tuigan_sta = tui; + } + else + { + my74hc595_setpin(0, 1); + my74hc595_setpin(1, 0); + updatepin(); + tuigan_sta = suo; + } +} + + +uint8_t get_tuigan_status() +{ + return tuigan_sta; +} + diff --git a/src/tuigan.h b/src/tuigan.h new file mode 100644 index 0000000..a514d95 --- /dev/null +++ b/src/tuigan.h @@ -0,0 +1,17 @@ +#ifndef _TUIGAN_H_ +#define _TUIGAN_H_ + +#include "Arduino.h" +#include "my74hc595.h" + +enum tuigan_status +{ + tui, + suo +}; + +void tuigan(enum tuigan_status status); +uint8_t get_tuigan_status(); + +#endif + diff --git a/src/updatebyme.cpp b/src/updatebyme.cpp new file mode 100644 index 0000000..675fd2d --- /dev/null +++ b/src/updatebyme.cpp @@ -0,0 +1,233 @@ +#include "updatebyme.h" +#include "SDmanger.h" +void UpDateClassByme::performUpdate(Stream &updateSource, size_t updateSize) +{ + if (Update.begin(updateSize)) + { + Serial.println("Writes : "); + size_t written = Update.writeStream(updateSource); + if (written == updateSize) + { + Serial.println("Writes : " + String(written) + " successfully"); + } + else + { + Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?"); + } + if (Update.end()) + { + Serial.println("OTA finished!"); + if (Update.isFinished()) + { + Serial.println("Restart ESP device!"); + abort(); + } + else + { + Serial.println("OTA not fiished"); + } + } + else + { + Serial.println("Error occured #: " + String(Update.getError())); + } + } + else + { + Serial.println("Cannot beggin update"); + } +} + + +size_t UpDateClassByme::DownloadFirmwareForurl(String version) +{ + String url="/weather/firmware/esp32/firmware_"+version+".bin"; + int err = http->get(url.c_str()); + if (err != 0) + { + Serial.println(F("Date failed to connect")); + vTaskDelay(10000); + return 0; + } + int status = http->responseStatusCode(); + if (!status) + { + vTaskDelay(10000); + return 0; + } + Serial.print(F("Response status code: ")); + Serial.println(status); + // SerialMon.println(F("Response Headers:")); + size_t count=0; + while (http->headerAvailable()) + { + String headerName = http->readHeaderName(); + String headerValue = http->readHeaderValue(); + Serial.println("Date: " + headerName + " : " + headerValue); + if (headerName=="Content-Length") + { + count=headerValue.toInt(); + /* code */ + } + + + } + Serial.println("lenth is "+String(count)); + if (http->isResponseChunked()) + { + Serial.println(F("Date The response is chunked")); + } + char *str=new char[1000]; + int lent=count/1000; + File file; + + file = SD_MMC.open("/update.bin", "wb"); + + for (size_t i = 0; i < lent; i++) + { + http->readBytes(str,1000); + file.write((const byte *)str,1000); + Serial.print("download "); + Serial.println(String(i*100/lent)); + /* code */ + } + http->readBytes(str,count%1000); + file.write((const byte *)str,count%1000); + file.flush(); + file.close(); + //performUpdate(*http,cout); + + + // http->readBytes + Serial.println("adfasdfsadfsdfsd"); + //Serial.write(str,cout); + //Serial.println(body.length()); + http->stop(); + return count; + // return body; +} + +void UpDateClassByme::initme(String Version) +{ + CurrentVersion=Version; + +} +void UpDateClassByme::updateversion() +{ + String Targetversion=GetValueFromNet("/weather/php/upVserion.php",StationID+":"+CurrentVersion); +} +bool UpDateClassByme::CheckAndUpdate() +{ + + + String Targetversion=GetValueFromNet("/weather/php/GetVserion.php",StationID); + if ( Targetversion=="0"||Targetversion==CurrentVersion||Targetversion=="") + { + return true; + + } + Serial.println("targetvesion is:"+Targetversion); + + //size_t countdownload1=375584; + size_t countdownload1=DownloadFirmwareForurl(Targetversion); + if (countdownload1<10000) + { + return false; + } + Serial.println("count download is:"+String(countdownload1)); + + String MD5=GetValueFromNet("/weather/php/GetMD5Value.php",Targetversion); + + if (MD5=="0") + { + return false; + + } + + // updater.setMD5(MD5); + MD5Builder md5my; + + + File file; + + file = SD_MMC.open("/update.bin", "rb"); + md5my.begin(); + md5my.addStream(file,countdownload1); + md5my.calculate(); + String md5com=md5my.toString(); + Serial.println("md5 comput is:"+md5my.toString()); + Serial.println("md5 should is:"+MD5); + file.close(); + if (MD5==md5com) + { + file = SD_MMC.open("/update.bin", "rb"); + performUpdate(file,countdownload1); + return true; + /* code */ + } + + + + + return true; + + + + + + + +} + + + + + + + + + +UpDateClassByme::UpDateClassByme(HttpClient *httptm) +{ + http=httptm; + StationID="000"; +} +String UpDateClassByme::GetValueFromNet(String url,String Key) +{ + + http->beginRequest(); + http->post(url.c_str()); + + + http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, Key.length()); + http->endRequest(); + int err = http->write((const byte *)Key.c_str(), Key.length()); + Serial.print("send date size"); + Serial.println(err); + + if (err == 0) + { + + http->stop(); + return "-1"; + } + int status = http->responseStatusCode(); + if (!status) + { + http->stop(); + return "-1"; + } + + int length = http->contentLength(); + if (length == 0) + { + http->stop(); + return "-1"; + } + + String body = http->responseBody(); + Serial.println("body:"+body); + + http->stop(); + return body; +} diff --git a/src/updatebyme.h b/src/updatebyme.h new file mode 100644 index 0000000..1603bca --- /dev/null +++ b/src/updatebyme.h @@ -0,0 +1,23 @@ +#ifndef UPDATEBYME_H +#define UPDATEBYME_H +#include +#include +class UpDateClassByme{ + public: +UpdateClass updater; +UpDateClassByme(HttpClient *httptm); +void performUpdate(Stream &updateSource, size_t updateSize); +HttpClient *http; +size_t DownloadFirmwareForurl(String version); +String CurrentVersion; +void initme(String Version); +bool CheckAndUpdate(); +String StationID; +String GetValueFromNet(String url,String Key); +void updateversion(); +}; + + + + +#endif \ No newline at end of file diff --git a/src/wifidebug.cpp b/src/wifidebug.cpp new file mode 100644 index 0000000..5677636 --- /dev/null +++ b/src/wifidebug.cpp @@ -0,0 +1,41 @@ +#include +#include + + + +#ifdef DebugCalibration +const char *ssid = "SmartForCalibration"; +const char *password = "123456789"; +IPAddress local_IP(192, 168, 1,2); +IPAddress gateway(192, 168, 1, 2); +IPAddress subnet(255, 255, 255, 0); +TCPserver *tcpserver; +void beginWIFI(){ + tcpserver=new TCPserver(23); + WiFi.mode( WIFI_MODE_AP ); + Serial.println("init webserver ok 33333"); + WiFi.softAPConfig(local_IP, gateway, subnet); + boolean result = WiFi.softAP(ssid, password); + + tcpserver->loop(); + +} +void tcploop(){ + tcpserver->loop(); +} + + +void printbytcpserver(String str) +{ + tcpserver->SendDataToClinet(str); + tcpserver->loop(); + // Serial.println(str); + +} +#endif +#ifndef DebugCalibration +void beginWIFI(){} +void printbytcpserver(String str){} +void tcploop(){} +#endif + diff --git a/src/wifidebug.h b/src/wifidebug.h new file mode 100644 index 0000000..cf5012f --- /dev/null +++ b/src/wifidebug.h @@ -0,0 +1,16 @@ + +#include"Arduino.h" +#include"Define.h" +#ifdef DebugCalibration +#include"WiFi.h" +#endif + +void beginWIFI(); +void printbytcpserver(String str); +void tcploop(); + + + + + +