Giter Site home page Giter Site logo

Comments (2)

pvint avatar pvint commented on August 14, 2024

Original comment by Xose Pérez (Bitbucket: [Xose Pérez](https://bitbucket.org/Xose Pérez), ).


Move the wifiSetup() line below the pinMode lines. The pin is not initialized when you try to turn the LED on.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by mmoraes80 (Bitbucket: mmoraes80, GitHub: mmoraes80).


Fixed it... My code already had wifiSetup() line below pinMode lines but for some reason, AMICA ESP8266 NodeMCU board has inverted logic for the built in LED.

Now it blinks while connecting and stays on when connected (in case you are not able to use the serial monitor);

#!arduino

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.h"

#define SERIAL_BAUDRATE 115200

boolean waitState = true;

// Assign Arduino Friendly Names to GPIO pins

#define D0 16
#define D1 5  // I2C Bus SCL (clock)
#define D2 4  // I2C Bus SDA (data)
#define D3 0
#define D4 2  // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3  // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)

fauxmoESP fauxmo;

void wifiSetup() {

  // Set WIFI module to STA mode

  WiFi.mode(WIFI_STA);

  // Connecting

  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait

  while (WiFi.status() != WL_CONNECTED) {
    waitState = !waitState;
    digitalWrite(D4, waitState);
    delay(100);
    Serial.print(".");
  }

  digitalWrite(D4, LOW); // LED has inverted logic, so to turn it on, pin needs to be on LOW.

  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();

  // Pin Mode Setting

  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);

  // Wifi Initialization

  wifiSetup();

  // 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("switch one");       // Device_ID = 0
  //fauxmo.addDevice("switch two");     // Device_ID = 1
  //fauxmo.addDevice("switch three");   // Device_ID = 2

  // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY is 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 (device_id == 0) digitalWrite(D5, !state);
    //if (device_id == 1) digitalWrite(D6, !state);

  });

  // Callback to retrieve current state (for GetBinaryState queries)

  fauxmo.onGetState([](unsigned char device_id, const char * device_name) {

    if (device_id == 0) return digitalRead(D5);
    //if (device_id == 1) return digitalRead(D6);

  });

}

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());

  }

}

from fauxmoesp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.