MQTT in Arduino: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 20: | Zeile 20: | ||
In the example code, enter your Wifi connection information: | In the example code, enter your Wifi connection information: | ||
<source lang="js" line start="2" highlight="4-6"> | |||
const char ssid[] = "yourNetworkName"; | const char ssid[] = "yourNetworkName"; | ||
const char pass[] = "yourNetworkPassword"; | const char pass[] = "yourNetworkPassword"; | ||
</source> | |||
The client.connect function takes the following information: | The client.connect function takes the following information: | ||
< | <source lang="js" line start="2" highlight="4-6"> | ||
client.connect("client ID", "name of your instance", "token secret") | |||
</source> | |||
It connects you to the online Shiftr Broker's public namespace called "try": | It connects you to the online Shiftr Broker's public namespace called "try": | ||
<source lang="js" line start="2" highlight="4-6"> | |||
void connect() { | void connect() { | ||
Zeile 36: | Zeile 46: | ||
} | } | ||
</source> | |||
The client.begin function takes the Broker's URL or IP: | The client.begin function takes the Broker's URL or IP: | ||
< | <source lang="js" line start="2" highlight="4-6"> | ||
void setup() { | void setup() { | ||
client.begin("broker.shiftr.io", net); | client.begin("broker.shiftr.io", net); //client.begin("URL or IP", net); | ||
.... } | .... } | ||
</source> | |||
=SHIFTR Desktop App= | =SHIFTR Desktop App= |
Version vom 17. Mai 2022, 16:23 Uhr
see also MQTT Brokers and https://www.kobakant.at/DIY/?p=9140
Shiftr has libraries for various platforms including Arduino and Processing that make it very easy to code MQTT Clients.
SHIFTR ONLINE
To code an Arduino Client we use Shiftr's MQTT library for Arduino. There are other MQTT libraries for Arduino that would also work.
Install
the Shiftr.io Arduino MQTT library:
Sketch --> Include Library --> Manage Libraries --> search: "MQTT"
scroll down until you see "MQTT by Joel Gaehwiller"
Open Example
File --> Examples --> MQTT --> "ESP32DevelopmentBoard"
In the example code, enter your Wifi connection information:
const char ssid[] = "yourNetworkName";
const char pass[] = "yourNetworkPassword";
The client.connect function takes the following information:
client.connect("client ID", "name of your instance", "token secret")
It connects you to the online Shiftr Broker's public namespace called "try":
void connect() {
while (!client.connect("arduino", "try", "try")) { ..... }
}
The client.begin function takes the Broker's URL or IP:
void setup() {
client.begin("broker.shiftr.io", net); //client.begin("URL or IP", net);
.... }
SHIFTR Desktop App
Download the Shiftr MQTT library (see above).
Open the Shiftr MQTT example (see above).
The following items in the code need to be changed. Leave the "name of your instance" and the "token secret" blank:
void connect() { //client.connect("client ID", "name of your instance", "token secret") while (!client.connect("arduino", "", "")) { ..... } }
Insert your IP address instead of the Shiftr URL:
void setup() { client.begin("192.168.0.9", net); .... }
if you need to use a different port number other than the default 1883 (for example 2883), you can specify it like this:
client.begin(“192.168.0.10”, 2883, net);
more info: https://github.com/256dpi/arduino-mqtt#api