π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
[BSS μμ νκ³ κΉ¨λν μ§νμ² νμΉ λ° μ΄ν 3] MQTT μμ λ°μ κ°μ lcd λ‘ μΆλ ₯νκΈ° λ³Έλ¬Έ
π©π» IoT (Embedded)/Arduino
[BSS μμ νκ³ κΉ¨λν μ§νμ² νμΉ λ° μ΄ν 3] MQTT μμ λ°μ κ°μ lcd λ‘ μΆλ ₯νκΈ°
μ§μ§μνμΉ΄ 2023. 12. 22. 09:44728x90
λ°μν
β MQTT
β pub.ino
//pub.ino
#include "WiFiS3.h"
#include <Ethernet.h>
#include <PubSubClient.h>
// WiFi and MQTT μ
ν
#define WLAN_SSID "class924"
#define WLAN_PASS "kosta90009"
#define MQTT_SERVER "192.168.0.51"
#define MQTT_PORT 1883
#define MQTT_USERNAME "your_mqtt_username"
#define MQTT_KEY "your_mqtt_password"
#define TRIG 9 // TRIG ν μ€μ (μ΄μν 보λ΄λ ν)
#define ECHO 8 // ECHO ν μ€μ (μ΄μν λ°λ ν)
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = WLAN_SSID; // your network SSID (name)
char pass[] = WLAN_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;
WiFiClient ethClient;
PubSubClient mqtt(ethClient);
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup()
{
Serial.begin(9600);
delay(10);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
connectWiFi();
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true)
;
}
// WiFi.firmwareVersion() : λͺ¨λμμ μ€ν μ€μΈ νμ¨μ΄ λ²μ μ λ¬Έμμ΄λ‘ λ°ν
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 Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(5000);
}
printWifiStatus();
delay(5000);
// MQTT broker
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
connectMQTT();
}
void loop()
{
long duration, distance;
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH); // 물체μ λ°μ¬λμ΄λμμ¨ μ΄μνμ μκ°μ λ³μμ μ μ₯ν©λλ€.
// 34000*μ΄μνκ° λ¬Όμ²΄λ‘ λΆν° λ°μ¬λμ΄ λμμ€λμκ° /1000000 / 2(μ볡κ°μ΄μλλΌ νΈλκ°μ΄κΈ°λλ¬Έμ λλκΈ°2λ₯Ό ν΄μ€λλ€.)
// μ΄μνμΌμμ 거리κ°μ΄ μ κ³μ°κ°κ³Ό λμΌνκ² Cmλ‘ νμ°λλ κ³μ°κ³΅μ μ
λλ€. μμμ΄ κ°λ¨ν΄μ§λλ‘ μ μ©νμ΅λλ€.
distance = duration * 17 / 1000;
// PCλͺ¨λν°λ‘ μ΄μν 거리κ°μ νμΈ νλ μ½λ μ
λλ€.
// Serial.println(duration ); //μ΄μνκ° λ°μ¬λμ΄ λμμ€λ μκ°μ 보μ¬μ€λλ€.
// Serial.print("\nDistance : ");
// Serial.print(distance); // μΈ‘μ λ 물체λ‘λΆν° 거리κ°(cmκ°)μ 보μ¬μ€λλ€.
// Serial.println(" Cm");
char buffer2[100];
char str[] = "cm";
ltoa(distance, buffer2, 10);
strcat(buffer2, str);
Serial.print(buffer2); // μΈ‘μ λ 물체λ‘λΆν° 거리κ°(cmκ°)μ 보μ¬μ€λλ€.
delay(1000); // 1μ΄λ§λ€ μΈ‘μ κ°μ 보μ¬μ€λλ€.
// Publish MQ-135μΌμ κ° MQTT ν ν½ μ€μ
mqtt.publish("sensor/mq135", buffer2);
// λκΈ°λ₯Ό μ£ΌκΈ° μν λλ μ΄
delay(5000);
}
void connectWiFi()
{
Serial.println("Connecting to WiFi");
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void connectMQTT()
{
Serial.println("Connecting to MQTT");
while (!mqtt.connected())
{
if (mqtt.connect("MQ135Client"))
{
Serial.println("Connected to MQTT");
}
else
{
Serial.print("Failed to connect to MQTT, rc=");
Serial.print(mqtt.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
// WiFi.localIP() : WiFi μ΄λμ IP μ£Όμ
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
β sub.ino
//sub.ino
#include "WiFiS3.h"
#include <PubSubClient.h>
// LCDλ₯Ό μ¬μ©νκΈ° μν΄ λΌμ΄λΈλ¬λ¦¬λ₯Ό μΆκ°ν©λλ€.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define WLAN_SSID "class924"
#define WLAN_PASS "kosta90009"
#define MQTT_SERVER "192.168.0.51"
#define MQTT_PORT 1883
#define MQTT_USERNAME "your_mqtt_username"
#define MQTT_KEY "your_mqtt_password"
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// 16x2 LCDκ°μ²΄λ₯Ό μμ±ν©λλ€. μ΄λ νμΈν I2Cμ μ£Όμκ°μ λ£μ΄μ€λλ€.
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiClient ethClient;
PubSubClient mqtt(ethClient);
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
Serial.begin(9600);
delay(10);
connectWiFi();
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
connectMQTT();
delay(2000);
mqtt.setCallback(callback);
//Display μ΄κΈ°ν
// lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.init();
lcd.backlight();
}
void loop() {
if (!mqtt.connected()) {
connectMQTT();
}
mqtt.loop();
delay(1000);
}
void connectWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void connectMQTT() {
Serial.println("Connecting to MQTT");
while (!mqtt.connected()) {
if (mqtt.connect("LCDClient")) {
Serial.println("Connected to MQTT");
mqtt.subscribe("sensor/mq135");
} else {
Serial.print("Failed to connect to MQTT, rc=");
Serial.print(mqtt.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
// μΆλ ₯ νμΈ
Serial.print(message);
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(message);
delay(2000);
lcd.clear();
}
728x90
λ°μν
'π©βπ» IoT (Embedded) > Arduino' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Comments