Learning Goals 5 min
The L03-34 smart lamp worked only on your local WiFi. Today's version works from anywhere on Earth with internet — same physical LED, but reachable from a coffee shop across town. By the end of this lesson you will:
- Add three Properties to a Cloud-controlled lamp:
powerOn(bool),brightness(int 0–255),colour(CloudColor). - Use callback functions to react instantly to dashboard changes (PWM on a real LED).
- Demonstrate "control from anywhere" — toggle the lamp from a different network than the device's WiFi.
Warm-Up 10 min
Hardware: supported Cloud-capable board + one LED (or RGB LED for the stretch). Wire LED + 220 Ω resistor to a PWM-capable pin (D3 on Nano ESP32).
Reuse the DeskMonitor pattern
This lesson is essentially the smart lamp rebuilt on top of the IoT Cloud's machinery. Three properties, three callbacks, ~50 lines of total sketch code (down from ~150 hand-rolled in L03-34).
New Concept · Multi-Property control 20 min
The Properties
| Property | Type | Permission | Effect |
|---|---|---|---|
powerOn | bool | READWRITE | Switch widget. When false, LED off regardless of brightness. |
brightness | int (0–255) | READWRITE | Slider widget. |
colour | CloudColor | READWRITE | Colour picker. Only used in stretch (RGB LED). |
uptime | int | READ | Value widget. Confirms device is alive. |
The callbacks
#include "thingProperties.h"
const int LED_PIN = 3;
void setup() {
Serial.begin(9600);
delay(1500);
pinMode(LED_PIN, OUTPUT);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
}
void applyLed() {
int duty = powerOn ? brightness : 0;
analogWrite(LED_PIN, duty);
}
void onPowerOnChange() { applyLed(); }
void onBrightnessChange() { applyLed(); }
// onColourChange() — stretch, see §4.
void loop() {
ArduinoCloud.update();
uptime = millis() / 1000;
}Single applyLed() helper called by both callbacks. Single source of truth (powerOn + brightness). Clean.
Why callbacks, not polling
You could check the property values each loop iteration. But callbacks fire only when something changes — much more efficient, and naturally avoid redundant analogWrites.
Demonstrating "from anywhere"
Once running, the LED responds to the dashboard regardless of where the controlling phone / browser is. Test:
- Open dashboard on phone connected to home WiFi → toggle → LED responds.
- Switch phone to mobile data (turn off WiFi). Dashboard still works (it's served from arduino.cc, not from your home network). Toggle → LED still responds.
- Walk down the street. Same test. Still works.
The device is on your home WiFi. The dashboard is in the cloud. The phone talks to the cloud over mobile data. The cloud talks to the device over its home WiFi. Round trip ~1 s.
Worked Example · Lamp v3 25 min
Step 1 — set up the Thing
New Thing named "CloudLamp". Attach your board. Add the four properties (powerOn, brightness, colour, uptime) with the permissions / types from §3.
Step 2 — wire the LED
LED + 220 Ω on D3.
Step 3 — paste the sketch from §3
Upload.
Step 4 — build the dashboard
- Switch widget bound to powerOn — "Lamp".
- Slider bound to brightness (0..255) — "Brightness".
- Colour picker bound to colour (if you have an RGB LED) — "Colour".
- Value bound to uptime — "Uptime".
Step 5 — test
- Toggle the switch — LED on/off.
- Drag slider — LED dims.
- Drag colour picker — RGB LED colour changes (if hooked up).
Step 6 — go remote
Phone off WiFi (cellular data only). Open Arduino IoT Remote → CloudLamp dashboard → toggle. LED responds. You're now controlling a device on your home network from a phone that's on a different network entirely. Real cloud.
Basic 5 min
Goal: Add an onTime int property that increments each second the lamp is on. Reset to 0 when the lamp turns off.
1. Think first (on paper, no typing yet):
- Will a callback run once a second by itself? If not, which part of the sketch does run all the time?
- How will the sketch know a whole second has passed, without
delay()? What must it remember? - Which Property tells you whether the lamp is on?
2. Code it. Add onTime (int, READ) on the Thing's page and a Value widget for it. Then change your CloudLamp sketch.
3. Upload and observe. Switch the lamp on and watch the widget for 20 seconds. Switch it off, then on again.
It works if: onTime counts up by about 1 each second while the lamp is on (about 20 after 20 s), drops to 0 when you switch it off, and starts again from 0.
Challenge 1 5 min
Goal: Add a scheduleHour int property (0..23). If the current hour (from NTP — built into the Cloud library if your board supports it) equals scheduleHour, force powerOn = true.
1. Think first (on paper, no typing yet):
- The Cloud library can give you the local time as seconds since 1970. A day has 86,400 seconds and an hour 3,600. How do you get "which hour of today" from that?
- Where should the check live — in a callback or in
loop()? Why? - When you change
powerOnfrom the sketch, what else must happen so the LED really comes on?
2. Code it. Set your time zone on the Thing's page. Add scheduleHour (int, READWRITE) and a Slider (0–23). Then change your CloudLamp sketch. Print the hour you calculate to the Serial Monitor so you can check it.
3. Upload and observe. Switch the lamp off. Set the slider to the hour it is now.
It works if: the printed hour matches the clock on the wall, and within a few seconds of setting scheduleHour to the current hour the lamp comes on by itself and the dashboard switch flips to on.
Challenge 2 5 min
Goal: Wire an RGB LED (3 PWM pins). Add a CloudColor property. Bind a Colour picker widget. The picker on the dashboard should drive the RGB live.
1. Think first (on paper, no typing yet):
- Which callback runs when the picker moves? Write its name.
- How do you get three brightness numbers out of one colour?
- Does the lamp's power switch need to matter too? What should the RGB LED do when the lamp is off?
2. Code it. Wire the RGB LED to three PWM pins, each leg with its own resistor. Fill in onColourChange() in your CloudLamp sketch.
3. Upload and observe. Pick pure red, green and blue on the dashboard, then white.
It works if: the real LED follows the picker within a second or two, and shows red, green and blue when you pick them.
Challenge 3 · Fix the lamp that ignores its switch 10 min
A classmate wrote CloudLamp without the applyLed() helper. It has three faults.
#include "thingProperties.h"
const int LED_PIN = 3;
void setup() {
Serial.begin(9600);
delay(1500);
pinMode(LED_PIN, OUTPUT);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}
void loop() {
ArduinoCloud.update();
uptime = millis() / 1000;
delay(3000);
}
void onPowerOnChange() {
if (powerOn) {
analogWrite(LED_PIN, 255);
} else {
analogWrite(LED_PIN, 0);
}
}
void onBrightnessChange() {
analogWrite(LED_PIN, brightness);
}- Moving the slider while the lamp is off: what happens?
- Switching the lamp on after setting the slider to 50: what happens?
- Why does every tap feel slow?
Fix all three and upload. Time five taps with a stopwatch, before and after.
It works if the slider never lights a lamp that is off. The switch must bring back the last brightness, and taps feel quicker.
Recap 5 min
Cloud-controlled LED = three properties (powerOn, brightness, colour) + their callbacks + a single applyLed helper. Sketch is ~50 lines (was 150+ hand-rolled in L03-34). Phone or browser anywhere on the internet can control your device on your home WiFi. Latency 1–3 s typical; not for real-time use. Tomorrow we polish sharing / permissions for multi-user demos.
- Callback-driven sketch
- Sketch reacts to events (property changes) rather than polling. Cleaner code and lower CPU.
- Cloud latency
- Round trip from one client to the device via the cloud. ~1 s typical; varies with network and load.
- Offline behaviour
- What the device does when WiFi is unavailable. Library keeps last state; physical outputs unchanged.
- Public link
- Read-only (and sometimes read-write) shareable URL to a dashboard. No login required for viewers.
- Single source of truth
- One
applyLed()helper called by every relevant callback. Avoids divergence between properties and hardware state.
Extra Mission 5 min
Part 1 — Design a safe cloud-controlled gadget
Pick something at home to control from anywhere, such as a fan or a reading lamp. Design it on paper.
Your design must include:
- Its one job, and every Property with type and permission.
- The dashboard widgets.
- One thing the sketch does by itself if the cloud is slow or offline.
- One thing you would never let a public link control, and why.
Part 2 — Make it
Build it with an LED, buzzer or servo standing in for the real output. Include your offline rule in the sketch. Share a dashboard link with someone off your network. Time how long their tap takes to reach your device.
Bring back next class: the uploaded sketch, a dashboard screenshot, and your measured delay in seconds.