Post topics, source code that relate to the Arduino Platform

User avatar
By Mdcory
#82166 I am pretty new to ESP8266 and Arduino.

I am trying to use a reed switch that when reads high sends data to Firebase. I am able to connect to Firebase and able to get the code to set data in the Setup part of the code but it fails when it tries after the switch is high.

I have been working on this for over a week on and off and I have finally given up and seeking help. If anyone can help it would be greatly appreciated. Like I said, I am able to connect and set data in the Setup part so I know the wifi and Firebase info is correct. It's probably ugly, still learning and have been messing forever.

Code: Select all#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define WIFI_SSID "XX"
#define WIFI_PASSWORD "XX"
#define FIREBASE_HOST "XX"
#define FIREBASE_AUTH "XX" // secret (40 chars)

WiFiClient espClient;

// pin is 0.
const int   multiButton = 0;

void setup() {
  // Configure the pin mode as an input.
  pinMode(multiButton, INPUT);
 
  // Attach an interrupt to the pin, assign the onChange function as a handler and trigger on changes (LOW or HIGH).
  attachInterrupt(multiButton, onChange, CHANGE);
 
  Serial.begin(9600);

// We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
// End WIFI
 
// Firebase Connect
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
// Firebase.setInt("gauge",8);   <--------- If I put this here it sets it in Firebase
// End Firebase Connect

}

// Holds the current button state.
volatile int state;

// Holds the last time debounce was evaluated (in millis).
volatile long lastDebounceTime = 0;

// The delay threshold for debounce checking.
const int debounceDelay = 1000;

// Gets called by the interrupt.
void onChange() {
  // Get the pin reading.
  int reading = digitalRead(multiButton);

// Ignore dupe readings.
  if(reading == state) return;

  boolean debounce = false;
 
// Check to see if the change is within a debounce delay threshold.
  if((millis() - lastDebounceTime) <= debounceDelay) {
    debounce = true;
  }

// This update to the last debounce check is necessary regardless of debounce state.
  lastDebounceTime = millis();

// Ignore reads within a debounce delay threshold.
  if(debounce) return; 

// All is good, persist the reading as the state.
  state = reading;

// Work with the value now.

  Serial.println("8);
    Firebase.setInt("gauge",8);
      if (Firebase.failed()) {
      Serial.print("Firebase failed:");
      Serial.println(Firebase.error()); 
      return;
  }

delay(1000);

}

// Main part of your loop code.
void loop() {

}