mirror of
https://gitlab.com/zwirbel/sprocket-device-bb1.git
synced 2026-03-22 09:04:14 +01:00
add digital switch
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"mqttServer": "192.168.1.2",
|
"mqttClientName" : "bb1",
|
||||||
"port": 1883,
|
"mqttBrokerHost" : "192.168.1.2",
|
||||||
"clientName": "bb1",
|
"mqttBrokerPort" : 1883,
|
||||||
"inTopic": "wirelos/bb1-in/",
|
"mqttRootTopic" : "wirelos/bb1"
|
||||||
"outTopic": "wirelos/bb1-out/"
|
|
||||||
}
|
}
|
||||||
50
src/config.h
50
src/config.h
@@ -18,15 +18,14 @@
|
|||||||
#define AP_PASSWORD "th3r31sn0sp00n"
|
#define AP_PASSWORD "th3r31sn0sp00n"
|
||||||
#define STATION_SSID "MyAP"
|
#define STATION_SSID "MyAP"
|
||||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||||
#define HOSTNAME "sprocket"
|
#define HOSTNAME "bb1"
|
||||||
#define CONNECT_TIMEOUT 10000
|
#define CONNECT_TIMEOUT 10000
|
||||||
|
|
||||||
// mqtt config
|
// mqtt config
|
||||||
#define MQTT_CLIENT_NAME "bb1"
|
#define MQTT_CLIENT_NAME "bb1"
|
||||||
#define MQTT_HOST "192.168.1.2"
|
#define MQTT_HOST "192.168.1.2"
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
#define MQTT_TOPIC_IN "wirelos/bb1-in/"
|
#define MQTT_ROOT_TOPIC "wirelos/bb1"
|
||||||
#define MQTT_TOPIC_OUT "wirelos/bb1-out/"
|
|
||||||
|
|
||||||
// OTA config
|
// OTA config
|
||||||
#define OTA_PORT 8266
|
#define OTA_PORT 8266
|
||||||
@@ -38,4 +37,49 @@
|
|||||||
#define WEB_DEFAULT_FILE "index.html"
|
#define WEB_DEFAULT_FILE "index.html"
|
||||||
#define WEB_PORT 80
|
#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
|
#endif
|
||||||
125
src/main.cpp
125
src/main.cpp
@@ -7,64 +7,99 @@
|
|||||||
#include <WebConfigPlugin.h>
|
#include <WebConfigPlugin.h>
|
||||||
#include <WebApiPlugin.h>
|
#include <WebApiPlugin.h>
|
||||||
#include <MqttPlugin.h>
|
#include <MqttPlugin.h>
|
||||||
|
#include "inputs/rotary/RotaryPlugin.h"
|
||||||
|
#include "inputs/analog/AnalogInputPlugin.h"
|
||||||
|
#include "inputs/digital/DigitalInputPlugin.h"
|
||||||
|
|
||||||
WiFiNet *network;
|
WiFiNet *network;
|
||||||
Sprocket *sprocket;
|
Sprocket *sprocket;
|
||||||
DisplayPlugin *displayPlugin;
|
DisplayPlugin *screen;
|
||||||
WebServerPlugin *webServerPlugin;
|
WebServerPlugin *webServer;
|
||||||
WebConfigPlugin *webConfigPlugin;
|
WebConfigPlugin *webConfig;
|
||||||
WebApiPlugin *webApiPlugin;
|
WebApiPlugin *webApi;
|
||||||
MqttPlugin *mqttPlugin;
|
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);
|
pinMode(16, OUTPUT);
|
||||||
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
|
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
|
||||||
delay(50);
|
delay(50);
|
||||||
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
|
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
delay(3000);
|
|
||||||
enableOled();
|
|
||||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||||
displayPlugin = new DisplayPlugin({0x3c, 4, 15, 12, 4, 0});
|
network = new WiFiNet(SPROCKET_MODE, STATION_SSID, STATION_PASSWORD, AP_SSID, AP_PASSWORD, HOSTNAME, CONNECT_TIMEOUT);
|
||||||
mqttPlugin = new MqttPlugin({MQTT_CLIENT_NAME, MQTT_HOST, MQTT_PORT, MQTT_TOPIC_IN, MQTT_TOPIC_OUT});
|
initScreen();
|
||||||
webServerPlugin = new WebServerPlugin({WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_PORT});
|
init();
|
||||||
webConfigPlugin = new WebConfigPlugin(webServerPlugin->server);
|
addPlugins();
|
||||||
webApiPlugin = new WebApiPlugin(webServerPlugin->server);
|
configure();
|
||||||
sprocket->addPlugin(displayPlugin);
|
activate();
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|||||||
Reference in New Issue
Block a user