added source
updated gitignore updated platformio.ini
This commit is contained in:
parent
d1590527e7
commit
1107bb5d30
4 changed files with 103 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
.pio
|
||||
.vscode/
|
||||
platformio_keyble.ini
|
|
@ -7,8 +7,12 @@
|
|||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
[platformio]
|
||||
extra_configs = platformio_keyble.ini
|
||||
|
||||
[env:d1_mini]
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
framework = esp8266-nonos-sdk
|
||||
framework = arduino
|
||||
lib_deps = knolleary/pubsubclient
|
||||
monitor_speed = 115200
|
6
platformio_keyble.ini.example
Normal file
6
platformio_keyble.ini.example
Normal file
|
@ -0,0 +1,6 @@
|
|||
[env:d1_mini]
|
||||
build_flags =
|
||||
-D WIFI_SSID=\"wifi ssid\"
|
||||
-D WIFI_PASS=\"wifi password\"
|
||||
-D MQTT_SERVER=\"mqtt broker address\"
|
||||
-D MQTT_PORT=mqtt port number
|
90
src/main.c++
Normal file
90
src/main.c++
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with your WiFi credentials
|
||||
const char* ssid = WIFI_SSID;
|
||||
const char* password = WIFI_PASS;
|
||||
|
||||
// Update these with your MQTT Broker details
|
||||
const char* mqtt_server = MQTT_SERVER;
|
||||
const unsigned int mqtt_port = MQTT_PORT; // Default port
|
||||
|
||||
const char* mqtt_topic = "backdoor/control";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
// Connecting to a WiFi network
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
payload[length] = '\0'; // Null-terminate the payload
|
||||
String message = String((char*)payload);
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.println(message);
|
||||
|
||||
if (String(topic) == mqtt_topic) {
|
||||
if (message == "unlock") {
|
||||
digitalWrite(D3, LOW);
|
||||
delay(500); // 1/2 second
|
||||
digitalWrite(D3, HIGH);
|
||||
} else if (message == "lock") {
|
||||
digitalWrite(D4, LOW);
|
||||
delay(500); // 1/2 second
|
||||
digitalWrite(D4, HIGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (client.connect("ESP8266Client")) {
|
||||
Serial.println("connected");
|
||||
// Subscribe
|
||||
client.subscribe(mqtt_topic);
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
Serial.println("Starting setup");
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
client.setCallback(callback);
|
||||
|
||||
pinMode(D3, OUTPUT);
|
||||
pinMode(D4, OUTPUT);
|
||||
digitalWrite(D3, HIGH);
|
||||
digitalWrite(D4, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
}
|
Loading…
Reference in a new issue