无法使用 mosquitto/mqtt 连接到本地主机服务器上的 Thingsboard 运行
Unable to connect to Thingsboard running on localhost server using mosquitto/mqtt
我无法通过 MQTT 连接到本地主机上的 thingsboard 运行。我的 arduino 代码与 Thingsboard 的演示服务器配合得很好。它连接成功,并且能够在最新遥测下看到值。在本地 运行 时,我得到
"Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]"
arduino 串口监视器出错。
我已经安装了 tb-gateway 和 mosquitto 代理。以下是所做的更改:
tb-gateway.yml
path: storage
bufferSize: 1000
connection:
host: "127.0.0.1"
port: 1882
retryInterval: 3000
mqtt-config.json
{
"host": "127.0.0.1",
"port": 1883,
"ssl": false,
"retryInterval": 3000,
}
thingsboard.yml
mqtt:
# Enable/disable mqtt transport protocol.
enabled: "${MQTT_ENABLED:true}"
bind_address: "${MQTT_BIND_ADDRESS:0.0.0.0}"
bind_port: "${MQTT_BIND_PORT:1882}"
timeout: "${MQTT_TIMEOUT:10000}"
netty:
这是我的 arduino 代码:
#include <Wire.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;
// Select SDA and SCL pins for I2C communication
const uint8_t scl = 22;
const uint8_t sda = 21;
#define TOKEN "oPfC3R8p3LLKJ4lBU9u2"
#define WIFI_AP ""
#define WIFI_PASSWORD ""
char thingsboardServer[] = "127.0.0.1";
unsigned long lastSend;
int status = WL_IDLE_STATUS;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// sensitivity scale factor respective to full scale setting provided in
datasheet
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
int16_t AccelX, AccelY, AccelZ, Temperature;
void setup() {
Serial.begin(115200);
InitWiFi();
client.setServer( thingsboardServer, 1883 );
Wire.begin(sda, scl);
MPU6050_Init();
lastSend = 0;
}
void loop() {
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendData();
lastSend = millis();
}
delay(10);
client.loop();
}
void getAndSendData(){
double Ax, Ay, Az, T, Gx, Gy, Gz;
Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
//divide each with their sensitivity scale factor
Ax = (double)AccelX/AccelScaleFactor;
Ay = (double)AccelY/AccelScaleFactor;
Az = (double)AccelZ/AccelScaleFactor;
T = (double)Temperature/340+36.53; //temperature formula
Serial.println("Collecting Data ");
Serial.print("Ax: "); Serial.print(Ax);
Serial.print(" Ay: "); Serial.print(Ay);
Serial.print(" Az: "); Serial.print(Az);
Serial.print(" T: "); Serial.print(T);
// Prepare a JSON payload string
String payload = "{";
payload += "\"Ax\":"; payload += String(Ax); payload += ",";
payload += "\"Ay\":"; payload += String(Ay); payload += ",";
payload += "\"Az\":"; payload += String(Az); payload += ",";
payload += "\"temperature\":"; payload += String(T);
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("%. Sent to Thingsboard.");
}
void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}
// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, (uint8_t)14);
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
}
//configure MPU6050
void MPU6050_Init(){
delay(150);
.......
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
.......
}
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("fa5c6fe0-05cc-11e9-bfc6-27e4379bb947", TOKEN,
NULL,NULL,NULL,NULL,NULL,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 );
}
}
}
如果我遗漏任何配置或做错了什么,请告诉我。
您的 ESP8266 代码已将 IP 地址 thingsboardServer
设置为 127.0.0.1 - 这对 ESP8266 永远不起作用。
127.0.0.1 是一个特殊的 IP 地址 - 它是 localhost
- 它总是指向 "self"。在计算机 运行 ThingsBoard 上使用,它指的是那台计算机,但在另一台计算机上使用它指的是另一台计算机。其他计算机或设备将无法使用 127.0.0.1 连接到 ThingsBoard 服务器。
这就像你说 "I like cake" 然后你的朋友说 "I like pie"。 127.0.0.1 是 "I" - 你的朋友说 "I like pie" 并不意味着你喜欢馅饼。
您需要找到您尝试连接的服务器的正确 IP 地址。如果您 运行 它在 Linux 计算机上,您可以使用 /sbin/ifconfig
列出您的网络接口。忽略 lo
- 即 loopback
,即 localhost
和 127.0.0.1。如果服务器有有线(以太网)连接,请查找 en0
或 enp0s##
或类似的东西,并带有 inet addr
。如果它已连接到 wifi,请查找 wlan0
或 wlp5s0
之类的内容。名称因 OS 和硬件而异,因此您必须自己弄清楚。
例如,在我的 Linux 台电脑上:
enp0s25 Link encap:Ethernet HWaddr 70:85:c2:00:12:76
inet addr:10.0.1.104 Bcast:10.0.1.255 Mask:255.255.255.0
inet6 addr: fe80::7886:c2fe:fd00:1872/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:846697237 errors:0 dropped:0 overruns:0 frame:0
TX packets:96279263 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:167800766971 (167.8 GB) TX bytes:34595177085 (34.5 GB)
Interrupt:20 Memory:fb400000-fb420000
那台电脑的IP地址是10.0.1.104。
这也适用于 Macintosh。
如果您在 Windows 上 运行,this article will help you find your IP address. Do not use a web service like https://www.whatismyip.com/ - 它将告诉您路由器的 IP 地址,而不是计算机的 IP 地址。
获得正确的 IP 地址后,在 ESP8266 代码中使用它而不是 127.0.0.1。
在您的 tb-gateway.yml
和 mqtt-config.json
文件中,您告诉服务器只监听 127.0.0.1。您应该仔细检查文档,但您几乎肯定需要在两个文件中将其更改为 0.0.0.0。这应该告诉服务器监听所有网络设备。如果不进行此更改,它将无法与同一台计算机上 运行 之外的任何内容进行通信。
完成后,请注意您网络上的任何内容都可以与您的 MQTT 服务器通信;如果这对您来说是个问题,您应该为它配置适当的访问控制。
我无法通过 MQTT 连接到本地主机上的 thingsboard 运行。我的 arduino 代码与 Thingsboard 的演示服务器配合得很好。它连接成功,并且能够在最新遥测下看到值。在本地 运行 时,我得到
"Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]"
arduino 串口监视器出错。
我已经安装了 tb-gateway 和 mosquitto 代理。以下是所做的更改:
tb-gateway.yml
path: storage
bufferSize: 1000
connection:
host: "127.0.0.1"
port: 1882
retryInterval: 3000
mqtt-config.json
{
"host": "127.0.0.1",
"port": 1883,
"ssl": false,
"retryInterval": 3000,
}
thingsboard.yml
mqtt:
# Enable/disable mqtt transport protocol.
enabled: "${MQTT_ENABLED:true}"
bind_address: "${MQTT_BIND_ADDRESS:0.0.0.0}"
bind_port: "${MQTT_BIND_PORT:1882}"
timeout: "${MQTT_TIMEOUT:10000}"
netty:
这是我的 arduino 代码:
#include <Wire.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
// MPU6050 Slave Device Address
const uint8_t MPU6050SlaveAddress = 0x68;
// Select SDA and SCL pins for I2C communication
const uint8_t scl = 22;
const uint8_t sda = 21;
#define TOKEN "oPfC3R8p3LLKJ4lBU9u2"
#define WIFI_AP ""
#define WIFI_PASSWORD ""
char thingsboardServer[] = "127.0.0.1";
unsigned long lastSend;
int status = WL_IDLE_STATUS;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// sensitivity scale factor respective to full scale setting provided in
datasheet
const uint16_t AccelScaleFactor = 16384;
const uint16_t GyroScaleFactor = 131;
int16_t AccelX, AccelY, AccelZ, Temperature;
void setup() {
Serial.begin(115200);
InitWiFi();
client.setServer( thingsboardServer, 1883 );
Wire.begin(sda, scl);
MPU6050_Init();
lastSend = 0;
}
void loop() {
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendData();
lastSend = millis();
}
delay(10);
client.loop();
}
void getAndSendData(){
double Ax, Ay, Az, T, Gx, Gy, Gz;
Read_RawValue(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_XOUT_H);
//divide each with their sensitivity scale factor
Ax = (double)AccelX/AccelScaleFactor;
Ay = (double)AccelY/AccelScaleFactor;
Az = (double)AccelZ/AccelScaleFactor;
T = (double)Temperature/340+36.53; //temperature formula
Serial.println("Collecting Data ");
Serial.print("Ax: "); Serial.print(Ax);
Serial.print(" Ay: "); Serial.print(Ay);
Serial.print(" Az: "); Serial.print(Az);
Serial.print(" T: "); Serial.print(T);
// Prepare a JSON payload string
String payload = "{";
payload += "\"Ax\":"; payload += String(Ax); payload += ",";
payload += "\"Ay\":"; payload += String(Ay); payload += ",";
payload += "\"Az\":"; payload += String(Az); payload += ",";
payload += "\"temperature\":"; payload += String(T);
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("%. Sent to Thingsboard.");
}
void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
}
// read all 14 register
void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, (uint8_t)14);
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
}
//configure MPU6050
void MPU6050_Init(){
delay(150);
.......
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
.......
}
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("fa5c6fe0-05cc-11e9-bfc6-27e4379bb947", TOKEN,
NULL,NULL,NULL,NULL,NULL,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 );
}
}
}
如果我遗漏任何配置或做错了什么,请告诉我。
您的 ESP8266 代码已将 IP 地址 thingsboardServer
设置为 127.0.0.1 - 这对 ESP8266 永远不起作用。
127.0.0.1 是一个特殊的 IP 地址 - 它是 localhost
- 它总是指向 "self"。在计算机 运行 ThingsBoard 上使用,它指的是那台计算机,但在另一台计算机上使用它指的是另一台计算机。其他计算机或设备将无法使用 127.0.0.1 连接到 ThingsBoard 服务器。
这就像你说 "I like cake" 然后你的朋友说 "I like pie"。 127.0.0.1 是 "I" - 你的朋友说 "I like pie" 并不意味着你喜欢馅饼。
您需要找到您尝试连接的服务器的正确 IP 地址。如果您 运行 它在 Linux 计算机上,您可以使用 /sbin/ifconfig
列出您的网络接口。忽略 lo
- 即 loopback
,即 localhost
和 127.0.0.1。如果服务器有有线(以太网)连接,请查找 en0
或 enp0s##
或类似的东西,并带有 inet addr
。如果它已连接到 wifi,请查找 wlan0
或 wlp5s0
之类的内容。名称因 OS 和硬件而异,因此您必须自己弄清楚。
例如,在我的 Linux 台电脑上:
enp0s25 Link encap:Ethernet HWaddr 70:85:c2:00:12:76
inet addr:10.0.1.104 Bcast:10.0.1.255 Mask:255.255.255.0
inet6 addr: fe80::7886:c2fe:fd00:1872/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:846697237 errors:0 dropped:0 overruns:0 frame:0
TX packets:96279263 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:167800766971 (167.8 GB) TX bytes:34595177085 (34.5 GB)
Interrupt:20 Memory:fb400000-fb420000
那台电脑的IP地址是10.0.1.104。
这也适用于 Macintosh。
如果您在 Windows 上 运行,this article will help you find your IP address. Do not use a web service like https://www.whatismyip.com/ - 它将告诉您路由器的 IP 地址,而不是计算机的 IP 地址。
获得正确的 IP 地址后,在 ESP8266 代码中使用它而不是 127.0.0.1。
在您的 tb-gateway.yml
和 mqtt-config.json
文件中,您告诉服务器只监听 127.0.0.1。您应该仔细检查文档,但您几乎肯定需要在两个文件中将其更改为 0.0.0.0。这应该告诉服务器监听所有网络设备。如果不进行此更改,它将无法与同一台计算机上 运行 之外的任何内容进行通信。
完成后,请注意您网络上的任何内容都可以与您的 MQTT 服务器通信;如果这对您来说是个问题,您应该为它配置适当的访问控制。