#include <WiFi.h>
#include <PubSubClient.h>
#define PIR_PIN 28
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
int lastState = LOW;
void setup_wifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void reconnect() {
while (!client.connected()) {
client.connect("pico_in");
}
}
void setup() {
pinMode(PIR_PIN, INPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
int val = digitalRead(PIR_PIN);
if (val == HIGH && lastState == LOW) {
client.publish("gate/in", "1");
delay(1000); // debounce
}
lastState = val;
delay(100);
}