diff --git a/clientlua b/clientlua new file mode 100644 index 0000000..73a0c92 --- /dev/null +++ b/clientlua @@ -0,0 +1,79 @@ +-- Description: Client MQTT pour jouer une mélodie sur un ESP8266 +client = mqtt.client("AlarmeStarWars","localhost",1883,false) +client:connect("student","ensibs") + +-- Initialiser la broche GPIO2 pour la sortie +pio.pin.setdir(pio.OUTPUT, pio.GPIO2) + +-- Définir la mélodie simplifiée Star Wars +local melody = { + {"A4", 500}, {"A4", 500}, {"F4", 350}, {"C5", 150}, + {"A4", 500}, {"F4", 350}, {"C5", 150}, {"A4", 1000}, + {"E5", 500}, {"E5", 500}, {"E5", 500}, + {"F5", 350}, {"C5", 150}, {"G4", 500}, + {"F4", 350}, {"C5", 150}, {"A4", 1000} +} + +-- Fréquences associées aux notes (en Hz) +local notes = { + ["A4"] = 440, ["F4"] = 349, ["C5"] = 523, + ["E5"] = 659, ["F5"] = 698, ["G4"] = 392 +} + +-- Allumer la broche GPIO2 +function ledon() + pio.pin.sethigh(pio.GPIO2) +end + +-- Éteindre la broche GPIO2 +function ledoff() + pio.pin.setlow(pio.GPIO2) +end + +-- Jouer une note +function playTone(freq, duration) + local period = 1000000 / freq -- période en microsecondes + local cycles = (duration * 1000) / period + + for i = 1, cycles do + ledon() + tmr.delay(period / 2) -- moitié HIGH + ledoff() + tmr.delay(period / 2) -- moitié LOW + end +end + +-- Fonction pour jouer la mélodie +function playMelody() + for i, note in ipairs(melody) do + local n, d = note[1], note[2] + local freq = notes[n] or 0 + if freq == 0 then + -- Pause : silence + ledoff() + tmr.delay(d * 1000) + else + playTone(freq, d) + end + -- Petite pause entre les notes + tmr.delay(50 * 1000) + end +end + +-- Lancer la musique +-- playMelody() + +-- Cette fonction sera appelée chaque fois qu'un message est reçu sur le canal souscrit +function recp(mess) + if mess then + playMelody() -- Jouer la mélodie à chaque message reçu + end +end + +-- Boucle principale pour maintenir le script actif +while true do + tmr.delay(1000) -- Attendre 1 seconde entre les itérations +end + +client:subscribe("NOM_CHANNEL", mqtt.QOS0, recp) + diff --git a/wifi_client.lua b/wifi_client.lua deleted file mode 100644 index 0cec7d4..0000000 --- a/wifi_client.lua +++ /dev/null @@ -1,32 +0,0 @@ -function start_wifi() - print("Starting WiFi...") - wifi.setmode(wifi.STATIONAP) - wifi.sta.config("SachamamaGoingHome", "GoFoundIt") -- Set SSID and password - wifi.sta.connect() -end - -local dataReceive={} - -start_wifi() - -tmr.alarm(0, 1000, 1, function() - if wifi.sta.status() == wifi.STA_GOTIP then - print("WiFi connected!") - print("IP Address: " .. wifi.sta.getip()) - tmr.stop(0) - - elseif wifi.sta.status() == wifi.STA_CONNECTING then - print("Connecting to WiFi...") - else - print("WiFi connection failed.") - end -end) - --- Function to send data to the server -function sendData(data) - local conn = net.createConnection(net.TCP, 0) - conn:on("receive", function(conn, payload) print(payload) end) - conn:connect(80, "") -end - - diff --git a/wifi_server.lua b/wifi_server.lua deleted file mode 100644 index 94e06b6..0000000 --- a/wifi_server.lua +++ /dev/null @@ -1,9 +0,0 @@ --- Initialize the Wi-Fi server -wifi.setmode(wifi.STATIONAP) -wifi.sta.config("SachamamaGoingHome", "GoFoundIt") -wifi.sta.connect() -wifi.sta.http.start() - -wifi.sta.http.on("receive", function(conn, payload) - print(payload) -end) \ No newline at end of file