Connection lost or connection refused in Firebase stream after some time Arduino Nano IoT
fre887 opened this issue · 9 comments
Dear Mobizt,
Can you help me with the following. Since a few days I'm having problems with the Firebase connection to my Arduino nano 33 IoT. The Arduino establishes the code, but won't keep it running. After some time, it just says 'connection refused', or 'connection lost'. After that it says 'stream timeout'.
Maybe it's my code?
`
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include "paj7620.h"
#include "Firebase_Arduino_WiFiNINA.h"
FirebaseData HeartbeatData;
#define PIN_RING 5
#define NUMPIXELS_RING 9 //Coded as GRB
String heartbeatLevel = ""; //Stresslevel indicator
int previousGestureTime = 0;
Adafruit_NeoPixel pixels_ring = Adafruit_NeoPixel(NUMPIXELS_RING, PIN_RING, NEO_RGB + NEO_KHZ800);
// constants for min and max PWM
const int minPWM = 0;
const int maxPWM = 65;
// Global Fade Value
// but be bigger than byte and signed, for rollover
float fadeValue = 0;
int InOrOut = 1;
// How smooth to fade?
float fadeIncrement = .075;
float fadeIncrementFire = .2;
// millis() timing Variables
int previousFadeMillis;
unsigned long previousDatabaseCheck;
unsigned long currentMillis = 0;
unsigned long previousMillis;
// How fast to increment?
const unsigned long fadeInterval = 3000;
const unsigned long fadeIntervalFire = 1000;
const unsigned long holdBreathInterval = 1; // Must be longer than time it takes to light the LEDS!
int excerciseID = 0;
boolean excerciseChosen = false;
boolean gestureOK = false;
boolean once = false;
boolean notBreathing = true;
void setup() {
pixels_ring.begin(); // This initializes the NeoPixel library.
pixels_ring.setPixelColor(NUMPIXELS_RING, pixels_ring.Color(0, 0, 0)); //Set all pixels to OFF
pixels_ring.show(); // This sends the updated pixel color to the hardware.
Serial.begin(9600);
uint8_t error = 0;
error = paj7620Init(); // initialize Paj7620 registers
if (error) {
Serial.print("INIT ERROR,CODE:");
Serial.println(error);
} else {
Serial.println("INIT OK");
}
Serial.println("Please input your gestures:\n");
Serial.print("Connecting to Wi-Fi");
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
status = WiFi.begin(secret, secret);
Serial.print(".");
for (int i = 0; i < NUMPIXELS_RING; i = i + 1) {
pixels_ring.setPixelColor(i, pixels_ring.Color(0, 0, 0)); //White neutral color LEDS
pixels_ring.show();
}
delay(50);
}
Serial.println("Connected with IP: ");
Serial.println(WiFi.localIP());
for (int i = 0; i < NUMPIXELS_RING; i = i + 1) {
pixels_ring.setPixelColor(i, pixels_ring.Color(50, 0, 0)); //White neutral color LEDS
pixels_ring.show();
}
delay(2000);
for (int i = 0; i < NUMPIXELS_RING; i = i + 1) {
pixels_ring.setPixelColor(i, pixels_ring.Color(0, 0, 0)); //White neutral color LEDS
pixels_ring.show();
}
Firebase.begin("secret", "secret", "secret", "secret");
Firebase.reconnectWiFi(true);
if (!Firebase.beginStream(HeartbeatData, "/actions/excercise")) {
Serial.println("------Can't begin stream connection------");
Serial.println("REASON: " + HeartbeatData.errorReason());
Serial.println();
}
if (Firebase.setBool(HeartbeatData, "/actions/response", false)) {} //Variabelen uitzetten
if (Firebase.setBool(HeartbeatData, "/actions/stopped", false)) {} //Variabelen uitzetten
if (Firebase.setBool(HeartbeatData, "/actions/response-breath", false)) {} //Variabelen uitzetten
if (Firebase.setBool(HeartbeatData, "/actions/response-negative", false)) {} //Variabelen uitzetten
}
void loop() {
normalBehaviour();
}
void normalBehaviour() {
int val = 0;
if (!Firebase.readStream(HeartbeatData)) {
Serial.println("Can't read stream data");
Serial.println("REASON: " + HeartbeatData.errorReason());
Serial.println();
}
if (HeartbeatData.streamTimeout()) {
Serial.println("Stream timeout, resume streaming...");
Serial.println();
}
if (HeartbeatData.streamAvailable()) {
Serial.println("-------Stream Data available-------");
Serial.println("STREAM PATH: " + HeartbeatData.streamPath());
Serial.println("EVENT PATH: " + HeartbeatData.dataPath());
Serial.println("DATA TYPE: " + HeartbeatData.dataType());
Serial.println("EVENT TYPE: " + HeartbeatData.eventType());
Serial.print("VALUE: ");
if (HeartbeatData.dataType() == "int") {
Serial.println(HeartbeatData.intData());
}
Serial.println();
val = HeartbeatData.intData();
}
if (val == 101) {
heartbeatLevel = "high";
}
if (val == 100) {
heartbeatLevel = "normal";
}
if (val == 99) {
heartbeatLevel = "low";
}
if (val == 404) {
excerciseChosen = false;
gestureOK = false;
}
if (val == 303) {
gestureOK = false;
}
if (val == 304) {
gestureOK = true;
}
else if (val > 0 && val < 60) {
excerciseID = val;
excerciseChosen = true;
gestureOK == false;
}
else if (excerciseChosen == true) {
uint8_t data = 0, data1 = 0, error;
error = paj7620ReadReg(0x43, 1, &data);
if (excerciseID == 51 || excerciseID == 52) {
if (gestureOK == false) {
switch (data) {
case GES_UP_FLAG:
//some code
break;
case GES_DOWN_FLAG:
//some code
break;
default:
//some code
break;
}
}
else if (gestureOK == true) {
switch (data) {
case GES_FORWARD_FLAG:
//some code
break;
}
}
}
if (excerciseID == 1 || excerciseID == 2 || excerciseID == 6 || excerciseID == 7 || excerciseID == 21) {
if (gestureOK == false) {
switch (data) {
case GES_UP_FLAG:
//some code
break;
case GES_DOWN_FLAG:
//some code
break;
default:
//some code
break;
}
} else if (gestureOK == true) {
switch (data) {
case GES_FORWARD_FLAG:
//some code
break;
}
}
}
}
}
`
Maybe it's the combination with the PAJ7620 Gesture sensor of the Neopixel library I'm using in my compelte code? Hope you can help me!
Greetz,
Fre
Please see this #18.
You need to add root certificate of your Firebase database host too.
Hi mobizt,
Thank for your response, but this aint the problem. Firebase does connect and gives me the correct values, but after some time it just shuts down the connection or something.
Guessing it has something to do with the gesture sensor code.
`// Includes enum definition of GES_* return values from readGesture()
#include "RevEng_PAJ7620.h"
#include "Firebase_Arduino_WiFiNINA.h"
FirebaseData firebaseData;
// Create gesture sensor driver object
RevEng_PAJ7620 sensor = RevEng_PAJ7620();
void setup() {
Serial.begin(115200);
Serial.print("Connecting to Wi-Fi");
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
status = WiFi.begin("xxx", "xxx");
Serial.print(".");
delay(50);
}
Serial.println("Connected with IP: ");
Serial.println(WiFi.localIP());
Firebase.begin("xxx", "xxx", "xxx", "xxx");
Firebase.reconnectWiFi(true);
if (!Firebase.beginStream(firebaseData, "/actions/excercise")) {
//Could not begin stream connection, then print out the error detail
Serial.println(firebaseData.errorReason());
}
Serial.println("PAJ7620 sensor demo: Recognizing all 9 gestures.");
if ( !sensor.begin() ) {
Serial.print("PAJ7620 I2C error - halting");
while (true) { }
}
Serial.println("PAJ7620 init: OK");
Serial.println("Please input your gestures:");
}
void loop() {
Gesture gesture; // Gesture is an enum type from RevEng_PAJ7620.h
gesture = sensor.readGesture(); // Read back current gesture (if any) of type Gesture
switch (gesture) {
case GES_FORWARD:
Serial.print("GES_FORWARD");
break;
case GES_BACKWARD:
Serial.print("GES_BACKWARD");
break;
case GES_LEFT:
Serial.print("GES_LEFT");
break;
case GES_RIGHT:
Serial.print("GES_RIGHT");
break;
case GES_UP:
Serial.print("GES_UP");
break;
case GES_DOWN:
Serial.print("GES_DOWN");
break;
case GES_CLOCKWISE:
Serial.print("GES_CLOCKWISE");
break;
case GES_ANTICLOCKWISE:
Serial.print("GES_ANTICLOCKWISE");
break;
case GES_WAVE:
Serial.print("GES_WAVE");
break;
}
if ( gesture != GES_NONE ){
Serial.print(", Code: ");
Serial.println(gesture);
}
if (!Firebase.readStream(firebaseData)) {
//If read stream was failed, print the error detail.
Serial.println(firebaseData.errorReason());
}
if (firebaseData.streamTimeout()) {
//If stream timeout, just notify
Serial.println("Stream timeout, resume streaming...");
Serial.println();
}
if (firebaseData.streamAvailable()) {
Serial.println("-------Stream Data available-------");
Serial.println("STREAM PATH: " + firebaseData.streamPath());
Serial.println("EVENT PATH: " + firebaseData.dataPath());
Serial.println("DATA TYPE: " + firebaseData.dataType());
Serial.println("EVENT TYPE: " + firebaseData.eventType());
Serial.print("VALUE: ");
if (firebaseData.dataType() == "int") {
Serial.println(firebaseData.intData());
}
Serial.println();
}
}`
This is my console log
10:44:06.434 -> .Connected with IP:
10:44:06.471 -> 192.168.2.157
10:44:08.711 -> PAJ7620 sensor demo: Recognizing all 9 gestures.
10:44:08.748 -> PAJ7620 init: OK
10:44:08.748 -> Please input your gestures:
10:44:09.031 -> -------Stream Data available-------
10:44:09.031 -> STREAM PATH: /actions/excercise
10:44:09.031 -> EVENT PATH: /
10:44:09.031 -> DATA TYPE: int
10:44:09.031 -> EVENT TYPE: put
10:44:09.031 -> VALUE: 2
10:44:09.031 ->
10:44:09.963 -> connection lost
And without the Gesture sensor code it works perfectly fine. Any ideas?
If the WiFI connection lost, check your board power supply as it may not provide enough current or voltage is unstable due to long or bad cable.
It ain't the cable either, tried like 4 different ones. Going to try if it is the sensor, i'll place a new one and test it!
It doesn't matter, check your board voltage for drop out with oscilloscope or use super capacitor or large capacitor connect between +V and GND on your board.
So what would resolve the problem? Buying a new nano of placing that capacitor? What can I do in the future to prevent frying my Arduino?
I don't know you should check what I mentioned or separate the sensor supply.
Ublox nina w102 module contains ESP32 chip inside which the power is not enough when the data transmission occured.