Add MQTT publishing
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								code/lib/umqtt/robust.mpy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								code/lib/umqtt/robust.mpy
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								code/lib/umqtt/simple.mpy
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								code/lib/umqtt/simple.mpy
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										95
									
								
								code/main.py
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								code/main.py
									
									
									
									
									
								
							| @@ -23,17 +23,14 @@ __version__ = 0.3 | |||||||
| import time | import time | ||||||
| import sys | import sys | ||||||
| import machine | import machine | ||||||
|  | import esp | ||||||
| import neopixel | import neopixel | ||||||
| from encoder import Encoder | from encoder import Encoder | ||||||
|  | import ntptime | ||||||
|  | from umqtt.robust import MQTTClient | ||||||
| import light_modes | import light_modes | ||||||
|  | import config | ||||||
|  |  | ||||||
| # Paramètres |  | ||||||
|  |  | ||||||
| NB_PIX = 67                  # Nombre de pixels de la 1e bande de LED |  | ||||||
| NB_PIX2 = 67                 # Nombre de pixels de la 2e bande de LED |  | ||||||
| MAX_BRIGHT = 100             # Luminosité max (100 max.) |  | ||||||
| USR_COLOR = [255, 130, 20]   # Couleur de base (à luminosité max) |  | ||||||
|  |  | ||||||
| # Déclaration des objets et initialisation des variables | # Déclaration des objets et initialisation des variables | ||||||
|  |  | ||||||
| @@ -48,8 +45,8 @@ ENC_PIN_C = 13      # broche du clic central de l'encodeur | |||||||
| WIFI_LED_PIN = 0 | WIFI_LED_PIN = 0 | ||||||
|  |  | ||||||
| # With ESP8266 (no timing parameter) | # With ESP8266 (no timing parameter) | ||||||
| NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX) | NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), config.NB_PIX) | ||||||
| NPXL_STRIP2 = neopixel.NeoPixel(machine.Pin(NEOPIX2_PIN), NB_PIX) | # NPXL_STRIP2 = neopixel.NeoPixel(machine.Pin(NEOPIX2_PIN), config.NB_PIX2) | ||||||
|  |  | ||||||
| # Eteint tout à l'initialisation | # Eteint tout à l'initialisation | ||||||
| NPXL_STRIP.fill([0, 0, 0]) | NPXL_STRIP.fill([0, 0, 0]) | ||||||
| @@ -57,7 +54,7 @@ NPXL_STRIP.write() | |||||||
|  |  | ||||||
| # Encodeur rotatif | # Encodeur rotatif | ||||||
| ENCODER = Encoder(ENC_PIN_A, ENC_PIN_B, | ENCODER = Encoder(ENC_PIN_A, ENC_PIN_B, | ||||||
|                   min_val=0, max_val=MAX_BRIGHT, |                   min_val=0, max_val=config.MAX_BRIGHT, | ||||||
|                   clicks=1) |                   clicks=1) | ||||||
|  |  | ||||||
| # Bouton | # Bouton | ||||||
| @@ -66,18 +63,64 @@ ENC_BUT = machine.Pin(ENC_PIN_C, machine.Pin.IN) | |||||||
| # LED status WIFI_LED_PIN | # LED status WIFI_LED_PIN | ||||||
| WIFI_LED = machine.Pin(WIFI_LED_PIN, machine.Pin.OUT) | WIFI_LED = machine.Pin(WIFI_LED_PIN, machine.Pin.OUT) | ||||||
|  |  | ||||||
|  | # RTC setup | ||||||
|  | rtc = machine.RTC() | ||||||
|  |  | ||||||
| # initialisation des variables d'état | # initialisation des variables d'état | ||||||
| BRIGHTN = 50            # Luminosité (0 - 100) | BRIGHTN = 50            # Luminosité (0 - 100) | ||||||
| PWR = True              # Est-ce que l'éclairage est allumé ? | PWR = True              # Est-ce que l'éclairage est allumé ? | ||||||
| BUTTN_STATE = 1 | BUTTN_STATE = 1 | ||||||
| CURRENT_MODE = 0 | CURRENT_MODE = 0 | ||||||
| PWROFF_DELAY = 0 | PWROFF_DELAY = 0 | ||||||
|  | MQTT_DELAY = 2          # Délais en sec. avant d'envoyer les données au MQTT | ||||||
|  |  | ||||||
|  | # Connection au broker MQTT | ||||||
|  | CLIENT = MQTTClient(client_id=config.CLIENT_ID, | ||||||
|  |                     server=config.MQTT_HOST, | ||||||
|  |                     user=config.MQTT_USERNAME, | ||||||
|  |                     password=config.MQTT_PASSWD, | ||||||
|  |                     port=config.MQTT_PORT) | ||||||
|  | CLIENT.DEBUG = True | ||||||
|  | CLIENT.connect() | ||||||
|  |  | ||||||
| ############# | ############# | ||||||
| # FUNCTIONS # | # FUNCTIONS # | ||||||
| ############# | ############# | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def set_rtc(): | ||||||
|  |     while True: | ||||||
|  |         try: | ||||||
|  |             return ntptime.settime() | ||||||
|  |         except OSError as e: | ||||||
|  |             if e.errno == 110: | ||||||
|  |                 time.sleep(2) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def now(clock): | ||||||
|  |     """Return a string representing date/time now.""" | ||||||
|  |     return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*clock.datetime()) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def MqttPublish(topic, message, retain=False, qos=0, sleep=0): | ||||||
|  |     """MQTT publish helper.""" | ||||||
|  |     global CLIENT | ||||||
|  |  | ||||||
|  |     CLIENT.publish("{location}/{device}/{topic}".format(location=config.LOCATION, | ||||||
|  |                                                         device=CLIENT.client_id, | ||||||
|  |                                                         topic=topic), | ||||||
|  |                    message, | ||||||
|  |                    retain=retain, | ||||||
|  |                    qos=qos) | ||||||
|  |     time.sleep_ms(sleep) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def MqttMultiPublish(payload, sleep=0): | ||||||
|  |     """Send multiple data""" | ||||||
|  |     for pl in payload: | ||||||
|  |         MqttPublish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep) | ||||||
|  |  | ||||||
|  |  | ||||||
| def power_cycle(): | def power_cycle(): | ||||||
|     """ON/OFF function""" |     """ON/OFF function""" | ||||||
|     global PWR |     global PWR | ||||||
| @@ -102,6 +145,10 @@ def power_cycle(): | |||||||
|     while ENC_BUT.value() == 0: |     while ENC_BUT.value() == 0: | ||||||
|         time.sleep_ms(10) |         time.sleep_ms(10) | ||||||
|  |  | ||||||
|  |     # Envoi de la nouvelle info au MQTT | ||||||
|  |     MqttMultiPublish([['power/date', now(rtc), True, 1], | ||||||
|  |                       ['power/value', "{}".format(PWR), True, 0]]) | ||||||
|  |  | ||||||
|  |  | ||||||
| def update_button_c(button, button_state, pwr, mode): | def update_button_c(button, button_state, pwr, mode): | ||||||
|     """Surveille le bouton C (clic central)""" |     """Surveille le bouton C (clic central)""" | ||||||
| @@ -130,6 +177,9 @@ def update_button_c(button, button_state, pwr, mode): | |||||||
|             if mode >= len(light_modes.MODES_LST): |             if mode >= len(light_modes.MODES_LST): | ||||||
|                 mode = 0 |                 mode = 0 | ||||||
|  |  | ||||||
|  |             MqttMultiPublish([['mode/date', now(rtc), True, 1], | ||||||
|  |                               ['mode/value', "{}".format(mode), True, 1]]) | ||||||
|  |  | ||||||
|     return mode |     return mode | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -144,6 +194,26 @@ def update_wifi_led(): | |||||||
| # BOUCLE PRINCIPALE # | # BOUCLE PRINCIPALE # | ||||||
| ##################### | ##################### | ||||||
|  |  | ||||||
|  | set_rtc() | ||||||
|  |  | ||||||
|  | # Envoi de l'état de départ au broker | ||||||
|  | data = [["last_boot", now(rtc), True, 1], | ||||||
|  |         ["location", config.LOCATION, True, 1], | ||||||
|  |         ["sys/wifi_ssid", WLAN.config('essid'), True, 0], | ||||||
|  |         ["sys/ip", WLAN.ifconfig()[0], True, 1], | ||||||
|  |         ["sys/esp8266_device_id", "{:d}".format(esp.flash_id()), True, 1], | ||||||
|  |         ['mode/date', now(rtc), True, 1], | ||||||
|  |         ["mode/value", "{}".format(CURRENT_MODE), True, 1], | ||||||
|  |         ['brightness/date', now(rtc), True, 1], | ||||||
|  |         ["brightness/value", "{}".format(BRIGHTN), True, 1], | ||||||
|  |         ['power/date', now(rtc), True, 1], | ||||||
|  |         ["power/value", "{}".format(PWR), True, 1], | ||||||
|  |         ] | ||||||
|  | MqttMultiPublish(data) | ||||||
|  |  | ||||||
|  | time.sleep(1) | ||||||
|  |  | ||||||
|  |  | ||||||
| while True: | while True: | ||||||
|  |  | ||||||
|     # Si on a un changement de valeur, on met à jour la luminosité |     # Si on a un changement de valeur, on met à jour la luminosité | ||||||
| @@ -151,11 +221,14 @@ while True: | |||||||
|         if BRIGHTN != ENCODER.value: |         if BRIGHTN != ENCODER.value: | ||||||
|             print(ENCODER.value) |             print(ENCODER.value) | ||||||
|             BRIGHTN = ENCODER.value |             BRIGHTN = ENCODER.value | ||||||
|  |             MqttMultiPublish([['brightness/date', now(rtc), True, 1], | ||||||
|  |                               ['brightness/value', "{}".format(BRIGHTN), True, 0]]) | ||||||
|  |  | ||||||
|         # Mise à jour des LED |         # Mise à jour des LED | ||||||
|         light_modes.update_neopixel(CURRENT_MODE, |         light_modes.update_neopixel(CURRENT_MODE, | ||||||
|                                     NPXL_STRIP, |                                     NPXL_STRIP, | ||||||
|                                     USR_COLOR, BRIGHTN) |                                     config.USR_COLOR, | ||||||
|  |                                     BRIGHTN) | ||||||
|  |  | ||||||
|     # Si on laisse appuyer 2 secondes: extinction |     # Si on laisse appuyer 2 secondes: extinction | ||||||
|     CURRENT_MODE = update_button_c(ENC_BUT, |     CURRENT_MODE = update_button_c(ENC_BUT, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user