找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 338|回复: 0
打印 上一主题 下一主题
收起左侧

STM32单片机连接阿里云

[复制链接]
跳转到指定楼层
楼主
ID:1095937 发表于 2024-3-17 17:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用STM32F103C8T6连接阿里云,上传温湿度,并包含两个开关控制。

  1. #include "stm32f10x.h"                  // Device header
  2. #include "OLED.H"
  3. #include <stdio.h>
  4. #include "Delay.h"
  5. #include "MyUSART.H"
  6. #include "AD.H"
  7. #include "esp.h"
  8. #include "DHT11.H"
  9. #include "MySPI.H"
  10. #include "PWM.H"
  11. #include "LED.H"

  12. int Temperature;
  13. int Shidu;
  14. uint8_t Lock='0';
  15. uint8_t Switch2='0';
  16. extern char RECS[200];
  17. extern char Data[5];

  18. uint16_t cnt;
  19. uint16_t AD;
  20. float Volatge;


  21. void Init(void)
  22. {
  23.         uint8_t Judge=0;
  24.         OLED_Init();
  25.         OLED_ShowString(1,1,"Linking...");
  26.         MyUSART_Init(); //初始化串口
  27.         PWM_Init();
  28.         do
  29.         {
  30.                 Judge = esp_Init();
  31.                 OLED_ShowString(1,1,"error code:    ");
  32.                 OLED_ShowNum(2,1,Judge,1);
  33.         }while(Judge);  //连接阿里云直到成功
  34.        
  35.         RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA ,ENABLE);
  36.         GPIO_InitTypeDef GPIO_InitStructure;
  37.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  38.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_0;
  39.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  40.         GPIO_Init(GPIOA,&GPIO_InitStructure);
  41.         GPIO_ResetBits(GPIOA,GPIO_Pin_2);//LED初始化为低电平(关)
  42.        
  43. }

  44. int main(void)
  45. {
  46.         Init();
  47. //        AD_Init ();
  48.         OLED_Clear();
  49.         OLED_ShowString (1,1,"Secure");
  50.         OLED_ShowCHinese (1,12,4);
  51.         OLED_ShowCHinese (1,14,5);
  52.         OLED_ShowCHinese (2,1,0);
  53.         OLED_ShowCHinese (2,3,2);
  54.         OLED_ShowCHinese (2,5,3);
  55.         OLED_ShowCHinese (3,1,1);
  56.         OLED_ShowCHinese (3,3,2);
  57.         OLED_ShowCHinese (3,5,3);
  58.         while(1)
  59.         {
  60.                 AD=ADC_GetValue();
  61.                 Volatge =(float) AD/4055*3.3;
  62.                 if(Volatge>=0.2)
  63.                 LED_ON();
  64.                 cnt++;
  65.                 if(cnt%150==0)
  66.                         DHT11_GetData(); //约每1s执行一次温湿度采集
  67.                 if(cnt==600) //约每6s执行一次数据上报
  68.                 {
  69.                         if(Esp_PUB() == 1)
  70.                         {
  71.                                 OLED_ShowString(1,1,"publish failed");
  72.                                 Delay_ms(500);
  73.                                 OLED_Clear();
  74.                         }
  75.                         cnt=0;
  76.                 }
  77.                
  78.                 Shidu=Data[0];
  79.                 Temperature=Data[2];
  80.                 OLED_ShowNum(2,7,Temperature,2);
  81.                 OLED_ShowSignedNum(2,12,40,2);
  82.                 OLED_ShowNum(3,7,Shidu,2);
  83.                 OLED_ShowSignedNum(3,12,60,2);
  84.                 CommandAnalyse ();
  85.                 if(Lock=='0')
  86.                         PWM_SetCompare(5);
  87.                 else if(Lock=='1')
  88.                         PWM_SetCompare(15);
  89.                 if(Switch2=='0')
  90.                         GPIO_ResetBits(GPIOA,GPIO_Pin_2);
  91.                 else if(Switch2=='1')
  92.                         GPIO_SetBits(GPIOA,GPIO_Pin_2);
  93.         }
  94.        
  95. }

复制代码

原理图: 无
仿真: 无
代码: STM32F103C8T6阿里云.7z (184.71 KB, 下载次数: 15)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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