Hello,
I have an arduino code to make my arduino communicate with mqtt. Only when I listen in the mqtt integration with #, I don’t see anything going.
I try to communicate 10 dht22.
if I connect to my broker with mqttlens, I see the messages go through.
When I use mqtt explorer I don’t see my arduino but when I send messages with mqttlens they appear.
Does anyone know how to help me?
I’m a french user than the code is in french.
Here is my code:
// Arduino Mqtt et des capteurs DHT22 (temp/humidite )
//
// Exemple de reconnexion MQTT - non bloquant
// une recopie-modif du wiki FHEM : https://wiki.fhem.de/wiki/MQTT_Einführung_Teil_3
//
// Ce croquis montre comment garder le client connecté en utilisant
// une fonction de reconnexion non bloquante, Si le client perd sa connexion,
// il tente de se reconnecter toutes les 5 secondes sans bloquer la boucle principale.
//
//
// -------------- debut ---------------
#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define DHTPIN1 24
#define DHTPIN2 26
#define DHTPIN3 28
#define DHTPIN4 30
#define DHTPIN6 34
#define DHTPIN7 36
#define DHTPIN8 38
#define DHTPIN9 40
#define DHTTYPE1 DHT22
#define DHTTYPE2 DHT22
#define DHTTYPE3 DHT22
#define DHTTYPE4 DHT22
#define DHTTYPE6 DHT22
#define DHTTYPE7 DHT22
#define DHTTYPE8 DHT22
#define DHTTYPE9 DHT22
DHT dht1 (DHTPIN1, DHTTYPE1);
DHT dht2 (DHTPIN2, DHTTYPE1);
DHT dht3 (DHTPIN3, DHTTYPE1);
DHT dht4 (DHTPIN4, DHTTYPE1);
DHT dht6 (DHTPIN6, DHTTYPE1);
DHT dht7 (DHTPIN7, DHTTYPE1);
DHT dht8 (DHTPIN8, DHTTYPE1);
DHT dht9 (DHTPIN9, DHTTYPE1);
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; // adresses mac
IPAddress ip{ 192,168,1,61}; //ip du client
IPAddress server{ 192, 168, 1, 2}; //ip du serveur MQTT (le Broker)
// ------------- mqtt ---------------//
void callback(char* topic, byte* payload, unsigned int length)
{
// gérer le message est arrivé
}
EthernetClient ethClient;
PubSubClient client(ethClient);
long lastReconnectAttempt = 0;
boolean reconnect()
{
if (client.connect("Arduino_rez", "home/Arduino_rez", 0, true, "offline"))
{
// Once connected, publish an announcement...
client.publish("home/Arduino_rez","online", true);
// ... and resubscribe
client.subscribe("inTopic");
}
return client.connected();
}
// Réservez la zone de stockage pour sauver la clôture //
// capteur0
static char humidity[15];
static char temperature[15];
float h = 0.0;
float h_alt = 0.0;
float t = 0.0;
float t_alt = 0.0;
// capteur1
static char char_h1[15];
static char char_t1[15];
float h1 = 0.0;
float h1_alt = 0.0;
float t1 = 0.0;
float t1_alt = 0.0;
// capteur2
static char char_h2[15];
static char char_t2[15];
float h2 = 0.0;
float h2_alt = 0.0;
float t2 = 0.0;
float t2_alt = 0.0;
// capteur3
static char char_h3[15];
static char char_t3[15];
float h3 = 0.0;
float h3_alt = 0.0;
float t3 = 0.0;
float t3_alt = 0.0;
// capteur4
static char char_h4[15];
static char char_t4[15];
float h4 = 0.0;
float h4_alt = 0.0;
float t4 = 0.0;
float t4_alt = 0.0;
// capteur5
static char char_h5[15];
static char char_t5[15];
float h5 = 0.0;
float h5_alt = 0.0;
float t5 = 0.0;
float t5_alt = 0.0;
// capteur6
static char char_h6[15];
static char char_t6[15];
float h6 = 0.0;
float h6_alt = 0.0;
float t6 = 0.0;
float t6_alt = 0.0;
// capteur7
static char char_h7[15];
static char char_t7[15];
float h7 = 0.0;
float h7_alt = 0.0;
float t7 = 0.0;
float t7_alt = 0.0;
// capteur8
static char char_h8[15];
static char char_t8[15];
float h8 = 0.0;
float h8_alt = 0.0;
float t8 = 0.0;
float t8_alt = 0.0;
// capteur9
static char char_h9[15];
static char char_t9[15];
float h9 = 0.0;
float h9_alt = 0.0;
float t9 = 0.0;
float t9_alt = 0.0;
// remise a zero du compteur "millisecondes" depuis le dernier appel.
unsigned long previousMillis = 0;
// valeur intervalle de fréquence d'utilisation du capteur, en millisecondes (60000=1mn)
const long interval = 6000;
//***************** Setup **********************//
void setup()
{
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
delay(1500);
lastReconnectAttempt = 0;
Serial.begin(9600);
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
dht1.begin();
dht2.begin();
dht3.begin();
dht4.begin();
dht6.begin();
dht7.begin();
dht8.begin();
dht9.begin();
}
//***************** Loop **********************
void loop()
{
if (!client.connected())
{
long now = millis();
if (now - lastReconnectAttempt > 5000) // calcul temps pour reconnection client
{
lastReconnectAttempt = now;
// Tentative de reconnexion
if (reconnect())
{
lastReconnectAttempt = 0;
}
}
}
else
{
client.loop(); // Client connected
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) // calcul temps ecouler du capteur
{
previousMillis = currentMillis;
// ----- lecture des capteurs ------
h1 = dht1.readHumidity(); //humidite 1
t1 = dht1.readTemperature(); //Temperature 1
h2 = dht2.readHumidity(); //humidite 2
t2 = dht2.readTemperature(); //Temperature 2
h3 = dht3.readHumidity(); //humidite 3
t3 = dht3.readTemperature(); //Temperature 3
h4 = dht4.readHumidity(); //humidite 4
t4 = dht4.readTemperature(); //Temperature 4
h6 = dht6.readHumidity(); //humidite 6
t6 = dht6.readTemperature(); //Temperature 6
h7 = dht7.readHumidity(); //humidite 7
t7 = dht7.readTemperature(); //Temperature 7
h8 = dht8.readHumidity(); //humidite 8
t8 = dht8.readTemperature(); //Temperature 8
h9 = dht9.readHumidity(); //humidite 9
t9 = dht9.readTemperature(); //Temperature 9
}
// Vérifiez si un numéro valide est retourné.
// Si "NaN" "Not a Number" (pas un nombre) est renvoyé, affiche erreurs capteur.
if (isnan(t) || isnan(h) || isnan(t1) || isnan(h1) || isnan(t2) || isnan(h2) || isnan(t3) || isnan(h3) || isnan(t4) || isnan(h4) || isnan(t5) || isnan(h5) || isnan(t6) || isnan(h6) || isnan(t7) || isnan(h7) || isnan(t8) || isnan(h8) || isnan(t9) || isnan(h9) )
{
Serial.println(" un DHT22 n'a pas pu être lu");
client.publish("home/Arduino_rez","Erreur capteur",true);
// "true" envoie le message conservé, c'est-à-dire que le message reste sur le courtier
//jusqu'à ce que quelque chose de nouveau arrive.
}
else if (h == h_alt && t == t_alt && h1 == h1_alt && t1 == t1_alt && h2 == h2_alt && t2 == t2_alt && h3 == h3_alt && t3 == t3_alt && h4 == h4_alt && t4 == t4_alt && h5 == h5_alt && t5 == t5_alt && h6 == h6_alt && t6 == t6_alt && h7 == h7_alt && t7 == t7_alt && h8 == h8_alt && t8 == t8_alt && h9 == h9_alt && t9 == t9_alt )
{
//ne fais rien
}
else
{
client.publish("home/Arduino_rez","en Ligne", true);
dtostrf(h,6, 1, humidity);
dtostrf(t,6, 1, temperature);
dtostrf(h1,6, 1, char_h1);
dtostrf(t1,6, 1, char_t1);
dtostrf(h2,6, 1, char_h2);
dtostrf(t2,6, 1, char_t2);
dtostrf(h3,6, 1, char_h3);
dtostrf(t3,6, 1, char_t3);
dtostrf(h4,6, 1, char_h4);
dtostrf(t4,6, 1, char_t4);
dtostrf(h5,6, 1, char_h5);
dtostrf(t5,6, 1, char_t5);
dtostrf(h6,6, 1, char_h6);
dtostrf(t6,6, 1, char_t6);
dtostrf(h7,6, 1, char_h7);
dtostrf(t7,6, 1, char_t7);
dtostrf(h8,6, 1, char_h8);
dtostrf(t8,6, 1, char_t8);
dtostrf(h9,6, 1, char_h9);
dtostrf(t9,6, 1, char_t9);
client.publish("home/Arduino_rez/ch_heather/humidite",humidity, true);
client.publish("home/Arduino_rez/ch_heather/Temperature",temperature, true);
client.publish("home/Arduino_rez/cuisine/humidite",char_h1, true);
client.publish("home/Arduino_rez/cuisine/Temperature",char_t1, true);
client.publish("home/Arduino_rez/caves/humidite",char_h1, true);
client.publish("home/Arduino_rez/caves/Temperature",char_t1, true);
client.publish("home/Arduino_rez/salle_de_jeu/humidite",char_h1, true);
client.publish("home/Arduino_rez/salle_de_jeu/Temperature",char_t1, true);
client.publish("home/Arduino_rez/salle_de_bain/humidite",char_h1, true);
client.publish("home/Arduino_rez/salle_de_bain/Temperature",char_t1, true);
client.publish("home/Arduino_rez/ch_parents/humidite",char_h1, true);
client.publish("home/Arduino_rez/ch_parents/Temperature",char_t1, true);
client.publish("home/Arduino_rez/ch_pacey/humidite",char_h1, true);
client.publish("home/Arduino_rez/ch_pacey/Temperature",char_t1, true);
client.publish("home/Arduino_rez/bureau/humidite",char_h1, true);
client.publish("home/Arduino_rez/bureau/Temperature",char_t1, true);
client.publish("home/Arduino_rez/salon/humidite",char_h1, true);
client.publish("home/Arduino_rez/salon/Temperature",char_t1, true);
client.publish("home/Arduino_rez/ch_kirsten/humidite",char_h1, true);
client.publish("home/Arduino_rez/ch_kirsten/Temperature",char_t1, true);
h_alt = h; // annuler l'ancienne lecture ..
t_alt = t; // ... pour ne réagir qu'aux changements
h1_alt = h1;
t1_alt = t1;
h2_alt = h2;
t2_alt = t2;
h3_alt = h3;
t3_alt = t3;
h4_alt = h4;
t4_alt = t4;
h5_alt = h5;
t5_alt = t5;
h6_alt = h6;
t6_alt = t6;
h7_alt = h7;
t7_alt = t7;
h8_alt = h8;
t8_alt = t8;
h9_alt = h9;
t9_alt = t9;
// ------------ Visu sur terminal ------------------
Serial.print("humidite ch.Heather: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature ch.Heather: ");
Serial.print(t);
Serial.println(" C");
Serial.print("humidite cuisine: ");
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Temperature cuisine: ");
Serial.print(t1);
Serial.println(" C");
Serial.print("humidite caves: ");
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Temperature caves: ");
Serial.print(t2);
Serial.println(" C");
Serial.print("humidite salle de jeu: ");
Serial.print(h3);
Serial.print(" %\t");
Serial.print("Temperature salle de jeu: ");
Serial.print(t3);
Serial.println(" C");
Serial.print("humidite salle de bain: ");
Serial.print(h4);
Serial.print(" %\t");
Serial.print("Temperature salle de bain: ");
Serial.print(t4);
Serial.println(" C");
Serial.print("humidite ch. parents: ");
Serial.print(h5);
Serial.print(" %\t");
Serial.print("Temperature ch. parents: ");
Serial.print(t5);
Serial.println(" C");
Serial.print("humidite ch. Pacey: ");
Serial.print(h6);
Serial.print(" %\t");
Serial.print("Temperature ch. Pacey: ");
Serial.print(t6);
Serial.println(" C");
Serial.print("humidite bureau: ");
Serial.print(h7);
Serial.print(" %\t");
Serial.print("Temperature bureau: ");
Serial.print(t7);
Serial.println(" C");
Serial.print("humidite salon: ");
Serial.print(h8);
Serial.print(" %\t");
Serial.print("Temperature salon: ");
Serial.print(t8);
Serial.println(" C");
Serial.print("humidite ch. kirsten: ");
Serial.print(h9);
Serial.print(" %\t");
Serial.print("Temperature ch. kirsten: ");
Serial.print(t9);
Serial.println(" C");
}
}
//-------- Fin Pgm --------------
Thank’s