Hello everyone
I own a weather station at home. The weather station has its own program (CumulusMX), where I can read the various weather data. Now I want to make (part of) the weather data visible on LCD screen (16.2). I've gotten to the point where the Arduino MKR WIFI 1010 is connected to the weather station via MQTT. The most important topic concerns the weather forecast: CumulusMX/Forecast arrives. I get to read this weather forecast on the serial monitor:
Received a message with topic 'CumulusMX/Forecast', length 74 bytes:
Af en toe neerslag, verslechtering weer beeld - Occasional rain, worsening
However, I can't seem to get the topic "CumulusMX/Forecast" on the second line of LCD screen. The first line works: "Weather forecast: ".
Can anyone help me to get the above topic on the LCD screen. As for the hardware: Arduino MKR WIFI 1010 - Grove LCD RGB Backlight – Arduino MKR connector carrier.
The software so far:
#include <Wire.h>
#include <rgb_lcd.h>
rgb_lcd lcd;
#include <ArduinoMqttClient.h>
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
#include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
#include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA)
#include <WiFi.h>
#endif
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
#define SECRET_SSID "xxxxx"
#define SECRET_PASS "yyyyy"
char SSID[] = SECRET_SSID; // your network SSID (name)
char PASS[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
// flashed in the WiFi module.
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "192.168.1.209";
int port = 1883;
const char topic[] = "CumulusMX/Forecast";
char msg[20] = { 'W', 'e', 'e', 'r', 's', 'v', 'o', 'o', 'r', 'u','i', 't', 'z', 'i', 'c','h', 't', 'e', 'n', ': '};
int i = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to WiFi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(SSID);
while (WiFi.begin(SSID, PASS) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
// You can provide a unique client ID, if not set the library uses Arduino-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
Serial.print("Subscribing to topic: ");
Serial.println(topic);
Serial.println();
// subscribe to a topic
mqttClient.subscribe(topic);
// topics can be unsubscribed using:
// mqttClient.unsubscribe(topic);
Serial.print("Waiting for messages on topic: ");
Serial.println(topic);
Serial.println();
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
// Print a message to the LCD.
lcd.setCursor(0,0);
int i = 0;
lcd.clear();
for (int i = 0; i <20 ; i++)
lcd.print(msg[i]);
delay (250);
char swaq = msg[0];
for (i =0; i < 20; i++)
msg[i] = msg[i+1];
msg[19] = swaq;
int messageSize = mqttClient.parseMessage();
if (messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();
Serial.println();
}
}
I look forward to the solution.
Greet,
Henk