mealsowheels

Neokey

Jan 24th, 2022 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include "Adafruit_NeoKey_1x4.h"
  2. #include "seesaw_neopixel.h"
  3. #include <PubSubClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <ArduinoOTA.h>
  6. #include <Servo.h>
  7.  
  8. void callback(char* topic, byte* payload, unsigned int length);
  9.  
  10. #define MQTT_SERVER "<REDACTED>" //IP addy of MQTT broker eg. 192.168.1.4
  11. const char* ssid = "<REDACTED>"; //name of broadcasted wifi access point
  12. const char* password = "<REDACTED>"; //wifi password
  13.  
  14. //This is the MQTT Topic that will be used to manage if the TallyWhacker should be whacking
  15. //Refer to my YAML component entry
  16. char const* neokey = "home/neokey";
  17. uint8_t j=0; // this variable tracks the colors of the LEDs cycle.
  18.  
  19. Adafruit_NeoKey_1x4 ssneokey;
  20. Servo servo;
  21. WiFiClient wifiClient;
  22. PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
  23.  
  24. void setup()
  25. {
  26. //A name given to your ESP8266 module when discovering it as a port in ARDUINO IDE
  27. ArduinoOTA.setHostname("TallyWhacker");
  28. ArduinoOTA.begin(); // OTA initialization
  29.  
  30. Serial.begin(115200);
  31. delay(100);
  32. Serial.println("Serial initialized.");
  33.  
  34. //start wifi subsystem
  35. WiFi.begin(ssid, password);
  36. //attempt to connect to the WIFI network and then connect to the MQTT server
  37. reconnect();
  38.  
  39. //wait a bit before starting the main loop
  40. delay(500);
  41.  
  42. while (! Serial) delay(100);
  43.  
  44. if (! ssneokey.begin(0x30)) {
  45. Serial.println("Could not start NeoKey, check wiring?");
  46. while(1) delay(100);
  47. }
  48.  
  49. Serial.println("NeoKey started!");
  50.  
  51. for (uint16_t i=0; i<ssneokey.pixels.numPixels(); i++) {
  52. ssneokey.pixels.setPixelColor(i, Wheel(map(i, 0, ssneokey.pixels.numPixels(), 0, 255)));
  53. ssneokey.pixels.show();
  54. delay(50);
  55. }
  56.  
  57. for (uint16_t i=0; i<ssneokey.pixels.numPixels(); i++) {
  58. ssneokey.pixels.setPixelColor(i, 0x000000);
  59. ssneokey.pixels.show();
  60. delay(50);
  61. }
  62. }
  63.  
  64. void loop()
  65. {
  66. //reconnect if connection is lost
  67. if (!client.connected() && WiFi.status() == 3) {reconnect();}
  68.  
  69. ArduinoOTA.handle();
  70.  
  71. //maintain MQTT connection
  72. client.loop();
  73. //MUST delay to allow ESP8266 WIFI functions to run
  74. delay(10);
  75.  
  76. uint8_t buttons = ssneokey.read();
  77.  
  78. //for (int i=0; i< ssneokey.pixels.numPixels(); i++) {
  79. // ssneokey.pixels.setPixelColor(i, Wheel(((i * 256 / ssneokey.pixels.numPixels()) + j) & 255));
  80. //}
  81.  
  82. if (buttons & (1<<0)) {
  83. // Serial.println("Button A");
  84. client.publish("home/neokey/buttona", "1");
  85. } else {
  86. //ssneokey.pixels.setPixelColor(0, 0);
  87. }
  88.  
  89. if (buttons & (1<<1)) {
  90. // Serial.println("Button B");
  91. client.publish("home/neokey/buttonb", "1");
  92. } else {
  93. // ssneokey.pixels.setPixelColor(1, 0);
  94. }
  95.  
  96. if (buttons & (1<<2)) {
  97. // Serial.println("Button C");
  98. client.publish("home/neokey/buttonc", "1");
  99. } else {
  100. //ssneokey.pixels.setPixelColor(2, 0);
  101. }
  102.  
  103. if (buttons & (1<<3)) {
  104. // Serial.println("Button D");
  105. client.publish("home/neokey/buttond", "1");
  106. } else {
  107. //ssneokey.pixels.setPixelColor(3, 0);
  108. }
  109.  
  110. neokey.pixels.show();
  111. delay(100); // don't print too fast
  112. //j++; // make colors cycle
  113. }
  114.  
  115. // Input a value 0 to 255 to get a color value.
  116. // The colors are a transition r - g - b - back to r.
  117. uint32_t Wheel(byte WheelPos) {
  118. if(WheelPos < 85) {
  119. return seesaw_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  120. } else if(WheelPos < 170) {
  121. WheelPos -= 85;
  122. return seesaw_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3);
  123. } else {
  124. WheelPos -= 170;
  125. return seesaw_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3);
  126. }
  127. return 0;
  128. }
  129.  
  130. void reconnect()
  131. {
  132. //attempt to connect to the wifi if connection is lost
  133. if(WiFi.status() != WL_CONNECTED)
  134. {
  135. //loop while we wait for connection
  136. while (WiFi.status() != WL_CONNECTED)
  137. {
  138. delay(500);
  139. }
  140. }
  141.  
  142. //make sure we are connected to WIFI before attemping to reconnect to MQTT
  143. if(WiFi.status() == WL_CONNECTED)
  144. {
  145. // Loop until we're reconnected to the MQTT server
  146. while (!client.connected())
  147. {
  148. // Generate client name based on MAC address and last 8 bits of microsecond counter
  149. String clientName;
  150. clientName += "neokey-";
  151. uint8_t mac[6];
  152. WiFi.macAddress(mac);
  153. clientName += macToStr(mac);
  154.  
  155. //if connected, subscribe to the topic(s) we want to be notified about
  156. //Delete "tallywhacker", and "<password>" here if you are not using any password
  157. //<password> is for your MQTT broker
  158. if (client.connect((char*) clientName.c_str(), "<REDACTED>", "<REDACTED>"))
  159. { //Update accordingly with your MQTT account
  160. client.subscribe(neokey);
  161. }
  162. //otherwise print failed for debugging
  163. else{abort();}
  164. }
  165. }
  166. }
  167.  
  168. //generate unique name from MAC addr
  169. String macToStr(const uint8_t* mac)
  170. {
  171. String result;
  172.  
  173. for (int i = 0; i < 6; ++i)
  174. {
  175. result += String(mac[i], 16);
  176. if (i < 5)
  177. {
  178. result += ':';
  179. }
  180. }
  181. return result;
  182. }
Add Comment
Please, Sign In to add comment