bouton-evenement.ino

Particle Boron · File : bouton-evenement.ino · Language : Arduino · Use case : Publier un événement cloud à chaque appui sur un bouton

Each press publishes a JSON event to the Particle cloud, readable by other boards (subscribers) or a webhook. The debounce wiring matches bare Arduino: the framework just adds the publish layer.

Hardware

A Particle Boron module (nRF52840, Wi-Fi + Bluetooth LE, cellular variant where applicable) with on-board antenna. 1 push button between D2 and GND.

Wiring

Button between D2 and GND; the pull-up resistor is internal (INPUT_PULLUP).

How it works

After edge validation (30 ms), snprintf builds a JSON frame and Particle.publish sends it to the cloud. Subscribers (Particle.subscribe) and webhooks receive it near real time.

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

Also publish the internal temperature (getTemperature), or trigger a local action on a received event (subscribe + handler).

bouton-evenement.ino

// Bouton sur D2 : publish d'un evenement cloud a chaque appui.
int bouton = D2;              // bouton vers GND, patte libre sur D2
bool etatPrecedent = HIGH;
unsigned long dernierBord = 0;

void setup() {
  pinMode(bouton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  bool lecture = digitalRead(bouton);
  unsigned long maintenant = millis();

  if (lecture != etatPrecedent) {
    dernierBord = maintenant;
  }
  if (maintenant - dernierBord >= 30 && lecture != etatPrecedent) {
    etatPrecedent = lecture;
    if (lecture == LOW) {                 // appui (PULLUP -> LOW)
      char trame[48];
      snprintf(trame, sizeof(trame),
               "{\"appui\":%lu,\"uptime\":%lu}",
               millis(), millis());
      bool ok = Particle.publish("bouton-presse", trame, 60, PUBLIC);
      Serial.println(ok ? "evenement publie" : "publication refusee");
      // NB : limite de flux cloud ~1 publish/s : ne pas spammer.
    }
  }
}

← Back to the board