Look, i got it!


cyberWinkeKatze.­ino
06.12.2019 16:26:08
0.01 MB (4a5c023d0f0f034e36c00052db9ce57a)

http://files.q3t.de/a/get/5e7247d87d9c0489dffd93ed05dbe99d

#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino //needed for library #include <ESP8266WebServer.h> #include <DNSServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager #include <Wire.h> // Include Wire if you're using I2C #include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library #include <PubSubClient.h> #include <Adafruit_NeoPixel.h> #define PIXEL_PIN 2 #define PIXEL_COUNT 2 // select wich pin will trigger the configuraton portal when set to LOW // ESP-01 users please note: the only pins available (0 and 2), are shared // with the bootloader, so always set them HIGH at power-up #define TRIGGER_PIN 1 #define B_LEFT_PIN 4 #define B_RIGHT_PIN 5 #define PIN_RESET 255 // #define DC_JUMPER 0 // I2C Addres: 0 - 0x3C, 1 - 0x3D const char* mqtt_server = "mqtt.winkekatze24.de"; MicroOLED oled(PIN_RESET, DC_JUMPER); // Example I2C declaration WiFiClient espClient; PubSubClient client(espClient); Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); int led_fade = 1; void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("\n Starting"); delay(2000); strip.begin(); // Serial.println("A"); strip.show(); // Initialize all pixels to 'off' // Serial.println("B"); led_off(); // Serial.println("C"); // pinMode(TRIGGER_PIN, INPUT); // Serial.println("D"); pinMode(B_LEFT_PIN, INPUT); // Serial.println("E"); pinMode(B_RIGHT_PIN, INPUT); // Serial.println("F"); // These three lines of code are all you need to initialize the // OLED and print the splash screen. // Before you can start using the OLED, call begin() to init // all of the pins and configure the OLED. oled.begin(); // Serial.println("G"); // clear(ALL) will clear out the OLED's graphic memory. // clear(PAGE) will clear the Arduino's display buffer. oled.clear(ALL); // Clear the display's memory (gets rid of artifacts) // Serial.println("H"); oled.setFontType(0); oled.setCursor(0, 0); oled.clear(PAGE);//clear the screen before we draw our image // is configuration portal requested? if ( digitalRead(TRIGGER_PIN) == LOW && false) { //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset settings - for testing //wifiManager.resetSettings(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds //wifiManager.setTimeout(120); //it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1 // WiFi.mode(WIFI_STA); oled.print("Web config..."); oled.display(); if (!wifiManager.startConfigPortal("esp")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } else { // put your setup code here, to run once: oled.print("connecting..."); oled.display(); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset saved settings //wifiManager.resetSettings(); //set custom ip for portal //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); //fetches ssid and pass from eeprom and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration wifiManager.autoConnect("AutoConnectAP"); //or use this for auto generated name ESP + ChipID //wifiManager.autoConnect(); //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); oled.setCursor(0, 0); oled.clear(PAGE); oled.print(WiFi.localIP()); oled.display(); } client.setServer(mqtt_server, 1883); client.setCallback(callback); led_fade = 1; } void led_off() { led_fade = 0; for(int i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, strip.Color(0, 0, 0)); } strip.show(); } void led_on() { led_fade = 1; } void callback(char* topic, byte* payload, unsigned int length) { if((String)topic=="cyberWinkeKatze/display/cls") { oled.clear(ALL); oled.clear(PAGE); oled.setCursor(0, 0); oled.display(); } else if((String)topic=="cyberWinkeKatze/display/print") { for (int i = 0; i < length; i++) { oled.print((char)payload[i]); } oled.display(); } else if((String)topic=="cyberWinkeKatze/display/println") { for (int i = 0; i < length; i++) { oled.print((char)payload[i]); } oled.print("\n"); oled.display(); } else if((String)topic=="cyberWinkeKatze/led/on") { led_on(); } else if((String)topic=="cyberWinkeKatze/led/off") { led_off(); } else if((String)topic=="winkekatze/allcats" || (String)topic=="winkekatze/cyberWinkeKatze") { int led_was = led_fade; led_off(); oled.clear(ALL); oled.clear(PAGE); for(int i=0; i<3; i++) { oled.setCursor(0, 0); oled.print("=^_^=/\n"); oled.display(); delay(150); oled.setCursor(0, 0); oled.print("=^_^=|\n"); oled.display(); delay(150); } oled.setCursor(0, 0); oled.print("=^.-=/\n"); oled.display(); delay(150); oled.setCursor(0, 0); oled.print("=^-^=/\n"); oled.display(); delay(300); oled.clear(ALL); oled.clear(PAGE); oled.display(); if(led_was==1) led_on(); } Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); client.publish("cyberWinkeKatze/status", "fishing"); } void reconnect() { // Loop until we're reconnected oled.print("\nconnecting to mqtt"); oled.display(); while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if(client.connect("cyberWinkeKatze", "cyberWinkeKatze/connected", 2, true, "0")) { client.publish("cyberWinkeKatze/connected", "1", true); Serial.println("connected"); client.subscribe("winkekatze/allcats"); client.subscribe("winkekatze/cyberWinkeKatze"); client.subscribe("cyberWinkeKatze/display/print"); client.subscribe("cyberWinkeKatze/display/println"); client.subscribe("cyberWinkeKatze/display/cls"); client.subscribe("cyberWinkeKatze/led/on"); client.subscribe("cyberWinkeKatze/led/off"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } oled.print("\nperfect!"); oled.display(); delay(1000); oled.clear(ALL); oled.clear(PAGE); oled.setCursor(0, 0); } // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if(WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } void loop() { static int button = 0, button_last=0, button_millies = 0, button_lock = 0, action=0; static int led_millis = 0; static uint16_t led_num=0, hue=0; if (!client.connected()) { reconnect(); } if(digitalRead(B_LEFT_PIN) == LOW) button = 1; else if(digitalRead(B_RIGHT_PIN) == LOW) button = 2; else button = 0; if(button==0 && button_lock==1) { if((millis()-button_millies)>50) { button_millies = 0; button_lock = 0; } } else if(button_lock==1) button = 0; if(button==button_last && button!=0) { if((millis()-button_millies)>50) { action = button; button = button_last = button_millies = 0; button_lock = 1; } } else if(button!=0) { button_last = button; button_millies = millis(); } if(action) { if(action==1) { client.publish("outTopic", "left"); Serial.print("L\n"); client.publish("winkekatze/allcats", "wink"); } if (action==2) { client.publish("outTopic", "right"); Serial.print("R\n"); } action = 0; } if((millis()-led_millis)>20 && led_fade==1) { hue++; if(hue>255) { hue = 0; //led_num++; //if(led_num>=strip.numPixels()) led_num = 0; } for(led_num=0; led_num<strip.numPixels(); led_num++) { strip.setPixelColor(led_num, Wheel(((led_num*255/strip.numPixels())+hue) & 255)); } strip.show(); led_millis=millis(); } client.loop(); }
timings [ include: 82µs, load: 12µs, generate: 50µs, action: 12ms, final: 228µs, total: 13ms ]