forked from tanchou/Verilog
Update serial communication code: change COM port in read_rx.py and add ESP32 command interpretation in esp32_read.py
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import serial
|
import serial
|
||||||
import time
|
import time
|
||||||
|
|
||||||
ser = serial.Serial('COM6', 115200)
|
ser = serial.Serial('COM5', 115200)
|
||||||
ser.timeout = 1
|
ser.timeout = 1
|
||||||
|
|
||||||
# Boucle pour lire les données du port série
|
# Boucle pour lire les données du port série
|
||||||
|
111
Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino
Normal file
111
Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
|
||||||
|
const char* ssid = "ESP32-Louis";
|
||||||
|
const char* password = "motdepasse";
|
||||||
|
|
||||||
|
WebServer server(80);
|
||||||
|
|
||||||
|
void handleRoot() {
|
||||||
|
digitalWrite(2,HIGH);
|
||||||
|
wifi_sta_list_t sta_list;
|
||||||
|
esp_wifi_ap_get_sta_list(&sta_list);
|
||||||
|
|
||||||
|
String page = "";
|
||||||
|
page += "<!DOCTYPE html>";
|
||||||
|
page += "<html>";
|
||||||
|
page += "<head>";
|
||||||
|
page += "<title>ESP32</title>";
|
||||||
|
page += "<meta charset=\"UTF-8\">";
|
||||||
|
page += "</head>";
|
||||||
|
page += "<body>";
|
||||||
|
page += "<h1>Appareils connectés à l'ESP32</h1>";
|
||||||
|
page += "<ul>";
|
||||||
|
|
||||||
|
page += "<ul>";
|
||||||
|
|
||||||
|
for (int i = 0; i < sta_list.num; i++) {
|
||||||
|
const wifi_sta_info_t& client = sta_list.sta[i];
|
||||||
|
char macStr[18];
|
||||||
|
snprintf(macStr, sizeof(macStr),
|
||||||
|
"%02X:%02X:%02X:%02X:%02X:%02X",
|
||||||
|
client.mac[0], client.mac[1], client.mac[2],
|
||||||
|
client.mac[3], client.mac[4], client.mac[5]);
|
||||||
|
page += "<li>MAC : ";
|
||||||
|
page += macStr;
|
||||||
|
page += "</li>";
|
||||||
|
}
|
||||||
|
|
||||||
|
page += "</ul>";
|
||||||
|
page += "<p>Nombre total : " + String(sta_list.num) + "</p>";
|
||||||
|
|
||||||
|
page += "</body></html>";
|
||||||
|
|
||||||
|
server.send(200, "text/html", page);
|
||||||
|
digitalWrite(2,LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onClientConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||||
|
digitalWrite(2, HIGH);
|
||||||
|
|
||||||
|
const wifi_event_ap_staconnected_t* conn = reinterpret_cast<const wifi_event_ap_staconnected_t*>(&info);
|
||||||
|
|
||||||
|
|
||||||
|
byte packet[11]; // 2 header + 1 code + 6 MAC + 1 fin
|
||||||
|
packet[0] = 0x02;
|
||||||
|
packet[1] = 0x02;
|
||||||
|
packet[2] = 0x01;
|
||||||
|
|
||||||
|
// Copier l'adr MAC dans le tableau
|
||||||
|
memcpy(&packet[3], conn->mac, 6);
|
||||||
|
|
||||||
|
packet[9] = 0x03;
|
||||||
|
|
||||||
|
Serial.write(packet, sizeof(packet));
|
||||||
|
digitalWrite(2, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onClientDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||||
|
digitalWrite(2, HIGH);
|
||||||
|
|
||||||
|
const wifi_event_ap_stadisconnected_t* disc = reinterpret_cast<const wifi_event_ap_stadisconnected_t*>(&info);
|
||||||
|
|
||||||
|
byte packet[11];
|
||||||
|
packet[0] = 0x02;
|
||||||
|
packet[1] = 0x02;
|
||||||
|
packet[2] = 0x00;
|
||||||
|
|
||||||
|
memcpy(&packet[3], disc->mac, 6);
|
||||||
|
packet[9] = 0x03;
|
||||||
|
|
||||||
|
Serial.write(packet, sizeof(packet));
|
||||||
|
digitalWrite(2, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
if (!WiFi.softAP(ssid, password)) {
|
||||||
|
byte packet[] = {0x02, 0x01, 0x00, 0x03};
|
||||||
|
Serial.write(packet, sizeof(packet));
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFi.onEvent(onClientConnected, ARDUINO_EVENT_WIFI_AP_STACONNECTED);
|
||||||
|
WiFi.onEvent(onClientDisconnected, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED);
|
||||||
|
|
||||||
|
delay(1000); // Donne un peu de temps pour démarrer le WiFi
|
||||||
|
|
||||||
|
|
||||||
|
server.on("/", handleRoot);
|
||||||
|
server.begin();
|
||||||
|
|
||||||
|
pinMode(2, OUTPUT);
|
||||||
|
|
||||||
|
byte packet[] = {0x02, 0x01, 0x01, 0x03};
|
||||||
|
Serial.write(packet, sizeof(packet));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
server.handleClient();
|
||||||
|
}
|
46
Semaine_2/Projet_esp32/esp32_code/Projet_code/esp32_read.py
Normal file
46
Semaine_2/Projet_esp32/esp32_code/Projet_code/esp32_read.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import serial
|
||||||
|
|
||||||
|
def format_mac(mac_bytes):
|
||||||
|
return ":".join(f"{b:02X}" for b in mac_bytes)
|
||||||
|
|
||||||
|
def interpret_command(data):
|
||||||
|
if len(data) == 10 and data[0] == 0x02 and data[1] == 0x02 and data[-1] == 0x03:
|
||||||
|
cmd_type = data[2]
|
||||||
|
mac = data[3:9]
|
||||||
|
if cmd_type == 0x01:
|
||||||
|
return f"[+] Appareil connecté : {format_mac(mac)}"
|
||||||
|
elif cmd_type == 0x00:
|
||||||
|
return f"[-] Appareil déconnecté : {format_mac(mac)}"
|
||||||
|
else:
|
||||||
|
return f"[?] Commande inconnue : {data.hex()}"
|
||||||
|
elif len(data) == 4 and data[0] == 0x02 and data[1] == 0x01 and data[-1] == 0x03:
|
||||||
|
return "[*] Démarrage ESP32"
|
||||||
|
else:
|
||||||
|
return "[!] Donnée non reconnue"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
port = "COM5" # Remplace par ton port série (ex. /dev/ttyUSB0 sous Linux)
|
||||||
|
baudrate = 115200
|
||||||
|
|
||||||
|
with serial.Serial(port, baudrate, timeout=1) as ser:
|
||||||
|
print(f"Lecture sur {port} à {baudrate} bauds...\n")
|
||||||
|
buffer = bytearray()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
byte = ser.read()
|
||||||
|
if not byte:
|
||||||
|
continue
|
||||||
|
|
||||||
|
buffer += byte
|
||||||
|
|
||||||
|
# Si on détecte le début (0x02) et fin (0x03), on traite
|
||||||
|
if buffer[0] == 0x02 and 0x03 in buffer:
|
||||||
|
end_index = buffer.index(0x03)
|
||||||
|
packet = buffer[:end_index + 1]
|
||||||
|
buffer = buffer[end_index + 1:]
|
||||||
|
|
||||||
|
print("Reçu :", ' '.join(f'{b:02X}' for b in packet))
|
||||||
|
print("↳", interpret_command(packet), "\n")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Reference in New Issue
Block a user