2024年7月2日 星期二

WOKWI Active Buzzer & MQTT

WOKWI Active Buzzer & MQTT








#include <WiFi.h>
#include <PubSubClient.h>
// Define NodeMCU D2 pin  connect to Piezo Buzzer
#define BUZZER_PIN  2

// Update these with values suitable for your network.
const char* ssid =  "Wokwi-GUEST";   // your network SSID (name)
const char* password = "";   // your network password
//const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
const char* mqtt_server = "broker.mqttgo.io";


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//=========================================================
void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    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);
  int p =(char)payload[0]-'0';
  // if MQTT comes a 0 turn off LED on D2
  if(p==0)
  {
    digitalWrite(BUZZER_PIN, HIGH);
    Serial.println(" Turn Off BUZZER! ");
  }
  // if MQTT comes a 1, turn on BUZZER on pin D2
  if(p==1)
  {
    digitalWrite(BUZZER_PIN, LOW);
    Serial.println(" Turn On BUZZER! ");
  }
  Serial.println();
} //end callback
//=========================================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("alex9ufo/Command");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()
//=========================================================
void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
 
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN,HIGH);
}
//=========================================================
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.setCallback(callback);
  client.loop();

}
//=========================================================

沒有留言:

張貼留言

Arduino 物聯網應用 - 上課教材

Arduino 物聯網應用 - 上課教材 https://dic.vbird.tw/arduino/list.php Arduino 物聯網應用 - 課程列表 我們會從 Arduino 的認識、IDE 環境的熟悉、與操作電腦的序列埠連動的功能、Arduino 開發語言的熟悉、 簡...