mirror of
https://gitlab.com/zwirbel/sprocket-device-bb1.git
synced 2025-12-14 17:35:24 +01:00
add digital switch
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"mqttServer": "192.168.1.2",
|
||||
"port": 1883,
|
||||
"clientName": "bb1",
|
||||
"inTopic": "wirelos/bb1-in/",
|
||||
"outTopic": "wirelos/bb1-out/"
|
||||
"mqttClientName" : "bb1",
|
||||
"mqttBrokerHost" : "192.168.1.2",
|
||||
"mqttBrokerPort" : 1883,
|
||||
"mqttRootTopic" : "wirelos/bb1"
|
||||
}
|
||||
50
src/config.h
50
src/config.h
@@ -18,15 +18,14 @@
|
||||
#define AP_PASSWORD "th3r31sn0sp00n"
|
||||
#define STATION_SSID "MyAP"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
#define HOSTNAME "sprocket"
|
||||
#define HOSTNAME "bb1"
|
||||
#define CONNECT_TIMEOUT 10000
|
||||
|
||||
// mqtt config
|
||||
#define MQTT_CLIENT_NAME "bb1"
|
||||
#define MQTT_HOST "192.168.1.2"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_TOPIC_IN "wirelos/bb1-in/"
|
||||
#define MQTT_TOPIC_OUT "wirelos/bb1-out/"
|
||||
#define MQTT_ROOT_TOPIC "wirelos/bb1"
|
||||
|
||||
// OTA config
|
||||
#define OTA_PORT 8266
|
||||
@@ -38,4 +37,49 @@
|
||||
#define WEB_DEFAULT_FILE "index.html"
|
||||
#define WEB_PORT 80
|
||||
|
||||
/*
|
||||
connecting Rotary encoder
|
||||
CLK (A pin) - to any microcontroler intput pin with interrupt
|
||||
DT (B pin) - to any microcontroler intput pin with interrupt
|
||||
SW (button pin) - to any microcontroler intput pin
|
||||
VCC - to microcontroler VCC (then set ROTARY_ENCODER_VCC_PIN -1)
|
||||
GND - to microcontroler GND
|
||||
*/
|
||||
#define ROTARY_ENCODER_A_PIN 35
|
||||
#define ROTARY_ENCODER_B_PIN 34
|
||||
#define ROTARY_ENCODER_BUTTON_PIN 21
|
||||
#define ROTARY_ENCODER_VCC_PIN -1 /*put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */
|
||||
#define ROTARY_ENCODER_UPDATE_INTERVAL 50
|
||||
#define ROTARY_ENCODER_LOWER_BOUND 0
|
||||
#define ROTARY_ENCODER_UPPER_BOUND 10
|
||||
#define ROTARY_ENCODER_CIRCULATE_VALUES true
|
||||
#define ROTARY_ENCODER_TOPIC "r1"
|
||||
|
||||
// Pot
|
||||
#define POT_THRESHOLD 32
|
||||
#define POT_POLL_INTERVAL 100
|
||||
#define POT_PIN_1 36
|
||||
#define POT_PIN_2 37
|
||||
#define POT_PIN_3 38
|
||||
#define POT_PIN_4 39
|
||||
#define POT_TOPIC_1 "a1"
|
||||
#define POT_TOPIC_2 "a2"
|
||||
#define POT_TOPIC_3 "a3"
|
||||
#define POT_TOPIC_4 "a4"
|
||||
|
||||
// switch
|
||||
#define SWITCH_PIN 17
|
||||
#define SWITCH_THRESHOLD 1
|
||||
#define SWITCH_UPDATE_INTERVAL 100
|
||||
#define SWITCH_TOPIC "d1"
|
||||
#define SWITCH_PIN_MODE INPUT
|
||||
|
||||
// Display
|
||||
#define DISPLAY_I2C_ADR 0x3c
|
||||
#define DISPLAY_SDA 4
|
||||
#define DISPLAY_SCL 15
|
||||
#define DISPLAY_FONT_SIZE 12
|
||||
#define DISPLAY_ROWS 4
|
||||
#define DISPLAY_GEOMETRY 0
|
||||
|
||||
#endif
|
||||
125
src/main.cpp
125
src/main.cpp
@@ -7,64 +7,99 @@
|
||||
#include <WebConfigPlugin.h>
|
||||
#include <WebApiPlugin.h>
|
||||
#include <MqttPlugin.h>
|
||||
#include "inputs/rotary/RotaryPlugin.h"
|
||||
#include "inputs/analog/AnalogInputPlugin.h"
|
||||
#include "inputs/digital/DigitalInputPlugin.h"
|
||||
|
||||
WiFiNet *network;
|
||||
Sprocket *sprocket;
|
||||
DisplayPlugin *displayPlugin;
|
||||
WebServerPlugin *webServerPlugin;
|
||||
WebConfigPlugin *webConfigPlugin;
|
||||
WebApiPlugin *webApiPlugin;
|
||||
MqttPlugin *mqttPlugin;
|
||||
DisplayPlugin *screen;
|
||||
WebServerPlugin *webServer;
|
||||
WebConfigPlugin *webConfig;
|
||||
WebApiPlugin *webApi;
|
||||
MqttPlugin *mqtt;
|
||||
|
||||
void enableOled()
|
||||
RotaryPlugin *r1;
|
||||
AnalogInputPlugin *a1;
|
||||
AnalogInputPlugin *a2;
|
||||
AnalogInputPlugin *a3;
|
||||
AnalogInputPlugin *a4;
|
||||
DigitalInputPlugin *d1;
|
||||
|
||||
void init()
|
||||
{
|
||||
screen = new DisplayPlugin({DISPLAY_I2C_ADR, DISPLAY_SDA, DISPLAY_SCL, DISPLAY_FONT_SIZE, DISPLAY_ROWS, DISPLAY_GEOMETRY});
|
||||
mqtt = new MqttPlugin({MQTT_CLIENT_NAME, MQTT_HOST, MQTT_PORT, MQTT_ROOT_TOPIC}, true, true);
|
||||
webServer = new WebServerPlugin({WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_PORT});
|
||||
webConfig = new WebConfigPlugin(webServer->server);
|
||||
webApi = new WebApiPlugin(webServer->server);
|
||||
d1 = new DigitalInputPlugin({SWITCH_PIN,SWITCH_THRESHOLD, SWITCH_UPDATE_INTERVAL, SWITCH_TOPIC, SWITCH_PIN_MODE});
|
||||
a1 = new AnalogInputPlugin({POT_PIN_1, POT_THRESHOLD, POT_POLL_INTERVAL, POT_TOPIC_1});
|
||||
a2 = new AnalogInputPlugin({POT_PIN_2, POT_THRESHOLD, POT_POLL_INTERVAL, POT_TOPIC_2});
|
||||
a3 = new AnalogInputPlugin({POT_PIN_3, POT_THRESHOLD, POT_POLL_INTERVAL, POT_TOPIC_3});
|
||||
a4 = new AnalogInputPlugin({POT_PIN_4, POT_THRESHOLD, POT_POLL_INTERVAL, POT_TOPIC_4});
|
||||
r1 = new RotaryPlugin({ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_UPDATE_INTERVAL, ROTARY_ENCODER_LOWER_BOUND, ROTARY_ENCODER_UPPER_BOUND, ROTARY_ENCODER_CIRCULATE_VALUES, ROTARY_ENCODER_TOPIC});
|
||||
}
|
||||
|
||||
void configure()
|
||||
{
|
||||
|
||||
screen->display->init();
|
||||
screen->display->flipScreenVertically();
|
||||
screen->splashScreen("Wibbly", "Wobbly", "0.0.1", "Sprocket");
|
||||
r1->rotaryEncoder->setup([] { r1->rotaryEncoder->readEncoder_ISR(); });
|
||||
webServer->server->serveStatic("/mqttConfig.json", SPIFFS, "mqttConfig.json");
|
||||
}
|
||||
|
||||
void addPlugins()
|
||||
{
|
||||
sprocket->addPlugin(screen);
|
||||
sprocket->addPlugin(webServer);
|
||||
sprocket->addPlugin(webConfig);
|
||||
sprocket->addPlugin(webApi);
|
||||
sprocket->addPlugin(a1);
|
||||
sprocket->addPlugin(a2);
|
||||
sprocket->addPlugin(a3);
|
||||
sprocket->addPlugin(a4);
|
||||
sprocket->addPlugin(r1);
|
||||
sprocket->addPlugin(d1);
|
||||
sprocket->addPlugin(mqtt);
|
||||
}
|
||||
|
||||
String getInfoString()
|
||||
{
|
||||
return "Heap: " + String(ESP.getFreeHeap()) + "\n" +
|
||||
"Host: " + String(network->config.hostname) + "\n" +
|
||||
"AP: " + String(network->config.stationSSID) + "\n" +
|
||||
"IP: " + WiFi.localIP().toString() + "\n" +
|
||||
"Gateway: " + WiFi.gatewayIP().toString();
|
||||
}
|
||||
|
||||
void activate() {
|
||||
if (network->connect())
|
||||
{
|
||||
sprocket->activate();
|
||||
screen->display->flipScreenVertically();
|
||||
sprocket->publish("ui/print", getInfoString());
|
||||
}
|
||||
}
|
||||
|
||||
void initScreen(){
|
||||
pinMode(16, OUTPUT);
|
||||
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
|
||||
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
|
||||
delay(50);
|
||||
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(3000);
|
||||
enableOled();
|
||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||
displayPlugin = new DisplayPlugin({0x3c, 4, 15, 12, 4, 0});
|
||||
mqttPlugin = new MqttPlugin({MQTT_CLIENT_NAME, MQTT_HOST, MQTT_PORT, MQTT_TOPIC_IN, MQTT_TOPIC_OUT});
|
||||
webServerPlugin = new WebServerPlugin({WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_PORT});
|
||||
webConfigPlugin = new WebConfigPlugin(webServerPlugin->server);
|
||||
webApiPlugin = new WebApiPlugin(webServerPlugin->server);
|
||||
sprocket->addPlugin(displayPlugin);
|
||||
sprocket->addPlugin(webServerPlugin);
|
||||
sprocket->addPlugin(webConfigPlugin);
|
||||
sprocket->addPlugin(webApiPlugin);
|
||||
sprocket->addPlugin(mqttPlugin);
|
||||
|
||||
displayPlugin->display->init();
|
||||
displayPlugin->display->flipScreenVertically();
|
||||
displayPlugin->splashScreen("Wibbly", "Wobbly", "0.0.1", "Sprocket");
|
||||
|
||||
network = new WiFiNet(
|
||||
SPROCKET_MODE,
|
||||
STATION_SSID,
|
||||
STATION_PASSWORD,
|
||||
AP_SSID,
|
||||
AP_PASSWORD,
|
||||
HOSTNAME,
|
||||
CONNECT_TIMEOUT);
|
||||
network->init();
|
||||
if (network->connect())
|
||||
{
|
||||
sprocket->activate();
|
||||
displayPlugin->display->flipScreenVertically();
|
||||
sprocket->publish("ui/clear", "");
|
||||
sprocket->publish("ui/print",
|
||||
"Heap: " + String(ESP.getFreeHeap()) + "\n" +
|
||||
"Host: " + String(network->config.hostname) + "\n" +
|
||||
"AP: " + String(network->config.stationSSID) + "\n" +
|
||||
"IP: " + WiFi.localIP().toString() + "\n" +
|
||||
"Gateway: " + WiFi.gatewayIP().toString());
|
||||
}
|
||||
network = new WiFiNet(SPROCKET_MODE, STATION_SSID, STATION_PASSWORD, AP_SSID, AP_PASSWORD, HOSTNAME, CONNECT_TIMEOUT);
|
||||
initScreen();
|
||||
init();
|
||||
addPlugins();
|
||||
configure();
|
||||
activate();
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
Reference in New Issue
Block a user