Hardware components | ||||||
| × | 1 | ||||
| × | 3 | ||||
| × | 2 | ||||
| × | 3 | ||||
| × | 3 | ||||
| × | 2 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
| ||||||
Hand tools and fabrication machines | ||||||
| ||||||
|
During the COVID-19 phase, we can get into trouble in our school by having to many people entering the welcome area of the administration. In order to reduce the people flow, the director decided to install some kind of bell-with-feedback system. This is how I was given the mission to build a fitting device.
Of course I did not want anyone to touch something, so the bell had to be touch-less for the person who rings it. On the other side, some kind of feedback had to be given (either "you may enter" or "please wait").
As both locations, one outside, the other on the inside of the welcome area, are covered by our WiFi, I decided to go wireless and use the MQTT protocol to make both devices interact with each other.
The first one contains a wight LED shining right onto an LDR. If you put your finger between them, the change of luminosity is being registered, which triggers the bell. It also contains a speaker, so the use get instants feedback about is action. Besides this there is a green and a red LED which can be triggered by the second device and which indicates the person to either enter or wait.
The second device is somewhat simpler as it only contains a speaker and two buttons. Each of the latter triggers the corresponding LED on the first device.
#include <SPI.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include "pitches.h"
// network settings
char ssid[] = "*";
char pass[] = "*";
int status = WL_IDLE_STATUS; // the Wifi radio's status
// mqtt broker settings
#define mqtt_server "*"
#define mqtt_port 1883 // TLS=8883, clear text = 1883
#define mqtt_user "*"
#define mqtt_pass "*"
#define mqtt_clientid "*"
// WiFiClient espClient;
WiFiClient espClient;
PubSubClient client(espClient);
#define BUT_RED 0
#define BUT_GREEN 2
#define BUZZER 1
long lastMsg = 0;
void setup() {
Serial.begin(115200);
// init pins
pinMode(BUT_RED,INPUT);
pinMode(BUT_GREEN,INPUT);
pinMode(BUZZER,OUTPUT);
tone(BUZZER,440,100);
Serial.println("> Doing WIFI");
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// success!
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
tone(BUZZER,550,200);
// connect to mqtt _server
connect();
client.loop();
tone(BUZZER,660,100);
}
void loop() {
if(!client.connected())
{
Serial.println("Not connected!");
connect();
}
client.loop();
long now = millis();
if (now - lastMsg > 100) {
lastMsg = now;
int but = digitalRead(BUT_GREEN);
if(but==1)
{
Serial.println("Entrez!");
client.publish("lam.bell.green","1");
delay(100);
}
but = digitalRead(BUT_RED);
if(but==1)
{
Serial.println("Attendez!");
client.publish("lam.bell.red","1");
delay(100);
}
client.loop();
}
client.loop();
}
void buzz()
{
tone(BUZZER,660,700);
tone(BUZZER,550,700);
tone(BUZZER,440,700);
}
void entrez()
{
for(int i=50; i<=200; i+=10)
{
tone(BUZZER,i,10);
}
noTone(BUZZER);
}
void attendez()
{
for(int i=440; i>=220; i-=10)
{
tone(BUZZER,i,10);
}
noTone(BUZZER);
}
void callback(char* topic, byte* payload, unsigned int length) {
String data = "";
for(int i=0; i<length; i++) data+=(char)payload[i];
//Serial.println("Got: "+(String)topic+" value: "+data);
if((String)topic=="lam.bell.buzz")// && data=="1")
{
Serial.println("Buzzing!");
buzz();
}
}
void connect() {
// set the server data
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_clientid,mqtt_user,mqtt_pass)) {
Serial.println("connected");
// subscribe to event
client.subscribe("lam.bell.buzz");
delay(200);
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
#include <SPI.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include "pitches.h"
// network settings
char ssid[] = "*";
char pass[] = "*";
int status = WL_IDLE_STATUS; // the Wifi radio's status
// mqtt broker settings
#define mqtt_server "*"
#define mqtt_port 1883 // TLS=8883, clear text = 1883
#define mqtt_user "*"
#define mqtt_pass "*"
#define mqtt_clientid "*"
// WiFiClient espClient;
WiFiClient espClient;
PubSubClient client(espClient);
#define LED_RED 3
#define LED_GREEN 2
#define BUZZER 1
#define LDR A0
int lightInit = 0;
long lightOff = 0;
long lastMsg = 0;
void setup() {
Serial.begin(115200);
// init pins
pinMode(LED_RED,OUTPUT);
pinMode(LED_GREEN,OUTPUT);
pinMode(BUZZER,OUTPUT);
tone(BUZZER,440,100);
// make first light reading
lightInit = analogRead(LDR);
digitalWrite(LED_RED,HIGH);
digitalWrite(LED_GREEN,HIGH);
Serial.println("> Doing WIFI");
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// success!
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(LED_RED,LOW);
tone(BUZZER,550,200);
// connect to mqtt _server
connect();
client.loop();
digitalWrite(LED_GREEN,LOW);
tone(BUZZER,660,100);
}
void loop() {
if(millis()>lightOff)
{
digitalWrite(LED_RED,LOW);
digitalWrite(LED_GREEN,LOW);
}
if(!client.connected())
{
Serial.println("Not connected!");
connect();
}
client.loop();
long now = millis();
if (now - lastMsg > 100) {
lastMsg = now;
int light = analogRead(LDR);
client.loop();
if(light<=lightInit-100)
{
client.loop();
client.publish("lam.bell.buzz","1");
Serial.println("Buzzing ...");
buzz();
client.loop();
}
}
client.loop();
}
void buzz()
{
tone(BUZZER,660,700);
tone(BUZZER,550,700);
tone(BUZZER,440,700);
}
void entrez()
{
for(int i=50; i<=200; i+=10)
{
tone(BUZZER,i,10);
}
noTone(BUZZER);
}
void attendez()
{
for(int i=440; i>=220; i-=10)
{
tone(BUZZER,i,10);
}
noTone(BUZZER);
}
void callback(char* topic, byte* payload, unsigned int length) {
String data = "";
for(int i=0; i<length; i++) data+=(char)payload[i];
//Serial.println("Got: "+(String)topic+" value: "+data);
if((String)topic=="lam.bell.green")// && data=="1")
{
Serial.println("Entrez!");
digitalWrite(LED_GREEN,HIGH);
digitalWrite(LED_RED,LOW);
entrez();
entrez();
entrez();
//digitalWrite(LED_GREEN,LOW);
lightOff=millis()+10*1000;
}
if((String)topic=="lam.bell.red")// && data=="1")
{
Serial.println("Attendez!");
digitalWrite(LED_RED,HIGH);
digitalWrite(LED_GREEN,LOW);
attendez();
attendez();
attendez();
//digitalWrite(LED_RED,LOW);
lightOff=millis()+10*1000;
}
}
void connect() {
// set the server data
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_clientid,mqtt_user,mqtt_pass)) {
Serial.println("connected");
// subscribe to event
client.subscribe("lam.bell.red");
delay(200);
client.subscribe("lam.bell.green");
delay(200);
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
Comments