本帖最后由 zhangt0713 于 2021-6-28 18:30 编辑
六月接近尾声,赶上社区的WiFi主题月,利用手上现有的资源,实现一个小功能。 实现内容:LPC800开发板,采集温湿度数据,通过WIFI上传到阿里云物联网平台,并在阿里云物联网平台实时显示,做个开关按钮,通过WIFI控制开发板上的蓝色指示灯亮灭.
一、硬件组成+开发工具 硬件:LPC800DIP、ESP32模块、GXHT30温湿度传感器 组成
开发工具:Keil、阿里云物联网平台 二、组成框图
三、开发准备 1.阿里云物联网平台主要采用MQTT协议进行数据传输,注册一个阿里云物联网平台账号,建立项目,产品设备 2.阿里云三元组,和密钥计算, 3.采用MQTT.fx,模拟连接物联网平台,实现数据的发送 4.利用串口调试助手连接ESP32,发送AT指令连接阿里云物联网平台上的设备。 以上步骤完成后可以在,LPC800上实现具体的功能: LPC800主要温湿度的采集,并通过AT命令控制ESP32的WIFI连接。用到的资源有,串口,定时器,I2C。 四、主要初始化和命令 1.温湿度传感器的数据通过LPC800的I2C实现温湿度的读取 按照上述步骤初始化I2C, - /* function to write a byte to GXHT30*/
- void Write_GXHT30_CMD(uint8_t CMD_MSB, uint8_t CMD_LSB)
- {
- WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait the master state to be idle
- LPC_I2C0->MSTDAT = (I2C_ADDR_GXHT30<<1) | 0; // Address with 0 for RWn bit (WRITE)
- LPC_I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
- WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX); // Wait for the address to be ACK'd
- LPC_I2C0->MSTDAT = CMD_MSB; // Send the data to the slave
- LPC_I2C0->MSTCTL = CTL_MSTCONTINUE; // Continue the transaction
- WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX); // Wait for the data to be ACK'd
- LPC_I2C0->MSTDAT = CMD_LSB; // Send the data to the slave
- LPC_I2C0->MSTCTL = CTL_MSTCONTINUE; // Continue the transaction
- WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX); // Wait for the data to be ACK'd
- LPC_I2C0->MSTCTL = CTL_MSTSTOP; // Send a stop to end the transaction
- }
- void Read_GXHT30_CMD(uint8_t *data,uint8_t Num)
- {
- uint8_t i;
-
- WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait the master state to be idle
- LPC_I2C0->MSTDAT = (I2C_ADDR_GXHT30<<1) | 1; // Address with 1 for RWn bit (READ)
- LPC_I2C0->MSTCTL = CTL_MSTSTART; // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
- for (i=0;i<Num;i++)
- {
- while(!(LPC_I2C0->STAT & STAT_MSTPEND));
- data[i] = LPC_I2C0->MSTDAT;
- LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
-
- }
- LPC_I2C0->MSTCTL = CTL_MSTSTOP; // Send a stop to end the transaction
- }
- void GXHT30_DATA(void)
- {
-
- uint8_t buff[6];
- uint16_t tem,hum;
- Write_GXHT30_CMD(0x24, 0x16);
- Delay_ms(20);
- Read_GXHT30_CMD(buff,6);
- tem = ((buff[0]<<8) | buff[1]);//
- hum = ((buff[3]<<8) | buff[4]);//
- Temperature =(175.0 * (float)tem/65535.0 -45.0); //
- Humidity= (100.0*hum/65535.0);
- }
复制代码
2.LPC800通过串口AT命令控制ESP32,联网,发布数据 - uint8_t ESP32_Aliyun_Test(void)
- {
- uint8_t res=0;
- res = ESP32_Send_Cmd(CMD_RESTORE,"ready",50);
- res = ESP32_Send_Cmd(CMD_AT,"OK",50);//AT
- res = ESP32_Send_Cmd(CMD_ATE0,"OK",50);//ATE0
- res = ESP32_Send_Cmd(CMD_Mode,"OK",50);//WIFI STA mode
- res = ESP32_Send_Cmd(CMD_NTP, "OK", 50); //
- res = ESP32_Send_Cmd(CMD_CWJAP,"OK",1000);
- res = ESP32_Send_Cmd(CMD_MQTTUSERCFG, "OK", 50); //Aliyun
- res = ESP32_Send_Cmd(CMD_MQTTCONN, "OK", 50); //Aliyun
- return res;
- }
复制代码3. 配置LPC800定时器,定时2S,每两秒上传一次温湿度数据
- void Timer_Init(void)
- {
- LPC_SYSCON->SYSAHBCLKCTRL |= MRT;
- // Configure the MRT
- // Give the module a reset
- LPC_SYSCON->PRESETCTRL &= (MRT_RST_N);
- LPC_SYSCON->PRESETCTRL |= ~(MRT_RST_N);
- // Mode = repeat, interrupt = enable
- LPC_MRT->Channel[0].CTRL = (MRT_Repeat<<MRT_MODE) | (1<<MRT_INTEN);
- LPC_MRT->Channel[1].CTRL = (MRT_Repeat<<MRT_MODE) | (1<<MRT_INTEN);
- // Enable the MRT interrupt in the NVIC
- NVIC_EnableIRQ(MRT_IRQn);
- // Start MRT channel 0
- // LPC_MRT->Channel[0].INTVAL = MRT0_TIME;
- // Start MRT channel 1
- // LPC_MRT->Channel[1].INTVAL = MRT1_TIME;
- }
复制代码
4.阿里云topic,采用sprintf 格式化字符串
格式化字符串的时候容易出错,基本上都是格式不对,尤其是针对字符串中含有(“”),转义字符要用够数量,本人之前这个搞了很久。
最终效果如视频所示
|