我刚开始使用Arduino / ESP32 IoT并寻找合适的平台(本地和远程托管)。 Blynk可以正常工作,但仅限于手机。因此,我尝试了Thinger.io和ThingsBoard.io从Arduino / ESP8266 / ESP32系统上的远程传感器发送,存储和可视化数据。 不幸的是,我的网络似乎阻止了MQTT协议(我无法访问路由器)。因此,我正在寻找使用HTTP协议的代码示例。
我尝试了以下示例中记录的示例 https://thingsboard.io/docs/samples/esp8266/temperature/ 使用ESP8266和DHT22传感器通过MQTT上传温度
// WiFi and connection to Thingsboard Server
#define WIFI_AP "WLAN-XX"
#define WIFI_PASSWORD "78s1A"
#define TOKEN "eWdqwftFL"
char thingsboardServer[] = "http://192.168.11.103";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
unsigned long lastSend;
void setup() {
Serial.begin(57600);
// Wifi and Thingsboard
InitWiFi();
client.setServer( thingsboardServer, 8883 );
lastSend = 0;
}
void InitWiFi()
{
Serial.println("Connecting to WiFi network ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi network");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("ESP8266 Device", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
预期:连接到Thingsboard服务器。 我尝试了其他端口(9090、1883、5683) 串行控制台
Found BME280 sensor! Success.
Connecting to WiFi network ...
.....................Connected to WiFi network
Temp: 20.25°C Humidity: 35.13% RH Pressure: 99276.86Pa
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]