Device to Database: Phone Notifcation

I made very minimal changes to Don’s code in class to send notifications to my phone from the data send to the class’s MQTT broker from my device.

The adjust code is below (I’ve changed my phone number to just all 5’s):

const mqtt = require('mqtt');
const mqttClient = mqtt.connect(process.env.MQTT_SERVER);
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });

mqttClient.on('connect', () => {
    console.log('MQTT Connected');
    mqttClient.subscribe('itp/device_rk/temperature');
});

mqttClient.on('message', (topic, message) => {
    console.log(topic, message.toString());
    const temperature = Number(message.toString());

    if (temperature > 70) {
        const alertMessage = `Temperature in Rashida's room ${temperature}°F exceeds the high temperature limit of 70°F`
        // mqttClient.publish('itp/device_xx/alert', alertMessage);
        sendSms('+15555555555', alertMessage);
    }
});

async function sendSms(number, message) {

    var params = {
        Message: message,
        PhoneNumber: number
    };

    try {
        // Send the message
        let response = await new AWS.SNS({ apiVersion: '2010-03-31' }).publish(params).promise();
        console.log(`MessageID is ${response.MessageId}`);
    } catch (e) {
        console.error(e, e.stack)
    }
}

Screenshot of proof:

Actuating something was also part of what was suggested for the assignment. I adjusted my old arduino code so that an LED blinked when my device published to an MQTT topic. Proof: