buzzer-alarme.ino

Particle Boron · File : buzzer-alarme.ino · Language : Arduino · Use case : Buzzer d'alarme déclenché par un événement cloud

A cloud event "alarme" triggers a two-tone siren on a passive buzzer, and "off" stops it. The sound pattern is non-blocking (tone with duration + 200 ms step), so the alarm stays drivable while sounding.

Hardware

A Particle Boron module (nRF52840, Wi-Fi + Bluetooth LE, cellular variant where applicable) with on-board antenna. 1 passive buzzer 3.3-5 V.

Wiring

Buzzer between D6 and GND. tone() generates the frequency; an active buzzer would not work (fixed beep).

How it works

Particle.subscribe runs the handler on each "alarme" event; loop() alternates two frequencies every 200 ms without ever blocking the cloud link.

Running it

Install the Particle CLI (particle setup), flash with particle flash --serial or OTA (particle flash <name> firmware.bin). The Device Cloud app or REST API complete the deployment.

Variants

Add a local mute button, or blink LED D7 in sync with the sound.

buzzer-alarme.ino

// Buzzer passif sur D6 : sequence d'alarme declenchee par evenement.
const int BUZZER = D6;
bool alarme = false;

void surEvenement(String event, String data) {
  if (data == "on")  alarme = true;
  if (data == "off") alarme = false;
}

void setup() {
  pinMode(BUZZER, OUTPUT);
  Particle.subscribe("alarme", surEvenement, MY_DEVICES);
  Serial.begin(9600);
}

void loop() {
  static uint32_t t0 = 0;
  static int etape = 0;
  if (millis() - t0 < 200) return;
  t0 = millis();

  if (alarme) {
    // Motif deux tons repetes (la, si, la, si...).
    static const int NOTES[] = {440, 587};
    tone(BUZZER, NOTES[etape % 2], 180);
    etape++;
  } else {
    noTone(BUZZER);
  }
}

// Declencher depuis une autre carte ou l'API :
// Particle.publish("alarme", "on", 60, PRIVATE);

← Back to the board