I have an Arduino Mega connected in ESP-01 Wifi Module, I've tested the wifi module multiple times with a sample code (sending data to firebase) and its working fine but once I combine it with a pre-made code, the ESP-01 keeps restarting (connecting to wifi then suddenly disconnects and connect again)
I also have this in my serial:
ets Jan 8 2013,rst cause:4, boot mode:(3,7) wdt reset load 0x4010f000, len 1384, room 16 tail 8 chksum 0x2d csum 0x2d vac02aff5 ~ld
this is my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include<Servo.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#define FIREBASE_HOST "smarttrash-d96fa-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "dOVh2HQBz4FGqsyFYzlUZwLHTlQxR3Ag1wNMMqdA"
#define WIFI_SSID "Pastorpili_2.4g"
#define WIFI_PASSWORD "Pastorpili_Wifi_2.4G"
FirebaseData firebaseData;
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo servo1;
Servo servo2;
const int servo1pin = 13;
const int servo2pin = 12;
const int relayPin = 11;
const int infrared = 10;
const int inductive = 9;
const int highPin = 8;
const int moderatePin = 7;
const int lowPin = 6;
bool flag = true;
int detectWait = 5;
const int dispenseTime = 5000; //in millisecs
int metalCounter = 0;
int plasticCounter = 0;
int metalLimit = 2;
int plasticLimit = 2;
bool metal = true;
bool plastic = true;
void setup() {
Serial.begin(9600);
ConnectToWifi();
lcd.begin();
lcd.backlight();
lcd.clear();
pinMode(infrared, INPUT);
pinMode(inductive, INPUT);
pinMode(highPin, INPUT);
pinMode(moderatePin, INPUT);
pinMode(lowPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
servo1.attach(servo1pin);
servo2.attach(servo2pin);
servo1.write(120);
servo2.write(70);
delay(50);
}
void loop() {
int highVal = digitalRead(highPin);
int moderateVal = digitalRead(moderatePin);
int lowVal = digitalRead(lowPin);
if (digitalRead(infrared) == 0) {
if (flag) {
if (waitFor(inductive)) {
OpenMetalLid();
}
else {
OpenPlasticLid();
}
flag = false;
}
}
else {
flag = true;
lcd.setCursor(0, 0);
lcd.print("Plastic: ");
lcd.print(plasticCounter);
lcd.print("|");
lcd.print(plasticLimit);
lcd.setCursor(0, 1);
lcd.print("Metal: ");
lcd.print(metalCounter);
lcd.print("|");
lcd.print(metalLimit);
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("N/A");
lcd.print(" ");
}
if (highVal == 0) {
lcd.setCursor(0, 3);
lcd.print("Alcohol: ");
lcd.print("FULL");
lcd.print(" ");
}
else if (moderateVal == 0) {
lcd.setCursor(0, 3);
lcd.print("Alcohol: ");
lcd.print("MODERATE");
}
else if (lowVal == 0) {
lcd.setCursor(0, 3);
lcd.print("Alcohol: ");
lcd.print("LOW");
lcd.print(" ");
}
else {
lcd.setCursor(0, 3);
lcd.print("Alcohol: ");
lcd.print("EMPTY");
lcd.print(" ");
metal = false;
plastic = false;
}
if ( metalCounter >= metalLimit) {
metal = false;
}
if ( plasticCounter >= plasticLimit ) {
plastic = false;
}
yield();
}
bool waitFor(int pin) {
for (int i = 0; i < detectWait; i++) {
if (digitalRead(pin) == 1 ) {
return true;
}
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("PROCESSING");
delay(500);
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print(" ");
delay(500);
}
return false;
}
void OpenPlasticLid() {
if (plastic) {
Serial.println("Plastic");
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("PLASTIC");
lcd.print(" ");
servo1.write(0); //open
servo2.write(20);
delay(2000);
servo1.write(120); //close
servo2.write(70);
delay(50);
plasticCounter++;
delay(1000);
Dispense();
}
else {
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("PLASTIC");
lcd.print(" ");
delay(2000);
}
}
void OpenMetalLid() {
if (metal) {
Serial.println("Metal");
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("METAL");
lcd.print(" ");
servo2.write(180); //open
servo1.write(170);
delay(2000);
servo2.write(70); //close
servo1.write(120);
delay(50);
metalCounter++;
delay(1000);
Dispense();
}
else {
lcd.setCursor(0, 2);
lcd.print("Scan: ");
lcd.print("METAL");
lcd.print(" ");
delay(2000);
}
}
void Dispense() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing...");
digitalWrite(relayPin, LOW);
delay(dispenseTime);
digitalWrite(relayPin, HIGH);
delay(50);
lcd.clear();
}
void ConnectToWifi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
Firebase.reconnectWiFi(true);
delay(1000);
if (Firebase.setInt(firebaseData, "/state", 0)) { // On successful Write operation, function returns 1
Serial.println("Connected to Firebase");
delay(1000);
}
else {
Serial.println(firebaseData.errorReason());
}
}