servo-cloud.ino

Particle Photon · 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 Photon module (2.4 GHz Wi-Fi, STM32 microcontroller, metal shielding) powered by USB or VIN. 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

With the Particle CLI: particle flash --serial (DFU mode, double reset) or OTA flash once the board is online. Safe Mode (reset + WiFi) covers troubleshooting.

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