Hello,
I own an Arduino Mega 2560 and I am trying to send temperature values from an dht22 via MQTT to Node-red in im my local network. A typical use-case I thought.
The temperature sensor works fine, the Ethernetshield works fine (I can ping the devie from my computer). My MQTT Server (mosquitto 2.0.4 running on Ubuntu 20.04 LTS) is accessible from my WindowsComputer using MQTT Explorer without any security settings without Problems.
Now I tried to send a simple message (client.publish("Temperatur","Test")) from my Arduino to my MQTT broker writing some code similar to the one which can be found under pubsubclient/examples/mqtt_esp8266 at master · knolleary/pubsubclient · GitHub .
// Include Secion
// ==============
// Sensor related
#include <DHT.h>
// Modbus related
#include <ModbusMaster.h>
// Ethernet related
#include <SPI.h>
#include <Ethernet.h>
// MQTT related
#include <PubSubClient.h>
// Define Section
// ==============
// Sensor Related
#define DHTPIN 53 // Hier die Pin Nummer eintragen wo der Sensor angeschlossen ist
#define DHTTYPE DHT22 // Hier wird definiert was für ein Sensor ausgelesen wird. In
// unserem Beispiel möchten wir einen DHT11 auslesen, falls du
// ein DHT22 hast einfach DHT22 eintragen
DHT dht(DHTPIN, DHTTYPE);
// Wait a few seconds between measurements.
// Hier definieren wir die Verweilzeit die gewartet wird
// bis der Sensor wieder ausgelesen wird. Da der DHT11
// auch ca. 2 Sekunden hat um seine Werte zuaktualisieren
// macht es keinen sinn ihn schneller auszulesen!
// Modbus related
//#define MAX485_DE 3
//#define MAX485_RE_NEG 2
//ModbusMaster node; //object node for class ModbusMaster
//void preTransmission() { //Function for setting stste of Pins DE & RE of RS-485
// digitalWrite(MAX485_RE_NEG, 1);
// digitalWrite(MAX485_DE, 1);}
//void postTransmission() {
// digitalWrite(MAX485_RE_NEG, 0);
// digitalWrite(MAX485_DE, 0);}
// Ethernet related
byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x0A, 0x01};
// IPAddress ip(192, 168, 1, 22); // Will not be used since the Network has an DHCP Server
void setup_ethernet() {
delay(10);
// We start by connecting to a Ethernet network
Ethernet.begin(mac); // Use Ethernet.begin(mac, ip) instead if you want to set the IP-Adress manually
//while (Ethernet.linkStatus() != LinkON) {
// delay(500); }
}
// MQTT related
EthernetClient ethClient;
PubSubClient client(ethClient);
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);}
Serial.println();
Serial.println("-----------------------");}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ArduinoImKartoffelkeller")) {
Serial.println("connected");delay(500);} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(500);}}}
unsigned long lastMsg = 0;
void setup()
{
// Sensor related
dht.begin();
Serial.println("Sensor related setup completed.");
// Modbus related
// pinMode(MAX485_RE_NEG, OUTPUT);
// pinMode(MAX485_DE, OUTPUT);
// digitalWrite(MAX485_RE_NEG, 0);
// digitalWrite(MAX485_DE, 0);
Serial.begin(115200); //Baud Rate as 115200
//node.begin(1, Serial); //Slave ID as 1
//node.preTransmission(preTransmission); //Callback for configuring RS-485 Transreceiver correctly
// node.postTransmission(postTransmission);
// Ethernet related
setup_ethernet();
// MQTT related
client.setServer("192.168.1.121", 1883);
client.setCallback(callback);
}
void loop(){
// float Humidity = dht.readHumidity();
// Modbus related
//int TotalSystemPower=0;
//TotalSystemPower=node.readInputRegisters(0x4000,1);
//node.writeSingleRegister(0x40001,TotalSystemPower); //Writes 2 to 0x40001 holding register
//node.writeSingleRegister(0x40000,dht.readHumidity()); //Writes value to 0x40000 holding register
//node.writeSingleRegister(0x40001,dht.readTemperature()); //Writes 2 to 0x40001 holding register
// MQTT related
if (!client.connected()){
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
// Read temperature value and store into a variable
Serial.println(" Reading temperature");
String TemperatureString =String(dht.readTemperature(),1);
char buf[5];
unsigned int len=5;
TemperatureString.toCharArray(buf, len);
Serial.println(" Sending temperature");
client.publish("Temperatur","Test");
Serial.println(" Temperature sent.");
}
}
On the serial Console I See:
11:04:51.612 ->
11:04:56.326 -> Attempting MQTT connection...connected
11:04:56.841 -> Reading temperature
11:04:56.841 -> Sending temperature
And then, nothing more happens.
I'm looking for a solution by using google and trying things out for hours, but without success.
I would be happy for any hints.