Arduino|ESP8266|Sensor HC-SR04 | Firebase

This is the code for my project, the problem is that it hard resets instead of uploading.

I'm new to arduino, I don't have any experience in using it, just now

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
#include <Wire.h>
//#include <Adafruit_Sensor.h>
//#include <Adafruit_BME280.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"

// Insert your network credentials
#define WIFI_SSID "**********"
#define WIFI_PASSWORD "********"

// Insert Firebase project API Key
#define API_KEY "**********"

// Insert Authorized Email and Corresponding Password
#define USER_EMAIL "@gmail.com"
#define USER_PASSWORD "
***!"

// Insert RTDB URLefine the RTDB URL
#define DATABASE_URL "************.asia-southeast1.firebasedatabase.app"

// Define Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// Variable to save USER UID
String uid;

// Database main path (to be updated in setup with the user UID)
String databasePath;

// Database child nodes
String distancePath = "/distance";
String timePath = "/timestamp";

// Parent Node (to be updated in every loop)
String parentPath;

FirebaseJson json;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

// Variable to save current epoch time
int timestamp;

// Timer variables (send new readings every three minutes)
unsigned long sendDataPrevMillis = 0;
unsigned long timerDelay = 120000;

// defines pins numbers
const int trigPin = 5; //D4
const int echoPin = 4; //D3

//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701

// defines variables
long duration;
int distance;

// Function that gets current epoch time
unsigned long getTime() {
timeClient.update();
unsigned long now = timeClient.getEpochTime();
return now;
}

void setup(){
Serial.begin(115200);

// Initialize BME280 sensor
timeClient.begin();

// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());

// Assign the api key (required)
config.api_key = API_KEY;

// Assign the user sign in credentials
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

// Assign the RTDB URL (required)
config.database_url = DATABASE_URL;

Firebase.reconnectWiFi(true);
fbdo.setResponseSize(4096);

// Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

// Assign the maximum retry of token generation
config.max_token_generation_retry = 5;

// Initialize the library with the Firebase authen and config
Firebase.begin(&config, &auth);

// Getting the user UID might take a few seconds
Serial.println("Getting User UID");
while ((auth.token.uid) == "") {
Serial.print('.');
delay(1000);
}
// Print user UID
uid = auth.token.uid.c_str();
Serial.print("User UID: ");
Serial.println(uid);

// Update database path
databasePath = "/UsersData/" + uid + "/readings";
}

void loop(){

// Send new readings to database
if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance= duration*0.034/2;

//Get current timestamp
timestamp = getTime();
Serial.print ("time: ");
Serial.println (timestamp);

parentPath= databasePath + "/" + String(timestamp);

json.set(distancePath.c_str(), String(distance));
json.set(timePath, String(timestamp));
Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());

}
}

This is the error it produces

Executable segment sizes:

ICACHE : 32768 - flash instruction cache

IROM : 511456 - code in flash (default or ICACHE_FLASH_ATTR)

IRAM : 28417 / 32768 - code in IRAM (IRAM_ATTR, ISRs...)

DATA : 1632 ) - initialized variables (global, static) in RAM/HEAP

RODATA : 2516 ) / 81920 - constants (global, static) in RAM/HEAP

BSS : 28504 ) - zeroed variables (global, static) in RAM/HEAP

Sketch uses 544021 bytes (52%) of program storage space. Maximum is 1044464 bytes.
Global variables use 32652 bytes (39%) of dynamic memory, leaving 49268 bytes for local variables. Maximum is 81920 bytes.
esptool.py v3.0
Serial port COM4
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 58:bf:25:d9:97:80
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 548176 bytes to 396762...
Writing at 0x00000000... (4 %)
Writing at 0x00004000... (8 %)
Writing at 0x00008000... (12 %)
Writing at 0x0000c000... (16 %)
Writing at 0x00010000... (20 %)
Writing at 0x00014000... (24 %)
Writing at 0x00018000... (28 %)
Writing at 0x0001c000... (32 %)
Writing at 0x00020000... (36 %)
Writing at 0x00024000... (40 %)
Writing at 0x00028000... (44 %)
Writing at 0x0002c000... (48 %)
Writing at 0x00030000... (52 %)
Writing at 0x00034000... (56 %)
Writing at 0x00038000... (60 %)
Writing at 0x0003c000... (64 %)
Writing at 0x00040000... (68 %)
Writing at 0x00044000... (72 %)
Writing at 0x00048000... (76 %)
Writing at 0x0004c000... (80 %)
Writing at 0x00050000... (84 %)
Writing at 0x00054000... (88 %)
Writing at 0x00058000... (92 %)
Writing at 0x0005c000... (96 %)
Writing at 0x00060000... (100 %)
Wrote 548176 bytes (396762 compressed) at 0x00000000 in 35.2 seconds (effective 124.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

How did you create such a large and relatively complex code with no Arduino experience?

I look for a project that is almost the same as mine and edit the data from there.
I can read a bit of the code from there

I based the code here:

The "hard reset" means it is done uploading and is starting your sketch.

Is Serial Monitor set to 115200 baud?

If so, and you never get the "connecting" message, your sketch is hanging in timeClient.begin(); or WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Add a Serial.print() message before each of those to see which one is hanging.

I solved it, Thank you, I just used a different firebase

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.