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

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

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

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

μ§•μ§•μ•ŒνŒŒμΉ΄ 2023. 12. 22. 09:46
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 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.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