Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Adafruit_NeoKey_1x4.h"
- #include "seesaw_neopixel.h"
- #include <PubSubClient.h>
- #include <ESP8266WiFi.h>
- #include <ArduinoOTA.h>
- #include <Servo.h>
- void callback(char* topic, byte* payload, unsigned int length);
- #define MQTT_SERVER "<REDACTED>" //IP addy of MQTT broker eg. 192.168.1.4
- const char* ssid = "<REDACTED>"; //name of broadcasted wifi access point
- const char* password = "<REDACTED>"; //wifi password
- //This is the MQTT Topic that will be used to manage if the TallyWhacker should be whacking
- //Refer to my YAML component entry
- char const* neokey = "home/neokey";
- uint8_t j=0; // this variable tracks the colors of the LEDs cycle.
- Adafruit_NeoKey_1x4 ssneokey;
- Servo servo;
- WiFiClient wifiClient;
- PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
- void setup()
- {
- //A name given to your ESP8266 module when discovering it as a port in ARDUINO IDE
- ArduinoOTA.setHostname("TallyWhacker");
- ArduinoOTA.begin(); // OTA initialization
- Serial.begin(115200);
- delay(100);
- Serial.println("Serial initialized.");
- //start wifi subsystem
- WiFi.begin(ssid, password);
- //attempt to connect to the WIFI network and then connect to the MQTT server
- reconnect();
- //wait a bit before starting the main loop
- delay(500);
- while (! Serial) delay(100);
- if (! ssneokey.begin(0x30)) {
- Serial.println("Could not start NeoKey, check wiring?");
- while(1) delay(100);
- }
- Serial.println("NeoKey started!");
- for (uint16_t i=0; i<ssneokey.pixels.numPixels(); i++) {
- ssneokey.pixels.setPixelColor(i, Wheel(map(i, 0, ssneokey.pixels.numPixels(), 0, 255)));
- ssneokey.pixels.show();
- delay(50);
- }
- for (uint16_t i=0; i<ssneokey.pixels.numPixels(); i++) {
- ssneokey.pixels.setPixelColor(i, 0x000000);
- ssneokey.pixels.show();
- delay(50);
- }
- }
- void loop()
- {
- //reconnect if connection is lost
- if (!client.connected() && WiFi.status() == 3) {reconnect();}
- ArduinoOTA.handle();
- //maintain MQTT connection
- client.loop();
- //MUST delay to allow ESP8266 WIFI functions to run
- delay(10);
- uint8_t buttons = ssneokey.read();
- //for (int i=0; i< ssneokey.pixels.numPixels(); i++) {
- // ssneokey.pixels.setPixelColor(i, Wheel(((i * 256 / ssneokey.pixels.numPixels()) + j) & 255));
- //}
- if (buttons & (1<<0)) {
- // Serial.println("Button A");
- client.publish("home/neokey/buttona", "1");
- } else {
- //ssneokey.pixels.setPixelColor(0, 0);
- }
- if (buttons & (1<<1)) {
- // Serial.println("Button B");
- client.publish("home/neokey/buttonb", "1");
- } else {
- // ssneokey.pixels.setPixelColor(1, 0);
- }
- if (buttons & (1<<2)) {
- // Serial.println("Button C");
- client.publish("home/neokey/buttonc", "1");
- } else {
- //ssneokey.pixels.setPixelColor(2, 0);
- }
- if (buttons & (1<<3)) {
- // Serial.println("Button D");
- client.publish("home/neokey/buttond", "1");
- } else {
- //ssneokey.pixels.setPixelColor(3, 0);
- }
- neokey.pixels.show();
- delay(100); // don't print too fast
- //j++; // make colors cycle
- }
- // Input a value 0 to 255 to get a color value.
- // The colors are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos) {
- if(WheelPos < 85) {
- return seesaw_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- } else if(WheelPos < 170) {
- WheelPos -= 85;
- return seesaw_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3);
- } else {
- WheelPos -= 170;
- return seesaw_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- return 0;
- }
- void reconnect()
- {
- //attempt to connect to the wifi if connection is lost
- if(WiFi.status() != WL_CONNECTED)
- {
- //loop while we wait for connection
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- }
- }
- //make sure we are connected to WIFI before attemping to reconnect to MQTT
- if(WiFi.status() == WL_CONNECTED)
- {
- // Loop until we're reconnected to the MQTT server
- while (!client.connected())
- {
- // Generate client name based on MAC address and last 8 bits of microsecond counter
- String clientName;
- clientName += "neokey-";
- uint8_t mac[6];
- WiFi.macAddress(mac);
- clientName += macToStr(mac);
- //if connected, subscribe to the topic(s) we want to be notified about
- //Delete "tallywhacker", and "<password>" here if you are not using any password
- //<password> is for your MQTT broker
- if (client.connect((char*) clientName.c_str(), "<REDACTED>", "<REDACTED>"))
- { //Update accordingly with your MQTT account
- client.subscribe(neokey);
- }
- //otherwise print failed for debugging
- else{abort();}
- }
- }
- }
- //generate unique name from MAC addr
- String macToStr(const uint8_t* mac)
- {
- String result;
- for (int i = 0; i < 6; ++i)
- {
- result += String(mac[i], 16);
- if (i < 5)
- {
- result += ':';
- }
- }
- return result;
- }
Add Comment
Please, Sign In to add comment