No serial output

good day! can you help me what is wrong with my code because when i try to print out the value of Datastring via serial there is no output

#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <MQUnifiedsensor.h>
#include <virtuabotixRTC.h>
#include <dht.h>
#include <SPI.h>
#include <SD.h>

//system variables
unsigned long previousMillis = 0;
String DataString;
String _date, _time;
bool armed = false;
byte rowPins[4] = {38, 39, 40, 41}; //connect to the row pinouts of the keypad
byte colPins[4] = {42, 43, 44, 45}; //connect to the column pinouts of the keypad
char PIN[4];
char initialPIN[4];
char newPIN[4];
char inputKey = 0;
int keyCounter = 0;
int keyCheckCounter = 0;
char keys[4][4] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

//Sensor Variables
int FireSensorData;
int SW420Data;
double temp;
double humi;

//class instances
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
MQUnifiedsensor CarbonMonoxide("Arduino Mega", 5, 10, A13,"MQ-6");
MQUnifiedsensor LiquifiedPetroleumGas("Arduino Mega", 5, 10, A14,"MQ-6");
virtuabotixRTC RealTimeClock(6,7,8); //SCLK, DAT, RST Pins
dht DHT22Sensor;
File MicroSDModule;

void HaltAlarm();
void TriggerAlarm();
//String getRTC();
//String getPPM_CO;
//String getPPM_LPG;
//String getFireSensor();


void setup() { // put your setup code here, to run once
 Serial.begin(115200);
 Serial3.begin(115200);
 Serial2.begin(115200);
 RealTimeClock.setDS1302Time(00, 30, 02, 24, 12, 18, 2023);
 setWiring();
 HaltAlarm();
 initialize();
 initCO();
}
void loop() {
  inputKey = keypad.getKey(); // get a key (if pressed)
  if (inputKey == '#') {
    resetPIN();
  }
  if (keyCounter >= 4) {
    delay(200);
    for (int counter = 0; counter < 4; counter++) {
      initialPIN[counter] = EEPROM.read(counter);
    }
    if (!(strncmp(PIN, initialPIN, 4))) {
      keyCheckCounter = 0;
      lcd.clear();
      lcd.print("System disarming...");
      delay(2000);
      if(armed == true)
        armed = false;
      TriggerAlarm();
      lcd.setCursor(0, 1);
      lcd.print("System disarmed.");
      delay(10000);
      lcd.clear();
      lcd.print("Arming system.... ");
      delay(10000);
      armed = true;
      lcd.clear();
      lcd.print("System is armed.");
      lcd.setCursor(0, 1);
      lcd.print("Sensor Activated.");
      delay(2000);
      lcd.clear();
      lcd.print("Enter PIN:");
      keyCounter = 0;
    } else {
      keyCheckCounter = keyCheckCounter + 1;
      lcd.clear();
      lcd.print("Wrong PIN input.");
      lcd.setCursor(0, 1);
      lcd.print("Please Try Again.");
      if (keyCheckCounter >= 3) {
        keyCheckCounter = 0;
        lcd.clear();
        lcd.print("Retry in ");
        for (int i = 10; i > 0; i--) {
          lcd.setCursor(9, 0);
          lcd.print((i / 10) % 10);
          lcd.print(i % 10);
          delay(1000);
        }
      } else {
        delay(1000);
      }
      lcd.clear();
      lcd.print("Enter PIN: ");
      keyCounter = 0;
    }
  }
  if (inputKey == '*') {
    if (keyCounter > 0)
      PIN[keyCounter--];
    lcd.setCursor(keyCounter+11, 0);
    if (keyCounter > 0)
      lcd.print("*   ");
    else 
      lcd.print("    ");
    delay(100);
  } else {
    if (inputKey) {
      PIN[keyCounter++] = inputKey;
      lcd.setCursor(keyCounter+11, 0);
      lcd.print("*  ");
      delay(100);
    }
  }
 if(inputKey == 'A'){
    lcd.clear();
    lcd.print("Calling Ambulance..");
    //call("ATD+ +639704911690");
    //NotificationLog("Called One Hospital Command");
    lcd.clear();
    lcd.print("Calling ended..");
    delay(2000);
    lcd.clear();
    lcd.print("Enter PIN: ");
  }
  if(inputKey == 'B'){
    lcd.clear();
    lcd.print("Calling Police Station 1..");
    //call("ATD+ +639184644371");
    //NotificationLog("Called PNP");
    lcd.clear();
    lcd.print("Calling ended..");
    delay(2000);
    lcd.clear();
    lcd.print("Enter PIN: ");
    
  }
  if(inputKey == 'C'){
    lcd.clear();
    lcd.print("Calling Fire Station..");
    //call("ATD+ +639230839429");    
    //NotificationLog("Called BFP");
    lcd.clear();
    lcd.print("Calling ended..");
    delay(2000);
    lcd.clear();
    lcd.print("Enter PIN: ");
  }
  if(inputKey == 'D'){
    lcd.clear();
    lcd.print("Calling Papa..");
    //call("ATD+ +639981986397");  
    //NotificationLog("Called Contact Person");
    lcd.clear();
    lcd.print("Calling ended..");
    delay(2000);
    lcd.clear();
    lcd.print("Enter PIN: ");  
  }

  unsigned long _millis = millis();
  if(_millis - previousMillis >= 1000){
    DataString = getRTC();
    DataString = DataString + getDHT22Sensor();
    DataString = DataString + getPPM_CO();
    DataString = DataString + getPPM_LPG();
    DataString = DataString + getReedSwitch();
    DataString = DataString + getPIRSensor();
    DataString = DataString + getFireSensor();
    DataString = DataString + getSW420Sensor();
    DataString = DataString + getWaterLevelSensor();
    Serial.println(DataString);
    SensorLog(DataString);
    previousMillis = _millis;
    Serial2.println(DataString);  
  }
 
   
}

