Network Connected Thermostat, trials and accomplishments

The final project for Connected Devices was to create an IOT thermostat that would record the temperature in whatever room it was placed in and then send that temperature to a remote server. The thermostat was required to have a working UI and post the temperature to the server once every hour for a week straight.

After a lot of trial and error I was able to achieve this using the MKR 1010 microcontroller, much assistance from Tom Igoe, and Koji, and a few repos listed below.

https://github.com/tigoe/Wifi101_examples/blob/master/ConnDevClient/ConnDevClient.ino

https://github.com/tigoe/MakingThingsTalk2

The initial setup of the MKR1010 took quite a while. Having to make sure that the most up to date firmware was present on the device, adding the mac address to itpsandbox, and then receiving a static ip address for the device. Once all of this was done and I was assigned a specific session key I could begin really working.

I made the decision to use dweet to test that I was sending data correctly. First I wanted to test the server with a static value and once that sent properly I set up the temperature reading circuit and sent the temperature data to dweet. Both of those worked correctly so I moved onto the real thing.

Screen Shot 2019-02-22 at 3.11.27 PM.png
Screen Shot 2019-02-23 at 11.02.48 AM.png

This is the part of the assignment where I wish I could say everything worked perfectly the first try but I ended up spending more time than I care to admit trying to fix an error that made little sense to me. Multiple people attempted to help and it wasn’t until Tom took a look at my code and realized that in my call to the server address I had added “https://” this created a -2 HTTP Status in my code even when everything looked like it was sending to the server properly.

Once this issue was fixed I got my first 400 Status, this wasn’t ideal but I was still elated because at least I knew that it was because of a JSON formatting error rather than some unknown.

Screen Shot 2019-02-24 at 3.07.31 PM.png

After a lot of tweaking to the JSON I was sending I finally got a 201 status.

Screen Shot 2019-02-24 at 6.57.42 PM.png

Curling to the server proved that I was successful and could focus on doing a bit of fine tuning and working on the UI. I decided not to invest as much time in that aspect so I stuck to a simple LCD screen and potentiometer combination. The LCD displays the current room temperature in Celsius and the potentiometer simply turns the temperature display on or off depending on the state of rotation.

IMG_20190225_192227.jpg

Here is a video of the working device:

A link to the code repo can be found here: Link

Creating a basic Web Interface

This week’s assignment was to create a web interface for an existing web-connected consumer device. In class we specifically went over the hue bulb system and that is the device I created the web interface for. I began the assignment by going over the documentation provided to us regarding the hue system, assigned IP, and clip api. After getting my login credentials from the clip API I began doing basic calls of the hue bulb. i.e turning it on and off and changing the colors manually.

Screen Shot 2019-02-11 at 7.01.31 PM.png

After playing around with this system for a bit I began to focus my attention on creating my own interface. I referenced the hue control repo from Tom Igoe, and node hue api repo from Peter Murray. I had difficulty understanding portions of getting the api to work so it took a good amount of time to really wrap my head around what was happening. Timothy Lobiak was also extremely helpful in giving me guidance and sharing code snippets on how to create the colorMode sliders and storing values.

I ended up deciding to use p5.js due to my familiarity with the DOM elements. Even with this I was a bit rusty so with the language. My goal for functionality was to be able to turn the light on and off with a button, then have three sliders that controlled the hue, saturation, and brightness and have the bulb change state in real time depending on the position of each slider.

this is the basic interface functioning.

Assignment 1: Creating a Web Server

Concept

The first assignment for Connected Devices required us to make a basic server using Node.JS and Express. For the server i drew heavily on example code used in class last Tuesday and also used Tom Igoe’s Four Line Server example. Finally the Express.JS routing guide was extremely helpful.

For my server I decided to make a basic remote thermostat that would let a user control the temperature in their apartment as well as see/change the state of the thermostat. i.e whether it is in heat/cold/off state.

The four REST endpoint I utilized are as follows:

1. /state lists the current state of the thermostat (heat, cold, off)
2. /state/(off,heat,cold) inputing any of the three states will change the current thermostat states
3. /temperature lists the current temperature inside of the apartment
4. /temperature/(increase,decrease) will either increase or decrease the current temperature

Issues

My biggest issues with completing the assignment came from my lack of experience with html and getting a better interface/more functionality out of the server. For Example I would like the user to be able to be prompted to manually set the temperature if they like. As I learn more about Node.js/Express and interfacing I know these types of issues will subside.

Code

Below is the code broken down into sections:

Here is a link to the Github Repo

//four line server example created by Tom Igoe
var express = require('express');
var server = express();
server.use('/',express.static('public'));
server.get('/temperature',temperature);
server.get('/temperature/:changeTemp',modifyTemp);
server.get('/state',checkState);
server.get('/state/:newState',changeThermState);
server.listen(8080);

//inital state of the Serverstat
var thermostatState = 'off';
var thermTemp = 64;

/*
CheckState is responsible for checking the current state of the Serverstat(either heat/off/cold) and the current
temperature in Farenheit
*/
function checkState(request, response){
  response.send('the current state is: ' + thermostatState + ', at a temperature of ' + thermTemp + ' degrees Farenheit');
  response.end;
}

/*
changeThermState allows the user to manually change the state of the thermostat when they enter
one of the three preset states
*/
function changeThermState(request, response){
  var newThermState = request.params.newState;
  if(newThermState == 'off' || newThermState == 'cold' || newThermState == 'heat'){
    thermostatState = newThermState;
    response.send('the state has been set to ' + thermostatState);
    }
    else{
      response.send('please only input "off/cold/heat"');
    }
    response.end;
}
/*
temperature sets a random temperature value to the output when the state is changed.
*/
function temperature(request, response){
  if(thermostatState == 'heat'){
    var temp =  Math.floor(Math.random()*(80-70+1)+70);
    if(temp <= 69){
      temp = 73;
    }
  }

  if(thermostatState == 'cold'){
    var temp =  Math.floor(Math.random()*(65-55+1)+55);
  }

  else{var temp =  64;}
  thermTemp = temp;
  response.send('the current temperature inside the apartment is: ' + temp + ' degrees Farenheit');
  response.end;

}
/*
modifytemp allows the user to either increase or decreaase the temperature depending
on their preferences
*/
function modifyTemp(request, response){
  var tempChange = request.params.changeTemp;;

    if(tempChange == 'increase'){
    thermTemp++;
    response.send('the temperature has been increased to ' + thermTemp);
    }

    else if(tempChange == 'decrease'){
      thermTemp--;
      response.send('the temperature has been decreased to ' + thermTemp);
    }
    else{
      response.send('Please input either "increase/decrease"');

    }
    response.end;

}