1.FireBeetle ESP32-E产品简介
FireBeetle ESP32-E是一款基于ESP-WROOM-32E双核芯片的主控板,它专为IoT设计。
它支持WIFI和蓝牙双模通信
并具有体积小巧
、超低功耗
、板载充电电路
、接口易用
等特性。可灵活的用于家庭物联网改装
、工业物联网改装
、可穿戴设备
等等。
通过和IFTTT等物联网平台
的连接,你可轻松制作出你独有的特色物联网智能家居系统。
FireBeetle ESP32-E深度支持ArduinoIDE编程
,并且即将支持Scratch图形化编程及MicroPython编程
。
我们提供了详细的在线教程和应用案例
,以及上千种免焊接
的Gravity接口传感器与执行器,可轻松上手制作,大幅度降低你的学习时间。邮票孔的设计
,让它可以方便的嵌入你设计的PCB上,大大缩减你的原型开发成本以及原型测试时间。
2.FireBeetle系列是什么
FireBeetle是一个高性能且更mini的Arduino开源开发板系列,现在FireBeetle不仅完全兼容arduino的开发环境,并且拥有更丰富的软硬件资源,它将支持makecode、Mind+、pinpong、MicroPython的开发环境(即将完善),无论是图形化编程、C语言、python编程还是JS编程都可以随心所欲的操作硬件。
它是如此的灵活并且开源,我们为您提供了最简单的编程方式!最详细的教程资料!上千种易用的Gravity外设!无论您是学生、电子爱好者、艺术家或者设计师,它都将是您开启电子世界的最佳伙伴,不用再面对复杂的电路、烧脑的程序、五花八门的通信协议,使用FireBeetle别再犹豫!成就您的无限可能!
3.特性
- DFRobot FireBeetle V2系列兼容,体积小巧仅25.4 × 60 mm
- ESP32双核低功耗主控,WiFi+BT4.0二合一
- GDI显示接口,单线连接显示屏
- 板载充电电路以及PH2.0锂电池接口
4.性能描述
- 工作电压:3.3V
- 输入电压:3.3V~5.5V
- 支持低功耗:10uA
- 支持最大放电电流:600mA@3.3V LDO
- 支持最大充电电流:500mA
- 支持USB充电
- 处理器:Tensilica LX6双核处理器(一核处理高速连接;一核独立应用开发)
- 主频:240MHz
- SRAM:520KB
- Flash:32Mbit
- Wi-Fi标准:FCC/CE/TELEC/KCC
- Wi-Fi协议:802.11 b/g/n/d/e/i/k/r (802.11n,速度高达150 Mbps),A-MPDU和A-MSDU聚合,支持0.4us防护间隔
- 频率范围:2.4~2.5 GHz
- 蓝牙协议:符合蓝牙v4.2 BR/EDR和BLE标准
- 蓝牙音频:CVSD和SBC音频低功耗:10uA
- 工作电流:80mA(平均)
- 频率范围:2.4~2.5GHz
- 支持Arduino一键下载
- 支持micropython
- 片上时钟:40MHz晶振、32.768KHz晶振
- 数字I/O x10(arduino默认)
- 模拟输入 x5(arduino默认)
- SPI x1(arduino默认)
- I2C x1(arduino默认)
- I2S x1(arduino默认)
- RGB_LED:5/D8
- 接口方式:FireBeetle V2系列兼容
- 工作温度:-40℃~+85℃
- 模块尺寸:25.4 × 60(mm)
- 安装孔尺寸:M2安装孔 孔径2.0mm
5.外观指示图
- Type-C: USB接口:4.75v-5.5v
- PH2.0锂电池接口:3.5-4.2v
- 2/D9 LED灯:使用2/D9引脚控制的LED灯
- 充电指示灯:指示充电方式的红色LED,通过三种方式指示充电状态:1、充满电或未充电时熄灭2、充电时常亮3、USB供电,未连接锂电池时高频闪烁
- RST复位引脚:单击复位按钮,将程序复位
- 5/D8 WS2812指示灯:使用5/D8引脚控制的WS2812RGB灯珠
- 低功耗焊盘:此焊盘专为低功耗设计,默认为连接状态,使用小刀轻轻刮断中间的细线即可断开,断开后可降低500μA静态功耗,通过程序控制主控进入睡眠模式后可将功耗降低至13μA。注意:焊盘断开后仅USB方式供电可驱动RGB灯
- GDI显示接口:DFRobot专用显示屏接口,详情后文GDI显示接口
- ESP32模组:乐鑫公司推出的最新ESP32-E模组
- 按钮:连接27/D4 的按钮
6.引脚布局
6.1 概述
FireBeetle ESP32-E多达22个物理GPIO,其中34-39 仅用作输入管脚,其他的既可以作为输入又可以作为输出管脚。 所有逻辑电压均为3.3V
- Control :FireBeetle的使能以及复位引脚
- Touch:具有电容触摸功能的引脚
- Analog :具有模拟功能的引脚
- Port PIN: 芯片默认的物理引脚号,可直接使用此数字控制对应引脚
- IDE :在arduino IDE中,FireBeetle将引脚号码进行了重映射,您也可以直接使用此符号控制对应引脚
- RTC PIN: FireBeetle具有低功耗功能,在进入deepsleep模式下仅RTC pin可以被使用
- Power :FireBeetle将供电电源以及稳压后的3.3v电源通过引脚引出,方便用户使用
- GND :FireBeetle的公共地引脚
6.2 Power
- GND-这是所有电源和逻辑的公共接地
- VCC -这是USB/锂电池输入的正电压(5V-USB 供电时输出USB电压, 3.7V-锂电池供电时输出电池电压)
- 3V3-这是3.3V稳压器的输出,可以提供500mA峰值
6.3 Control
- RST - 与ESP32的复位引脚相连,可将程序复位
- EN - 这是3.3V稳压器的使能引脚。它已上拉,因此接地能够禁用3.3V稳压器
6.4 GPIO
- D2至D13-这些都是通用引脚,通常我们将其当作数字引脚使用,也可以使用其复用功能。
- A0至A4-这些都是模拟输入引脚,其中A0-A3仅能作为输入引脚使用。
- SDA -I2C(线)数据引脚。
- SCL -I2C(线)时钟引脚。
- SCK / MOSI / MISO-这些是硬件SPI引脚,您可以将它们用作日常的GPIO引脚(但建议使其保持空闲状态,因为它们最适合用于高速SPI硬件)。
6.5 UART
ESP32共有2个UART端口, 其中UART0用于与PC通信
串口名 | Arduino | TX | RX |
---|---|---|---|
UART0 | Serial | pin1 | pin3 |
UART2 | Serial2 | pin17 | pin16 |
7. 首次使用
7.1 Arduino环境配置
当您首次使用FireBeetle主控,您需要了解以下步骤
- 添加IDE中的json链接
- 下载主控的核心
- 选择开发板以及串口
- 打开示例程序并烧录
- 了解串口监视器
Arduino IDE 编译环境配置
- 配置URL网址到Arduino IDE
打开Arduino IDE,点击File->Preferences,如下图所示:
在新打开的界面中,点击如下图红色圆圈中的按钮
将如下链接地址复制到新弹出的对话框中:http://download.dfrobot.top/FireBeetle/package_DFRobot_index.json
点击OK - 更新板卡
打开Tools->Board:->Boards Manager...,如下图所示:
Boards Manager会自动更新板卡,如下图所示:
更新完成后,会在列表中看到FireBeetle-ESP32主板(现已更新至0.0.8版本),点击安装:
安装完成后,列表会显示已经安装FireBeetle-ESP32主板,如下图所示:
- 配置URL网址到Arduino IDE
打开Arduino IDE,点击File->Preferences,如下图所示:
7.2 Blink
如果这是您第一次接触arduino,此例程将是您的第一个程序
烧录此程序您将看到LED规律闪烁
FireBeetle Board-ESP32默认Blink灯连接到D9/2
选择主板与端口
点击Tools->Board:,选择FireBeetle-ESP32-E
点击Port选择对应的串口
编写程序
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
- 将以上程序粘贴到程序框中
- 点击箭头等待程序编译并烧录至开发板
烧录成功
- 如图所示及烧录成功
7.3 蓝牙基础示例
ESP32具有蓝牙功能,此例程将示范使用两块FireBeetle ESP32-E进行蓝牙透传。
通过其中一块ESP32向另一块ESP32发送数据,是使用蓝牙无线通信的最基本模型。
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
//
//This example demostrates master mode bluetooth connection and pin
//it creates a bridge between Serial and Classical Bluetooth (SPP)
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
//
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "ESP32test";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
void setup() {
Serial.begin(115200);
//SerialBT.setPin(pin);
SerialBT.begin("ESP32master", true);
//SerialBT.setPin(pin);
Serial.println("The device started in master mode, make sure remote BT device is on!");
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
connected = SerialBT.connect(name);
//connected = SerialBT.connect(address);
if(connected) {
Serial.println("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
SerialBT.connect();
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
结果
通过主机串口发送:“我是主机” 从机串口显示“从机接收:我是主机” 通过从机串口发送:“我是从机” 主机串口显示“主机接收:我是从机”
成员函数
SerialBT.begin()
说明:初始化蓝牙模块。SerialBT.disconnect()
说明:断开设备连接
返回值:ture/falseSerialBT.connect()
说明:连接设备 返回值:ture/falseSerialBT.available() 说明:判断蓝牙是否接收到数据
SerialBT.read() 说明:读取蓝牙接收的信息 返回值:string
SerialBT.write()
说明:通过蓝牙发送信息
7.4 WIFI基础示例
ESP32具有WIFI功能,以下示例使用ESP32创建了一个wifi服务器,使用客户端连接到该服务器,遥控LED的亮灭
/*
WiFiAccessPoint.ino 创建了一个wifi热点并提供了一个web服务
Steps:
1. 连接到这个wifi "yourAp"
2. 访问 http://192.168.4.1/H 来开灯或者访问http://192.168.4.1/L 来关灯
OR
Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
// 设置你的wifi与密码
const char *ssid = "esp32";
const char *password = "";
WiFiServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);//将LED引脚设置为输出模式
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// 配置wifi以及获取IP地址.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
结果
使用手机连接该wifi,通过浏览器访问192.168.4.1,如图所示显示ip地址为192.168.4.1,服务已开启
使用浏览器访问该ip地址得到如下图所示
尝试分别点击链接控制LED吧
成员函数
begin() 说明:wifi模块初始化
softAP(ssid,password) 说明:将WiFi配置为AP模式,并设置名称以及密码 参数:
- ssid:AP模式的wifi名字
- password:ap模式的wifi密码
disconnect() 说明:断开客户端连接
connect() 说明:客户端连接
read() 说明:读取wifi接受到的数据
write() 说明:通过wifi发送数据
7.5 霍尔传感器
ESP32自带霍尔传感器 , 当有磁场靠近时,会显示正负值
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.printf("hallRead:%d\n",hallRead());
delay(200);
}
结果
成员函数
- hallRead()
说明:模组内部霍尔传感器读取值
返回值:返回0-255整数,N极为正值,S极为负值,当磁场越强绝对值越大
7.5 电容按键
ESP32提供了电容触摸传感器的功能, 共有T0,T2~T9 共 9个touch传感器可用.分别对应引脚4、2、15、13、12、14、27、33、32. 无需设置PinMode,touchRead()返回值为0~255. 触摸强度越大,返回值越小。 烧录此例程,将使用4/D12引脚作为触摸按键,并通过串口监视器返回触摸值
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.printf("touch:%d\n",touchRead(4));
}
结果
成员函数
- TouchRead(pin)
说明:无需设置PinMode
参数:
- pin:需要调用的触摸传感器引脚
- 返回值* :返回值为0~255. 触摸强度越大,返回值越小。
7.6 GDI显示接口
此接口为DFRbot专用GDI显示屏接口,使用18pin-FPC线连接,单线材连接屏幕,为您提供最简捷的屏幕使用方式。
以下是GDI接口使用的引脚列表
FPC PINS | FireBeetle ESP32 PINS | Description |
---|---|---|
VCC | 3V3 | 3.3V |
BLK(PWM调光) | 12/D13 | 背光 |
GND | GND | GND |
SCLK | 18/SCK | SPI时钟 |
MOSI | 23/MOSI | 主机输出,从机输入 |
MISO | 19/MISO | 主机输入,从机输出 |
DC | 25/D2 | 数据/命令 |
RES | 26/D3 | 复位 |
CS | 14/D6 | TFT片选 |
SDCS | 13/D7 | SD卡片选 |
FCS | 0/D5 | 字库 |
TCS | 4/D12 | 触摸 |
SCL | 22/SCL | I2C时钟 |
SDA | 21/SDA | I2C数据 |
INT | 16/D11 | INT |
BUSY-TE | 17/D10 | 防撕裂引脚 |
X1 | NC | 自定义引脚1 |
X2 | NC | 自定义引脚2 |
使用FPC链接屏幕时根据GDL demo配置所需对应的引脚号即可,通常只需要根据不同主控配置三个引脚 支持GDI的显示屏:
- 1.54" 240x240 IPS广视角TFT显示屏
- 2.0" 320x240 IPS广视角TFT显示屏
- 2.8" 320x240 IPS TFT电阻触摸显示屏
- 3.5" 480x320 IPS TFT电容触摸显示屏
/*ESP32 and ESP8266*/
#elif defined(ESP32) || defined(ESP8266)
#define TFT_DC 25
#define TFT_CS 14
#define TFT_RST 26
具体使用方式请参考,GDL显示屏wiki:https://wiki.dfrobot.com.cn/_SKU_DFR0664_2.0_240x320_LCD
NOTE: 请不要重复使用相关引脚
7.7 RGB_LED
FastLED是一款功能强大却简单易用的可以控制WS2812、LPD8806等LED光带的Arduino第三方库。目前FastLED是公认的Arduino开发者应用最为广泛的LED控制库之一。 FireBeetle将FastLED集成在核心库中,以下代码将演示如何使用5/D8连接的RGB_LED
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 1
// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN 5
#define CLOCK_PIN 13
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
// Uncomment/edit one of the following lines for your leds arrangement.
// ## Clockless types ##
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // GRB ordering is assumed
}
void loop() {
//灯珠显示红色
leds[0] = CRGB::Red;
FastLED.show();
delay(500);
// 灯珠显示绿色
leds[0] = CRGB::Green;
FastLED.show();
delay(500);
// 灯珠显示蓝色
leds[0] = CRGB::Blue;
FastLED.show();
delay(500);
}
成员函数
leds[0] = CRGB::Red 说明: 将第0颗灯珠设置为红色
FastLED.show() 说明:点亮或更新灯珠颜色
leds[0].r = 255 说明:将光带上第1个LED灯珠的RGB数值中R数值设置为255
leds[0].g = 125 说明:将光带上第1个LED灯珠的RGB数值中G数值设置为125
leds[0].b = 0 说明:将光带上第1个LED灯珠的RGB数值中B数值设置为0
7.8 sleep模式
在sleep模式下可以将功耗降低至10μA(断开低功耗焊盘),以下demo将演示定时进入sleep模式
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void setup(){
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
/*
First we configure the wake up source
We set our ESP32 to wake up every 5 seconds
*/
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
/*
Next we decide what all peripherals to shut down/keep on
By default, ESP32 will automatically power down the peripherals
not needed by the wakeup source, but if you want to be a poweruser
this is for you. Read in detail at the API docs
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
Left the line commented as an example of how to configure peripherals.
The line below turns off all RTC peripherals in deep sleep.
*/
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
//Serial.println("Configured all RTC Peripherals to be powered down in sleep");
/*
Now that we have setup a wake cause and if needed setup the
peripherals state in deep sleep, we can now start going to
deep sleep.
In the case that no wake up sources were provided but deep
sleep was started, it will sleep forever unless hardware
reset occurs.
*/
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
//This is not going to be called
}
成员函数
esp_sleep_get_wakeup_cause()
说明:函数可用于检查哪个唤醒源触发了从睡眠模式中唤醒。esp_deep_sleep_start()
说明:进入睡眠模式esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR)
说明:函数可用于使用定时器启用深度睡眠唤醒
8.Arduino基础教程
8.1 GPIO
数字IO
digitalRead(pin)
说明:从指定的数字引脚读取值HIGH或LOW
参数:- pin:您要读取的Arduino引脚号
digitalWrite(pin,value)
说明:将HIGH或LOW值写入数字引脚
参数:- pin:Arduino引脚号。
- value:HIGH或LOW。
pinMode(pin, mode)
说明:将指定的引脚配置为充当输入或输出
参数:- pin:用于设置模式的Arduino引脚号。
- mode:INPUT,OUTPUT,或INPUT_PULLUP。
按键控制LED
模拟IO
AnalogRead(pin)
说明:从指定的模拟引脚读取值。
参数- pin:要读取的模拟输入引脚的名称
AnalogReference(type) 说明:配置用于模拟输入的参考电压 参数:
- type
8.2 PWM
ledcSetup(ledChannel, freq, resolution) ** **说明:将模拟值(PWM wave)写入引脚。可用于以不同的亮度点亮LED或以不同的速度驱动电动机。调用之后ledcWrite(),该引脚将生成指定占空比的稳 定矩形波,直到同一引脚上的下一次调用ledcWrite()或调用digitalRead()或digitalWrite()为止 参数:
pin:要写入的ESP32引脚。允许的数据类型:int。
channel:ESP32 有一个 LED PWM 控制器,具有从0到15共16个独立的通道,可以配置为生成具有不同属性的 PWM 信号。
ledcAttachPin(pin, channel) ** **说明:将模拟值(PWM wave)写入引脚。可用于以不同的亮度点亮LED或以不同的速度驱动电动机。调用之后ledcWrite(),该引脚将生成指定占空比的稳 定矩形波,直到同一引脚上的下一次调用ledcWrite()或调用digitalRead()或digitalWrite()为止 参数:
pin:要写入的ESP32引脚。允许的数据类型:int。
channel:ESP32 有一个 LED PWM 控制器,具有从0到15共16个独立的通道,可以配置为生成具有不同属性的 PWM 信号。
ledcWrite(pin, dutycycle) ** **说明:生成 PWM 信号的通道和占空比作为参数 参数:
8.3 串口
Serial.begin(speed) 说明:设置以每秒比特数(波特)为单位的串行数据传输的数据速率。要与串行监视器通信,请确保使用其屏幕右下角菜单中列出的波特率之一。 参数:
- speed:以每秒位数(波特)为单位。允许的数据类型:long。
Serial.available()
说明:检测串口是否有数据输入
输入
Serial.read() 说明:读取传入的串行数据。
Serial.peek()
说明:返回传入串行数据的下一个字节(字符),而不将其从内部串行缓冲区中删除。
输出
- Serial.print()
说明:串口打印 - Serial.println()
说明:串口打印并自动换行
软件串口
- SoftwareSerial()
运行时间函数
- micros() 说明:返回自Arduino开发板开始运行当前程序以来的微秒数
- millis() 说明:返回自Arduino开发板开始运行当前程序以来经过的毫秒数
延时函数
delay(ms) 说明: 参数:ms:要暂停的毫秒数。允许的数据类型:unsigned long。
delayMicroseconds(us) 说明:暂停程序一段由参数指定的时间(以微秒为单位)。一毫秒有一千微秒,一秒有一百万微秒。 参数:us:要暂停的微秒数。允许的数据类型:unsigned int。
8.3 调声函数
tone(pin, frequency, duration)
说明:在引脚上生成指定频率(和50%占空比)的方波。可以指定一个持续时间,否则wave继续直到调用noTone()为止。该引脚可以连接到压电蜂鸣器或其他扬声器以播放音调。
参数:- pin:在其上生成音调的Arduino引脚。
- frequency:音调的频率,以赫兹为单位。允许的数据类型:unsigned int。
- duration:提示音的持续时间(以毫秒为单位)(可选)。允许的数据类型:unsigned long。
noTone(pin) 说明:停止由触发的方波的生成tone()。如果没有声音产生,则无效。 参数:
pin:停止产生音调的Arduino引脚
frequency:音调的频率,以赫兹为单位。允许的数据类型:unsigned int。
duration:提示音的持续时间(以毫秒为单位)(可选)。允许的数据类型:unsigned long。
8.4 中断
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) 说明:外部中断 参数:
- pin:Arduino引脚号。
- ISR:发生中断时要调用的ISR;此函数必须不带任何参数,并且不返回任何内容。有时将此功能称为中断服务程序。
- mode:定义何时触发中断。预定义了四个常数作为有效值:
detachInterrupt(digitalPinToInterrupt(pin))
说明:关闭给定的中断。 参数:- pin:需要禁用的中断引脚
interrupts() 说明:重新启用中断(在被noInterrupts()禁用之后。中断允许某些重要任务在后台发生并默认启用。当禁用中断时,某些功能将不起作用,并且传入通信可能会被忽略。可能会稍微破坏代码的时序,但是对于特别关键的代码部分可能会被禁用。
noInterrupts() 说明:禁用中断(您可以使用来重新启用它们interrupts())。中断允许某些重要任务在后台发生,并且默认情况下启用。禁用中断时,某些功能将不起作用,并且传入的通信可能会被忽略。但是,中断可能会稍微破坏代码的时序,并且可能会在代码的特别关键的部分将其禁用。
8.5 I2C
I2C主从机与引脚
与串口的一对一通信方式不同,总线通信通常有主机(Master)和从机(Slave)之分。通信时,主机负责启动和终止数据传送,同时还要输出时钟信号;从机会被主机寻址,并且响应主机的通信请求,通信速率的控制由主机完成,主机会通过SCL引脚输出时钟信号供总线上的所有从机使用。同时,I2C是一种半双工通信方式,即总线上的设备通过SDA引脚传输通信数据,数据的发送和接收由主机控制,切换进行。ESP32有两个I2C控制器(也称为端口),负责处理两条I2C总线上的通信。每个I2C控制器都可以作为主机或从机运行。引脚21 默认的SDA, 引脚22是默认的SCL
begin(address) 说明:初始化I2C连接,并作为主机或者从机设备加入I2C总线。 参数:
read() 说明:在主机中,当使用requestFrom()函数发送数据请求信号后,需要使用read()函数来获取数据;在从机中需要使用该函数读取主机发送来的数据。 参数:
available() 说明:返回接收到的字节数。 参数:
write:() 说明:当为主机状态时,主机将要发送的数据加入发送队列;当为从机状态时机发送数据至发起请求的主机。 参数:
- valuc:以单字节发送。
- string:以一系列字节发送。
- data:以字节形式发送数组。
requestFrom(address,quantity) 说明:主机向从机发送数据请求信号,使用requestFrom()后,从机端可以使用onRequest()注册一个事件用以响应主机的请求;主机可以通过available()和 read()函数读取这些数据。 参数:
- quantity,请求的字节数。
- address,设备的地址。
beginTransmission(address) 说明:设定传输数据到指定地址的从机设备。随后可以使用write()函数发送数据,并搭配endTransmission(函数结束数据传输。 参数:
- address:设备的地址
endTransmission 说明:结束数据传输 参数:
- stop:boolean型值,当其值为true时将发送一个停止信息,释放I2C总线,当没有填写 stop参数时,等效使用true;当为 false时,将发送一个重新开始信息﹐并继续保持I2C 总线的有效连接
onReceive() 说明:8. onReceive()
功能:该函数可在从机端注册一个事件,当从机收到主机发送的数据时即被触发。
语法:Wire.onReceive( handler
参数:- handler,当从机接收到数据时可被触发的事件。该事件带有一个int型参数(从主机读到的字节数)且没有返回值,如void myHandler(int numBytes)。
onRequest(handler)
说明:注册一个事件,当从机接收到主机的数据请求时即被触发。语法:Wire.onRequest(handler)
参数:- handler,可被触发的事件。该事件不带参数和返回值,如 void myHandler()。
8.6 SPI
SPI引脚 ESP32有四个SPI外设,分别为SPI0、SPI1、HSPI和VSPI。 SPI0是专用于Flash的缓存,ESP32将连接的SPI Flash设备映射到内存中。 SPI1和SPI0 使用相同的硬件线,SPI1用于写入flash芯片。 HSPI和VSPI可以任意使用。 SPI1、HSPI和VSPI共有三条片选线,因此作为SPI主机允许ESP32 至多驱动三个SPI设备。
begin()
说明:初始化SPI通信,调用该函数后,SCK.MOSI,SS引脚将被设置为输出模式,且 SCK和 MOSI引脚被拉低,SS引脚被拉高。end()
说明:关闭SPI总线通信。setBitOrder()
说明:设置传输顺序。setBitOrder()
说明:设置通信时钟。时钟信号由主机产生,从机不用配置。但主机的SPI 时钟频率应该在从机允许的处理速度范围内。
9.进阶教程
9.1 使用SD库
SD类
begin(cspin)
说明:初始化SD卡库和 SD卡。当使用SD.begin()时﹐默认将Arduino SPI的SS引脚连接到SD卡的CS使能选择端;也可以使用begin(cspin)指定一个引脚连接到SD卡的CS使能选择端,但仍需保证 SPI 的SS引脚为输出模式,否则SD卡库将无法运行。
参数:cspin:连接到SD 卡CS端的Arduino引脚。
返回值: boolean型值,为 true表示初始化成功;为 false表示初始化失败。exists()
说明:检查文件或文件夹是否存在于SD卡中。语法:SD. exists( filename)
参数: filename:需要检测的文件名。其中可以包含路径,路径用“/”分隔。
返回值: boolean型值,为 true表示文件或文件夹存在;为false表示文件或文件夹不存在。open()
说明:打开SD卡上的一个文件。如果文件不存在,且以写入方式打开,则Arduino会创建一个指定文件名的文件。(所在路径必须事先存在)
语法:SD.open( filename)SD.open(filename,mode)
参数: filename:需要打开的文件名。其中可以包含路径,路径用“/”分隔。
mode(可选):打开文件的方式,默认使用只读方式打开。也可以使用以下两种方式打开文件:
FILE_READ:只读方式打开文件;FILE_WRITE,写入方式打开文件。
返回值:返回被打开文件对应的对象﹔如果文件不能打开,则返回false。
FILE_WRITE,写入方式打开文件。
返回值:返回被打开文件对应的对象;如果文件不能打开,则返回false。remove()
说明:从SD卡移除一个文件。如果文件不存在,则函数返回值是不确定的,因此在移除文件之前,最好使用SD. exists(filename)先检测文件是否存在。
语法:SD. remove( filename)
参数: filename,需要移除的文件名。其中可以包含路径,路径用“!”分隔。
返回值: boolean型值,为true表示文件移除成功;为false表示文件移除失败。mkdir(filename)
说明:创建文件夹。
参数:- filename,需要创建的文件夹名。其中可以包含路径,路径用“/”分隔。返回值: boolean型值,为 true表示创建成功;为false表示创建失败。
rmdir(filename)
说明:移除文件夹。被移除的文件夹必须是空的。语法:SD.rmdir( filename)
参数:- filename,需要移除的文件夹名。其中可以包含路径,路径用“/”分隔。
- 返回值* : boolean型值,为 true表示移除成功;为false表示移除失败。
File类
File类提供了读/写文件的功能,该类的功能与之前使用的串口相关函数的功能非常类似。其成员函数如下。
available()
说明:检查当前文件中可读数据的字节数。语法:file. available()
参数:- file:一个 File类型的对象。返回值:可用字节数。
close()
说明:关闭文件,并确保数据已经被完全写入SD卡中。语法:file. close()
参数:- file:一个 File类型的对象。返回值:无。
flush()
说明:确保数据已经写入SD卡。当文件被关闭时,flush()会自动运行。语法: file.flush
参数:- file:一个File类型的对象。返回值:无。
peek()
说明:读取当前所在字节,但并不移动到下一个字节。
参数- file:一个 File类型的对象。
- 返回值*:下一个字节或者下一个字符。如果没有可读数据,则返回一1。
position( )
说明:获取当前在文件中的位置(即下一个被读/写的字节的位置)。语法:file. position()
参数:- file:一个 File类型的对象。返回值:在当前文件中的位置。
print()
说明:输出数据到文件。要写入的文件应该已经被打开,且等待写入。语法:file. print(data)file. print(data,BASE)
参数:- file:一个 File类型的对象。
- data,要写入的数据(可以是类型char, byte ,int .long 或 String)。
- BASE(可选),指定数据的输出形式:BIN(二进制);oCT(八进制);DEC(十进制);HEX(十六进制)。 返回值;发送的字节数。
println()
说明:输出数据到文件,并回车换行。语法:file. println(data)file,println(data,BASE)
参数:- file:一个 File类型的对象。
- data:要写入的数据(类型可以是char , byte , int , long或String)。
- BASE(可选),指定数据的输出形式:BIN(二进制>;OCT(八进制);DECK十进制;HEX(十六进制)返回值;发送的宇节数。
seek()
说明;跳转到指定位置。该位置必须在·到该文件大小之间。语法:file. seek( pos)
参数:- file:一个 File类型的对象。pos,需要查找的位置。
- 返回值*: boolean型值,为 true表示跳转成功;为false表示跳转失败。
size()
说明:获取文件的大小。语法:filue. size()
参数:- file:一个File类型的对象。
- 返回值*:以字节为单位的文件大小。
read()
说明:读取1B数据。语法:file.read参数:- file:一个 File类型的对象。
- 返回值*:下一个字节或者字符;如果没有可读数据,则返回一1。
write()
说明;写入数据到文件。语法:file. write(data)file. write(buf,len)
参数:- file:一个File类型的对象。
- data:要写入的数据,类型可以是 byte ,char或字符串(char * ) 。buf,一个字符数组或者宁节数据。
- len:buf数组的元素个数。
- 返回值*:发送的字节数。
isDirectory()
说明:判断当前文件是否为目录。语法:file.isDirectory()
参数:- file:一个File类型的对象。
- 返回值*: boolcan型值;为 true表示是目录;为 false表示不是目录。
openNextFile()
说明:打开下一个文件。语法:file.openNextFile()
参数:- file:一个 File类型的对象。
- 返回值*:下一个文件对应的对象。
rewindDirectory()
说明:回到当前目录中的第一个文件。语法:file.rewindDirectory()
参数: file,一个File类型的对象。返回值:无。
9.2红外遥控
IRrecv类
IRrecv类可用于接收红外信号并对其解码。在使用该类之前,需要实例化一个该类的对象。其成员函数如下。
IRrecv()
说明: IRrecv类的构造函数。可用于指定红外一体化接收头的连接引脚。语法:IRrecvobject(recvpin)
参数:- object,用户自定义的对象名。
- recvpin,连接到红外一体化接收头的Arduino引脚编号。
enableIRIn()
说明 *:初始化红外解码。语法:IRrecv. enableIRIn()
*参数:- IRrecv,一个IRrecv类的对象。
decode()
说明;检查是否接收到编码。语法: IRrecv.decode( &.results)
参数:- IRrecv,一个IRrecv类的对象。
- results.一个decode_results 类的对象。
- 返回值*:int型值,为О表示接收到编码;为1表示没有接收到编码。
resume()
说明;接收下一个编码。语法:IRrecv.resume()
参数:- IRrecv,一个IRrecv类的对象。返回值:无。
IRsend类
IRsend类可以对红外信号编码并发送。
IRsend(object)
说明:IRsend类的构造函数。
参数: object,一个IRsend类的对象。sendNEC()
说明:以NEC编码格式发送指定值。语法:IRsend.sendNEC(data,nbits)
参数:- IRsend,一个IRsend类的对象。data,发送的编码值。
- nbits ,编码位数。
sendSony()
说明:以 Sony编码格式发送指定值。语法: IRsend.sendSony( data,nbits)
参数- IRsend:一个IRsend类的对象。data,发送的编码值。
- nbits:编码位数。
sendRaw()
说明:发送原始红外编码信号。语法 : IRsend.sendRaw( buf,len,hz)
object:- IRsend:一个IRsend类的对象。buf ,存储原始编码的数组。len,数组长度。
- hz:红外发射频率。
9.3WIFI
ESP32同时支持STA以及AP模式的WiFi连接。
- STA 模式:ESP32模块通过路由器连接互联网,手机或电脑通过互联网实现对设备的远程控制。
- AP 模式:ESP32模块作为热点,实现手机或电脑直接与模块通信,实现局域网无线控制。
- STA+AP 模式:两种模式的共存模式,即可以通过互联网控制可实现无缝切换,方便操作。
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
HTTPClient http;
const char* ssid="dfrobotOffice";
const char* password="dfrobot2011";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 28800;
const int daylightOffset_sec = 0;
DynamicJsonDocument doc(1024);
DynamicJsonDocument doc1(1024);
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtian time");
return ;
}
Serial.println(&timeinfo,"%A, %B %d %Y %H:%M:%S");
}
void printLocalWeather(){
http.begin("http://www.weather.com.cn/data/cityinfo/101270101.html");
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK){
String pageData = http .getString();
//Serial.println(pageData);
deserializeJson(doc,pageData);
JsonObject obj = doc.as<JsonObject>();
String weatherInfo = obj["weatherinfo"];
deserializeJson(doc1,weatherInfo);
JsonObject obj1 = doc1.as<JsonObject>();
String city = obj1["city"];
String temp1 = obj1["temp1"];
String temp2 = obj1["temp2"];
String weather = obj1["weather"];
String cityInfo ="地点:"+ city;
String tempInfo =" 温度: " + temp1 + "~" + temp2;
String cityWeatherinfo = " 天气状况: " + weather;
Serial.println("获得天气情况如下:");
printLocalTime();
Serial.print(cityInfo);
Serial.print(tempInfo);
Serial.println(cityWeatherinfo);
}else{
Serial.println("GET ERR");
}
http.end();
}
void setup() {
Serial.begin(115200);
Serial.printf("Connecting to %s",ssid);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// printLocalWeather();
}
void loop() {
if(WiFi.status() == WL_CONNECTED){
printLocalWeather();
}else{
Serial.println("WiFi Disconnect");
}
}
说明:本Demo实现了通过WiFi功能获取网络时间以及通过访问国家气象局提供的http://www.weather.com.cn/datalcityinfo/101010100.html来获取天气情况,本接口中“101010100"为城市代码。
注意:该例程需要下载ArduinoJson库,下载方式如下图
结果
WiFiClass
- begin()
说明:开启WiFi并连接到指定的无线网络 - status()
说明:获取WiFi状态
HTTPClient
- begin()
说明:对传入网址信息参数进行解析 - GET()
说明:向服务器发送get请求 - end() 说明:结束本次连接
DynamicJsonDocument
- deserializeJson()
说明:解析Json - as()
说明:获取顶节点,并把它转成T类型
9.4 蓝牙
本demo建立了一个BlE_Server,可以为客户端提供数据和发送通知,当服务器接收到客户端发来的数据时会将收到的数据以通知的方式发送给客户端。即本Demo中BLE服务端提供的通知服务只作用于将接收到的客户端数据的回传。
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "DFCD0001-36E1-4688-B7F5-EA07361B26A8"
#define CHARACTERISTIC1_UUID "DFCD000A-36E1-4688-B7F5-EA07361B26A8"
bool deviceConnected = false;
BLEServer *pServer;
BLEService *pService;
BLECharacteristic* pCharacteristic;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++){
Serial.print(value[i]);
}
Serial.println();
Serial.println("*********");
pCharacteristic->notify();
}
}
};
void setupBLE()
{
BLEDevice::init("DFRobot_ESP32"); //创建BLE设备
pServer = BLEDevice::createServer(); //创建BLE服务器
pServer->setCallbacks(new MyServerCallbacks()); //设置服务器的回调函数
pService = pServer->createService(SERVICE_UUID); //创建BLE服务
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC1_UUID,
BLECharacteristic::PROPERTY_READ|
BLECharacteristic::PROPERTY_NOTIFY|
BLECharacteristic::PROPERTY_WRITE); //创建服务的特征值
pCharacteristic->setCallbacks(new MyCallbacks()); //设置特征值的回调函数
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Hello DFRobot");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void setup() {
Serial.begin(115200);
setupBLE();
}
void loop() {
delay(3000);
}
结果
本demo使用FireBeetle ESP32-E作为BLE的服务端,客户端可以是手机。在手机上安装蓝牙助手,与ESP32模块建立BLE连接。这里对iPhone手机提供的Light Blue操作进行演示,Android系统的手机也有类似的蓝牙软件助手。
手机客户端操作如下:
BLEDevice
init()
说明:创建一个BLE设备createServer()
说明:创建BLE服务器
BLEServer
createService()
说明:创建一个BLE服务setCallbacks()
说明:创建服务器回调函数start()
说明:开启服务器getAdvertising()
说明:配置广播功能
BLEService
- createCharateristic()
说明:创建服务的特征值
### BLECharacteristic
- setCallbacks()
说明:设置特征值回调函数 - addDescriptor()
说明: - setValue()
说明:设置特征值的value值 - getValue()
说明:获得特征值的value值 - notify()
说明:发通知
BLEAdvertising
- start()
说明:开启广播
10.阿里云IoT
什么是阿里云
阿里云IoT致力于实现万物互联的美好世界,为生态合作伙伴提供基于云端一体化、安全物联网基础平台等,在通过该平台高效连接,管理设备的同时,其开放能力使合作伙伴更高效、低成本地构建各种创新的物联网应用场景。 阿里云物联网平台为设备提供安全可靠的连接通信能力,向下连接海量设备,支撑设备数据采集上云;向上提供云端API,指令数据通过API调用下发至设备端,实现远程控制。 此外阿里云IoT还提供了丰富的开发服务,用户可以直接在该平台上搭建Web可视化、移动应用、服务开发等开发服务,这降低了物联网项目开发的难度,有了它,用户无需任何专业的开发技巧也可开发自己的项目。
智能灯光
图片
所需元件
- FireBeetle ESP32-E x1
- FireBeetle Gravity IO扩展板 x1
下载arduino阿里云库
在阿里云创建产品
- 创建产品
- 定义产品功能
- 为产品添加设备
- 导入产品
- 创建产品
烧录arduino程序
#include <WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include "DFRobot_Aliyun.h" #define BEDROOD_LIGHT D2 /*配置WIFI名和密码*/ const char * WIFI_SSID = "WIFI_SSID"; const char * WIFI_PASSWORD = "WIFI_PASSWORD"; /*配置设备证书信息*/ String ProductKey = "you Product Key"; String ClientId = "12345";/*自定义ID*/ String DeviceName = "you Device Name"; String DeviceSecret = "you Device Secret"; /*配置域名和端口号*/ String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; uint16_t PORT = 1883; /*需要操作的产品标识符*/ String Identifier = "you Identifier"; /*需要上报和订阅的两个TOPIC*/ const char * subTopic = "you sub Topic";//****set const char * pubTopic = "you pub Topic";//******post DFRobot_Aliyun myAliyun; WiFiClient espClient; PubSubClient client(espClient); static void openLight(){ digitalWrite(BEDROOD_LIGHT, HIGH); } static void closeLight(){ digitalWrite(BEDROOD_LIGHT, LOW); } void connectWiFi(){ Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID,WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP Adderss: "); Serial.println(WiFi.localIP()); } void callback(char * topic, byte * payload, unsigned int len){ Serial.print("Recevice ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < len; i++){ Serial.print((char)payload[i]); } Serial.println(); StaticJsonBuffer<300> jsonBuffer; JsonObject& root = jsonBuffer.parseObject((const char *)payload); if(!root.success()){ Serial.println("parseObject() failed"); return; } const uint16_t LightStatus = root["params"][Identifier]; if(LightStatus == 1){ openLight(); }else{ closeLight(); } String tempMseg = "{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":"+(String)LightStatus+"},\"method\":\"thing.event.property.post\"}"; char sendMseg[tempMseg.length()]; strcpy(sendMseg,tempMseg.c_str()); client.publish(pubTopic,sendMseg); } void ConnectAliyun(){ while(!client.connected()){ Serial.print("Attempting MQTT connection..."); /*根据自动计算的用户名和密码连接到Alinyun的设备,不需要更改*/ if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){ Serial.println("connected"); client.subscribe(subTopic); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); pinMode(BEDROOD_LIGHT,OUTPUT); /*连接WIFI*/ connectWiFi(); /*初始化Alinyun的配置,可自动计算用户名和密码*/ myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); client.setServer(myAliyun.mqtt_server,PORT); /*设置回调函数,当收到订阅信息时会执行回调函数*/ client.setCallback(callback); /*连接到Aliyun*/ ConnectAliyun(); /*开机先关灯*/ closeLight(); /*上报关灯信息*/ client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\": {\""+Identifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str()); } void loop(){ if(!client.connected()){ ConnectAliyun(); } client.loop(); }
11.IFTTT
什么是IFTTT
IFTTT是If This Then That的缩写,事实上是让你的网络行为能够引发连锁反应、让你使用更为方便,其宗旨是“Put the internet to work for you”(让互联网为你服务)。IFTTT旨在帮助人们利用各网站的开放API,监控用户设置的 Trigger,如果 Trigger 被触发则执行用户设置的 Action ,通常我们可以创建 N 个 Applet ,来满足我们的各种自动化需求。
数据邮件
下载IFTTT库
配置IFTTT
创建Trigger
- 登录IFTTT,点击Create创建你的小程序。
- 点击This创建输入Webhooks
- 点击进入Webhooks,选择“Receive a web request”。
- 然后创建我们的Event Name“message”,点击Create trigger完成“this”的设置。
- 创建Action
- 设置好触发“this”后,继续添加动作指令“that”
- 点击“That”创建搜索并点击"Email",然后选择"Send me an email"。
- 绑定接收邮件的Email,输入邮箱地址,点击"send PIN"。
- 将邮箱中收到的验证码复制进来,然后点击“connect”
- Email绑定成功后,就可以编辑邮件内容了,在"subject"中输入邮件主题,在"Body"中输入邮件内容,然后点击"Create action"完成创建。
- 点击"Continue"后,进行Review,然后点击"Finish"完成创建。
- 登录IFTTT,点击Create创建你的小程序。
找到IFTTT_Key
- 点击"My services"-"Webhooks"-"Documentation",在图示位置复制你的密钥
- 点击"My services"-"Webhooks"-"Documentation",在图示位置复制你的密钥
烧录arduino程序
打开内置的例程
#include <WiFi.h> #include <HTTPClient.h> //Configure WiFi name and password char *WIFI_SSID = "WIFI_SSID"; char *WIFI_PASSWORD = "WIFI_PASSWORD"; //Configure IFTTT char *IFTTT_ENVENT = "Your_Event"; char *IFTTT_KEY = "Your_Key"; //IFTTT Send Message char *IFTTT_VALUE_1 = "Value1"; char *IFTTT_VALUE_2 = "Value2"; char *IFTTT_VALUE_3 = "Value3"; HTTPClient ifttt; unsigned long lastTime = 0; unsigned long timerDelay = 10000; void setup() { Serial.begin(115200); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Wifi Connect Success"); } void loop() { //Send an HTTP POST request every 10 seconds if ((millis() - lastTime) > timerDelay) { //Check WiFi connection status if(WiFi.status()== WL_CONNECTED){ ifttt.IFTTTBeging(IFTTT_ENVENT,IFTTT_KEY); int dataSendState = ifttt.IFTTTSend(IFTTT_VALUE_1,IFTTT_VALUE_2,IFTTT_VALUE_3); Serial.println(dataSendState);//Whether the printing data is sent successfully }else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } }
配置arduino程序中的参数
//Configure WiFi name and password char *WIFI_SSID = "WIFI_SSID";//输入WiFi名称 char *WIFI_PASSWORD = "WIFI_PASSWORD";//输入WiFi密码 //Configure IFTTT char *IFTTT_ENVENT = "Your_Event";//输入事件名称 char *IFTTT_KEY = "Your_Key";//输入IFTTT中找到的Key //IFTTT Send Message char *IFTTT_VALUE_1 = "Value1"; char *IFTTT_VALUE_2 = "Value2"; char *IFTTT_VALUE_3 = "Value3";//配置在邮件信息中的三个值
结果 在邮箱中收到由FireBeetle ESP32-E发出的数据
尺寸图
- pin脚间距:2.54mm
- 安装孔间距:
- 安装孔尺寸:2mm
- 主板尺寸:25.4.00mm×60.00mm
- 板厚:1.6mm
常见问题
驱动安装
FireBeetle-ESP32采用CH340串口芯片,在绝大部分的设备中,都可以免驱使用。若您发现插上设备,驱动没有自动安装,也可以自行手动安装:
Windows驱动安装——点击下载CH340驱动程序
Mac驱动安装——点击下载CH340驱动程序
更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。