1

Here is the code but I still can't figure out how to do it can send one time message only:

if(temperature>25){
      //for sending the message to the email
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@tssb.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was higher than the temperature of 25 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });          

    }else if(temperature<19){
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@tssb.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was smaller than the temperature of 19 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });    

      const from = 'Nexmo';
      const to = 'phone number';
      const text = 'The device with topic ' + topic + ' was smaller than the temperature of 19 which is ' + temperature;

      nexmo.message.sendSms(from, to, text, (error, response) => {
        if(error) {
          throw error;
        } else if(response.messages[0].status != '0') {
          console.error(response);
          throw 'Nexmo returned back a non-zero status';
        } else {
          console.log(response);
        }
      });

    }

The situation is that when mqtt publish the topic with temperature which is more than 25 it will send a notification message to the email or phone number but it only occurred one time which mean the temperature next time publish to the mqtt broker if is more than 25 it will not send again the message until the temperature is less than 19 then it will send again the notification email message and the same it only occurred one time. Please help, thank you.

johnhour
  • 119
  • 3
  • 14
  • Does your `sendMail` function perhaps check if a similar message was already sent? Otherwise, I think we would need to see the surrounding code this piece is contained in. - Small aside: you are duplicating quite a lot of code. – AsheraH May 23 '18 at 07:25
  • may I know how to do it? Yeah, I just want to see the messages send it will be repeated or not if the temperature is more than 25 or less than 19 – johnhour Jun 08 '18 at 08:37

0 Answers0