Hi! I am trying to send sensorreadings via WEB MQTT and the PubSubClient from the Arduino but I don't seem to get any useful data out of it.
The Webserver displays the correct values and something is sent to my MQTT Instance (Mosquitto) on Ubuntu but it mostly just shows (null) instead of the actual sensor reading. I am quite sure it is some sort of data or format problem but I couldn't figure it out.
Any help is appreciated!
Code:
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
merged with https://www.digikey.at/en/maker/blogs/2018/how-to-use-basic-mqtt-on-arduino
20210226cj
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
String thisString = "TestStringGlobal" ;
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB };
IPAddress ip(192, 168, 10, 5);
// mqtt server Make sure to leave out the http and slashes!
const char* mqttserver = "192.168.10.186";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
int sensorReading[6];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Ethernet WebServer/MQTT Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// Set the MQTT server to the server stated above ^
mqttClient.setServer(mqttserver, 1883);
// Attempt to connect to the server with the ID "myClientID"
// We could also enter MQTT Username and password here as arguments for the Function connect()
if (mqttClient.connect("myClientID"))
{
Serial.println("Connection has been established, well done");
//Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
// start the web server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void subscribeReceive(char* topic, byte* payload, unsigned int length) {
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
void loop() {
// This is needed at the top of the loop!
mqttClient.loop();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print("MQTT WEB Server <br />");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
sensorReading[analogChannel] = analogRead(analogChannel);
String thisString = String(analogRead(analogChannel), DEC);
mqttClient.publish("webmqtt", "thisString");
mqttClient.publish("webmqtt", analogRead(analogChannel)); //sends random (null) or other undefined jibberisch
mqttClient.publish("webmqtt", thisString); //doesn't send the variable and needs to be commented out to compile...
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading[analogChannel]);
client.println("<br />");
Serial.println(sensorReading[analogChannel]);
//mqttClient.publish("webmqtt", String thisString = String( client.println(sensorReading[analogChannel])));
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
// close the connection:
// Ensure that we are subscribed to the topic
mqttClient.subscribe("webmqtt");
// Attempt to publish a value to the topic
//mqttClient.publish("webmqtt", "Hello NEW World");
client.stop();
Serial.println("client disconnected");
}
delay(500);
}