servo-cloud.ino

Particle Boron · File : servo-cloud.ino · Language : Arduino · Use case : Tourner un servo à distance avec une commande cloud

The servo turns with a plain cloud call (Particle.function "tourner") and its position stays readable as a variable. The return-to-center routine after two minutes shows how to give a connected actuator sensible autonomy.

Hardware

A Particle Boron module (nRF52840, Wi-Fi + Bluetooth LE, cellular variant where applicable) with on-board antenna. 1 hobby servo (SG90) + external 5 V supply advised.

Wiring

Servo signal to A1, + to external 5 V, - to GND common with the board.

How it works

The Servo library generates the 50 Hz pulse; tourner() validates the 0-180 setpoint, pushes it to the servo and returns it to the cloud. The position variable stays queryable anytime.

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 memorized positions ("home", "scan"), or chain a position sequence for a repeated gesture.

servo-cloud.ino

// Servo d'angle sur A1, position commandee par Particle.function.
#include "Servo.h"

Servo myservo;
int position = 90;

int tourner(String args) {
  int v = args.toInt();
  if (v < 0 || v > 180) return -1;
  position = v;
  myservo.write(position);
  return position;
}

void setup() {
  myservo.attach(A1);   // signal du servo sur A1
  myservo.write(position);
  Particle.function("tourner", tourner);
  Particle.variable("position", position);
  Serial.begin(9600);
}

void loop() {
  // Petite routine : revenir au centre apres 2 min sans commande.
  static unsigned long dernierOrdre = 0;
  if (position != 90 && millis() - dernierOrdre > 120000) {
    dernierOrdre = millis();
    tourner("90");
  }
  // Met a jour la reference du temps a chaque commande recue.
  static int dernierePos = 90;
  if (position != dernierePos) {
    dernierePos = position;
    dernierOrdre = millis();
  }
}

// Commande : Particle.function "tourner" avec "45", "90", "135".

← Back to the board