Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #ifdef ESP32
- #include <WiFi.h>
- #else
- #include <ESP8266WiFi.h>
- #endif
- #include "fauxmoESP.h"
- #include "credentials.sample.h"
- #define SERIAL_BAUDRATE 115200
- #define REDPIN 14
- #define GREENPIN 13
- #define BLUEPIN 12
- int r, g, b;
- fauxmoESP fauxmo;
- // -----------------------------------------------------------------------------
- // Wifi
- // -----------------------------------------------------------------------------
- void wifiSetup() {
- // Set WIFI module to STA mode
- WiFi.mode(WIFI_STA);
- // Connect
- Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- // Wait
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(100);
- }
- Serial.println();
- // Connected!
- Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
- }
- void setup() {
- // Init serial port and clean garbage
- Serial.begin(SERIAL_BAUDRATE);
- Serial.println();
- Serial.println();
- //LED
- pinMode(REDPIN, OUTPUT);
- pinMode(GREENPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- analogWriteFreq(500);
- analogWriteRange(100);
- // Wifi
- wifiSetup();
- // LED
- // You can enable or disable the library at any moment
- // Disabling it will prevent the devices from being discovered and switched
- fauxmo.enable(true);
- // Add virtual devices
- fauxmo.addDevice("rot");
- fauxmo.addDevice("blau");
- fauxmo.addDevice("grün");
- // fauxmoESP 2.0.0 has changed the callback signature to add the device_id,
- // this way it's easier to match devices to action without having to compare strings.
- fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) {
- Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
- if (state) {
- if(device_id == 0){
- digitalWrite(REDPIN, 100);
- }
- if(device_id == 1){
- digitalWrite(GREENPIN, 100);
- }
- if(device_id == 2){
- digitalWrite(BLUEPIN, 100);
- }
- if(device_id == 3){
- digitalWrite(REDPIN, 100);
- digitalWrite(GREENPIN, 100);
- digitalWrite(BLUEPIN, 100);
- }
- } else {
- digitalWrite(REDPIN, 0);
- digitalWrite(GREENPIN, 0);
- digitalWrite(BLUEPIN, 0);
- if(device_id == 3){
- digitalWrite(REDPIN, 0);
- digitalWrite(GREENPIN, 0);
- digitalWrite(BLUEPIN, 0);
- }
- }
- });
- // Callback to retrieve current state (for GetBinaryState queries)
- /* fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
- }); */
- }
- void loop() {
- // Since fauxmoESP 2.0 the library uses the "compatibility" mode by
- // default, this means that it uses WiFiUdp class instead of AsyncUDP.
- // The later requires the Arduino Core for ESP8266 staging version
- // whilst the former works fine with current stable 2.3.0 version.
- // But, since it's not "async" anymore we have to manually poll for UDP
- // packets
- fauxmo.handle();
- static unsigned long last = millis();
- if (millis() - last > 5000) {
- last = millis();
- Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement