James Seigel
Published © MIT

Prestart

Need to preheat your cabin before you arrive? Need to shut off your hot tub you forgot on? Let's use AWS IoT to get it done!

IntermediateFull instructions provided2,938
Prestart

Things used in this project

Story

Read more

Schematics

Overview

Code

Particle Heat Control Code.

Arduino
This code exposes some remote methods for the particle eco system that enable us to hook up the IoT button to the Lambda code to turn on or off the relay controlling our target feature. In my case the temperature relay.
// -----------------------------------
// Controlling Heat Relay 
// -----------------------------------
// relay is D0, indicator is D7

int relay = D0;
int indicator = D7;
int thestate = 0;

void setup() {
   Spark.publish("inv-heat", "Starting up");

   pinMode(relay, OUTPUT);
   pinMode(indicator, OUTPUT);

   Spark.variable("hstate", &thestate, INT);
   Spark.function("heat", heatToggle);
   Spark.function("readheat", readHeat);


   // ensure on power on the relay is in a predictable state
   digitalWrite(relay, LOW);
   digitalWrite(indicator, LOW);
}

void loop() {
   // Nothing to do here
}

int readHeat(String command) {
    if (digitalRead(indicator) == HIGH) {
        Spark.publish("inv-heat-state", "ON");
        thestate = 1;
    } else {
        Spark.publish("inv-heat-state", "OFF");
        thestate = 0;
    }
    return 0;
}

int heatToggle(String command) {
    if (command == "on") {
        Spark.publish("inv-heat", "Turning on");
        digitalWrite(relay, HIGH);
        digitalWrite(indicator, HIGH);
        thestate = 1;
        Spark.publish("inv-heat-state", "ON");
        return 1;
    } else if (command == "off") {
        Spark.publish("inv-heat","Turning off");
        digitalWrite(relay, LOW);
        digitalWrite(indicator, LOW);
        thestate = 0;
        Spark.publish("inv-heat-state", "OFF");
        return 0;
    } else {
        return -1;
    }
}

AWS Lambda Conversion Function

JavaScript
This AWS Lambda function take the type of click from the IoT button and calls the remote method on our particle device with the appropriate arguments to turn the heat on or off. Note the access token and the actual device ID have been removed.
var https = require('https');

exports.handler = function(event, context) {
    // data to send to Particle for our remote message
    var data = {
        args: (event.clickType === "SINGLE" ? "on" : "off")
    };
    var options = {
        hostname: 'api.particle.io',
        port: 443,
        path: '/v1/devices/<device id>/heat?access_token=<access_token>',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': JSON.stringify(data).length
        }
    };   

    var req = https.request(options, function(res) {
        var body = '';
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            body += chunk;
        });
        res.on('end', function() {
            console.log('Successfully processed HTTPS response');
            // If we know it's JSON, parse it
            if (res.headers['content-type'] === 'application/json') {
                body = JSON.parse(body);
            }
            context.succeed(body);
        });
    });
    req.on('error', context.fail);
    req.write(JSON.stringify(data));
    req.end();
};

Credits

James Seigel

James Seigel

1 project • 9 followers
Geek, potter, hacker, father.

Comments