以下是ESP32与ThingsBoard的连接的代码:
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#define THINGSBOARD_ENABLE_PROGMEM 0
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
#include <WiFiNINA_Generic.h>
#elif defined(ESP32) || defined(RASPBERRYPI_PICO) || defined(RASPBERRYPI_PICO_W)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#endif
#define THINGSBOARD_ENABLE_PSRAM 0
#define THINGSBOARD_ENABLE_DYNAMIC 1
#ifndef LED_BUILTIN
#define LED_BUILTIN 99
#endif
#include <ThingsBoard.h>
#include <HardwareSerial.h> //导入ESP32串口操作库,使用这个库我们可以把串口映射到其他的引脚上使用
#include <Arduino.h>
HardwareSerial MySerial_stm32(1);
unsigned short i;
char temp;
char buffer_1[10]; // 定义一个缓冲区,用于存储读取到的数据
char buffer_2[10]; // 定义一个缓冲区,用于存储读取到的数据
int j = 0; // 缓冲区索引,用于记录当前读取到的位置
char temperature;
char humidity;
float a;
float b;
char temp_buffer[10];
char humi_buffer[10];
constexpr char WIFI_SSID[] = "WiFi名称";
constexpr char WIFI_PASSWORD[] = "WiFi密码";
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
constexpr char TOKEN[] = "ThingsBoard的访问令牌";
// Thingsboard we want to establish a connection too
constexpr char THINGSBOARD_SERVER[] = "ThingsBoard的地址";
// MQTT port used to communicate with the server, 1883 is the default unencrypted MQTT port.
constexpr uint16_t THINGSBOARD_PORT = 1883U;
// Maximum size packets will ever be sent or received by the underlying MQTT client,
// if the size is to small messages might not be sent or received messages will be discarded
constexpr uint32_t MAX_MESSAGE_SIZE = 256U;
// Baud rate for the debugging serial connection.
// If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable
constexpr uint32_t SERIAL_DEBUG_BAUD = 115200U;
// Initialize underlying client, used to establish a connection
WiFiClient wifiClient;
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(wifiClient, MAX_MESSAGE_SIZE);
// Attribute names for attribute request and attribute updates functionality
constexpr char BLINKING_INTERVAL_ATTR[] = "blinkingInterval";
constexpr char LED_MODE_ATTR[] = "ledMode";
constexpr char LED_STATE_ATTR[] = "ledState";
constexpr char GATEWAY_DATA_ATTR[]="temp_humi_Mode";
// Statuses for subscribing to rpc
bool subscribed = false;
// handle l