void resetPIN() {
  int counter = 0;
  lcd.clear();
  lcd.print("Current PIN: ");
  lcd.setCursor(13,0);
  delay(100);

  while (counter < 4) {
    char x = keypad.getKey();
    if (x) {
      delay(200);
      newPIN[counter++] = x;
      lcd.print(x);
    }
    x = 0;
  }

  delay(500);
  if ((strncmp(newPIN, initialPIN, 4))) {
    lcd.clear();
    lcd.print("Wrong PIN");
    lcd.setCursor(0, 1);
    lcd.print("Try Again");
    delay(1000);
  }
  else {
    counter = 0;
    lcd.clear();
    lcd.print("New PIN: ");
    lcd.setCursor(9, 0);
    while (counter < 4) {
      char x = keypad.getKey();
      if (x) {
        delay(200);
        initialPIN[counter] = x;
        lcd.print(x);
        EEPROM.write(counter, x);
        counter++;
      }
    }
    lcd.clear();
    lcd.print("PIN Changed..");
    delay(1000);
  }

  lcd.clear();
  lcd.print("Enter PIN: ");
  inputKey = 0;
}
void initialize(){
  lcd.begin();// initialize the lcd
  lcd.home();
  lcd.print("HOME SECURITY SYSTEM");
  delay(2000); // Waiting for a while
  lcd.clear();
  lcd.print("  Press # to  ");
  lcd.setCursor(0, 1);
  lcd.print("Change  PIN");
  delay(2000); // Waiting for a while
  lcd.clear();
  lcd.print("Enter PIN: "); //What's written on the lcd you can change
  if (EEPROM.read(100) == 0) {}
  else {
    for (int counter = 0; counter < 4; counter++) {
      EEPROM.write(counter, counter + 49);
    }
    EEPROM.write(100, 0);
  }
  for (int counter = 0; counter < 4; counter++) { //read code from EEPROM
    initialPIN[counter] = EEPROM.read(counter);
  }
  delay(2000); // Waiting for a while
}
void setWiring(){
  //Passive Infrared (PIR) Sensor
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  //SW420 Vibration Sensor
  pinMode(22, INPUT);
  // Fire Sensor
  pinMode(7, INPUT);
  //Reed Switch Pins
  pinMode(30, INPUT_PULLUP); // INPUT_PULLUP = input pin that uses a built-in 10 Kilo-Ohms resistor from microcontroller
  pinMode(31, INPUT_PULLUP);
  pinMode(32, INPUT_PULLUP);
  //MQ-6 Air Quality Sensors
  pinMode(A13, INPUT);
  pinMode(A14, INPUT);
  //Water Level Sensor
  pinMode(A13, INPUT);
  pinMode(36, OUTPUT);
  //DHT22 Temperature and Humidity Sensor
  pinMode(37, INPUT);
  //4x4 Keypad Module
  pinMode(38, INPUT);
  pinMode(39, INPUT);
  pinMode(40, INPUT);
  pinMode(41, INPUT);
  pinMode(42, INPUT);
  pinMode(43, INPUT);
  pinMode(44, INPUT);
  pinMode(45, INPUT);
  //4 Channnel Low Trigger Relay Module
  pinMode(46, OUTPUT);
  pinMode(47, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(49, OUTPUT);
  //MicroSD Module
  pinMode(53, INPUT);

  //Set Relay Module to Off
  digitalWrite(46, HIGH);
  digitalWrite(47, HIGH);
  digitalWrite(48, HIGH);
  digitalWrite(49, HIGH);

}
void initCO(){
  CarbonMonoxide.setRegressionMethod(1);
  CarbonMonoxide.setA(1000000000000000);
  CarbonMonoxide.setB(-13.5);
  CarbonMonoxide.init();
  float calculatedR0 = 0;
  for(int i = 1; i<=10; i ++){
    CarbonMonoxide.update(); // Update data, the arduino will read the voltage from the analog pin
    calculatedR0 += CarbonMonoxide.calibrate(10);
  }
  CarbonMonoxide.setR0(calculatedR0/10);
  if(isinf(calculatedR0)) {
    //sendSMS("Warning(A0): CO Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
    while(1);
  }
  if(calculatedR0 == 0){
    //sendSMS("Warning(A0): CO Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
    while(1);
  }
}
void initLPG(){
  LiquifiedPetroleumGas.setRegressionMethod(1);
  LiquifiedPetroleumGas.setA(1009.2);
  LiquifiedPetroleumGas.setB(-2.35);
  LiquifiedPetroleumGas.init();
  float calculatedR0 = 0;
  for(int i = 1; i<=10; i ++){
    LiquifiedPetroleumGas.update(); // Update data, the arduino will read the voltage from the analog pin
    calculatedR0 += LiquifiedPetroleumGas.calibrate(10);
  }
  LiquifiedPetroleumGas.setR0(calculatedR0/10);
  if(isinf(calculatedR0)) {
    //sendSMS("Warning(A0): LPG Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
    while(1);
  }
  if(calculatedR0 == 0){
    //sendSMS("Warning(A0): LPG Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
    while(1);
  }
}


//sensor functions
String getRTC(){
  String rtc;
  RealTimeClock.updateTime();
  rtc = String(RealTimeClock.month);
  rtc = rtc + '/';
  rtc= rtc + String(RealTimeClock.dayofmonth);
  rtc = rtc + '/';
  rtc = rtc + String(RealTimeClock.year);
  rtc = rtc + ",";
  rtc= rtc + String(RealTimeClock.hours);
  rtc = rtc + ':';
  rtc = rtc + String(RealTimeClock.minutes);
  rtc = rtc + ':';
  rtc = rtc + String(RealTimeClock.seconds);
  return rtc;
}

String getPPM_CO(){
  String COStrData;
  CarbonMonoxide.update();
  float COData = CarbonMonoxide.readSensor();
  COStrData = String(COData);
  if(COData >= 50){
    //sendSMS("Deadly concentration of Carbon Monoxide detected. Please contact your local authorities.");
    TriggerAlarm();
    COStrData = COStrData + ", Triggered,";
  }
  else
    COStrData = COStrData + ", Armed,";
  return COStrData;
}

String getPPM_LPG(){
  String LPGStrData;
  LiquifiedPetroleumGas.update();
  float LPGData = LiquifiedPetroleumGas.readSensor();
  LPGStrData = String(LPGData);
  if(LPGData >= 1000){
    //sendSMS("Dangerous concentration of Liquified Petroleum Gas detected. Please contact your local authorities.");
    TriggerAlarm();
    LPGStrData = LPGStrData + ", Triggered,";
  }
  else
    LPGStrData = LPGStrData + ", Armed,";
    return LPGStrData;
}

String getFireSensor(){
  String FireSensorStrData;
  FireSensorData = digitalRead(33);
  FireSensorStrData = String(FireSensorData);
  if(FireSensorData == LOW){
    //sendSMS("Fire detected at your house. Please contact your local Fire Station.");
    TriggerAlarm();   
    FireSensorStrData = FireSensorStrData + ", Triggered,";
  }
  else
    FireSensorStrData = FireSensorStrData + ", Armed,";
  return FireSensorStrData;
}

String getSW420Sensor(){
  String SW420StrData;
  SW420Data = digitalRead(22);
  SW420StrData = String(SW420Data);
  if(SW420Data == HIGH){
    //sendSMS("Earthquake detected. Locate a safe area and call local DRRM office.");
    TriggerAlarm();  
    SW420StrData = SW420StrData + ", Triggered,";
  }
  else
    SW420StrData = SW420StrData + ", Armed,";
  return SW420StrData;
}

String getDHT22Sensor(){
  String DHTStrData;
  double DHT22SensorData = DHT22Sensor.read(37);
  temp = DHT22Sensor.temperature;
  humi = DHT22Sensor.humidity;
  DHTStrData = DHTStrData + String(temp) + ',' + String(humi) + ',';
  if(temp >= 70.0){
    //sendSMS("Extreme tempearture detected. Locate a safe area and call local DRRM office.");
    TriggerAlarm();  
    DHTStrData = DHTStrData +  ", Triggered,";
  }
  else
    DHTStrData = DHTStrData + ", Armed,";
  return DHTStrData;
}

String getReedSwitch(){
  String ReedStrData;
  int reed1 = digitalRead(30);
  int reed2 = digitalRead(31);
  int reed3 = digitalRead(32);
  ReedStrData = String(reed1);
  if (reed1 == LOW){
    ReedStrData = ReedStrData + ", Main Door, Triggered,";
    TriggerAlarm();
  }
  else
    ReedStrData = ReedStrData + ", Main Door, Armed,";
  ReedStrData = ReedStrData + String(reed2);
  if (reed2 == LOW){
    ReedStrData = ReedStrData + ", Back Door, Triggered,";
    TriggerAlarm();
  }
  else
    ReedStrData = ReedStrData + ", Back Door, Armed,";
  ReedStrData = ReedStrData + String(reed3);
  if (reed3 == LOW){
    ReedStrData = ReedStrData + ", Window, Triggered,";
    TriggerAlarm();
  }
  else
    ReedStrData = ReedStrData + ", Window, Armed,";
  return ReedStrData;
}

String getWaterLevelSensor(){
  String WaterLevelStrData;
  digitalWrite(36, HIGH);
  int WaterLevelData = analogRead(A13);
  digitalWrite(36, LOW);
  WaterLevelStrData = String(WaterLevelData);
  if (WaterLevelData < 0 || WaterLevelData > 600){
    //sendSMS("Water Level Sensor is compromised. Please contact the service provider immediately.");
    WaterLevelStrData = WaterLevelStrData +", Error,";
  }
  else if(WaterLevelData >= 100 && WaterLevelData >= 450){
    //sendSMS("Water detected. Please do precautionary measures immediately.");
    WaterLevelStrData = WaterLevelStrData +  ", Armed,";
  }
  else if(WaterLevelData > 450 && WaterLevelData <= 600){
    //sendSMS("Water Level rising. Call your nearest DRRM Office immediately.");
    TriggerAlarm();
    WaterLevelStrData = WaterLevelStrData + ", Triggered,";
  }
  return WaterLevelStrData;
}

String getPIRSensor() {
  String PIRStrData;
  int PIR1 = digitalRead(4);
  int PIR2 = digitalRead(5);
  PIRStrData =String(PIR1);
  if (PIR1 == HIGH){
    PIRStrData = PIRStrData + ", Area 1, Triggered,";
    TriggerAlarm();
  }
  else
    PIRStrData = PIRStrData + ", Area 1, Armed,";
  PIRStrData = PIRStrData + String(PIR2);
  if (PIR2 == HIGH){
    PIRStrData = PIRStrData + ", Area 2, Triggered,";
    TriggerAlarm();
  }
  else
    PIRStrData = PIRStrData + ", Area 2, Armed,";
}


void SensorLog(String DataString){
  MicroSDModule = SD.open("DataLogFile.txt", FILE_WRITE);
  if(!MicroSDModule){
    Serial.print("Error Opening File. Restart the System");
    //sendSMS("Error Opening the File. Restart the System");
    lcd.clear();
    lcd.print("Error file Opening..");
    lcd.setCursor(0,1);
    lcd.print("Restart the system..");
    while(true){;}
    return;
  }
  else{
    MicroSDModule.println(DataString);
  }
  MicroSDModule.close();
}

//void NotificationLog(String Notif){
//  String datetime = _date + ' ' +_time + ' ';
//  MicroSDModule = SD.open("NotifLog.txt", FILE_WRITE);
//  if(!MicroSDModule){
//    Serial.print("Error Opening File. Restart the System");
//    //sendSMS("Error Opening the File. Restart the System");
//    lcd.clear();
//    lcd.print("Error logfile Opening..");
//    lcd.setCursor(0,1);
//    lcd.print("Restart the system..");
//    while(true){;}
//    return;
//  }
//  else{
//    MicroSDModule.print(datetime);
//    MicroSDModule.println(Notif);
//  }
//  MicroSDModule.close();
//}

void TriggerAlarm(){
  if(armed){
    digitalWrite(46, LOW);
    digitalWrite(47, LOW);
    digitalWrite(48, LOW);
    digitalWrite(49, LOW);  
  }
  else
    HaltAlarm();
}

void HaltAlarm() {
  digitalWrite(46, HIGH);
  digitalWrite(47, HIGH);
  digitalWrite(48, HIGH);
  digitalWrite(49, HIGH);
}


//SIM900 Module
//void call(String phone){
//  String num = phone;
//  Serial3.print(num);
//  if(Serial3.available()){
//    Serial.print((unsigned char)Serial3.read());
//  }
//  delay(2000);
//  Serial3.println("ATH");
//}
//
//void sendSMS(String message) {
//  String text = message + "\r";
//  Serial3.print("\r");
//  delay(500);
//  Serial3.print("AT+CMGF=1\r");
//  delay(500);
//  Serial3.print("AT+CMGS=\"+639981986397");
//  delay(1000);
//  Serial3.print(text);   //"Some Text + \r" format
//  delay(1000);
//  Serial3.write(0x1A);
//}

Please post an annotated schematic of how you have wired this, be sure to include links to technical information on the hardware devices. How did you determine there was no output?

1 Like

no output on serial monitor

Did you set Serial Monitor to 115200?

1 Like

yes and also for HW serial for nodemcu and sim808 communication

Is there a previous thread about this same project that we could read through for background?

Are TX and RX on the opposite pins? (RX to TX and TX to RX)

Isn't Serial Monitor using the USB connection?

I suggest you trim down you sketch for troubleshooting. Get pieces working then add them together.

you have a number of devices connected to the NodeMCU - did you test them in individual programs before attemmpting the code in post 1?
what appears on the LCD?
which NodeMCU are you using?

When sending data to serialx.begin() it does not appear on the monitor.
Serial.begin(115200); To Monitor
Serial3.begin(115200); To serial3 port not monitor
Serial2.begin(115200); To serial2 port not monitor
Hopefully this helps.

@horace il be just using the nodemcu to send the data gathered by the arduino mega to a web server
@camsysca yes usb

i just want to check the value that is stored on the DataString variable

For me, yes, but I only have one hardware serial port. I was assuming three hardware serial devices (Serial, Serial2, Serial3), all connected to something (one internally connected.

@horace what would be the upper limit of a String data type?

when using low power low SRAM microcontrollers such as UNO and Mega avoid String class - you can end up fragmenting the memory and crashing the program - use char arrays
if the devices are all attached to the Mega did you test them with individual programs
remember the Mega uses 5V logic the NodeMCU 3.3V - use a potential divider on the Mega Tx to NodeMCH Rx line

@horace when i add these it starts to neglect the functions needed to be called..

as the functions are called function parameters, etc are pushed onto the stack
as the function results are added to DataString new String objects could well be created on the stack
this can result in the stack becoming fragmented and running out of space and corrupting the memory - the program hanges or crashes resetting the processor
you could

  1. create a char array of sufficient size to hold all your data
    or
  2. print the values as you read tem
  if (_millis - previousMillis >= 1000) {
    DataString = getRTC();
    Serial.println(DataString);
    SensorLog(DataString);
    Serial2.println(DataString);
    DataString = getDHT22Sensor();
    Serial.println(DataString);
    SensorLog(DataString);
    Serial2.println(DataString);
    DataString = getPPM_CO();
    Serial.println(DataString);
    SensorLog(DataString);
    Serial2.println(DataString);

or move the complete project onto the ESP32 NodeMCU

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