查看: 3055|回复: 3

[主题月] LPC800+WIFI 数据上传,远程控制指示灯

[复制链接]
  • TA的每日心情
    慵懒
    2023-1-31 09:34
  • 签到天数: 202 天

    连续签到: 1 天

    [LV.7]常住居民III

    7

    主题

    1518

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    3979
    最后登录
    2025-9-3
    发表于 2021-6-28 00:07:33 | 显示全部楼层 |阅读模式
    本帖最后由 zhangt0713 于 2021-6-28 18:30 编辑

    六月接近尾声,赶上社区的WiFi主题月,利用手上现有的资源,实现一个小功能。
    实现内容:LPC800开发板,采集温湿度数据,通过WIFI上传到阿里云物联网平台,并在阿里云物联网平台实时显示,做个开关按钮,通过WIFI控制开发板上的蓝色指示灯亮灭.



           360截图20210627234609065.jpg    360截图20210627234452283.jpg

    一、硬件组成+开发工具
    硬件:LPC800DIP、ESP32模块、GXHT30温湿度传感器 组成
        硬件.jpg

    开发工具:Keil、阿里云物联网平台
    二、组成框图

          框图.PNG



    三、开发准备
    1.阿里云物联网平台主要采用MQTT协议进行数据传输,注册一个阿里云物联网平台账号,建立项目,产品设备
    2.阿里云三元组,和密钥计算,
    3.采用MQTT.fx,模拟连接物联网平台,实现数据的发送
    4.利用串口调试助手连接ESP32,发送AT指令连接阿里云物联网平台上的设备。
    以上步骤完成后可以在,LPC800上实现具体的功能:
    LPC800主要温湿度的采集,并通过AT命令控制ESP32的WIFI连接。用到的资源有,串口,定时器,I2C。
    四、主要初始化和命令
    1.温湿度传感器的数据通过LPC800的I2C实现温湿度的读取
          I2C.PNG   
    I2C读.PNG    
    I2C写.PNG
    按照上述步骤初始化I2C,
    1. /* function to write a byte to GXHT30*/
    2. void Write_GXHT30_CMD(uint8_t CMD_MSB, uint8_t CMD_LSB)
    3. {
    4.       WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait the master state to be idle
    5.       LPC_I2C0->MSTDAT = (I2C_ADDR_GXHT30<<1) | 0;    // Address with 0 for RWn bit (WRITE)
    6.       LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
    7.       WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the address to be ACK'd
    8.       LPC_I2C0->MSTDAT = CMD_MSB;                           // Send the data to the slave
    9.       LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;                // Continue the transaction
    10.       WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the data to be ACK'd
    11.             LPC_I2C0->MSTDAT = CMD_LSB;                           // Send the data to the slave
    12.       LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;                // Continue the transaction
    13.       WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the data to be ACK'd
    14.       LPC_I2C0->MSTCTL = CTL_MSTSTOP;                    // Send a stop to end the transaction
    15. }
    16. void Read_GXHT30_CMD(uint8_t *data,uint8_t Num)
    17. {
    18.           uint8_t i;
    19.          
    20.             WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait the master state to be idle
    21.       LPC_I2C0->MSTDAT = (I2C_ADDR_GXHT30<<1) | 1;    // Address with 1 for RWn bit (READ)
    22.       LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.               
    23.           for (i=0;i<Num;i++)
    24.           {
    25.                  while(!(LPC_I2C0->STAT & STAT_MSTPEND));
    26.                 data[i] = LPC_I2C0->MSTDAT;
    27.                 LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;        
    28.                
    29.           }
    30.          LPC_I2C0->MSTCTL = CTL_MSTSTOP;                    // Send a stop to end the transaction

    31. }

    32. void GXHT30_DATA(void)
    33. {
    34.         
    35.   uint8_t buff[6];
    36.         uint16_t tem,hum;        
    37.         Write_GXHT30_CMD(0x24, 0x16);
    38.         Delay_ms(20);
    39.         Read_GXHT30_CMD(buff,6);
    40.   tem = ((buff[0]<<8) | buff[1]);//
    41.         hum = ((buff[3]<<8) | buff[4]);//
    42.     Temperature =(175.0 * (float)tem/65535.0 -45.0); //
    43.     Humidity=  (100.0*hum/65535.0);

    44. }
    复制代码



    2.LPC800通过串口AT命令控制ESP32,联网,发布数据
    1. uint8_t ESP32_Aliyun_Test(void)

    2. {
    3. uint8_t res=0;
    4. res = ESP32_Send_Cmd(CMD_RESTORE,"ready",50);
    5. res = ESP32_Send_Cmd(CMD_AT,"OK",50);//AT
    6. res = ESP32_Send_Cmd(CMD_ATE0,"OK",50);//ATE0
    7. res = ESP32_Send_Cmd(CMD_Mode,"OK",50);//WIFI STA mode
    8. res = ESP32_Send_Cmd(CMD_NTP, "OK", 50); //
    9. res = ESP32_Send_Cmd(CMD_CWJAP,"OK",1000);
    10. res = ESP32_Send_Cmd(CMD_MQTTUSERCFG, "OK", 50); //Aliyun
    11. res = ESP32_Send_Cmd(CMD_MQTTCONN, "OK", 50); //Aliyun
    12. return res;
    13. }
    复制代码
    3. 配置LPC800定时器,定时2S,每两秒上传一次温湿度数据

    1. void Timer_Init(void)
    2. {
    3.         LPC_SYSCON->SYSAHBCLKCTRL |= MRT;
    4.          // Configure the MRT
    5.   // Give the module a reset
    6.   LPC_SYSCON->PRESETCTRL &= (MRT_RST_N);
    7.   LPC_SYSCON->PRESETCTRL |= ~(MRT_RST_N);

    8.   // Mode = repeat, interrupt = enable
    9.   LPC_MRT->Channel[0].CTRL = (MRT_Repeat<<MRT_MODE) | (1<<MRT_INTEN);
    10.   LPC_MRT->Channel[1].CTRL = (MRT_Repeat<<MRT_MODE) | (1<<MRT_INTEN);
    11.   // Enable the MRT interrupt in the NVIC
    12.    NVIC_EnableIRQ(MRT_IRQn);
    13.                 // Start MRT channel 0
    14. //         LPC_MRT->Channel[0].INTVAL = MRT0_TIME;
    15.     // Start MRT channel 1
    16. //  LPC_MRT->Channel[1].INTVAL = MRT1_TIME;
    17. }
    复制代码


    4.阿里云topic,采用sprintf 格式化字符串

    发布0.png
    格式化字符串的时候容易出错,基本上都是格式不对,尤其是针对字符串中含有(“”,转义字符要用够数量,本人之前这个搞了很久。

    发布.PNG
    最终效果如视频所示

    该会员没有填写今日想说内容.
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2025-7-11 08:53
  • 签到天数: 301 天

    连续签到: 2 天

    [LV.8]以坛为家I

    3938

    主题

    7559

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    40222
    最后登录
    2025-9-9
    发表于 2021-6-28 16:20:56 | 显示全部楼层
    这里LPC800主要作用是什么呢,可以分享一下
    qiandao qiandao
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2023-1-31 09:34
  • 签到天数: 202 天

    连续签到: 1 天

    [LV.7]常住居民III

    7

    主题

    1518

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    3979
    最后登录
    2025-9-3
     楼主| 发表于 2021-6-29 09:48:24 | 显示全部楼层
    NXP管管 发表于 2021-6-28 16:20
    这里LPC800主要作用是什么呢,可以分享一下

    好的 ,我来加上
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2025-5-15 09:29
  • 签到天数: 178 天

    连续签到: 1 天

    [LV.7]常住居民III

    11

    主题

    423

    帖子

    1

    金牌会员

    Rank: 6Rank: 6

    积分
    1395
    最后登录
    2025-9-9
    发表于 2021-10-22 20:58:52 | 显示全部楼层
    代码可以share吗?谢谢
    加油加油
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-9-10 03:02 , Processed in 0.086325 second(s), 24 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表