Due lampade, ESP32
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
/*
Lamp - one of two long-distance lamps: touch this one and the other one glows
Two ESP32s in two homes, each with a ring of LEDs and a touch pad. This sketch runs on both. Each lamp is a
Connectivitizer device that is called and calls:
- it serves a small web API and one HTML page (page.h, made from ../../ui/lamps-esp32.html) that the
Connectivitizer app shows full screen; the phone and the other lamp reach it through the library;
- when it is touched, it calls the other lamp itself: Connectivitizer.link() puts the other lamp's web
server on a port of this chip, and Connectivitizer.post() sends POST /glow there.
The web server knows nothing about Connectivitizer. Everything between the two lamps and between a lamp and
a phone is encrypted end to end with the shared key.
Wiring (ESP32 DevKit, 3.3 V logic; change the pins below for another board):
LED ring, 16 x WS2812B DIN -> GPIO13, through a level shifter to 5 V (for example a 74AHCT125)
(NeoPixel style) and 330 Ohm in series 5V -> its own 5 V supply GND -> that supply and GND
Touch pad a piece of copper foil or a metal part of the lamp, one wire -> GPIO4 (touch T0)
Each of the two can be replaced by a part of the bare board (SIMULATE_RING, BOOT_BUTTON_IS_TOUCH below).
What you need (https://connectivitizer.com), for each lamp:
1. a device key - Credentials > Create credential > Device key, with "Its devices may open sessions to
other devices" switched on -> DEVICE_KEY
2. a shared key - Credentials > Generate a shared key -> SHARED_KEY; the same key on both lamps
and in the app
Copy secrets.example.h to secrets.h and fill it in; the second lamp gets the two names swapped. The
Connectivitizer library is the only one to install: the sketch drives the ring itself.
*/
// ---------- parts of the bare board instead of the real ones ----------
// While one is true, /state says "simulated": true and the page shows "Simulated hardware".
const bool SIMULATE_RING = false; // true: no LED ring; the on-board LED is on while this lamp glows
const bool BOOT_BUTTON_IS_TOUCH = false; // true: no touch pad; the BOOT button of the board is the touch
// ---------- pins ----------
const int PIN_RING = 13; // data line of the ring
const int PIN_TOUCH = 4; // touch channel T0
const int PIN_BOARD_LED = 2; // the LED of most DevKit boards, for SIMULATE_RING
const int PIN_BOOT_BUTTON = 0; // LOW while pressed, for BOOT_BUTTON_IS_TOUCH
// ---------- the ring ----------
const int RING_LEDS = 16;
const float RING_BRIGHTNESS = 0.4f; // share of full brightness; decides how much current the ring draws
const uint32_t FRAME_MS = 33; // the ring is drawn 30 times a second
const uint32_t GLOW_SECONDS = 60; // how long this lamp glows when a glow arrives
// ---------- the touch pad ----------
// touchRead() of the classic ESP32 gives a smaller number while the pad is touched. The sketch measures the
// untouched pad when it starts and counts a reading below TOUCH_RATIO of that as a touch.
const float TOUCH_RATIO = 0.6f;
const uint32_t TOUCH_SAMPLE_MS = 20;
const int TOUCH_SAMPLES_NEEDED = 3; // readings in a row, so that one stray reading is no touch
// ---------- the other lamp ----------
const uint16_t OTHER_PORT = 8081; // the other lamp's web server, on this port of this chip
const uint32_t SEND_TIMEOUT_MS = 15000; // opening the session the first time takes a few seconds of that
const int HISTORY_ENTRIES = 30;
const char *const PATTERNS[] = { "glow", "pulse", "heartbeat", "wave" };
const int PATTERN_COUNT = 4;
WebServer server(80);
struct Glow {
bool on = false;
uint8_t red = 0, green = 0, blue = 0;
uint8_t pattern = 0;
uint64_t startedMs = 0;
uint32_t seconds = 0;
};
Glow hereGlow; // what this lamp shows
Glow otherGlow; // what this lamp knows of the other one: the glow it delivered there
struct Entry {
uint32_t id;
uint64_t atMs;
bool received; // false: sent
char color[8]; // "#rrggbb"
uint8_t pattern;
bool byTouch; // false: from the phone
int8_t delivered; // 1 yes, 0 no, -1 on its way; always 1 for a received glow
};
Entry entries[HISTORY_ENTRIES]; // kept in memory only
int entryCount = 0;
int entryNext = 0;
uint32_t nextEntryId = 1;
uint32_t receivedCount = 0;
uint32_t sentCount = 0; // glows that arrived
uint32_t changes = 0; // goes up with every change to the history; the page asks for /history then
// What the other lamp is known to be. Reachable is the outcome of the last call to it.
int otherReachable = -1; // 1 yes, 0 no, -1 not called yet
int lastSendStatus = 0; // the HTTP status of the last call, -1 without an answer, 0 before the first
bool otherAnswered = false;
uint64_t otherAnsweredMs = 0;
// A call to the other lamp waits for its answer, up to SEND_TIMEOUT_MS. It runs in a task of its own, so
// that the ring keeps moving and the web server keeps answering meanwhile. loop() hands the task one glow
// at a time and takes the outcome back; nothing else is shared between the two.
struct Outgoing {
uint32_t id;
char color[8];
uint8_t pattern;
bool byTouch;
};
struct Outcome {
uint32_t id;
int status;
uint32_t glowSeconds;
};
QueueHandle_t outgoingQueue = nullptr;
QueueHandle_t outcomeQueue = nullptr;
bool sending = false;
// Milliseconds since the start, 64 bit: millis() starts again at zero after 49 days.
uint64_t nowMs() {
return (uint64_t)(esp_timer_get_time() / 1000);
}
// ---------- colours and JSON ----------
int hexDigit(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
// "#rrggbb" into three numbers. False for anything else.
bool parseColor(const String &text, uint8_t &red, uint8_t &green, uint8_t &blue) {
if (text.length() != 7 || text[0] != '#') return false;
int digits[6];
for (int i = 0; i < 6; i++) {
digits[i] = hexDigit(text[i + 1]);
if (digits[i] < 0) return false;
}
red = (uint8_t)(digits[0] * 16 + digits[1]);
green = (uint8_t)(digits[2] * 16 + digits[3]);
blue = (uint8_t)(digits[4] * 16 + digits[5]);
return true;
}
int patternIndex(const String &name) {
for (int i = 0; i < PATTERN_COUNT; i++) {
if (name == PATTERNS[i]) return i;
}
return -1;
}
// Just enough JSON reading for the bodies of /glow and the other lamp's answer: flat objects with texts and
// numbers. Returns where the value of a key starts, or -1.
int jsonValueAt(const String &body, const char *key) {
String quoted = String("\"") + key + "\"";
int at = body.indexOf(quoted);
if (at < 0) return -1;
at = body.indexOf(':', at + quoted.length());
if (at < 0) return -1;
at++;
while (at < (int)body.length() && isspace((unsigned char)body[at])) at++;
return at < (int)body.length() ? at : -1;
}
// The text of "key": "text". False when the key is not there or its value is not a text.
bool jsonText(const String &body, const char *key, String &text) {
int at = jsonValueAt(body, key);
if (at < 0 || body[at] != '"') return false;
int end = body.indexOf('"', at + 1);
if (end < 0) return false;
text = body.substring(at + 1, end);
return true;
}
bool jsonNumber(const String &body, const char *key, float &value) {
int at = jsonValueAt(body, key);
if (at < 0 || !isdigit((unsigned char)body[at])) return false;
value = body.substring(at).toFloat();
return true;
}
// ---------- the ring ----------
// A WS2812B takes 24 bits per LED, green first, as pulses of 1.25 microseconds: a long high part for a one,
// a short one for a zero. The ESP32's RMT unit makes the pulses; the tick is 100 nanoseconds.
rmt_obj_t *ringRmt = nullptr;
bool ringReady = false;
rmt_data_t ringBits[RING_LEDS * 24];
void ringStart() {
ringReady = rmtInit(PIN_RING, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
ringRmt = rmtInit(PIN_RING, RMT_TX_MODE, RMT_MEM_64);
if (ringRmt != nullptr) {
rmtSetTick(ringRmt, 100);
ringReady = true;
}
if (!ringReady) Serial.println("The ring could not be started on PIN_RING.");
}
// Sends one picture to the ring: three bytes per LED, in the order red, green, blue.
void ringWrite(const uint8_t *rgb) {
if (!ringReady) return;
int at = 0;
for (int led = 0; led < RING_LEDS; led++) {
const uint8_t inOrder[3] = { rgb[led * 3 + 1], rgb[led * 3], rgb[led * 3 + 2] }; // green, red, blue
for (int part = 0; part < 3; part++) {
for (int bit = 7; bit >= 0; bit--) {
bool one = (inOrder[part] >> bit) & 1;
ringBits[at].level0 = 1;
ringBits[at].duration0 = one ? 8 : 4;
ringBits[at].level1 = 0;
ringBits[at].duration1 = one ? 4 : 8;
at++;
}
}
}
rmtWrite(PIN_RING, ringBits, RING_LEDS * 24, RMT_WAIT_FOR_EVER);
rmtWriteBlocking(ringRmt, ringBits, RING_LEDS * 24);
}
// How bright one LED is, 0 to 1, at `t` seconds into a glow that lasts `total` seconds: the pattern, faded
// in over 0.6 seconds and out over the last 3. The page draws its rings with the same formulas.
float ledLevel(uint8_t pattern, float t, float total, int index) {
float envelope = fminf(1.0f, fminf(t / 0.6f, (total - t) / 3.0f));
if (envelope <= 0.0f) return 0.0f;
float level = 1.0f;
if (pattern == 1) {
level = 0.35f + 0.65f * (0.5f - 0.5f * cosf(2.0f * PI * t / 2.4f));
} else if (pattern == 2) {
float x = fmodf(t / 1.2f, 1.0f); // two beats every 1.2 seconds
float first = (x - 0.08f) / 0.05f;
float second = (x - 0.30f) / 0.06f;
level = 0.18f + 0.82f * fmaxf(expf(-first * first), 0.7f * expf(-second * second));
} else if (pattern == 3) {
level = 0.25f + 0.75f * (0.5f + 0.5f * cosf(2.0f * PI * ((float)index / RING_LEDS - t / 3.0f)));
}
return level * envelope;
}
void startGlow(Glow &glow, uint8_t red, uint8_t green, uint8_t blue, uint8_t pattern, uint32_t seconds) {
glow.on = true;
glow.red = red;
glow.green = green;
glow.blue = blue;
glow.pattern = pattern;
glow.startedMs = nowMs();
glow.seconds = seconds;
}
// Seconds a glow still lasts; 0 ends it.
float remainingSeconds(Glow &glow) {
if (!glow.on) return 0.0f;
float remaining = (float)glow.seconds - (float)(nowMs() - glow.startedMs) / 1000.0f;
if (remaining <= 0.0f) {
glow.on = false;
return 0.0f;
}
return remaining;
}
void drawRing() {
static uint64_t lastFrame = 0;
static bool dark = false;
if (nowMs() - lastFrame < FRAME_MS) return;
lastFrame = nowMs();
bool glowing = remainingSeconds(hereGlow) > 0.0f;
if (SIMULATE_RING) {
digitalWrite(PIN_BOARD_LED, glowing ? HIGH : LOW);
return;
}
if (!glowing && dark) return; // the ring keeps its last picture: nothing to send while it is dark
uint8_t rgb[RING_LEDS * 3] = { 0 };
if (glowing) {
float t = (float)(nowMs() - hereGlow.startedMs) / 1000.0f;
for (int led = 0; led < RING_LEDS; led++) {
// The eye sees brightness in ratios: the square of the level makes the fades look even.
float level = ledLevel(hereGlow.pattern, t, (float)hereGlow.seconds, led);
float scale = level * level * RING_BRIGHTNESS;
rgb[led * 3] = (uint8_t)lroundf((float)hereGlow.red * scale);
rgb[led * 3 + 1] = (uint8_t)lroundf((float)hereGlow.green * scale);
rgb[led * 3 + 2] = (uint8_t)lroundf((float)hereGlow.blue * scale);
}
}
ringWrite(rgb);
dark = !glowing;
}
// ---------- the touch pad ----------
float touchBaseline = 0.0f;
void touchStart() {
if (BOOT_BUTTON_IS_TOUCH) {
pinMode(PIN_BOOT_BUTTON, INPUT_PULLUP);
return;
}
// The untouched pad: the mean of sixteen readings. Keep the hand away while the lamp starts.
uint32_t sum = 0;
for (int i = 0; i < 16; i++) {
sum += touchRead(PIN_TOUCH);
delay(10);
}
touchBaseline = (float)sum / 16.0f;
}
// True once for every touch, at the moment it begins.
bool touched() {
static uint64_t lastSample = 0;
static int inARow = 0;
static bool down = false;
if (nowMs() - lastSample < TOUCH_SAMPLE_MS) return false;
lastSample = nowMs();
bool now;
if (BOOT_BUTTON_IS_TOUCH) {
now = digitalRead(PIN_BOOT_BUTTON) == LOW;
} else {
float reading = (float)touchRead(PIN_TOUCH);
now = reading < touchBaseline * TOUCH_RATIO;
// The untouched pad drifts with the air and the supply; the baseline follows it slowly.
if (!now && !down) touchBaseline += (reading - touchBaseline) * 0.01f;
}
inARow = now == down ? 0 : inARow + 1;
if (inARow < TOUCH_SAMPLES_NEEDED) return false;
inARow = 0;
down = now;
return down;
}
// ---------- history ----------
Entry &addEntry(bool received, const char *color, uint8_t pattern, bool byTouch, int8_t delivered) {
Entry &entry = entries[entryNext];
entryNext = (entryNext + 1) % HISTORY_ENTRIES;
if (entryCount < HISTORY_ENTRIES) entryCount++;
entry.id = nextEntryId++;
entry.atMs = nowMs();
entry.received = received;
strlcpy(entry.color, color, sizeof(entry.color));
entry.pattern = pattern;
entry.byTouch = byTouch;
entry.delivered = delivered;
changes++;
return entry;
}
Entry *findEntry(uint32_t id) {
for (int i = 0; i < entryCount; i++) {
if (entries[i].id == id) return &entries[i];
}
return nullptr;
}
void addEntryJson(String &json, const Entry &entry) {
json += "{\"id\":";
json += entry.id;
json += ",\"secondsAgo\":";
json += (uint32_t)((nowMs() - entry.atMs) / 1000);
json += ",\"direction\":";
json += entry.received ? "\"received\"" : "\"sent\"";
json += ",\"color\":\"";
json += entry.color;
json += "\",\"pattern\":\"";
json += PATTERNS[entry.pattern];
json += "\",\"via\":";
json += entry.byTouch ? "\"touch\"" : "\"phone\"";
json += ",\"delivered\":";
json += entry.delivered < 0 ? "null" : entry.delivered ? "true" : "false";
json += "}";
}
void addGlowJson(String &json, Glow &glow) {
float remaining = remainingSeconds(glow);
if (remaining <= 0.0f) {
json += "null";
return;
}
char color[8];
snprintf(color, sizeof(color), "#%02x%02x%02x", glow.red, glow.green, glow.blue);
json += "{\"color\":\"";
json += color;
json += "\",\"pattern\":\"";
json += PATTERNS[glow.pattern];
json += "\",\"seconds\":";
json += glow.seconds;
json += ",\"remainingSeconds\":";
json += String(remaining, 1U);
json += "}";
}
String stateJson() {
String json;
json.reserve(520);
json += "{\"here\":{\"name\":\"" DEVICE_NAME "\",\"color\":\"" LAMP_COLOR "\",\"glow\":";
addGlowJson(json, hereGlow);
json += "},\"other\":{\"name\":\"" OTHER_DEVICE "\",\"glow\":";
addGlowJson(json, otherGlow);
json += ",\"reachable\":";
json += otherReachable < 0 ? "null" : otherReachable ? "true" : "false";
json += ",\"lastAnswerSecondsAgo\":";
if (otherAnswered) json += (uint32_t)((nowMs() - otherAnsweredMs) / 1000);
else json += "null";
json += ",\"lastStatus\":";
if (lastSendStatus != 0) json += lastSendStatus;
else json += "null";
json += "},\"glowSeconds\":";
json += GLOW_SECONDS;
json += ",\"patterns\":[";
for (int i = 0; i < PATTERN_COUNT; i++) {
if (i > 0) json += ",";
json += "\"";
json += PATTERNS[i];
json += "\"";
}
json += "],\"sending\":";
json += sending ? "true" : "false";
json += ",\"changes\":";
json += changes;
json += ",\"simulated\":";
json += (SIMULATE_RING || BOOT_BUTTON_IS_TOUCH) ? "true" : "false";
json += ",\"rssi\":";
json += WiFi.RSSI();
json += ",\"uptimeSeconds\":";
json += (uint32_t)(nowMs() / 1000);
json += "}";
return json;
}
String historyJson() {
String json;
json.reserve(160 + entryCount * 130);
json += "{\"received\":";
json += receivedCount;
json += ",\"sent\":";
json += sentCount;
json += ",\"entries\":[";
for (int i = 0; i < entryCount; i++) { // newest first
if (i > 0) json += ",";
addEntryJson(json, entries[(entryNext - 1 - i + 2 * HISTORY_ENTRIES) % HISTORY_ENTRIES]);
}
json += "]}";
return json;
}
// ---------- calling the other lamp ----------
void senderTask(void *) {
Outgoing job;
for (;;) {
if (xQueueReceive(outgoingQueue, &job, portMAX_DELAY) != pdTRUE) continue;
String body = "{\"from\":\"" DEVICE_NAME "\",\"color\":\"";
body += job.color;
body += "\",\"pattern\":\"";
body += PATTERNS[job.pattern];
body += "\",\"via\":";
body += job.byTouch ? "\"touch\"}" : "\"phone\"}";
String answer;
Outcome outcome = { job.id, 0, GLOW_SECONDS };
outcome.status = Connectivitizer.post(OTHER_DEVICE, "/glow", body, answer, SEND_TIMEOUT_MS);
float seconds = 0;
if (outcome.status == 200 && jsonNumber(answer, "glowSeconds", seconds) && seconds >= 1 && seconds <= 3600) outcome.glowSeconds = (uint32_t)seconds;
xQueueSend(outcomeQueue, &outcome, portMAX_DELAY);
}
}
// Sends a glow to the other lamp. False while the last one is still on its way. The entry it returns says
// "on its way" until the other lamp has answered, or "did not arrive" at once when this lamp is not online.
bool sendGlow(const char *color, uint8_t pattern, bool byTouch, Entry *&entry) {
if (sending) return false;
entry = &addEntry(false, color, pattern, byTouch, -1);
if (!Connectivitizer.connected()) {
entry->delivered = 0;
otherReachable = 0;
lastSendStatus = -1;
return true;
}
Outgoing job;
job.id = entry->id;
strlcpy(job.color, color, sizeof(job.color));
job.pattern = pattern;
job.byTouch = byTouch;
sending = xQueueSend(outgoingQueue, &job, 0) == pdTRUE;
if (!sending) entry->delivered = 0;
return true;
}
void takeOutcome() {
Outcome outcome;
if (xQueueReceive(outcomeQueue, &outcome, 0) != pdTRUE) return;
sending = false;
lastSendStatus = outcome.status;
otherReachable = outcome.status == 200 ? 1 : 0;
changes++;
Entry *entry = findEntry(outcome.id);
if (outcome.status == 200) {
otherAnswered = true;
otherAnsweredMs = nowMs();
sentCount++;
if (entry != nullptr) {
entry->delivered = 1;
uint8_t red = 0, green = 0, blue = 0;
parseColor(String(entry->color), red, green, blue);
startGlow(otherGlow, red, green, blue, entry->pattern, outcome.glowSeconds);
}
Serial.println("The glow arrived at " OTHER_DEVICE ".");
} else {
if (entry != nullptr) entry->delivered = 0;
Serial.printf("The glow did not arrive at " OTHER_DEVICE " (status %d, %s).\n", outcome.status, Connectivitizer.lastError());
}
}
// ---------- the web server ----------
void sendJson(int status, const String &json) {
server.sendHeader("Cache-Control", "no-store");
server.send(status, "application/json", json);
}
void sendError(int status, const char *message) {
String json = "{\"error\":\"";
json += message; // only fixed texts without quotes are passed here
json += "\"}";
sendJson(status, json);
}
// POST /glow comes from two sides. With "from" it is the other lamp: this lamp glows. Without it, it is the
// phone of this lamp's owner: this lamp sends the glow on to the other one.
void handleGlow() {
String body = server.arg("plain");
String from, color, patternName, via;
bool fromLamp = jsonText(body, "from", from);
if (!jsonText(body, "color", color)) color = fromLamp ? "" : LAMP_COLOR;
if (!jsonText(body, "pattern", patternName)) patternName = "glow";
color.toLowerCase();
uint8_t red = 0, green = 0, blue = 0;
if (!parseColor(color, red, green, blue)) {
sendError(400, "color is #rrggbb.");
return;
}
int pattern = patternIndex(patternName);
if (pattern < 0) {
sendError(400, "pattern is glow, pulse, heartbeat or wave.");
return;
}
if (fromLamp) {
// Keeps a misconfigured lamp out; it is not a second lock. Whoever holds the shared key and may open
// sessions to this lamp in the workspace can send this request.
if (from != OTHER_DEVICE) {
sendError(400, "from is not the other lamp of this pair.");
return;
}
bool byTouch = !(jsonText(body, "via", via) && via == "phone");
addEntry(true, color.c_str(), (uint8_t)pattern, byTouch, 1);
receivedCount++;
startGlow(hereGlow, red, green, blue, (uint8_t)pattern, GLOW_SECONDS);
String json = "{\"ok\":true,\"glowSeconds\":";
json += GLOW_SECONDS;
json += "}";
sendJson(200, json);
return;
}
Entry *entry = nullptr;
if (!sendGlow(color.c_str(), (uint8_t)pattern, false, entry)) {
sendError(409, "A glow is on its way.");
return;
}
String json = "{\"ok\":true,\"entry\":";
addEntryJson(json, *entry);
json += "}";
sendJson(200, json);
}
void setup() {
Serial.begin(115200);
if (SIMULATE_RING) {
pinMode(PIN_BOARD_LED, OUTPUT);
digitalWrite(PIN_BOARD_LED, LOW);
} else {
ringStart();
uint8_t dark[RING_LEDS * 3] = { 0 };
ringWrite(dark); // the ring is dark before anything else happens
}
touchStart();
outgoingQueue = xQueueCreate(1, sizeof(Outgoing));
outcomeQueue = xQueueCreate(1, sizeof(Outcome));
xTaskCreate(senderTask, "lamp-sender", 8192, nullptr, 1, nullptr);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// An ordinary web server. It knows nothing about Connectivitizer.
server.on("/state", HTTP_GET, []() { sendJson(200, stateJson()); });
server.on("/history", HTTP_GET, []() { sendJson(200, historyJson()); });
server.on("/glow", HTTP_POST, handleGlow);
// The screen of the device: the app asks for the manifest, then for the page it names.
server.on("/.cz/ui.json", HTTP_GET, []() { server.send_P(200, "application/json", MANIFEST_JSON); });
server.on("/.cz/ui.html", HTTP_GET, []() {
server.sendHeader("Cache-Control", "no-cache");
server.send_P(200, "text/html; charset=utf-8", PAGE_HTML, PAGE_HTML_LENGTH);
});
server.onNotFound([]() { sendError(404, "Not found."); });
server.begin();
Connectivitizer.setLog(Serial);
Connectivitizer.setServer(SERVER_URL);
Connectivitizer.setSharedKey(SHARED_KEY);
Connectivitizer.expose("api", 80); // the web server above, under the name "api"
// The service "api" of the other lamp, on port OTHER_PORT of this chip. The session to it opens with the
// first request and closes when the port has not been used for half a minute.
Connectivitizer.link(OTHER_DEVICE, "api", OTHER_PORT);
if (!Connectivitizer.begin(DEVICE_KEY, DEVICE_NAME)) {
Serial.println("Connectivitizer: check DEVICE_KEY, DEVICE_NAME, OTHER_DEVICE and SHARED_KEY");
}
}
void loop() {
server.handleClient();
drawRing();
takeOutcome();
remainingSeconds(otherGlow); // ends it when its time is over
if (touched()) {
Entry *entry = nullptr;
if (sendGlow(LAMP_COLOR, 0, true, entry)) Serial.println("Touched: calling " OTHER_DEVICE ".");
}
// On the serial monitor: the address in the Wi-Fi, to open http://<address>/.cz/ui.html in a browser.
static bool hadWifi = false;
bool hasWifi = WiFi.status() == WL_CONNECTED;
if (hasWifi != hadWifi) {
hadWifi = hasWifi;
if (hasWifi) {
Serial.print("Wi-Fi connected: http://");
Serial.print(WiFi.localIP());
Serial.println("/.cz/ui.html");
} else {
Serial.println("Wi-Fi lost.");
}
}
static bool wasConnected = false;
bool isConnected = Connectivitizer.connected();
if (isConnected != wasConnected) {
wasConnected = isConnected;
Serial.println(isConnected ? "Reachable through Connectivitizer." : "Not reachable right now.");
}
if (Connectivitizer.refused()) {
static uint64_t lastComplaint = 0;
if (lastComplaint == 0 || nowMs() - lastComplaint > 10000) { // no delay(): the ring must not wait
lastComplaint = nowMs();
Serial.printf("The platform refused this device: %s\n", Connectivitizer.lastError());
}
}
}