#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
// WiFi credentials
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
// Firebase credentials
#define FIREBASE_HOST ""
#define FIREBASE_AUTH ""
// RE and DE Pins set the RS485 module
// to Receiver or Transmitter mode
#define RE 2
#define DE 0
#define greenLedPin D7 // corresponds to S2
#define redLedPin D8 // corresponds to S3
// Modbus RTU requests for reading NPK values
const byte nitro[] = { 0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c };
const byte phos[] = { 0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc };
const byte pota[] = { 0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0 };
// A variable used to store NPK values
byte values[11];
// Firebase objects
FirebaseData firebaseData;
FirebaseJson firebaseJson;
// Sets up a new SoftwareSerial object
SoftwareSerial mod(14,12);
void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
Serial.begin(9600);
mod.begin(4800);
//FOR THE LED
// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
Serial.println("Connecting to WiFi...");
}
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin, LOW);
delay(2000);
digitalWrite(greenLedPin, HIGH);
Serial.println("Connected to WiFi!");
// Connect to Firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
while (!Firebase.beginStream(firebaseData, "/npk")) {
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, HIGH);
delay(2000);
digitalWrite(redLedPin, LOW);
delay(2000);
digitalWrite(redLedPin, HIGH);
Serial.println("Could not connect to Firebase");
delay(1000);
}
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin, LOW);
delay(2000);
digitalWrite(greenLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, LOW);
Serial.println("Connected to Firebase!");
// Initialize the time library
configTime(0, 0, "pool.ntp.org");
// Wait for the time to be synchronized
while (time(nullptr) < 1000000000UL) {
// Serial.println("Waiting for NTP time synchronization...");
delay(1000);
}
}
void loop() {
// Get the date
String date = getDate();
// Read values
byte val1, val2, val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
// Check if data is received
if (val1 == 255 || val2 == 255 || val3 == 255) {
Serial.println("NO DATA");
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
} else {
Serial.println("There's DATA");
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
// Print values to the serial monitor
// Serial.print("Nitrogen: ");
// Serial.print(val1);
// Serial.println(" mg/kg");
// Serial.print("Phosphorous: ");
// Serial.print(val2);
// Serial.println(" mg/kg");
// Serial.print("Potassium: ");
// Serial.print(val3);
// Serial.println(" mg/kg");
// //orginal
double nitrogen = static_cast<double>(val1);
String nitrogenString = String(nitrogen, 2); // convert double to string with 2 decimal places
double phosphorous = static_cast<double>(val2);
String phosphorousString = String(phosphorous, 2);
double potassium = static_cast<double>(val3);
String potassiumString = String(potassium, 2);
// Firebase.setDouble(firebaseData,"/" + date + "/nitrogen", nitrogen);
// Firebase.setDouble(firebaseData,"/" + date + "/phosphorous", phosphorous);
// Firebase.setDouble(firebaseData,"/" + date + "/potassium", potassium);
// Create a unique key for each entry
// Store NPK values using the unique key
firebaseJson.set("nitrogen", nitrogenString);
firebaseJson.set("phosphorus", phosphorousString);
firebaseJson.set("potassium", potassiumString);
if (Firebase.pushJSON(firebaseData, "/" + date , firebaseJson)) {
// Serial.println("Data saved successfully.");
} else {
// Serial.print("Pushing data to Firebase failed: ");
// Serial.println(firebaseData.errorReason());
}
delay(2000);
}
}
String getDate() {
// Get the current time
time_t now = time(nullptr);
// Convert to local time
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET", Last, Sun, Oct, 3, 60};
Timezone tz(CEST, CET);
time_t localTime = tz.toLocal(now);
// Format as YYYY-MM-DD
char dateStr[11];
snprintf(dateStr, sizeof(dateStr), "%04d-%02d-%02d", year(localTime), month(localTime), day(localTime));
return String(dateStr);
}
byte nitrogen() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(nitro, sizeof(nitro)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[4];
}
byte phosphorous() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(phos, sizeof(phos)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[4];
}
byte potassium() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(pota, sizeof(pota)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[4];
}
What is the serial debug output, in both cases, connected and unconnected? Please copy and paste the entire outputs here.
In both cases, there is no serial debug output. However, when the MAX485 is connected before powering on the board, there is no serial output and the board simply turns on. The code starts running only when the MAX485 is connected after the board is turned on..
This runs very early in your code, before very much else has happened. So you should bear down on that, because if that doesn't happen, the things that happen after don't seem to matter as much.
How do you know whether the code is running or not, without any serial output to look at? If it does run, isn't it kind of mysterious that you're not seeing debug prints?
What do you mean, "simply turns on"? The power LED comes on, or ???
Generally, we need a lot more detailed and specific information about the device behaviour.
When the program does run, there's data printed in the serial, yes. And or any message in the serial and it doesn't connect to the network so I'm assuming that the code didn't automatically start.
And yes, the LED just turns on and doesn't do anything.
Please copy and paste the entire output here.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.