π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
[BSS μμ νκ³ κΉ¨λν μ§νμ² νμΉ λ° μ΄ν 4] MQTT μμ MQ135 pub κ°μ sub μμ lcd λ‘ μΆλ ₯νκΈ° λ³Έλ¬Έ
π©π» IoT (Embedded)/Arduino
[BSS μμ νκ³ κΉ¨λν μ§νμ² νμΉ λ° μ΄ν 4] MQTT μμ MQ135 pub κ°μ sub μμ lcd λ‘ μΆλ ₯νκΈ°
μ§μ§μνμΉ΄ 2023. 12. 22. 09:45728x90
λ°μν
β MQTT
β 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.154"
#define MQTT_PORT 1883
#define MQ135_PIN A0
char ssid[] = WLAN_SSID;
char pass[] = WLAN_PASS;
int keyIndex = 0;
WiFiClient ethClient;
PubSubClient mqtt(ethClient);
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
delay(10);
pinMode(MQ135_PIN, 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() {
int airQuality = analogRead(MQ135_PIN);
Serial.print("Air Quality: ");
Serial.println(airQuality);
mqtt.publish("sensor/mq135", String(airQuality).c_str());
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.154"
#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