MQTT with GROOVY
public void doDemo() {
try {
client = new MqttClient("tcp://localhost:1883", "Sending");
client.connect();
client.setCallback(this);
client.subscribe("channel/908020/temp");
def dmap = [:] dmap << ["device_id":1234567]; dmap << ["device_type":"Arduino Uno"] dmap << ["time":"2019-11-14 11:10:00.222"]; dmap << ["entity":"soilmessurements"] dmap << ["soiltemp":"25.0"]; dmap << ["soilhumidity":"5.0"] def json = JsonOutput.toJson(dmap); MqttMessage message = new MqttMessage(); message.setPayload(json.getBytes()); client.publish("channel/908020/temp", message); } catch (MqttException e) ... }public void messageArrived(String topic, MqttMessage message) throws Exception { System.out.println("This message arrived:"+message); }
Mosquitto configurations
You can configure:
- Default listeners
- Extra listeners
- Persistence
- Bridges
Mosquitto and CA-Files
1.cafile Path to a cafile 1.capath Path to a directory with CA-Files 1.certfile Path to the PEM encoded server certificate 1.keyfile Path to the PEM encoded keyfile. 1. psk_hint option enables pre-shared-key support for this listener and also acts as an identifier for this listener
Note that the recommended port for MQTT over TLS is 8883, but this must be set manually.
ARDUINO C++
The Ardunio IDE supports C++, so the best thing is to learn C++. There are other alternatives like python and lua, but I 'm sure, if and how they support all the libraries.
dtostrf() convert from "Float" to char array
params
- the float value
- length of the whole string
- digits after the dot
- name of the result char array
Example:
char PufferChar1[20]; dtostrf(123.456789, 1, 5,PufferChar1); Serial.println(PufferChar1);
ARDUINO C++ Numeric Functions
int a = 1234; String myStr; myStr = String(a); //Converts integer to string
String val = “1234”; int result = val.toInt(); //Converts string to integer int to string conversion
2 PILOT-Projects - Your Experience
My Projects LoRaWan
- LoRaWan with the Dragino-Gateway
- At home I use the TTN-Forwarder, it works OK
- MQTT works good for uploading data from the Node
- there are not even examples how to download data (subscribe) from MQTT (with TTN it works)
- in a next step I could prepare that
My Projects MQTT
- Develop the Software for MQTT - communication with Mosquitto, ESP32, flutter,red-node and IOT-MQTT-Panel
- I spend much time to work with the MQTT-Lib for ESP32 pubsubclient, which seems to be the mainly used Lib for MQTT
- publish data from a BME280 (SPI/I2C) is never a problem
- subscribe only works for a small time
- perhaps it is question of timing-problems, some weaknesses in the WIFI-Lib on ESP32
- perhaps its also question, how much traffic is on the local network
My Projects MQTT
- Now I just try two other libraries
- AsyncTCP
- AsyncMQTT s. AsyncMQTTTestAdopted4BME280
- These fit my requirements. Just try to reconnect, if the payload-id coming back from the broker is Zero.
```
include
uint16t packetIdPub2 = mqttClient.publish("esp32/temperature", 2, true, temperatureString.cstr()); Serial.println(packetIdPub2); if (packetIdPub2 == 0){ connectToWifi(); }
My Projects MQTT
- Flutter is a development tool for IOS and Android written with dart
- using RED NODE for prototyping. It is a very good way to make prototyping for IOT with LORAWAN and MQTT
- using IOT-MQTT-Panel, an MQTT App for Android
- using a Paho-Java-Api to write a small groovy client, which works even well
My Projects UDP/TCP
1 UDP and Multicast with GROOVY on the PC and with C++ on the ESP32 (Multicast has a Problem on ESP32)
1.TCP-Example
What is the target of your IOT-training of teachers
What we learned from your projects
1.Not every sensor worked as expected 1. Both projects fit the needs for training teachers, if the maaterial is so improved that they will finish in less one week 1. If the material improves, it will work better 1. The teams themselv have learned much in the projects. The teachers even will.
What could improve the projects
- pluggable, ready to use modules
- learning C++ in the beginning, if this is used, on Raspberry Pi Python could be an alternative
- taking more poweful embedded devices for some work
- learn basic electronics before procjects start
- learn network protocols
- learn some IOT-frameworks
MQTT
TODO
- I will install gitea on my server leever-miteenanner But you have to wait for that. There I will publish all projects connected to TPTC
