Due to covid restrictions, maintaining any type of relationship is challenging. It seems like overnight all relationships became long-distance relationships. I wanted to let my loved ones know I am thinking of them and I care for them by designing a box to send them messages randomly via MQTT publish/subscribe protocol.
Hardware you'll need:
-ESP8266 WiFi development board
-Cable to connect ESP8266 must be able to power AND transmit data (VERY IMPORTANT)
-20x4 LCD screen for more characters (16x2 has less)
-breadboard & cables
Software needed:
-MQTT Hive
-Arduino IDE
-Website to publish messages: http://www.mqtt-dashboard.com/
Schematic:
How to start MQTT broker:
-Open terminal
-Find directory where HiveMQ is located
-locate bin folder within HiveMQ folder
-run command: ./run.sh
-after a couple of seconds HiveMQ should start running
* Note: change the uppercase lines to your unique wifi connection and MQTT connection.
Arduino Code to be uploaded onto ESP8266:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const char* ssid = "YOUR WIFI CONNECTION HERE";
const char* password = "YOUR PASSWORD";
const char* mqtt_server = "broker.mqttdashboard.com";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command from MQTT broker is : ");
Serial.print(topic);
Serial.println();
Serial.print(" publish data is:");
lcd.clear();
{
for (int i= 0; i < length; i++)
{
Serial.print((char)payload[i]);
lcd.setCursor(0, 0);
lcd.setCursor(i, 0);
lcd.write((char)payload[i]);
//lcd.setCursor(i, 0);
}
}
Serial.println();
}
void reconnect() {
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
String clientId = "THE CLIENT ID YOU USED IN HIVEMQ";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
client.subscribe("THE TOPIC SUBSCRIPTION NAME FROM HIVEMQ");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(6000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
lcd.init();
lcd.backlight();
lcd.setCursor (0,0);
lcd.begin(20,4);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.setCallback(callback);
client.loop();
delay(500); //delay for debounce
}
To publish message onto LCD:
-Click on web socket client
-Enter your client ID then press connect
-Under Publish change the topic to the topic name you chose and entered into your Arduino IDE code and enter the message
-Message should display on your LCD!
Enjoy sending some love to your loved ones!!!
Video:
留言