1

I am using nodeMCU esp8266 wifi module to send the data received by the ultrasonic distance sensor to the firebase. here is my code:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

//Firebase settings
#define FIREBASE_HOST "dis3333333333333333aseio.com"
#define FIREBASE_AUTH "Lb21333333333333333333v6HzPatg1HA"

//Wi-Fi settings
#define WIFI_SSID "hello"
#define WIFI_PASSWORD "123456789"

//Define trigger and echo digital pins
const int trigPin = 5;
const int echoPin = 4;

// The amount of time the ultrassonic wave will be travelling for
long duration = 0;
// Define the distance variable
double distance = 0;
double cm=0;
double statusTemp=0;
int temp=0;
unsigned long time_now=0 ;
unsigned long time_prev=0 ;
String stringOne="consump/day";
String stringTwo;


void setup()
{

    Serial.begin(9600);
    // Connect to Wi-Fi
    Serial.print("Wi-Fi...");
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print("Connecting...");
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(500);
    }
    Serial.println();
    Serial.print("Connected to: ");
    Serial.println(WiFi.localIP());

    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

    // Ultrasonic sensor, set echo as Input and trigger as Output




}

void loop()
{

    getDistance();
    time_now = millis();

    Serial.print("Distance: ");
    Serial.println(distance);

    delay(500);
}

void getDistance()
{
    pinMode(trigPin, OUTPUT);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    Serial.print("Time: ");
    Serial.println(time_now);
    if(statusTemp==0){
      time_prev=time_now;
      statusTemp=1;
      Serial.println("Hello");
    }
    if(statusTemp==1)
    {
      if((time_now - time_prev)> 10000){
        statusTemp=0;
        stringTwo=stringOne+temp;
        Serial.print(stringTwo);
        Firebase.setFloat(stringTwo, distance);
        temp++;
        Serial.print(temp);    
      }
        duration = pulseIn(echoPin, HIGH);

        distance = duration * 0.034 / 2;
        Firebase.setFloat("distance", distance);
        if (Firebase.failed()) { 
        Serial.print("setting /number failed:"); 
        Serial.println(Firebase.error());   
        return;
    }
 }
}
long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

It works totally fine but after some time the below thing appears enter image description here Is the following thing happening due to my code or something else is causing this.

2
  • Looks like some memory issue (from the descriptions of similar errors found through google). You are using String, which is not good. Read Majenko's blog entry about the Evils of Arduino Strings. Try to use char arrays instead (also called C-Strings). If that works, I will write a full answer. Commented Mar 29, 2020 at 22:17
  • have you tried the exception decoder? it should tell you the line of code that crashes. Commented Mar 31, 2020 at 18:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.