Firebase Cloud function not working accordingly with IOT

my firebase cloud functions are as follows oem purifier

exports.sendSMSNotfication = functions.database.ref('issue/{id}').onCreate(async (snapshot, context) => {
    const val = snapshot.val();

    var lampnumb=val.Issue.substring(0,7);
    var desc=val.Issue.substring(10);

    admin.database().ref('lamps/'+lampnumb).on('value', function (snapshot) {
        
        var data = snapshot.val();
           own=data.Owner;
           

    return admin.database().ref('owners/'+own).on('value', function (snapshot) {
        
            var data = snapshot.val();

            client.messages
                .create({
                    body: '\nAlert\nSmartLampPole ' + lampnumb + ' is facing an issue\nReported as ' + desc + '.',
                    from: '+**************',
                    to: data.Contact
                })
                .then(message => console.log(message.sid));

        
    });

});

});

exports.addHistory = functions.database.ref('issue/{id}').onCreate(async (snapshot, context) => {
    const val = snapshot.val();
    var lampnumb=val.Issue.substring(0,7);
    var desc=val.Issue.substring(10);


    var today = new Date();
var datetime = today.toLocaleString('en-GB', {
  timeZone: 'Asia/Calcutta'});
     
    var own="";
     
    admin.database().ref('lamps/'+lampnumb).on('value', function (snapshot) {
        
            var data = snapshot.val();
               own=data.Owner;
               


                return admin.database().ref('history').push().set({
                    Lamp: lampnumb,
                    Reported: datetime,
                    Completed: "",
                    Status: "Pending",
                    AssignedTo: "",
                    Issue: desc,
                    Owner: own
                    
                });

    });


});



exports.addKeyofHistory = functions.database.ref('history/{id}').onCreate(async (snapshot, context) => {
    const val = snapshot.val();
    const id = snapshot.key;

     

     admin.database().ref('issue').child(val.Lamp).update({
        HistoryId: id,
        Status: "Pending",
        LampId: val.Lamp,
        Description: val.Issue,
        Owner: val.Owner

    });

    return admin.database().ref('issue/'+val.Lamp+'/Issue').remove();


});

So here happens when the Arduino code creates an issue it should trigger the function and create a history rec and the issue data should be updated . But when the IOT device is connected this continuously change the history ID

I can not find the reason behind it.

The Arduino code is like this

static bool creported=false;
static bool breported=false;
 
void loop(){
 
      Serial.print(breported);     
     
     DHT.read11(dht_apin);
    
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Firebase.setFloat("lamps/lamp001/Humidity",DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Firebase.setFloat("lamps/lamp001/Temperature",DHT.temperature);
    Serial.println("C  ");
    delay(5000);

    int temp = digitalRead(ldr1);
    Serial.print("LDR 1 :");
    Serial.println(temp);
    Firebase.setFloat("lamps/lamp001/LDR1",temp);

    if(temp == HIGH)
    {
      /**float avg = 0;
           for(int i=0; i<1000; i++)
           {
               avg = avg + (.0264 * analogRead(crentSen) -13.51)/1000;
               delay(1);
           }
    
       Serial.print("Current: ");
       Serial.println(avg);
       Firebase.setFloat("lamps/lamp001/Current",avg);

**/
       digitalWrite(relay,LOW);
       delay(5000);     
       
       int volat = digitalRead(voltSen);
       int voltage = volat;

       Serial.print("Voltage: ");
       Serial.println(voltage);
       Firebase.setFloat("lamps/lamp001/Voltage",voltage);

       if (voltage == 1)
       {
             int temp1 = digitalRead(ldr2);
             Serial.print("LDR 2 :");
             Serial.println(temp1);
             creported=false;
             Firebase.setFloat("lamps/lamp001/LDR2",temp1);    
              if(temp1 == LOW)
              {
                 Serial.println("Light is working");
                 breported=false;
                 Serial.print(breported +" Bulb\n");  
              }
              else
              {
                 Serial.println("BULB IS BURNT");
                 String msg2 ="lamp001";
                 String msg1 ="BULB IS BURNT";

                 if(breported==false)
                 {
                   Firebase.setString("issue/lamp001/Issue",msg2+" - "+msg1);
                   breported=true;
                   Serial.print(breported +" Bulb\n");  
                 }
                  
                 
              }
       }
        else
       {
          Serial.println("CURRENT NOT PASSING");
          
          String msg4 ="lamp001";
          String msg3 ="CURRENT NOT PASSING";
          
            if(creported==false){
               Firebase.setString("issue/lamp001/Issue",msg4+" - "+msg3);
               creported=true;
               Serial.print(creported +" Current\n");  
            }
         
            
       }  
 
  }

  else
  {

    digitalWrite(relay,HIGH); 
  }

  timeClient.update();
  unsigned long epochTime = timeClient.getEpochTime();
  String formattedTime = timeClient.getFormattedTime();
  int currentHour = timeClient.getHours();
  int currentMinute = timeClient.getMinutes();
  int currentSecond = timeClient.getSeconds();
  String weekDay = weekDays[timeClient.getDay()];
  struct tm *ptm = gmtime ((time_t *)&epochTime); 
  int monthDay = ptm->tm_mday;
  int currentMonth = ptm->tm_mon+1;
  String currentMonthName = months[currentMonth-1];
  int currentYear = ptm->tm_year+1900;

  String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
  String dateFormat = "GMT+0530 (India Standard Time)";

  Serial.println(weekDay + " " + currentMonthName + " " + monthDay + " " + currentYear + " " + formattedTime + " " + dateFormat);
  String d;
  if(monthDay<10){
    d="0"+(String)monthDay;
  }
  else{
    d=(String)monthDay;
  }
  String TimeZ = weekDay + " " + currentMonthName + " " + d+ " " + (String)currentYear + " " + formattedTime + " " + dateFormat;
  Serial.println(TimeZ);
  Firebase.setString("lamps/lamp001/Time",TimeZ);
  Serial.println("");
 
  delay(2000);
 
}

Note: I have also rise my question already on stackoverflow but did not get my ans so I'm putting me same question here.

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