Я не могу подключиться к вещам, работающим на локальном хосте, через MQTTМой Arduino-код хорошо работал с демо-сервером Thingsboard.Он успешно подключился и смог увидеть значения в Последней Телеметрии.При локальном запуске я получаю ошибку
"Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]"
в последовательном мониторе Arduino.
Я установил tb-gateway и mosquitto broker.Следующие изменения сделаны:
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 );
}
}
}
Пожалуйста, дайте мне знать, если мне не хватает какой-либо конфигурации или что-то не так.