// sensors_param.cfgdes[0] адрес
// sensors_param.cfgdes[1] команда
// sensors_param.cfgdes[2] длительность
// sensors_param.cfgdes[3] вид протокола 0 старый 1 новый
void init_pms2(void);
void ICACHE_FLASH_ATTR
startfunc(){
// выполняется один раз при старте модуля.
init_pms2();
}
void ICACHE_FLASH_ATTR
timerfunc(uint32_t timersrc) {
// выполнение кода каждую 1 секунду
}
uint8_t dataPMS[32];
static uint8_t iCurrent2 = 0;
char start=0;
unsigned int crc_test,CRC_read;
uint16_t pms2_pm10,pms2_pm25,pms2_pm100,pms2_4,pms2_5,pms2_6;
uint16_t pms2_7,pms2_8,pms2_9,pms2_10,pms2_11,pms2_12;
// вычисление crc на самом деле простое сложение
unsigned int CRC16(uint8_t *buf)
{ uint8_t pos;
unsigned int crc = 0x00;
for (pos = 0; pos < 30; pos++)
{
crc=crc+buf[pos];
}
return crc;
}
read_pms2(void)
{
int lenght =(READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S))
{
WRITE_PERI_REG(0X60000914, 0x73); //WTD
uint8_t read = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
if((iCurrent2 == 0) && (read == 0x42))
{
start=1;
dataPMS[0] = read;
iCurrent2++;
continue;
}
if (start==1)
{
if(iCurrent2 <= 31 )
{
dataPMS[iCurrent2] = read;
iCurrent2++;
}
if(iCurrent2 == 32 )
{
iCurrent2 = 0;
CRC_read=((uint16_t)dataPMS[30] <<
+ dataPMS[31];
crc_test=CRC16(dataPMS);
if (crc_test==CRC_read)
{
pms2_pm10 = ((uint16_t)dataPMS[4] <<
+ dataPMS[5];
pms2_pm25 = ((uint16_t)dataPMS[6] <<
+ dataPMS[7];
pms2_pm100 = ((uint16_t)dataPMS[8] <<
+ dataPMS[9];
pms2_4 = ((uint16_t)dataPMS[10] <<
+ dataPMS[11];
pms2_5 = ((uint16_t)dataPMS[12] <<
+ dataPMS[13];
pms2_6 = ((uint16_t)dataPMS[14] <<
+ dataPMS[15];
pms2_7 = ((uint16_t)dataPMS[16] <<
+ dataPMS[17];
pms2_8 = ((uint16_t)dataPMS[18] <<
+ dataPMS[19];
pms2_9 = ((uint16_t)dataPMS[20] <<
+ dataPMS[21];
pms2_10 = ((uint16_t)dataPMS[22] <<
+ dataPMS[23];
pms2_11 = ((uint16_t)dataPMS[24] <<
+ dataPMS[25];
pms2_12 = ((uint16_t)dataPMS[26] <<
+ dataPMS[27];
}
else
{
pms2_pm10 = 999;
pms2_pm25 = 999;
pms2_pm100 = 999;
}
start=0;
}
}
}
}
void ICACHE_FLASH_ATTR
init_pms2(){
uart_init(BIT_RATE_9600);
ETS_UART_INTR_ATTACH(read_pms2, NULL);
//WRITE_PERI_REG(UART_INT_ENA(UART0), UART_RXFIFO_FULL_INT_ENA);
//ETS_UART_INTR_ENABLE();
}
void webfunc(char *pbuf) {
//os_sprintf(HTTPBUFF,"Это отладочный модуль С usb<br>"); // вывод данных на главной модуля
os_sprintf(HTTPBUFF,"1 %d <br>",pms2_pm10);
os_sprintf(HTTPBUFF,"2,5 %d <br>",pms2_pm25);
os_sprintf(HTTPBUFF,"10 %d <br>",pms2_pm100);
os_sprintf(HTTPBUFF,"crc %d <br>",crc_test);
os_sprintf(HTTPBUFF,"crc прочитано %d <br>",CRC_read);
os_sprintf(HTTPBUFF,"Ответ456 %d %d %d<br>",pms2_4,pms2_5,pms2_6);
os_sprintf(HTTPBUFF,"Ответ789 %d %d %d<br>",pms2_7,pms2_8,pms2_9);
os_sprintf(HTTPBUFF,"Ответ101112 %d %d %d<br>",pms2_10,pms2_11,pms2_12);
}