MQTT mit thingg.io auf ESP8266

Gast #7314960
Lesenswert?

Hallo,

ich wollte mal fragen ob hier jemand Erfahrung mit dem MQTT-Onlinebroker tingg.io gemacht hat. Ich schaffe es nicht den ESP mit dem Onlinebroker zu verbinden. Basis ist der Code, den tingg.io vorgibt

1
#include <PubSubClient.h>
2

3
const char* mqtt_server = "mqtt.tingg.io";
4
const int port = 1883;
5

6
const char* thing_id = "####";
7
const char* thing_key = "####";
8
const char* username = "thing";
9

10
PubSubClient client(wifiClient); 
11

12
void reconnect() {
13
  while (!client.connected()) {
14
    if (!client.connect(thing_id, username, thing_key)) {
15
      delay(1000);
16
    }
17
  }
18
}
19

20
void setup() {
21
  client.setServer(mqtt_server, port);
22
  client.setCallback(callback);
23
}
24

25
void loop() {
26
  if (!client.connected()) {
27
    reconnect();
28
  }
29

30
  client.publish("my-topic", "Hello World!");
31
}

Hat jetzt jedes "thing" was wohl einem Client entspricht eine eigene ID und Passwort? Ich hoffe jemand kann mir weiterhelfen.

Vielen Dank

#7314975
Lesenswert?

Jürgen schrieb:

Hat jetzt jedes "thing" was wohl einem Client entspricht eine eigene ID und Passwort?

die "Thing ID" ist in dem Beispielcode die MQTT-Client-ID

https://pubsubclient.knolleary.net/api

MQTT selber ist die relativ egal, müssen aber Unique sein, d.H. eigentlich kannst du da auch eine würfeln.

Wenn deren Visualisierungs-Oberfläche allerdings irgendwelche Automatismen an die Client-ID hängt, dann solltest du es so verwenden, wie sie es vorgeben.

Gast #7315013
Lesenswert?

ich bin etwas verwirrt. Für den "Anmeldeprozess" werden username, thing_ID, und thing_key benötigt. Wenn nur Username und Passwort benötigt wird, wozu ist dann die ID? Thingg.io vergibt für jedes "thing" einen neue ID und Key.

#7315027
Lesenswert?

Kann natürlich sein, dass die die Client-ID, die sonst eigentlich nur zum Verhindern mehrerer paralleler Verbindungen vom selben Device verwendet wird, auch für die Authentifizierung heranziehen. Hat den Vorteil, dass du einzelnen Devices den Zugriff abdrehen kannst, wenn deren Zugangsdaten verloren gehen. => Machs so wie die es wollen.

Gast #7317144
Lesenswert?

hier der Code mit dem ich es versuche. Die WLAN-Verbindung funktioniert, die Verbindung zu tingg.io jedoch nicht. Cer Code ist direkt von der tingg-webseite

1
#include <ESP8266WiFi.h>
2
#include <PubSubClient.h>
3

4
// Pins Config
5
const int LightPin = D0;
6
const int PhotoPin = A0;
7
const int DefaultPin = D4;
8

9
// Wifi Config
10
const char* ssid = "SSID";
11
const char* password = "PASSWORD";
12

13
// MQTT Config
14
const char* mqtt_server = "mqtt.tingg.io";
15
const int port = 1883; // 8883 for mqtts connections.
16

17
// Thing Config
18
const char* thing_id = "###";
19
const char* thing_key = "###";
20

21
// Topics Config
22
const char* pubTopic = "<pub_resource>";
23
const char* subTopic = "<sub_resource>";
24

25

26
// Default Config
27
const char* username = "thing";
28

29
WiFiClient espClient;
30
PubSubClient client(espClient);
31

32
// Vars
33
int val;
34
int prevVal = 0;
35
char buf[12];
36
long lastUpdateMillis = 0;
37

38

39
// Function to validate connection with the board
40
void blinking_loop(){
41
  while (ssid == "SSID") {
42
    digitalWrite(DefaultPin,HIGH);
43
    delay(500);
44
    digitalWrite(DefaultPin,LOW);
45
    delay(500);
46
    Serial.println("Default light should be blinking. Please set up your Wifi configuration.");
47
  }
48
}
49

50
void setup_wifi() {
51
  delay(10);
52
  Serial.print("Connecting to ");
53
  Serial.println(ssid);
54
  WiFi.mode(WIFI_STA);
55

56
  WiFi.begin(ssid, password);
57
  while (WiFi.status() != WL_CONNECTED) {
58
    delay(500);
59
    Serial.print(".");
60
  }
61

62
  randomSeed(micros());
63

64
  Serial.println("");
65
  Serial.print("Congratulations!! WiFi connected on IP address: ");
66
  Serial.println(WiFi.localIP());
67
  Serial.println("");
68
  Serial.println("Now, follow up on the steps for the MQTT configuration. ");
69
}
70

71
void reconnect() {
72
  while (!client.connected()) {
73
    Serial.print("Attempting to connect MQTT...");
74
    if (client.connect(thing_id, username, thing_key)) {
75
      Serial.println("connected");
76
      client.subscribe(subTopic);
77
    } else {
78
      Serial.print(" Still not connected...");      // Serial.print(client.state());
79
      Serial.println(" trying again in 5 seconds"); // Wait 5 seconds before retrying
80
      delay(5000);
81
    }
82
  }
83
}
84

85
String message(byte* payload, unsigned int length) {
86
  payload[length] = '\0';
87
  String s = String((char*)payload);
88
  return s;
89
}
90

91
void callback(char* topic, byte* payload, unsigned int length) {
92
  Serial.println(topic);
93
  String msg = message(payload, length);
94

95
  if ((msg == "ON") || (msg == "on") || (msg == "1")) {
96
    Serial.println("Light turned On");
97
    digitalWrite(LightPin,HIGH);
98
  }
99
  else if ((msg == "OFF") || (msg == "off") || (msg == "0")) {
100
    Serial.println("Light turned Off");
101
    digitalWrite(LightPin,LOW);
102
  }
103
  else {
104
    Serial.println("Unknown command. Please try: ON, on, 1, OFF, off or 0.");
105
  }
106
}
107

108
void setup() {
109
  pinMode(LightPin,OUTPUT);
110
  pinMode(PhotoPin,INPUT);
111
  pinMode(DefaultPin,OUTPUT);
112

113
  Serial.begin(115200);
114
  blinking_loop();
115
  setup_wifi();
116
  client.setServer(mqtt_server, port);
117
  client.setCallback(callback);
118
}
119

120
void loop() {
121
  if (!client.connected()) {
122
    reconnect();
123
  }
124
  client.loop();
125

126
  val = analogRead(PhotoPin);
127

128
  if (prevVal != val || millis() - lastUpdateMillis > 3000) {
129
    lastUpdateMillis = millis();
130
    prevVal = val;
131
    client.publish(pubTopic,itoa(val, buf, 10));
132
    Serial.print("Updating value to ");
133
    Serial.println(val);
134
    delay(250);
135
  }
136
  delay(50);
137
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren