Skip to content

Instantly share code, notes, and snippets.

@humphd
Created February 12, 2022 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphd/ce62014ca5fa37255197104bcaa906c3 to your computer and use it in GitHub Desktop.
Save humphd/ce62014ca5fa37255197104bcaa906c3 to your computer and use it in GitHub Desktop.
Chimes Arduino Sketch
#include <Adafruit_NeoPixel.h>
#include <HTTPClient.h>
#include <WiFi.h>
// Define a pixel strip of 1 pixel
Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
// Wifi
char ssid[] = "...";
char password[] = "...";
// Chime server
char server[] = "http://192.168.1.66:3000";
// Send a heartbeat once every 30 seconds
unsigned long timer;
const unsigned long HEARTBEAT_PERIOD_MS = (30 * 1000UL);
// Wait 10 seconds after a chime before processing more
const unsigned long CHIME_DELAY_MS = (10 * 1000UL);
void setupPins() {
// Set the pin labeled "MOSI" (or "MO") as a GPIO
// input, which is pulled down (to logic 0) if
// no signal is present. This is pulled up (to logic 1)
// when there is a signal on the LED side of the
// optoisolator.
pinMode(MOSI,INPUT_PULLDOWN);
// Turn on power to the Neopixel on the board
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
}
void setupNeopixel() {
// Initialize the Neopixel subsystem
pixels.begin();
pixels.setBrightness(50);
}
void setupWifi() {
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setupSerial() {
// Set up the serial subsystem for messages, if
// desired. The serial speed is insignificant
// because it's actually a USB connection.
Serial.begin(9600);
while(!Serial){ ; }
}
void setup() {
setupSerial();
setupPins();
setupWifi();
setupNeopixel();
}
void showPixel(uint32_t colour) {
pixels.fill(colour);
pixels.show();
}
void httpGet(char* path) {
// TODO: what's the best way to do this in Arduino? This? Use String?
char url[255] = "\0";
strcat(url, server);
strcat(url, path);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if(httpCode > 0) {
Serial.printf("[HTTP] GET %s %d\n", url, httpCode);
} else {
Serial.printf("[HTTP] GET %s failed, error: %s\n", url, http.errorToString(httpCode).c_str());
}
http.end();
}
// If the heartbeat period has expired, send an HTTP GET to /heartbeat
// and reset the heartbeat timer.
void heartbeat() {
if(millis() - timer > HEARTBEAT_PERIOD_MS) {
char path[] = "/heartbeat";
httpGet(path);
timer = millis();
}
}
// Send an HTTP GET to /chime and wait a bit before continuing to process signals
void chime() {
char path[] = "/chime";
httpGet(path);
// Wait a bit before we start processing more signals
delay(CHIME_DELAY_MS);
}
void loop() {
heartbeat();
// Uncomment the following line to see
// the pin values via the serial line.
// Use [Tools>Serial Monitor] in the Arduino IDE
// to observe this output.
//
// Serial.printf("GPIO: %d\n",digitalRead(MOSI));
// Display green on the Neopixel if there is
// a signal on the optoisolated input,
// display red otherwise. We could track the
// previous state and then output via pixels.show()
// only if there is a change, to reduce traffic
// to the Neopixel, if desired.
if (digitalRead(MOSI)) {
showPixel(0x00FF00);
chime();
} else {
showPixel(0xFF0000);
}
// Wait 10 mS
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment