client: finish
This commit is contained in:
parent
55603f969e
commit
c474aff5eb
3 changed files with 79 additions and 41 deletions
79
clientlua
Normal file
79
clientlua
Normal file
|
@ -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)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue