You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_MQTT.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // MQTT client
  2. #include <PubSubClient.h>
  3. #define MQTT_broker "192.168.0.11"
  4. #define MQTT_port 1883
  5. #define MQTT_user ""
  6. #define MQTT_password ""
  7. // DEFINITION DES TOPICS POUR CE MODULE -------------------------------------------
  8. char topic_lumiere[8 + DEVICEID_SIZE];
  9. char topic_lumiere_color[8 + 6 + DEVICEID_SIZE];
  10. char topic_lumiere_bright[8 + 11 + DEVICEID_SIZE];
  11. char topic_lumiere_anim[8 + 10 + DEVICEID_SIZE];
  12. char topic_lumiere_rempli[8 + 7 + DEVICEID_SIZE];
  13. WiFiClient espClient;
  14. PubSubClient clientMQTT(espClient); // Definition du client MQTT
  15. char g_CHAR_messageBuff[100];
  16. // --------------------------------------------------------------------------------
  17. // Reconnexion au serveur MQTT
  18. //
  19. void MQTT_connect() {
  20. DEBUG("Boucle jusqu'à obtenir une connexion");
  21. //Boucle jusqu'à obtenir une connexion
  22. while (!clientMQTT.connected()) {
  23. DEBUG("Connexion au serveur MQTT...");
  24. // ON arrive à se conecter au brocker MQTT
  25. if (clientMQTT.connect(HostName, MQTT_user, MQTT_password)) {
  26. DEBUG("OK");
  27. // Connection au brocker MQTT ratée
  28. } else {
  29. DEBUG("KO, erreur : ");
  30. DEBUG(clientMQTT.state());
  31. DEBUG(" On attend 5 secondes avant de recommencer");
  32. delay(5000);
  33. }
  34. }
  35. // Souscription aux topics
  36. clientMQTT.subscribe("lumiere/#");
  37. }
  38. // --------------------------------------------------------------------------------
  39. // Déclenche les actions à la réception d'un message MQTT.
  40. // lumiere/portal [ON|OFF] : Allumage de la barre de LEDS.
  41. // lumiere/portal/color [#RRVVBB] : Changement de couleur des LEDS.
  42. // lumiere/portal/animation [1/2/3/4/5] : Animation des LEDS.
  43. //
  44. void MQTT_callback(char* topic, byte* payload, unsigned int length) {
  45. // create character buffer with ending null terminator (string)
  46. char message[100];
  47. unsigned int i;
  48. for ( i = 0; i < length; i++) {
  49. message[i] = payload[i];
  50. }
  51. message[i] = '\0';
  52. // Traitement des topics
  53. // .....................................................................................
  54. Couleur c;
  55. if ( strcmp( topic, topic_lumiere ) ==0 ) {
  56. DEBUG("Detection du topics :" + String( topic_lumiere ));
  57. if ( String( message ) == "ON") {
  58. DEBUG("Allumage les leds");
  59. c.B = 255;
  60. LED_changeCouleur( c, 10 );
  61. } else if ( String( message ) == "OFF") {
  62. DEBUG("Extinction des leds");
  63. LED_changeCouleurInverse( c, 10 );
  64. }
  65. g_BOO_AnimationSeconde = false;
  66. // .....................................................................................
  67. } else if ( strcmp( topic, topic_lumiere_color) == 0) {
  68. DEBUG("Detection du topics :" + String( topic_lumiere_color ));
  69. // Test si on a une couleur RGB dans le message
  70. if ( LED_isAColor( message ) ) {
  71. // Définition de la couleur
  72. c = LED_ExtractRVB( message );
  73. DEBUG("Affichage de la couleur : " + String(c.R) + " " + String(c.V) + " " + String(c.B));
  74. // Changemnt des LEDS avec la couleur
  75. LED_changeCouleur( c, 10 );
  76. }
  77. // .....................................................................................
  78. } else if ( strcmp( topic, topic_lumiere_bright) == 0 ) {
  79. DEBUG("Detection du topics :" + String( topic_lumiere_bright ));
  80. // Test si on a bien une valeur numérique
  81. if ( LED_isADigit( message ) ) {
  82. DEBUG("Luminosite : " + String( message ));
  83. strip.setBrightness( String( message ).toInt() % 255 );
  84. strip.show();
  85. }
  86. // .....................................................................................
  87. } else if ( strcmp( topic, topic_lumiere_anim) ==0 ) {
  88. DEBUG("Detection du topics :" + String( topic_lumiere_anim ));
  89. DEBUG("Lancement de l'Animation avec le parametre :" + String( message ));
  90. LED_Animation(String( message ).toInt());
  91. // .....................................................................................
  92. } else if ( strcmp( topic, topic_lumiere_rempli) ==0 ) {
  93. DEBUG("Detection du topics :" + String( topic_lumiere_rempli ));
  94. DEBUG("Lancement du remplissage à :" + String( message ));
  95. LED_Remplissage(String( message ).toInt());
  96. }
  97. }
  98. // --------------------------------------------------------------------------------
  99. // Initialisation du brocker MQTT.
  100. //
  101. void MQTT_setup(){
  102. // Création du client MQTT
  103. DEBUG("Création du client MQTT");
  104. clientMQTT.setServer(MQTT_broker, MQTT_port); // Configuration de la connexion au serveur MQTT
  105. DEBUG("fonction de callback");
  106. clientMQTT.setCallback(MQTT_callback); // La fonction de callback qui est executée à chaque réception de message
  107. // Connection au Brocker MQTT
  108. DEBUG("Connection au Brocker MQTT");
  109. MQTT_connect();
  110. // Construction des topcs auxquels s'abonner.
  111. DEBUG("Construction des topcs auxquels s'abonner.");
  112. sprintf( topic_lumiere, "lumiere/%s", DeviceID);
  113. sprintf( topic_lumiere_color, "lumiere/color/%s", DeviceID);
  114. sprintf( topic_lumiere_bright, "lumiere/brightness/%s", DeviceID);
  115. sprintf( topic_lumiere_anim, "lumiere/animation/%s", DeviceID);
  116. sprintf( topic_lumiere_rempli, "lumiere/allume/%s", DeviceID);
  117. }