second commit

This commit is contained in:
2025-02-20 13:19:16 +08:00
commit 09256a1972
52 changed files with 8016 additions and 0 deletions

219
src/DS18B20.cpp Normal file
View File

@ -0,0 +1,219 @@
#include "DS18B20.h"
#include <log.h>
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<temp_number;i++)
{
temp[i] = DS18b20.getTempC(p[i]);
// temp[i] = getone_temp(i);
}
// write_log(log_path,"get temperatures ok",10);
}
float getone_temp(uint8_t address)
{
float temp;
DS18b20.requestTemperatures(); // Send the command to get temperatures
temp = DS18b20.getTempC(p[address]);
return temp;
}
// void set_ds18b20_address(uint8_t num,uint8_t * addr)
// {
// // uint8_t num = addr[0];
// memcpy(&sensoraddr[num],addr,8);
// }
//测试用
//温度监测
// void getall_temp(float *temp)
// {
// DS18b20.requestTemperatures(); // Send the command to get temperatures
// for (u_int32_t i = 0; i < temp_number; i++)
// {
// temp[i] = DS18b20.getTempC(sensoraddr[i]);
// }
// }
// uint8_t get_ds18b20_addr()
// {
// float temp[8];
// getall_temp(temp);
// for(size_t i = 0; i < temp_number; i++)
// {
// Serial.printf("%d temp: %2f ds18b20 addr :",i,temp[i]);
// for(int a = 0 ; a< 8 ;a++)
// {
// Serial.printf(" %x ",sensoraddr[i][a]);
// }
// Serial.println("");
// }
// return 0;
// }
// {
// "sensor0":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor1":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor2":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor3":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor4":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor5":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor6":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// },
// "sensor7":{
// "byte0":"",
// "byte1":"",
// "byte2":"",
// "byte3":"",
// "byte4":"",
// "byte5":"",
// "byte6":"",
// "byte7":""
// }
// }