-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoc
208 lines (159 loc) · 8.31 KB
/
Doc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
Documentation (English)
WiFiManager Class
The WiFiManager class is designed to manage WiFi connections on an ESP8266 using MicroPython. It can load and save WiFi network credentials, scan for available networks, connect to WiFi, and update the network credentials from a GitHub repository.
Methods
__init__(self, filename='wifi_networks.json', github_url='https://raw.githubusercontent.com/yourusername/yourrepo/main/wifi_networks.json')
Initializes the WiFiManager instance.
Parameters:
filename: The name of the file where WiFi network credentials are stored.
github_url: The URL of the GitHub file containing the updated WiFi network credentials.
load_wifi_networks(self)
Loads WiFi network credentials from the specified file.
Returns: A list of WiFi networks.
save_wifi_networks(self)
Saves the current WiFi network credentials to the specified file.
scan_networks(self)
Scans for available WiFi networks.
Returns: A list of available networks.
connect_to_wifi(self, ssid, password)
Attempts to connect to a WiFi network with the given SSID and password.
Parameters:
ssid: The SSID of the WiFi network.
password: The password of the WiFi network.
Returns: True if connected successfully, False otherwise.
manage_connections(self)
Manages the WiFi connections by attempting to connect to each network in the list.
Stops after three consecutive failures.
add_wifi_network(self, ssid, password)
Adds a new WiFi network to the list and saves it to the file.
Parameters:
ssid: The SSID of the new WiFi network.
password: The password of the new WiFi network.
update_wifi_networks_from_github(self)
Checks for an updated version of the WiFi network credentials file in a GitHub repository.
If the local file is outdated, it downloads and replaces it with the most updated version from GitHub.
MQTTManager Class
The MQTTManager class is designed to manage MQTT connections on an ESP8266 using MicroPython. It supports different security configurations like QoS, SSL/TLS security, user/password authentication, and persistence.
Methods
__init__(self, broker, port=1883, client_id='esp8266', username=None, password=None, ssl=False, ssl_params=None, qos=0, keepalive=60)
Initializes the MQTTManager instance.
Parameters:
broker: The address of the MQTT broker.
port: The port to connect to the MQTT broker (default is 1883).
client_id: The client ID to use for the connection.
username: The username for authentication (optional).
password: The password for authentication (optional).
ssl: Whether to use SSL/TLS for the connection (default is False).
ssl_params: SSL parameters (optional).
qos: The Quality of Service level (default is 0).
keepalive: The keepalive interval in seconds (default is 60).
connect(self)
Connects to the MQTT broker.
publish(self, topic, message)
Publishes a message to a specified topic.
Parameters:
topic: The topic to publish the message to.
message: The message to publish.
subscribe(self, topic)
Subscribes to a specified topic.
Parameters:
topic: The topic to subscribe to.
set_callback(self, callback)
Sets the callback function to handle incoming messages.
Parameters:
callback: The callback function.
check_msg(self)
Checks for incoming messages.
disconnect(self)
Disconnects from the MQTT broker.
subscribe_to_sys_topics(self)
Subscribes to the $SYS topics to monitor the broker status.
display_sys_status(self)
Displays the status of the broker by subscribing to the $SYS topics and printing the messages.
Example of Use
Creating an Instance and Connecting to the Broker
from mqtt_manager import MQTTManager
def main():
broker = 'your_broker_address'
port = 8883 # Use 8883 for SSL/TLS, 1883 for non-SSL
client_id = 'esp8266'
username = 'your_username'
password = 'your_password'
ssl = True
ssl_params = {'cert_reqs': ussl.CERT_NONE} # Adjust SSL parameters as needed
qos = 1
keepalive = 60
mqtt_manager = MQTTManager(
broker=broker,
port=port,
client_id=client_id,
username=username,
password=password,
ssl=ssl,
ssl_params=ssl_params,
qos=qos,
keepalive=keepalive
)
mqtt_manager.connect()
# Display broker status
mqtt_manager.display_sys_status()
if __name__ == '__main__':
main()
Documentation (Spanish)
Clase MQTTManager
La clase MQTTManager está diseñada para gestionar conexiones MQTT en un ESP8266 usando MicroPython. Soporta diferentes configuraciones de seguridad como QoS, seguridad SSL/TLS, autenticación de usuario/contraseña y persistencia.
Métodos
__init__(self, broker, port=1883, client_id='esp8266', username=None, password=None, ssl=False, ssl_params=None, qos=0, keepalive=60)
Inicializa la instancia de MQTTManager.
Parámetros:
broker: La dirección del broker MQTT.
port: El puerto para conectarse al broker MQTT (por defecto es 1883).
client_id: El ID del cliente para usar en la conexión.
username: El nombre de usuario para la autenticación (opcional).
password: La contraseña para la autenticación (opcional).
ssl: Si se debe usar SSL/TLS para la conexión (por defecto es False).
ssl_params: Parámetros SSL (opcional).
qos: El nivel de Calidad de Servicio (por defecto es 0).
keepalive: El intervalo de keepalive en segundos (por defecto es 60).
connect(self)
Conecta al broker MQTT.
publish(self, topic, message)
Publica un mensaje en un tema especificado.
Parámetros:
topic: El tema en el que publicar el mensaje.
message: El mensaje a publicar.
subscribe(self, topic)
Se suscribe a un tema especific
Clase WiFiManager
La clase WiFiManager está diseñada para gestionar conexiones WiFi en un ESP8266 usando MicroPython. Puede cargar y guardar credenciales de redes WiFi, escanear redes disponibles, conectarse a WiFi y actualizar las credenciales de red desde un repositorio de GitHub.
Métodos
__init__(self, filename='wifi_networks.json', github_url='https://raw.githubusercontent.com/yourusername/yourrepo/main/wifi_networks.json')
Inicializa la instancia de WiFiManager.
Parámetros:
filename: El nombre del archivo donde se almacenan las credenciales de las redes WiFi.
github_url: La URL del archivo de GitHub que contiene las credenciales actualizadas de las redes WiFi.
load_wifi_networks(self)
Carga las credenciales de las redes WiFi desde el archivo especificado.
Retorna: Una lista de redes WiFi.
save_wifi_networks(self)
Guarda las credenciales actuales de las redes WiFi en el archivo especificado.
scan_networks(self)
Escanea las redes WiFi disponibles.
Retorna: Una lista de redes disponibles.
connect_to_wifi(self, ssid, password)
Intenta conectarse a una red WiFi con el SSID y la contraseña dados.
Parámetros:
ssid: El SSID de la red WiFi.
password: La contraseña de la red WiFi.
Retorna: True si se conecta exitosamente, False de lo contrario.
manage_connections(self)
Gestiona las conexiones WiFi intentando conectarse a cada red en la lista.
Se detiene después de tres fallos consecutivos.
add_wifi_network(self, ssid, password)
Añade una nueva red WiFi a la lista y la guarda en el archivo.
Parámetros:
ssid: El SSID de la nueva red WiFi.
password: La contraseña de la nueva red WiFi.
update_wifi_networks_from_github(self)
Verifica si hay una versión actualizada del archivo de credenciales de redes WiFi en un repositorio de GitHub.
Si el archivo local está desactualizado, lo descarga y lo reemplaza con la versión más actualizada de GitHub.