add esp8266 and mqtt publish and color set
This commit is contained in:
parent
c5538211af
commit
e4cc86451f
2 changed files with 307 additions and 43 deletions
|
@ -17,7 +17,7 @@ monitor_speed = 115200
|
||||||
|
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
'-D MQTT_BASETOPIC="lightbutton_test"'
|
'-D MQTT_BASETOPIC="lightbuttons-A"'
|
||||||
|
|
||||||
lib_deps=
|
lib_deps=
|
||||||
ethernet
|
ethernet
|
||||||
|
@ -26,3 +26,23 @@ lib_deps=
|
||||||
adafruit/Adafruit seesaw Library@^1.7.8
|
adafruit/Adafruit seesaw Library@^1.7.8
|
||||||
|
|
||||||
adafruit/Adafruit MQTT Library@^2.5.8
|
adafruit/Adafruit MQTT Library@^2.5.8
|
||||||
|
mathertel/OneButton@^2.6.1
|
||||||
|
|
||||||
|
|
||||||
|
[env:wemosd1]
|
||||||
|
platform = espressif8266
|
||||||
|
board = d1_mini
|
||||||
|
framework = arduino
|
||||||
|
|
||||||
|
monitor_speed = 115200
|
||||||
|
|
||||||
|
|
||||||
|
build_flags =
|
||||||
|
'-D MQTT_BASETOPIC="lightbuttons-A"'
|
||||||
|
-D WIFI
|
||||||
|
|
||||||
|
lib_deps=
|
||||||
|
SPI
|
||||||
|
adafruit/Adafruit seesaw Library@^1.7.8
|
||||||
|
adafruit/Adafruit MQTT Library@^2.5.8
|
||||||
|
mathertel/OneButton@^2.6.1
|
||||||
|
|
326
src/main.cpp
326
src/main.cpp
|
@ -1,5 +1,10 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#ifndef WIFI
|
||||||
#include <ETH.h>
|
#include <ETH.h>
|
||||||
|
#endif
|
||||||
|
#ifdef WIFI
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#endif
|
||||||
#include "Adafruit_NeoKey_1x4.h"
|
#include "Adafruit_NeoKey_1x4.h"
|
||||||
#include "seesaw_neopixel.h"
|
#include "seesaw_neopixel.h"
|
||||||
|
|
||||||
|
@ -9,6 +14,8 @@
|
||||||
|
|
||||||
#include "credentials.h"
|
#include "credentials.h"
|
||||||
|
|
||||||
|
#include <OneButton.h>
|
||||||
|
|
||||||
static bool eth_connected = false;
|
static bool eth_connected = false;
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,22 +25,74 @@ WiFiClient client;
|
||||||
//WiFiClientSecure client;
|
//WiFiClientSecure client;
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||||
//Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
#ifdef MQTT_USERNAME
|
||||||
|
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
||||||
|
#else
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT);
|
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
Adafruit_MQTT_Publish statusuptimePub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status/uptime");
|
||||||
Adafruit_MQTT_Publish statusPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status");
|
Adafruit_MQTT_Publish statusPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status");
|
||||||
|
|
||||||
|
#ifndef WIFI
|
||||||
void WiFiEvent(WiFiEvent_t event);
|
void WiFiEvent(WiFiEvent_t event);
|
||||||
|
#endif
|
||||||
|
|
||||||
void MQTT_connect();
|
void MQTT_connect();
|
||||||
|
|
||||||
|
|
||||||
bool nokey_enabled=false;
|
|
||||||
|
bool neokey_enabled=false;
|
||||||
Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object
|
Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object
|
||||||
|
|
||||||
uint32_t Wheel(byte WheelPos);
|
uint32_t Wheel(byte WheelPos);
|
||||||
NeoKey1x4Callback blink(keyEvent evt);
|
NeoKey1x4Callback blink(keyEvent evt);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
OneButton button1;
|
||||||
|
void click1();
|
||||||
|
void doubleclick1();
|
||||||
|
void longPressStart1();
|
||||||
|
Adafruit_MQTT_Publish button1Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button1");
|
||||||
|
Adafruit_MQTT_Subscribe button1Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button1");
|
||||||
|
void button1ColorCB(uint32_t c);
|
||||||
|
uint32_t releasecolor1;
|
||||||
|
|
||||||
|
OneButton button2;
|
||||||
|
void click2();
|
||||||
|
void doubleclick2();
|
||||||
|
void longPressStart2();
|
||||||
|
Adafruit_MQTT_Publish button2Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button2");
|
||||||
|
Adafruit_MQTT_Subscribe button2Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button2");
|
||||||
|
void button2ColorCB(uint32_t c);
|
||||||
|
uint32_t releasecolor2;
|
||||||
|
|
||||||
|
OneButton button3;
|
||||||
|
void click3();
|
||||||
|
void doubleclick3();
|
||||||
|
void longPressStart3();
|
||||||
|
Adafruit_MQTT_Publish button3Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button3");
|
||||||
|
Adafruit_MQTT_Subscribe button3Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button3");
|
||||||
|
void button3ColorCB(uint32_t c);
|
||||||
|
uint32_t releasecolor3;
|
||||||
|
|
||||||
|
OneButton button4;
|
||||||
|
void click4();
|
||||||
|
void doubleclick4();
|
||||||
|
void longPressStart4();
|
||||||
|
Adafruit_MQTT_Publish button4Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button4");
|
||||||
|
Adafruit_MQTT_Subscribe button4Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button4");
|
||||||
|
void button4ColorCB(uint32_t c);
|
||||||
|
uint32_t releasecolor4;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t presscolor = 0x555555;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//define a callback for key presses
|
//define a callback for key presses
|
||||||
NeoKey1x4Callback blink(keyEvent evt) {
|
NeoKey1x4Callback blink(keyEvent evt) {
|
||||||
uint8_t key = evt.bit.NUM;
|
uint8_t key = evt.bit.NUM;
|
||||||
|
@ -61,44 +120,14 @@ void setup() {
|
||||||
while (! Serial) delay(10);
|
while (! Serial) delay(10);
|
||||||
|
|
||||||
|
|
||||||
// Connect to WiFi access point.
|
|
||||||
/*
|
|
||||||
Serial.println(); Serial.println();
|
|
||||||
Serial.print("Connecting to ");
|
|
||||||
Serial.println(WLAN_SSID);
|
|
||||||
|
|
||||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
Serial.println("WiFi connected");
|
|
||||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Add a handler for network events. This is misnamed "WiFi" because the ESP32 is historically WiFi only,
|
|
||||||
// but in our case, this will react to Ethernet events.
|
|
||||||
Serial.print("Registering event handler for ETH events...");
|
|
||||||
WiFi.onEvent(WiFiEvent);
|
|
||||||
Serial.print("Starting ETH interface...");
|
|
||||||
ETH.begin();
|
|
||||||
|
|
||||||
// Send Hello World Notification
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (! neokey.begin(0x30)) { // begin with I2C address, default is 0x30
|
if (! neokey.begin(0x30)) { // begin with I2C address, default is 0x30
|
||||||
Serial.println("Could not start NeoKey, check wiring?");
|
Serial.println("Could not start NeoKey, check wiring?");
|
||||||
nokey_enabled=false;
|
neokey_enabled=false;
|
||||||
}else{
|
}else{
|
||||||
nokey_enabled=true;
|
neokey_enabled=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nokey_enabled)
|
if (neokey_enabled)
|
||||||
{
|
{
|
||||||
Serial.println("NeoKey started!");
|
Serial.println("NeoKey started!");
|
||||||
|
|
||||||
|
@ -120,24 +149,151 @@ void setup() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WIFI //Only for Wifi
|
||||||
|
// Connect to WiFi access point.
|
||||||
|
Serial.println(); Serial.println();
|
||||||
|
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(WLAN_SSID);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
if ((millis()/500)%2==0) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0x0000FF);
|
||||||
|
}else{
|
||||||
|
neokey.pixels.setPixelColor(i, 0x00FF00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
eth_connected=true;
|
||||||
|
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0xFFFF00);
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
|
|
||||||
|
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0x000000);
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WIFI //Only for Ethernet
|
||||||
|
// Add a handler for network events. This is misnamed "WiFi" because the ESP32 is historically WiFi only,
|
||||||
|
// but in our case, this will react to Ethernet events.
|
||||||
|
Serial.print("Registering event handler for ETH events...");
|
||||||
|
WiFi.onEvent(WiFiEvent);
|
||||||
|
|
||||||
|
Serial.print("Starting ETH interface...");
|
||||||
|
ETH.begin();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
button1.attachClick(click1);
|
||||||
|
button1.attachDoubleClick(doubleclick1);
|
||||||
|
button1.attachLongPressStart(longPressStart1);
|
||||||
|
button1Color.setCallback(button1ColorCB);
|
||||||
|
mqtt.subscribe(&button1Color);
|
||||||
|
|
||||||
|
button2.attachClick(click2);
|
||||||
|
button2.attachDoubleClick(doubleclick2);
|
||||||
|
button2.attachLongPressStart(longPressStart2);
|
||||||
|
button2Color.setCallback(button2ColorCB);
|
||||||
|
mqtt.subscribe(&button2Color);
|
||||||
|
|
||||||
|
button3.attachClick(click3);
|
||||||
|
button3.attachDoubleClick(doubleclick3);
|
||||||
|
button3.attachLongPressStart(longPressStart3);
|
||||||
|
button3Color.setCallback(button3ColorCB);
|
||||||
|
mqtt.subscribe(&button3Color);
|
||||||
|
|
||||||
|
button4.attachClick(click4);
|
||||||
|
button4.attachDoubleClick(doubleclick4);
|
||||||
|
button4.attachLongPressStart(longPressStart4);
|
||||||
|
button4Color.setCallback(button4ColorCB);
|
||||||
|
mqtt.subscribe(&button4Color);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
MQTT_connect();
|
||||||
|
|
||||||
|
mqtt.processPackets(10);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// we handle all key events with the callbacks
|
// we handle all key events with the callbacks
|
||||||
if (nokey_enabled)
|
if (neokey_enabled)
|
||||||
{
|
{
|
||||||
neokey.read();
|
uint8_t buttons = neokey.read();
|
||||||
|
|
||||||
|
// Check each button, if pressed, light the matching neopixel
|
||||||
|
|
||||||
|
if (buttons & (1<<0)) {
|
||||||
|
//Serial.println("Button A");
|
||||||
|
neokey.pixels.setPixelColor(0, presscolor);
|
||||||
|
}else{
|
||||||
|
neokey.pixels.setPixelColor(0, releasecolor1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttons & (1<<1)) {
|
||||||
|
//Serial.println("Button B");
|
||||||
|
neokey.pixels.setPixelColor(1, presscolor);
|
||||||
|
}else{
|
||||||
|
neokey.pixels.setPixelColor(1, releasecolor2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttons & (1<<2)) {
|
||||||
|
//Serial.println("Button C");
|
||||||
|
neokey.pixels.setPixelColor(2, presscolor);
|
||||||
|
}else{
|
||||||
|
neokey.pixels.setPixelColor(2, releasecolor3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttons & (1<<3)) {
|
||||||
|
//Serial.println("Button D");
|
||||||
|
neokey.pixels.setPixelColor(3, presscolor);
|
||||||
|
}else{
|
||||||
|
neokey.pixels.setPixelColor(3, releasecolor4);
|
||||||
|
}
|
||||||
|
|
||||||
|
neokey.pixels.show();
|
||||||
|
|
||||||
|
button1.tick(buttons & (1<<0));
|
||||||
|
button2.tick(buttons & (1<<1));
|
||||||
|
button3.tick(buttons & (1<<2));
|
||||||
|
button4.tick(buttons & (1<<3));
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned long last_status_send=0;
|
static unsigned long last_status_send=0;
|
||||||
if (millis() - last_status_send > 1000){
|
if (millis() - last_status_send > 60000){
|
||||||
last_status_send=millis();
|
last_status_send=millis();
|
||||||
if (!eth_connected){
|
if (!eth_connected){
|
||||||
Serial.println("Waiting for ethernet connection");
|
Serial.println("Waiting for ethernet connection");
|
||||||
}else{
|
}else{
|
||||||
Serial.print(F("\nSending Hello!"));
|
Serial.print(F("\nSending Hello!"));
|
||||||
Serial.print("...");
|
Serial.print("...");
|
||||||
if (! statusPub.publish("Hello")) {
|
if (! statusuptimePub.publish((uint32_t)millis())) {
|
||||||
Serial.println(F("Failed"));
|
Serial.println(F("Failed"));
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F("OK!"));
|
Serial.println(F("OK!"));
|
||||||
|
@ -145,11 +301,11 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(10); // don't print too fast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// React to Ethernet events:-
|
// React to Ethernet events:-
|
||||||
|
#ifndef WIFI
|
||||||
void WiFiEvent(WiFiEvent_t event)
|
void WiFiEvent(WiFiEvent_t event)
|
||||||
{
|
{
|
||||||
switch (event) {
|
switch (event) {
|
||||||
|
@ -201,7 +357,7 @@ void WiFiEvent(WiFiEvent_t event)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/******************************************/
|
/******************************************/
|
||||||
|
|
||||||
|
@ -237,13 +393,101 @@ void MQTT_connect() {
|
||||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||||
Serial.println(mqtt.connectErrorString(ret));
|
Serial.println(mqtt.connectErrorString(ret));
|
||||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0x0000FF);
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
mqtt.disconnect();
|
mqtt.disconnect();
|
||||||
delay(5000); // wait 5 seconds
|
delay(5000); // wait 5 seconds
|
||||||
retries--;
|
retries--;
|
||||||
if (retries == 0) {
|
if (retries == 0) {
|
||||||
// basically die and wait for WDT to reset me
|
// basically die and wait for WDT to reset me
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0x440000);
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Serial.println("MQTT Connected!");
|
Serial.println("MQTT Connected!");
|
||||||
|
statusPub.publish("online");
|
||||||
|
for (uint16_t i=0; i<neokey.pixels.numPixels(); i++) {
|
||||||
|
neokey.pixels.setPixelColor(i, 0);
|
||||||
|
}
|
||||||
|
neokey.pixels.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void click1() {
|
||||||
|
Serial.println("Button 1 click.");
|
||||||
|
button1Pub.publish("click");
|
||||||
|
}
|
||||||
|
void doubleclick1() {
|
||||||
|
Serial.println("Button 1 doubleclick.");
|
||||||
|
button1Pub.publish("doubleclick");
|
||||||
|
}
|
||||||
|
void longPressStart1() {
|
||||||
|
Serial.println("Button 1 longPress start");
|
||||||
|
button1Pub.publish("longpress");
|
||||||
|
}
|
||||||
|
void button1ColorCB(uint32_t c) {
|
||||||
|
Serial.print("Button1Color CB="); Serial.println(c);
|
||||||
|
releasecolor1=c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void click2() {
|
||||||
|
Serial.println("Button 2 click.");
|
||||||
|
button2Pub.publish("click");
|
||||||
|
}
|
||||||
|
void doubleclick2() {
|
||||||
|
Serial.println("Button 2 doubleclick.");
|
||||||
|
button2Pub.publish("doubleclick");
|
||||||
|
}
|
||||||
|
void longPressStart2() {
|
||||||
|
Serial.println("Button 2 longPress start");
|
||||||
|
button2Pub.publish("longpress");
|
||||||
|
}
|
||||||
|
|
||||||
|
void button2ColorCB(uint32_t c) {
|
||||||
|
Serial.print("Button2Color CB="); Serial.println(c);
|
||||||
|
releasecolor2=c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void click3() {
|
||||||
|
Serial.println("Button 3 click.");
|
||||||
|
button3Pub.publish("click");
|
||||||
|
}
|
||||||
|
void doubleclick3() {
|
||||||
|
Serial.println("Button 3 doubleclick.");
|
||||||
|
button3Pub.publish("doubleclick");
|
||||||
|
}
|
||||||
|
void longPressStart3() {
|
||||||
|
Serial.println("Button 3 longPress start");
|
||||||
|
button3Pub.publish("longpress");
|
||||||
|
}
|
||||||
|
|
||||||
|
void button3ColorCB(uint32_t c) {
|
||||||
|
Serial.print("Button3Color CB="); Serial.println(c);
|
||||||
|
releasecolor3=c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void click4() {
|
||||||
|
Serial.println("Button 4 click.");
|
||||||
|
button4Pub.publish("click");
|
||||||
|
}
|
||||||
|
void doubleclick4() {
|
||||||
|
Serial.println("Button 4 doubleclick.");
|
||||||
|
button4Pub.publish("doubleclick");
|
||||||
|
}
|
||||||
|
void longPressStart4() {
|
||||||
|
Serial.println("Button 4 longPress start");
|
||||||
|
button4Pub.publish("longpress");
|
||||||
|
}
|
||||||
|
|
||||||
|
void button4ColorCB(uint32_t c) {
|
||||||
|
Serial.print("Button4Color CB="); Serial.println(c);
|
||||||
|
releasecolor4=c;
|
||||||
}
|
}
|
Loading…
Reference in a new issue