STM32最小系统板+ESP01S接入机智云教程篇
本次采用 STM32F103C8T6单片机、ESP-01S 模块、红外避障模块、蜂鸣器模块,OLED(0.96 iic)、继电器模块 、超声波模块,接入机智云平台,分别演示机智云平台的各类型数据点的用法。为嵌入式学习添**加瓦,快速接入机智云云平台。 适用的人群:入门嵌入式、学生、电子DIY爱好者 注:本项目用到的外设文件我会在文章最后上传附件,点击放大图片可以清晰参看。教程使用到的模块:https://club.gizwits.com/data/attachment/forum/202408/28/112902p1oiii19i1oqi42a.jpg.thumb.jpgSTM32F103C8T6https://club.gizwits.com/data/attachment/forum/202408/28/112902sdv9jarzwdd7vozv.jpg.thumb.jpgESP-01S
https://club.gizwits.com/data/attachment/forum/202408/28/112903niomr44gkamgikak.jpg.thumb.jpg超声波模块(HC-SR04)
https://club.gizwits.com/data/attachment/forum/202408/28/112903nar38eealzaaz5z3.jpg.thumb.jpg蜂鸣器模块
https://club.gizwits.com/data/attachment/forum/202408/28/112903bt7hjeyzpbmjnrli.jpg.thumb.jpgOLED(0.96 iic)
https://club.gizwits.com/data/attachment/forum/202408/28/112904n8bm4mjmbe97yi7i.jpg.thumb.jpg红外避障模块
https://club.gizwits.com/data/attachment/forum/202408/28/112904xahgqahahnag22aq.jpg.thumb.jpg继电器模块
https://club.gizwits.com/data/attachment/forum/202408/28/112904zqk8d9hudzwxku99.jpg.thumb.jpgUSB A TTL CH340
基本工作
第一步:进入机智云开发者中心创建智能产品 机智云开发者中心 (gizwits.com)详细参考机智云文档中心(产品创建 - Gizwits)https://club.gizwits.com/data/attachment/forum/202408/28/112905tjxdiddvxz28qijd.jpg.thumb.jpg分别新建数据点https://club.gizwits.com/data/attachment/forum/202408/28/112905c7qs7bbe6zeke876.jpg.thumb.jpg
第二步:ESP-01S烧录机智云固件 需要用到 USB A TTLCH340串口来对ESP01S进行烧录,连接图如下:https://club.gizwits.com/data/attachment/forum/202408/28/112905x42kkg1n4cjapdic.jpg.thumb.jpg
完成成连接,我们到机智云下载中心下载机智云对应的固件:机智云下载中心地址(Gizwits)https://club.gizwits.com/data/attachment/forum/202408/28/112906hdgmf28fdsdgbzb7.jpg.thumb.jpg
解压我们可以看到有好多种固件,带combine的是合并固件,烧录只需要选择这一个即可,分别有 8M 16M 32M 大小的固件给我们烧录,这时候就需要去查看我们的ESP01S的flash大小是多少了。https://club.gizwits.com/data/attachment/forum/202408/28/112906rygkcyvysumdwmng.jpg.thumb.jpg
通过去百度查找对应型号的esp8266,在查看该WIFI模组的Flash大小,我的为8M,所以我选择烧录 8M大小的合并固件。https://club.gizwits.com/data/attachment/forum/202408/28/112906qnhknikn3hueiuiu.png.thumb.jpg使用flash_download_tool工具下载(无需安装,解压直接使用)下载地址:工具|乐鑫科技 (espressif.com.cn)https://club.gizwits.com/data/attachment/forum/202408/28/112907vxev0vvynkkus2lu.jpg.thumb.jpg
双击打开:https://club.gizwits.com/data/attachment/forum/202408/28/112907kjjzdkjt33eqergc.jpg.thumb.jpg
选择 ESP8266https://club.gizwits.com/data/attachment/forum/202408/28/112907jiwmtz4s2q15b5zb.jpg.thumb.jpg
USB A TTL CH340 连接到电脑串口(如果显示不和我一样 USB-SERILACH340 (xxx),就是没有安装 CH340的驱动)https://club.gizwits.com/data/attachment/forum/202408/28/112908kvvqpqptsmm5jmqm.jpg.thumb.jpg添加文件:https://club.gizwits.com/data/attachment/forum/202408/28/113046bu3mi5cxir35reez.jpg.thumb.jpg烧录选项:https://club.gizwits.com/data/attachment/forum/202408/28/113047mtiuozl6ltuunl6b.jpg.thumb.jpg
好了就先擦除一下芯片以前的固件https://club.gizwits.com/data/attachment/forum/202408/28/113047xoojkkdsccjsy0u0.jpg.thumb.jpg
在进行烧录(显示红圈中的参数基本就烧录成功,硬件没问题,能读取到信息)https://club.gizwits.com/data/attachment/forum/202408/28/113048mzlpph3olnlzhih3.jpg.thumb.jpg
烧录完成:https://club.gizwits.com/data/attachment/forum/202408/28/113048zzztvbtdi9rpot0o.jpg.thumb.jpg
接下来就是测试了,把多余的线去除,理论上接线图如下:https://club.gizwits.com/data/attachment/forum/202408/28/113048u9te4lhxtros6f4o.jpg.thumb.jpg但是发现,我使用的是拓展坞供电,可能电流不够,导致掉串口,我就换了一种接法,使用STM32最小系统的3V3对ESP01s进行供电,连接图如下:https://club.gizwits.com/data/attachment/forum/202408/28/113049ckcbgztwwzcdpks5.jpg.thumb.jpg注意设备的共地很重要https://club.gizwits.com/data/attachment/forum/202408/28/113049cl6prxrnad33xc2p.jpg.thumb.jpg机智云下载中心下载机智云串口调试助手:下载连接:Gizwitshttps://club.gizwits.com/data/attachment/forum/202408/28/113049cfhhfth5u5xqx4tn.jpg.thumb.jpg解压后双击打开https://club.gizwits.com/data/attachment/forum/202408/28/113050yt1v39xxah6m66r5.jpg.thumb.jpg点击新增:https://club.gizwits.com/data/attachment/forum/202408/28/113050hxpd5ped5djomyej.jpg.thumb.jpg复制粘贴,我们新建产品的 pk 和 pshttps://club.gizwits.com/data/attachment/forum/202408/28/113050d72k7o5k2pl55lps.jpg.thumb.jpg新增成功:https://club.gizwits.com/data/attachment/forum/202408/28/113051z00yllxkdd17lt10.jpg.thumb.jpg
选择串口,打开串口,点击初始化,有RX回复表示串口通信成功。https://club.gizwits.com/data/attachment/forum/202408/28/113051rfp812t5ypv12tzl.jpg.thumb.jpg
接下来就是机智云的下载中心下载机智云的APP对设备进行配网下载地址:Gizwitshttps://club.gizwits.com/data/attachment/forum/202408/28/113052gtppsqtuk6vyszdm.jpg.thumb.jpg
手机打开浏览器,扫描二维码进行下载https://club.gizwits.com/data/attachment/forum/202408/28/113052zk2h5bcwuwi7nkif.jpg.thumb.jpg安装APPhttps://club.gizwits.com/data/attachment/forum/202408/28/113053umlkkk4koq4g5yz4.jpg.thumb.jpg https://club.gizwits.com/data/attachment/forum/202408/28/113053o7f8zw46wt6r4e8q.jpg.thumb.jpg
权限全部允许:https://club.gizwits.com/data/attachment/forum/202408/28/113054pc1dtotj5m12wnbd.jpg.thumb.jpg调试APP可以点击下方跳过登陆,直接使用https://club.gizwits.com/data/attachment/forum/202408/28/113054l3ght35p93w4wwc8.jpg.thumb.jpg
回到机智云串口调试助手点击初始化,过几秒在点击 Airlink 进入配网https://club.gizwits.com/data/attachment/forum/202408/28/113054sggmid83mnnz3m7o.jpg.thumb.jpg
在到手机APP操作:点击添加设备后,选择一键配网https://club.gizwits.com/data/attachment/forum/202408/28/113244chztt97ho27k2gof.jpg.thumb.jpg记住需要Wifi的网段在 2.4G网段,不用使用 5G 或者2.4G和5G合并的网络,可以使用电脑开热点进行配网。https://club.gizwits.com/data/attachment/forum/202408/28/113245cuj2juj32j34mxcq.jpg.thumb.jpghttps://club.gizwits.com/data/attachment/forum/202408/28/113245znfdr8mmw87mrf9d.jpg.thumb.jpg
在等待下图的过程中,可以发现机智云串口调试助手会有大量的数据接收到,可能是配网成功的现象。https://club.gizwits.com/data/attachment/forum/202408/28/113246xdwy8yoeoexxrffh.jpg.thumb.jpghttps://club.gizwits.com/data/attachment/forum/202408/28/113246amwwtt8g87owwyey.jpg.thumb.jpg
稍作等待完成,如果不行,就退回到主界面,看一看到ESP01S以及联网了,与我的手机在同一局域网内,我们点击连接即可https://club.gizwits.com/data/attachment/forum/202408/28/113247yt3132rl5r81pcol.jpg.thumb.jpg机智云串口调试助手,上报数据https://club.gizwits.com/data/attachment/forum/202408/28/113247tb0948gspnktd9w6.jpg.thumb.jpg
https://club.gizwits.com/data/attachment/forum/202408/28/113248gcqjir1c1yr7tncz.jpg.thumb.jpg到此完成ESP01S的配网流程,ESP01S可以直接接到STM32最小系统的串口进行使用了。
接下来是各个模块与单片机的连接,接线图如下:https://club.gizwits.com/data/attachment/forum/202408/28/113248fpxzdzet3dcc0oep.jpg.thumb.jpg
STM32代码下载与cobeMX配置利用机智云开发者中心的MCU开发便捷快速生成代码下载好并解压缩机智云平台生成的代码。
https://club.gizwits.com/data/attachment/forum/202408/28/113249ncobk1tbcxoybjxb.jpg.thumb.jpg
打开解压好的STM32CubeMX文件,记住路径不能有中文!https://club.gizwits.com/data/attachment/forum/202408/28/113916cfqor5ogffatwwq4.jpg.thumb.jpg
配置IO口输入模式来接收红外避障模块的电平硬件 PC15两种状态:没反射——D0输出高电平——灭灯反射——D0输出低电平——亮灯https://club.gizwits.com/data/attachment/forum/202408/28/113249l8mt36o35k2mo363.jpg.thumb.jpg
配置蜂鸣器控制引脚PB12:https://club.gizwits.com/data/attachment/forum/202408/28/113249iysh88qh7089krme.jpg.thumb.jpg配置继电器控制引脚 PB13https://club.gizwits.com/data/attachment/forum/202408/28/113250cjh6j22zf12yzhf0.jpg.thumb.jpg配置板载LED灯的PC13引脚https://club.gizwits.com/data/attachment/forum/202408/28/113250l13b377key5noz1k.jpg.thumb.jpg配置超声波Trig引脚为 PA5https://club.gizwits.com/data/attachment/forum/202408/28/113251tawa0bbwzdijic6t.jpg.thumb.jpg配置TIM3 选择通道1 并开启外部中断https://club.gizwits.com/data/attachment/forum/202408/28/113251ph9050qagqolqgf3.jpg.thumb.jpg https://club.gizwits.com/data/attachment/forum/202408/28/113252js22y2ps0saov2tt.jpg.thumb.jpg选择区分文件方便使用最后生成代码(不可以有中文路径!!)https://club.gizwits.com/data/attachment/forum/202408/28/113252vj1ggbnn0j72bz58.jpg.thumb.jpg
打开MDK-ARM工程文件夹https://club.gizwits.com/data/attachment/forum/202408/28/113253l31e51588mma3333.jpg.thumb.jpg
添加外设文件夹https://club.gizwits.com/data/attachment/forum/202408/28/113253kiqw0qmncswcwmyy.jpg.thumb.jpg
在project 中添加Hardware 文件夹的编译路径,以及添加进入工程https://club.gizwits.com/data/attachment/forum/202408/28/113253gmhfmyf4x1a0hn4m.jpg.thumb.jpghttps://club.gizwits.com/data/attachment/forum/202408/28/113254sq5dffyymbdadafm.jpg.thumb.jpg
补全外设的头文件,在main.h中添加#include"stm32f1xx_hal.h"https://club.gizwits.com/data/attachment/forum/202408/28/113350afa0aa20df3mi6ja.jpg.thumb.jpg
编译无误后继续https://club.gizwits.com/data/attachment/forum/202408/28/113350eujwcw6ush3cds6q.jpg.thumb.jpg
添加头文件https://club.gizwits.com/data/attachment/forum/202408/28/113351knformwnjn22e9w5.jpg.thumb.jpg
主函数中初始化OLEDhttps://club.gizwits.com/data/attachment/forum/202408/28/113351gk3xi3hh8htzl8ht.jpg.thumb.jpgWhile(1)死循环中,实现逻辑代码https://club.gizwits.com/data/attachment/forum/202408/28/113352hnijrunq4dsqjjdv.jpg.thumb.jpg
[*] while (1)
[*] {
[*] SR04_GetData();
[*] if(Last_i!=(uint32_t)distant)//当前的距离和上一次的距离不同则进入
[*] {
[*] // currentDataPoint.valuehongwai = ;//Add Sensor Data Collection
[*] OLED_ShowString(1,1,"dis:",OLED_8X16);
[*] OLED_ShowNum(32,1,distant,3,OLED_8X16);
[*] OLED_ShowString(1,16,"Buzzer:",OLED_8X16);
[*] currentDataPoint.valueHC_SR04 = distant;//Add Sensor Data Collection
[*] if(distant<=30)
[*] {
[*] OLED_ShowString(56,16,"ON ",OLED_8X16);
[*] HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET); //蜂鸣器 响
[*] currentDataPoint.valueBuzzer = 1;//Add Sensor Data Collection
[*]
[*] }else
[*] {
[*] OLED_ShowString(56,16,"OFF",OLED_8X16);
[*] HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET); //蜂鸣器 停
[*] currentDataPoint.valueBuzzer = 0;//Add Sensor Data Collection
[*] }
[*]
[*] Last_i=(uint32_t)distant;// Last_i 保存上一次的值
[*] }
[*] OLED_ShowString(1,32,"Relays:",OLED_8X16);
[*] if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13))
[*] {
[*] OLED_ShowString(56,32,"ON ",OLED_8X16);
[*] }else
[*] {
[*] OLED_ShowString(56,32,"OFF",OLED_8X16);
[*] }
[*]
[*] OLED_ShowString(1,48,"LED:",OLED_8X16);
[*] if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_15)) //读取 红外避障模块电平
[*] {//高电平,就是没有遮挡
[*] OLED_ShowString(56,48,"OFF",OLED_8X16);
[*] HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);//给板载LED低电平 灭
[*]
[*] }else
[*] {//低电平,有物体遮挡 板载LED亮
[*] OLED_ShowString(56,48,"ON ",OLED_8X16);
[*] HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);//给板载LED高电平 亮
[*] }
[*]
[*] OLED_Update();
[*] HAL_Delay(300);
[*]/* USER CODE END WHILE */
[*]
[*]/* USER CODE BEGIN 3 */
[*] userHandle();
[*] gizwitsHandle((dataPoint_t *)¤tDataPoint);
[*] }
复制代码
在gizwits_product.c 的第79行后面添加控制继电器的代码,如下图:https://club.gizwits.com/data/attachment/forum/202408/28/113352p33zva475ddgurd1.jpg.thumb.jpg
编译前勾选下图中的USEMicro LIBhttps://club.gizwits.com/data/attachment/forum/202408/28/113352qu7rio21x7nnuzir.jpg.thumb.jpg
机智云开发者中心查看数据https://club.gizwits.com/data/attachment/forum/202408/28/113353mni7mtimvf8wgenv.jpg.thumb.jpg
在APP中查看数据https://club.gizwits.com/data/attachment/forum/202408/28/113353cj667fsw63rpb5wf.jpg.thumb.jpg成果图:https://club.gizwits.com/data/attachment/forum/202408/28/113354no8zgv14bsvn78s1.jpg.thumb.jpg
页:
[1]