Created
February 29, 2020 23:36
-
-
Save oschettler/0dd0d95325684f8b95694d9fcfe06a12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tb_display.h> | |
#include <M5StickC.h> | |
#include <WiFi.h> | |
#include <Espalexa.h> | |
// prototypes | |
boolean connectWifi(); | |
//callback functions | |
void firstLightChanged(uint8_t brightness); | |
void secondLightChanged(uint8_t brightness); | |
// Change this!! | |
const char* ssid = "abc"; | |
const char* password = "XXX"; | |
boolean wifiConnected = false; | |
Espalexa espalexa; | |
const uint8_t DECKE_LED = 26; | |
const uint8_t NACHTTISCH_LED = 0; | |
void setup() | |
{ | |
M5.begin(); | |
pinMode(DECKE_LED, OUTPUT); | |
pinMode(NACHTTISCH_LED, OUTPUT); | |
tb_display_init(3); | |
//M5.Lcd.fillScreen(BLACK); | |
//M5.Lcd.setTextColor(WHITE, BLACK); | |
//M5.Lcd.setCursor(0, 0, 2); | |
wifiConnected = connectWifi(); | |
if (wifiConnected) { | |
//M5.Lcd.setTextColor(GREEN, BLACK); | |
//M5.Lcd.println("WiFi verbunden"); | |
tb_display_print_String("WiFi verbunden\n"); | |
espalexa.addDevice("Kasten Decke", firstLightChanged); //simplest definition, default state off | |
espalexa.addDevice("Kasten Nachttisch", secondLightChanged); //simplest definition, default state off | |
espalexa.begin(); | |
} | |
else { | |
while (1) { | |
tb_display_print_String("WiFi-Fehler\n"); | |
//M5.Lcd.setTextColor(RED, BLACK); | |
//M5.Lcd.println("WiFi-Fehler"); | |
delay(2500); | |
} | |
} | |
} | |
void loop() | |
{ | |
espalexa.loop(); | |
delay(1); | |
} | |
void firstLightChanged(uint8_t brightness) { | |
//M5.Lcd.fillScreen(GREEN); | |
//M5.Lcd.setCursor(0, 0, 2); | |
if (brightness) { | |
//M5.Lcd.printf("Decke AN %d", brightness); | |
tb_display_print_String("Decke AN\n"); | |
digitalWrite(DECKE_LED, HIGH); | |
} | |
else { | |
//M5.Lcd.printf("Decke AUS"); | |
tb_display_print_String("Decke AUS\n"); | |
digitalWrite(DECKE_LED, LOW); | |
} | |
} | |
void secondLightChanged(uint8_t brightness) { | |
//M5.Lcd.fillScreen(BLUE); | |
//M5.Lcd.setCursor(0, 0, 2); | |
if (brightness) { | |
//M5.Lcd.printf("Nachttisch AN %d", brightness); | |
tb_display_print_String("Nachtisch AN\n"); | |
digitalWrite(NACHTTISCH_LED, HIGH); | |
} | |
else { | |
//M5.Lcd.printf("Nachttisch AUS"); | |
tb_display_print_String("Nachtisch AUS\n"); | |
digitalWrite(NACHTTISCH_LED, LOW); | |
} | |
} | |
// connect to wifi – returns true if successful or false if not | |
boolean connectWifi() { | |
boolean state = true; | |
int i = 0; | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
//M5.Lcd.fillScreen(BLACK); | |
//M5.Lcd.setTextColor(WHITE, BLACK); | |
//M5.Lcd.setCursor(0, 0, 2); | |
//M5.Lcd.printf("Verbinde mit WiFi..."); | |
tb_display_print_String("Verbinde mit WiFi...\n"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
//M5.Lcd.println("."); | |
tb_display_print_String("."); | |
if (i > 20) { | |
state = false; break; | |
} | |
i++; | |
} | |
if (state) { | |
//M5.Lcd.printf("\nVerbunden mit %s\n%s", ssid, WiFi.localIP()); | |
tb_display_print_String("Verbunden mit "); | |
tb_display_print_String(ssid); | |
tb_display_print_String("\n"); | |
tb_display_print_String(WiFi.localIP().toString().c_str()); | |
} | |
else { | |
//M5.Lcd.printf("\nVerbindungsfehler"); | |
tb_display_print_String("Verbindungsfehler\n"); | |
} | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment