toujours pas compris ce que sont les
- topic to subscribe
(apparemment le retour des données dans le MQTT)
- topic to publish
(apparemment les données à envoyer au broker)
le programme suivant fonctionne :
(testé)
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Use NodeMCU to drive DHT11 and send temperature/humidity value to MQTT server
* Tutorial URL http://osoyoo.com/2016/11/24/use-nodemcu-to-send-temperaturehumidity-data-to-mqtt-iot-broker/
* CopyRight www.osoyoo.com
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include<dht.h>
dht DHT;
// Define NodeMCU D3 pin to as temperature data pin of DHT11
#define DHT11_PIN D3
// Update these with values suitable for your network.
const char* ssid = "Bbox-xxxxxxx";
const char* password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* mqtt_server = "broker.mqtt-dashboard.com";
//const char* mqtt_server = "iot.eclipse.org";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command is : [");
Serial.print(topic);
int p =(char)payload[0]-'0';
int chk = DHT.read11(DHT11_PIN);
// if MQTT comes a 0 message, show humidity
if(p==0)
{
Serial.println("to show humidity!]");
Serial.print(" Humidity is: " );
Serial.print(DHT.humidity, 1);
Serial.println('%');
}
// if MQTT comes a 1 message, show temperature
if(p==1)
{
// digitalWrite(BUILTIN_LED, HIGH);
Serial.println(" is to show temperature!] ");
int chk = DHT.read11(DHT11_PIN);
Serial.print(" Temp is: " );
Serial.print(DHT.temperature, 1);
Serial.println(' C');
}
Serial.println();
} //end callback
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("AlainCommand");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
int chk = DHT.read11(DHT11_PIN);
Serial.print(" Starting Humidity: " );
Serial.print(DHT.humidity, 1);
Serial.println('%');
Serial.print(" Starting Temparature ");
Serial.print(DHT.temperature, 1);
Serial.println('C');
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
// read DHT11 sensor every 10 seconds
if (now - lastMsg > 10000) {
lastMsg = now;
int chk = DHT.read11(DHT11_PIN);
String msg="Temperature temps reel : ";
msg= msg+ DHT.temperature;
msg = msg+" C ;Humidite temps reel : " ;
msg=msg+DHT.humidity ;
msg=msg+"%";
char message[58];
msg.toCharArray(message,58);
Serial.println(message);
//publish sensor data to MQTT broker
client.publish("AlainData", message);
}
}
console série (moniteur) :
13:46:41.514 -> 1384, room 16
13:46:41.514 -> tail 8
13:46:41.514 -> chksum 0x2d
13:46:41.514 -> csum 0x2d
13:46:41.514 -> v21db8fc9
13:46:41.514 -> ~ld
13:46:41.652 -> Connecting to Bbox-xxxxxxxx
13:46:42.149 -> .
13:46:42.149 -> WiFi connected
13:46:42.149 -> IP address:
13:46:42.149 -> 192.168.1.70
13:46:42.182 -> Starting Humidity: 20.0%
13:46:42.182 -> Starting Temparature 27.0C
13:46:42.182 -> Attempting MQTT connection...connected
13:46:42.281 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:46:52.283 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:47:02.286 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:47:07.187 -> Command is : [AlainCommandto show humidity!]
13:47:07.220 -> Humidity is: 20.0%
13:47:07.220 ->
13:47:11.594 -> Command is : [AlainCommand is to show temperature!]
13:47:11.660 -> Temp is: 27.08259
13:47:11.660 ->
13:47:12.289 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:47:16.828 -> Command is : [AlainCommandto show humidity!]
13:47:16.861 -> Humidity is: 17.0%
13:47:16.861 ->
13:47:22.294 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:47:23.387 -> Command is : [AlainCommand is to show temperature!]
13:47:23.420 -> Temp is: 27.08259
13:47:23.420 ->
13:47:28.190 -> Command is : [AlainCommand is to show temperature!]
13:47:28.256 -> Temp is: 27.08259
13:47:28.256 ->
13:47:32.297 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:47:42.299 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 1
13:47:52.301 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:48:02.303 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:48:12.308 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
13:48:22.310 -> Temperature temps reel : 27.00 C ;Humidite temps reel : 2
... / ...
13:52:42.331 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:52:52.330 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:02.331 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:12.332 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:22.333 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:32.335 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:38.461 -> Command is : [AlainCommandto show humidity!]
13:53:38.494 -> Humidity is: 21.0%
13:53:38.494 ->
13:53:42.336 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:53:42.966 -> Command is : [AlainCommand is to show temperature!]
13:53:43.032 -> Temp is: 26.08259
13:53:43.032 ->
13:53:52.337 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 1
13:54:02.338 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:54:12.339 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:54:22.342 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:54:32.347 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:54:42.349 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
13:54:52.350 -> Temperature temps reel : 26.00 C ;Humidite temps reel : 2
en somme tout fonctionne bien . j'ai réussi à calibrer mon programme toutes les 10 secondes contre 6 à l'origine .
je râle un peu : obligé d'utiliser un client spécial et non sur le web ... mais après tout ....
vu que c'est une apli domestique .... pourquoi pas ?
j'ai pas bien compris exactement ce qu'est mqtt et comment il fonctionne ???
dans les grandes lignes pour commencer .
Dernière modification par Debian Alain (03-02-2019 15:29:26)