【瑞萨RA0E1开发板评测报告】IIC OLED 测试
【瑞萨RA0E1开发板评测报告】IIC OLED 测试
基于前面关于瑞萨 e² studio 开发软件的使用,以及工程测试基础,本文进一步探索实现硬件 IIC OLED 的文字和图片显示。
1 背景
简单介绍 IIC 通信协议、OLED 显示原理、SS1306 驱动 IC 等。
IIC 通信协议
IIC (Inter-Integrated Circuit) 即集成电路总线。它是一种串行通信总线,使用多主从架构,如图所示。
飞利浦 Philips 公司于二十世纪八十年代初设计,最初是为了让主板、嵌入式系统或手机用以连接低速周边设备。
自2006年10月1日起,使用 IIC 协议不需要支付专利费,但制造商仍然需要付费以获取 IIC 从属设备地址。
IIC 通信协议的功能特点如下
只需要两条总线线路
没有严格的波特率要求,例如使用 RS232,主站生成总线时钟
所有组件之间存在简单的主/从关系连接到总线的每个设备都可通过唯一地址进行软件寻址
IIC 是真正的 多主 总线,提供仲裁和冲突检测
IIC 使用两根线进行通信:一根时钟线 SCL,一根数据线 SDA。
IIC 将 SCL 处于高时 SDA 拉低的动作作为开始信号,SCL 处于高时 SDA 拉高的动作作为结束信号;
传输数据时,SDA 在 SCL 低电平时改变数据,在 SCL 高电平时保持数据,每个 SCL 脉冲的高电平传递 1 位数据。
7-bit 设备的数据传输
软件模拟 IIC 一般是用 GPIO 管脚,软件控制管脚高低电平输出,以模拟 IIC 通信波形。
硬件 IIC 对应芯片上的 IIC 外设,有相应 IIC 驱动电路,其所使用的 IIC 管脚也是专用。
软硬件 IIC 对比
硬件 IIC 效率远高于软件 IIC,软件 IIC 不受管脚限制,接口更灵活;
软件模拟 IIC 是通过 GPIO 软件模拟寄存器的工作方式,而硬件 IIC 是直接调用内部寄存器进行配置;
性能方面,硬件 IIC 由于由专门的硬件模块处理,减少了软件干预,因此具有更高的可靠性和稳定性;
硬件 IIC 模块可以在后台自动处理数据传输,释放 CPU 资源,因此CPU占用率更低;
软件 IIC 的代码量多于硬件 IIC,相比之下更为复杂;
硬件 IIC 通常还支持更高的通信速率。
OLED 显示原理
OLED (Organic Electroluminescence Display) 即有机发光二极管。
OLED 由于同时具备自发光,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异特性,被认为是下一代的平面显示器新兴应用技术。
0.96英寸OLED基本参数
工作电压:3.3~5V(内置 3.3V LDO,IIC 通信接口电平是 3.3V 的)
像素点阵规模:128*64(128 列,64 行,共 8192 颗 LED)
驱动 IC:SSD1306
OLED 模块电路
SSD1306 驱动电路
详见:oled i2c , SSD1306 Datasheet .
2 方法说明
1.使用 e2 studio 建立硬件 IIC 框架;
2.编译 OLED 相关主函数,特别是初始化、打开 IIC、写入 IIC 等部分,或移植相关函数,并进行修改;
3.运行程序,构建项目,调试项目。
3 操作说明
1.新建项目;
2.引脚 Pins 定义;
3.新建Stacks - IIC master,并修改属性,定义地址 0x3C ;
4.移植并修改 oled.c 、 oled.h 、 oledfont.h 、 bmp.h 文件;
5.构建主函数 hal_entry.c,调试项目。
4 系统框图
5 代码
主函数 hal_entry.c
while(1)
{
/*---------- Picture ------------*/
OLED_Clear();//clear screen
OLED_DrawBMP(0,0,128,8,BMP1);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
/*----------- Text ------------*/
OLED_Clear();//clear screen
OLED_ShowString(0,0,"RA",16);
OLED_ShowCHinese(16,0,0);//生
OLED_ShowCHinese(32,0,1);//态
OLED_ShowCHinese(48,0,2);//工
OLED_ShowCHinese(64,0,3);//作
OLED_ShowCHinese(80,0,4);//室
OLED_ShowString(0,2,"2024.12.14",16);
OLED_ShowString(0,4,"Renesas RA0E1",16);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
}
oled.c
oled.h
oledfont.h
bmp.h
6 结果
如主代码所示,图片和文字交替显示。
while(1)
{
/*---------- Picture ------------*/
OLED_Clear();//clear screen
OLED_DrawBMP(0,0,128,8,BMP1);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
/*----------- Text ------------*/
OLED_Clear();//clear screen
OLED_ShowString(0,0,"RA",16);
OLED_ShowCHinese(16,0,0);//生
OLED_ShowCHinese(32,0,1);//态
OLED_ShowCHinese(48,0,2);//工
OLED_ShowCHinese(64,0,3);//作
OLED_ShowCHinese(80,0,4);//室
OLED_ShowString(0,2,"2024.12.14",16);
OLED_ShowString(0,4,"Renesas RA0E1",16);
R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
}
每隔 500ms 刷新页面一次。
7 演示视频
8 心得体会
瑞萨 RA0E1 芯片具有高性能、功耗比和性价比等优异特性,配合 Renesas 完善的开发环境,便捷的操作和 GPIO 配置、IIC 配置等,可以实现项目的快捷开发和调试。
感谢嘉立创和瑞萨举办的开发板试用活动,希望这次活动和相关测评能够为广大电子爱好者提供参考,并在今后的项目中得到应用。
#立创商城# #瑞萨# #单片机# #开发板# #OLED#
【瑞萨RA0E1开发板评测报告】简介、环境搭建、工程测试
【瑞萨RA0E1开发板评测报告】简介、环境搭建、工程测试
RA-Eco-RA0E1-32PIN-V1.0 是一款 基于48 MHz Arm® Cortex®-M23 内核架构 的核心板,主控芯片为 R7FA0E1073CFJ。
本文首先介绍了开发板和 MCU 的主要特点,之后介绍了开发环境的搭建流程、项目构建及工程调试,效果展示等,为后续的进一步研究奠定基础。
1 简介
本节介绍了开发板和 MCU 的主要特点。
外观
原理图
上电
出厂自带 LED 闪烁程序
主控 MCU
RA-Eco-RA0E1-32PIN-V1.0 开发板以 R7FA0E1073CFJ 为主控芯片,
RA0E1 产品组是 RA0 系列中的入门级 MCU,具有出色的成本效益和超低功耗。
Arm Cortex®-M23 内核,64kB 的闪存,1.6V~5.5V 宽电源电压范围,32MHz 的 CPU 性能。
RA0 系列适合成本敏感型应用,如低功耗、低成本的消费电子产品、小家电系统控制、工业系统控制和楼宇自动化。
RA系列
RA0E1
参数性能
32 MHz Arm Cortex-M23 内核
高达 64KB 闪存和 12KB SRAM
1KB 数据闪存(100,000 次编程/擦除 (P/E) 周期)
32 引脚封装选项
1.6V 至 5.5V 的宽电压范围
TAU(16 位通用 PWM 定时器)
TML32(32 位间隔定时器)
RTC 实时时钟
12 位模数转换器
TSN 温度传感器
SAU(UART、简单 SPI、简单 I2C)
UART
I2C 总线
TRNG 随机数发生器
安全功能
±1% 高精度、高速片上振荡器
灵活的电源架构,具有快速唤醒功能
HAL 驱动器代码
支持 Arm 生态系统和瑞萨电子原创开发工具
开发资料详见:RA0E1开发板资料 .
RA 生态社区论坛:https://ramcu.cn/
视频教程:RA生态工作室的个人空间_bilibili
Gitee仓库链接:ra生态工作室 (gitee.com)
开发工具
2 环境搭建
开发环境支持 e² studio 和 Keil ,这里主要介绍和使用前者。
下载并安装 瑞萨FSP (flexible software package) 灵活配置软件包 。
注意:不需要单独安装 e² studio,FSP 平台安装程序会安装 e² studio 和使用 FSP 所需的所有其他工具。
由于这里我们使用 RA0E1 芯片,仅安装 RA0 系列软件包即可。
3 工程测试
目标:Blink 实现板载 LED 交替闪烁
硬件准备
J-Link 调试器
若没有 J-Link 也可以使用 J-Link_OB 调试器,或自己 DIY ,开源项目推荐:
J-Link OB 改进板 - 立创开源硬件平台 .
工程建立
依次点击 文件 - 新建 - 瑞萨 C/C++ 项目 - Renesas RA
依次进行工程命名,路径设置,目标开发板选择,Device 选择 R7FA0E1073CFJ ,工具链选择 GNU ARM Embedded 。
根据原理图可知
MCU 引脚 P103 和 P112 分别控制 D1 和 D2 两个 LED 且为高电平点亮。
于是在 FSP Configuration 界面中对其进行配置,
进入 FSP 配置 界面,依次选择 Pins 标签 - Pin Selection - Ports - P1 - P103 - Pin Configuration - 更改 Mode 为 Output mode (initial Low) 即初始熄灭,点击 Generate Project Content 生成工程,双击打开位于 src 文件夹的主函数 hal_entry.c ,并插入主代码
代码
void hal_entry(void)
{
/* TODO: add your own code here */
while(1){
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_12, BSP_IO_LEVEL_LOW);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_12, BSP_IO_LEVEL_HIGH);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
项目构建、工程调试
右键项目,选择 构建项目
若正确无误,则输出 Build Finished. 0 errors, 0 warnings.
左侧目录选中目标项目,之后依次选择 运行 - 调试配置 - 选中对应的 Flat 执行文件 - 调试器 - 调试硬件选择 J-Link ARM - 点击 调试 按钮,即可进入调试流程
点击左上角的继续按钮进入调试,再次点击该按钮启动调试,
此时板载 LED 开始按照代码设定闪烁……
效果
#测评# #DIY设计# #原创#
OLED 显示雷达数据
OLED 显示雷达数据
本文结合之前关于串口打印雷达监测数据的研究,进一步扩展至 OLED 屏幕显示。
该项目整体分为两部分:
一、框架显示;
二、数据采集与填充显示。
为了减小 MCU 负担,采用 局部刷新 的方案。
1. 显示框架
所需库函数 Wire.h 、Adafruit_GFX.h 、Adafruit_SSD1306.h .
代码
#include[removed]#include [removed]
#include [removed]
#include "logo_128x64.h"
#include "logo_95x32.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
void setup()
{
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.clearDisplay(); // 清屏
display.drawBitmap(0, 0, logo, 128, 64, 1); //画出字符对应点阵数据
display.display();
delay(1000);
display.clearDisplay();
/*-------------------- Display picture and text ---------------------------*/
display.drawBitmap(16, 0, logo_small, 95, 32, 1);
display.setTextColor(WHITE); //设置字体颜色
display.setTextSize(2); //设置字体大小 1 is default 6x8, 2 is 12x16, 3 is 18x24
display.setCursor(0,33); //设置起始光标
display.print("v=");
display.setCursor(72,33); //设置起始光标
display.print("km/h");
display.setCursor(0,49); //设置起始光标
display.print("str=");
display.display();
}
void loop()
{
}
效果
2. 显示数据
目标:实现雷达监测数据的对应填充显示,包括速度 v 和信号强度 str
代码
思路:将之前帖子中实现的串口打印数据与 OLED 显示框架结合,将 v 和 str 两数据分别填充至 OLED 屏预留位置处即可。
#include[removed]#include [removed]
#include [removed]
#include "logo_128x64.h"
#include "logo_95x32.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
String comdata = "";
void setup()
{
Serial.begin(115200);
while (Serial.read() >= 0){}//clear serialbuffer
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.clearDisplay(); // 清屏
display.drawBitmap(0, 0, logo, 128, 64, 1); //画出字符对应点阵数据
display.display();
delay(1000);
display.clearDisplay();
/*-------------------- Display picture and text ---------------------------*/
display.drawBitmap(16, 0, logo_small, 95, 32, 1);
display.setTextColor(WHITE); //设置字体颜色
display.setTextSize(2); //设置字体大小 1 is default 6x8, 2 is 12x16, 3 is 18x24
display.setCursor(0,33); //设置起始光标
display.print("v=");
display.setCursor(80,33); //设置起始光标
display.print("km/h");
display.setCursor(0,49); //设置起始光标
display.print("str=");
display.display();
}
void loop()
{
if (Serial.available() > 0)
{
char data = Serial.read();
comdata += data;
if (data == '\n')
{// type of comdata: v=1.0 km/h, str=10151
int separatorIndex = comdata.indexOf(','); // 假设分隔符为逗号
if (separatorIndex != -1)
{
String part1 = comdata.substring(0, separatorIndex); // 第一个部分
String part2 = comdata.substring(separatorIndex + 1); // 第二个部分
// 打印分割后的数据
//Serial.println(part1); // type of part1: v=1.0 km/h
//Serial.println(part2); // type of part2: str=10151
/*------------ part1 : v=1.0 km/h ----------*/
int part1separatorIndex = part1.indexOf('='); //index of '='
if (part1separatorIndex != -1)
{
String vlc = part1.substring(part1separatorIndex + 1); // index of velocity, type of vlc is 1.0 km/h
// vlc: 1.0 km/h
int VLCseparatorIndex = vlc.indexOf(' '); // index of ' '
String v = vlc.substring(0, VLCseparatorIndex);// v only include number
float Vn = v.toFloat();
Serial.print(Vn); // print velocity number
Serial.print(',');
//display.setCursor(25,33); //设置起始光标
display.fillRect(25, 33, 60, 16, BLACK);
display.display();
display.setCursor(25,33); //设置起始光标
display.print(Vn);
display.display();
}
/*------------- part2 : str=10151 ------------------*/
int part2separatorIndex = part2.indexOf('='); //index of '='
if (part2separatorIndex != -1)
{
String strng = part2.substring(part2separatorIndex + 1); // strng only include number
int Sn = strng.toInt();
Serial.print(Sn); // print strength number
Serial.println();
//display.setCursor(49,49); //设置起始光标
display.fillRect(49, 49, 79, 16, BLACK);
//display.setPixelColor();
display.display();
display.setCursor(49,49); //设置起始光标
display.print(Sn);
display.display();
}
}
comdata = "";
}
}
}
效果
这里由于字体设置为 2 号,无法满足 km/h 单位的完整填充,因此被数据覆盖住一部分,可根据实际需求调整字体大小。
同时支持串口绘图和串口数据打印。
【CW32L010 Mini Board 测评】简介、点灯
【CW32L010 Mini Board 测评】简介、点灯CW32L010 Mini Board 是 武汉芯源半导体 设计的基于 CW32L010 的开发板,简介CW32L010 Mini Board 是基于 CW32L010F8U6 的 QFN 封装设计的最小系统核心板,产品规格与 CW32L010 芯片规格一致。便于客户灵活快速的评估芯片性能和产应用。1.0mm的板厚、单面元器件设计、半孔工艺设计使得CW32L010核心板可以作为一个模块元器件贴装在项目中,减少焊接QFN封装带来的不便。芯片型号命名规则产品参数 内核:ARM® Cortex®-M0+工作温度:-40℃ 至 85℃工作电压:1.62V 至 5.5V内置稳压器具有“正常”和“低功耗”两种工作模式,并且在复位后一直保持工作。“正常”模式:对应全速操作的状态。“低功耗”模式:对应部分供电工作状态,包括Sleep 和 DeepSleep 工作模式。存储容量64K 字节 FLASH,数据保持 25 年 @-40℃ ~ +85℃,支持擦写保护、读保护和安全运行库保护功能4K 字节 RAM,支持硬件奇偶校验22 字节 OTP 存储器复位和电源管理低功耗模式(Sleep,DeepSleep)上电和掉电复位(POR/BOR)可编程低电压检测器(LVD)时钟管理4 ~ 32MHz 晶体振荡器32kHz 低速晶体振荡器内置 48MHz RC 振荡器内置 32kHz RC 振荡器时钟监测系统允许独立关断各外设时钟支持最多 16+1 路 I/O 接口所有 I/O 口支持具备滤波的中断功能所有 I/O 口支持具备滤波的唤醒功能所有 I/O 口支持迟滞及上拉输入所有 I/O 口支持推挽及开漏输出12 位模数转换器最高 2M SPS 转换速度,各序列通道采样时间可单独配置具备 8 个转换结果寄存器内置 1.2V 电压基准模拟看门狗功能内置温度传感器双路电压比较器实时时钟和日历支持由 Sleep/DeepSleep 模式唤醒定时器16位高级控制定时器,支持6路输入捕获,支持6对带死区的互补PWM输出,支持双点比较,支持PWM 移相一组 16 位通用定时器三组 16 位基本定时器一组 16 位低功耗定时器独立看门狗定时器通信接口二路低功耗 UART,支持小数波特率,支持低功耗接收数据,支持可配置电平转换,其中一路支持LIN 通信接口一路 SPI 接口 24Mbit/s,支持 4~16bit位宽一路 I2C 接口 1Mbit/s,支持可配置电平转换,支持SMBUSIR 调制器,可编程占空比和极性串行调试接口 (SWD)80 位唯一 ID注意:板子出厂时默认为无程序状态,上电灯不亮。需要上传程序才可以点亮 LED. 相关资料:L010新品开发板-活动与资料合集 . 原理图开发环境搭建下载并安装 Keil - MDK IDE . 下载并安装芯片开发包 WHXY.CW32L010_DFP.1.0.0.pack . 代码打开例程 CW32L010_StandardPeripheralLib_V1.0.2 - Examples - GPIO - gpio_blink 修改 main.c 中的 GPIO 定义,由原理图可知,板载 LED 为 GPIO_B2 输出低电平点亮#defineLED_GPIO_PORTCW_GPIOB#define LED_GPIO_PINS GPIO_PIN_0
void Delay(uint16_t nCount);
int32_t main(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
SYSCTRL_HSI_Enable(SYSCTRL_HSIOSC_DIV12);
__SYSCTRL_GPIOA_CLK_ENABLE();
__SYSCTRL_GPIOB_CLK_ENABLE();
GPIO_InitStruct.IT = GPIO_IT_NONE;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pins = LED_GPIO_PINS;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
while (1)
{
GPIO_TogglePin(LED_GPIO_PORT, LED_GPIO_PINS);
Delay(0xFFFF);
}
}
重编译工程,上传工程至开发板。这里使用 PWLink 连接开发板。效果
ESP8266 通过 MQTT 协议实现 LED 的远程控制
ESP8266 通过 MQTT 协议实现 LED 的远程控制本文简要介绍了搭建 EMQX 服务器实现基于 MQTT 协议远程控制 NodeMCU ESP8266 板载 LED 的解决方案。简介1. MQTTMQTT(Message Queuing Telemetry Transport)是一种基于 publish/subscribe (发布/订阅) 模式的 轻量级 通讯协议,构建于 TCP/IP 协议上,由 IBM 在1999年发布。MQTT 可以实现用极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务 。2. EMQXEMQX 是一款大规模分布式物联网 MQTT 服务器,单集群支持 1 亿物联网设备连接,消息分发时延低于 1 毫秒。为高可靠、高性能的物联网实时数据移动、处理和集成提供动力。3. Home AssistantHome Assistant (HA) 家庭助理,是一款基于 Python 的智能家居开源系统,可以方便地连接各种外部设备,支持众多品牌的智能家居设备。方案安装 Home Assistant使用 Docker 容器安装 Home Assistant , 下载 Docker 软件;C 盘根目录新建文件夹 homeassistant ;下载部署文件 GitHub 并解压得到 docker-compose.yml 保存至上述 homeassistant 文件夹;Windows 打开 命令提示符 或 Windows PowerShell,输入如下代码实现自动下载镜像cd C:\homeassistant
docker-compose.yml
docker-compose up
安装过程需大约 30 分钟,即可在 Docker 容器中创建 Home Assistant 镜像;自动从镜像安装 Home Assistant 到容器。代码文件 docker-compose.yml 将映射 Home Assistant 配置文件到 C:/homeassistant 文件夹;浏览器输入网址 http://localhost:8123/ 进入Home Assistant 主界面,创建智能家居账号。详见:How to run Home Assistant Container on Windows using Docker - Kiril Peyanski's Blog . 安装 EMQX使用 EMQX 最简单的方式是在 EMQX Cloud 上创建完全托管的 MQTT 服务。这里我们使用 Docker 运行 EMQXWindows 命令行或 PowerShell 输入并执行如下代码docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:latest
等待进度条下载和部署完成(大约5分钟),即可获得 EMQX 服务器。Docker 内的 localhost 或 127.0.0.1 指向的是容器内部地址,如需访问宿主机地址请使用宿主机的真实 IP .详见:Gitee . 配置 EMQX(1)浏览器打开网址 http://localhost:18083/ ,初始登录账户名 admin 密码 public ;(2)依次打开 访问控制 - 客户端认证 - 创建 - Password-Based - 内置数据库 - (默认配置)- 创建 ;(3)用户管理 - 新建用户 - 自定义用户名和密码(建议 admin). 连接 EMQX 与 HA(1)命令行或 PowerShell 输入 ipconfig 获取本地计算机 IPv4 地址,如 42.34.25.153 (2)配置 Home Assistant ,依次点击设置 - 设备与服务 - 添加集成 - 搜索 MQTT - 填写代理信息。代理栏输入计算机 IP 地址,端口 1883,用户名和密码为 EMQX 中创建的用户信息。(3)点击 提交 后显示 成功创建 MQTT ,此时 集成 选项下出现 MQTT 条目,EMQX 网页 集群 的 总连接数 和 在线连接数 由 0 变为 1,表明 MQTT 设备已连接。参考:MQTT 接入 Home Assistant . 连接 ESP8266 与 EMQX将如下代码下载至 ESP8266 开发板,需要注意 MQTT 服务器地址,若是 EMQX Cloud,则根据创建远程节点填写地址;若是本地计算机或Docker容器,则填写本地物理 IP 地址。主题为 MQTTX 客户端定义的订阅名称,客户名和密码则对应 EMQX 服务器客户端用户定义。#include[removed]#include [removed]
#define LED 2 // on-board LED D4
// WiFi
const char *ssid = "xxx"; // Enter your WiFi name
const char *password = "xxxxxx"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "xx.xx.xx.xx"; // EMQX Server IP
const char *topic = "emqx/esp8266"; // MQTTX topic
const char *mqtt_username = "UART"; // EMQX Server User Name
const char *mqtt_password = "123456"; //EMQX Server User Password
const int mqtt_port = 1883;
bool ledState = false;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
delay(1000); // Delay for stability
// Connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to the WiFi network");
// Setting LED pin as output
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // Turn off the LED initially
// Connecting to an MQTT broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public EMQX MQTT broker connected");
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// Publish and subscribe
client.publish(topic, "hello emqx");
client.subscribe(topic);
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message: ");
String message;
for (int i = 0; i [removed]
ESP32模组在立创商城没货……PCB还在生产中,只能去某宝某鱼看看了。哦,还需要3D打印外壳,新手上路,一不小心就翻车,哈哈
CW32数字电流电压表终于完成了,等几个配件到货花了些时间,效果比较满意,没有制作3D外壳
Arduino Nano 和 ESP8266 读取 DHT11及 OLED 显示
Arduino Nano 和 NodeMCU ESP8266 读取 DHT11 环境温湿度数据及 OLED显示 Arduino Nano 开发板 引脚定义 实物展示 代码 /* https://breakrow.com/miliohm/temperature-and-humidity-sensor-dht11-with-arduino-tutorial-make-oled-termometer/ 10 - DHT11 pin OLED: SDA - SDA SCL - SCL */ #include#include#include#include#include#include "DHT.h" #define DHTPIN 10 // data connection pin of DHT11 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); int h; int t; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #definebitmap_height128#define bitmap_width 64 static const unsigned char PROGMEM logo_bmp[] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x78, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xF0, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFC, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFE, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFE, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0x7F, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0x1F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x0F, 0xF3, 0xEF, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x03, 0xE7, 0xCF, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x61, 0xC7, 0x9F, 0xE0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x71, 0xCF, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x71, 0xCF, 0xBF, 0xF3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x71, 0x9F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x71, 0x3F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x71, 0x38, 0x1F, 0xE7, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x02, 0x20, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x06, 0x46, 0x03, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x8C, 0xC7, 0x18, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xC7, 0x1C, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF9, 0xC7, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF9, 0xC7, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xF3, 0xC6, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xE3, 0xC0, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xE7, 0xE0, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void setup() { Serial.begin(9600); dht.begin(); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } // Clear the buffer display.clearDisplay(); printText(); delay(1500); } void loop() { h = dht.readHumidity(); t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } showBitmap(); printText(); display.display(); delay(500); display.clearDisplay(); } void printText() { display.setFont(&FreeMonoBold18pt7b); display.setTextColor(WHITE); // Draw white text display.setCursor(45, 28); // Start at top-left corner display.print(t); display.drawCircle(92, 8, 3, WHITE); display.setCursor(100, 27); display.print("C"); display.setCursor(45, 62); display.print(h); display.print("%"); } void showBitmap(void) { display.drawBitmap(0, 0, logo_bmp, bitmap_height, bitmap_width, WHITE); //display.display(); } 视频效果 BV1pNYCeNErk NodeMCU ESP8266 开发板 引脚定义 实物展示 代码 /* https://cloud.tencent.com/developer/article/1688146 GPIO0 (D3) - DHT11 pin GPIO4 (D2) - SDA GPIO5 (D1) - SCL */ #include#include#include#include#include#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #define DHTPIN 0 // Digital pin connected to the DHT sensor : GPIO0 - D3 // Uncomment the type of sensor in use: #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } delay(2000); display.clearDisplay(); display.setTextColor(WHITE); } void loop() { delay(2000); //read temperature and humidity float t = dht.readTemperature(); float h = dht.readHumidity(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); } // clear display display.clearDisplay(); // display temperature display.setTextSize(1); display.setCursor(0,0); display.print("Temperature: "); display.setTextSize(2); display.setCursor(0,17); display.print(t); display.print(" "); display.setTextSize(1); display.cp437(true); display.write(167); display.setTextSize(2); display.print("C"); // display humidity display.setTextSize(1); display.setCursor(0, 35); display.print("Humidity: "); display.setTextSize(2); display.setCursor(0, 45); display.print(h); display.print(" %"); display.display(); } 视频效果 BV1pNYCeNErb \n#单片机与嵌入式#