Skip to content

Instantly share code, notes, and snippets.

@Avotrix
Created June 29, 2020 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Avotrix/d1e52c3476e547913f3d3fe166fbf55e to your computer and use it in GitHub Desktop.
Save Avotrix/d1e52c3476e547913f3d3fe166fbf55e to your computer and use it in GitHub Desktop.
Firebase controlled LED using NodeMCU
#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUM_LEDS 1
const char* ssid = "XXXXXX";
const char* password = "XXXXXXX";
FirebaseData firebaseData;
Adafruit_NeoPixel leds(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Current color values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
Serial.begin(9600);
connectWifi();
leds.begin();
Firebase.begin("XXXXXXXXXXXXXXXXXXX "XXXXXXXXXXXXXXXXXXXXXXXXXX");
}
void loop() {
if (Firebase.getInt(firebaseData, "/red")) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != redValue) {
redValue = val;
setLedColor();
}
}
}
if (Firebase.getInt(firebaseData, "/green")) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != greenValue) {
greenValue = val;
setLedColor();
}
}
}
if (Firebase.getInt(firebaseData, "/blue")) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != blueValue) {
blueValue = val;
setLedColor();
}
}
}
}
void connectWifi() {
// Let us connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".......");
Serial.println("WiFi Connected....IP Address:");
Serial.println(WiFi.localIP());
}
void setLedColor() {
for (int i=0; i < NUM_LEDS; i++)
leds.setPixelColor(i, leds.Color(redValue, greenValue, blueValue));
leds.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment