Initial, very basic version of Kitchen PIR
This commit is contained in:
104
src/main.cpp
Normal file
104
src/main.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
const int PIR_PIN=23;
|
||||
const char* ssid = "JoNelsDarlings";
|
||||
const char* password = "Paris2016";
|
||||
const char* mqtt_server = "mqtt.ddns.kawomi.com";
|
||||
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient mqttClient(espClient);
|
||||
unsigned long lastMsg = 0;
|
||||
#define MSG_BUFFER_SIZE (50)
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
|
||||
void setup_wifi() {
|
||||
|
||||
delay(10);
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
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 reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!mqttClient.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Create a random client ID
|
||||
String clientId = "ESP32Client-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
// Attempt to connect
|
||||
if (mqttClient.connect(clientId.c_str())) {
|
||||
Serial.println("MQTT connected");
|
||||
// Once connected, publish an announcement...
|
||||
} else {
|
||||
Serial.print("MQTT failed, rc=");
|
||||
Serial.print(mqttClient.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void send_mqtt(String value) {
|
||||
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonDocument<CAPACITY> doc;
|
||||
|
||||
// create an object
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["status"] = value.c_str();
|
||||
|
||||
// serialize the object and send the result to Serial
|
||||
static char sensorStatus[150];
|
||||
serializeJson(doc, sensorStatus);
|
||||
Serial.println(sensorStatus);
|
||||
|
||||
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN,OUTPUT);
|
||||
pinMode(PIR_PIN,INPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!mqttClient.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
mqttClient.loop();
|
||||
|
||||
int pir = digitalRead(PIR_PIN);
|
||||
if (pir == HIGH) {
|
||||
send_mqtt("motion");
|
||||
digitalWrite(LED_BUILTIN,HIGH);
|
||||
delay(1000);
|
||||
digitalWrite(LED_BUILTIN,LOW);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user