😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[BSS μ•ˆμ „ν•˜κ³  κΉ¨λ—ν•œ μ§€ν•˜μ²  νƒ‘μŠΉ 및 μš΄ν–‰ 4] MQTT μ—μ„œ MQ135 pub 값을 sub μ—μ„œ lcd 둜 좜λ ₯ν•˜κΈ° λ³Έλ¬Έ

πŸ‘©‍πŸ’» IoT (Embedded)/Arduino

[BSS μ•ˆμ „ν•˜κ³  κΉ¨λ—ν•œ μ§€ν•˜μ²  νƒ‘μŠΉ 및 μš΄ν–‰ 4] MQTT μ—μ„œ MQ135 pub 값을 sub μ—μ„œ lcd 둜 좜λ ₯ν•˜κΈ°

μ§•μ§•μ•ŒνŒŒμΉ΄ 2023. 12. 22. 09:45
728x90
λ°˜μ‘ν˜•

⭐ 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
λ°˜μ‘ν˜•
Comments