IoT Weekend Project – Temperature Tracking

During Christmas and New Year’s Eve I had a few days off and decided to dive a little bit more into data science with Go. I came up with the idea of a little IoT device that tracks room temperature, saves it into a NoSQL database and then shows the history on a graph drawn with Go.

Set up

  • Arduino UNO
  • TMP Sensor
  • Node.js
  • mLab Account
  • Go

First of all I had to set up my temperature tracker. I used an Arduino Uno and a standard TMP temperature sensor. The temperature output will be transferred to pin A0.

Designed with http://fritzing.org

Preparing Arduino

The next step was installing Firmata on Arduino to make it accessible by Node.js. I therefore connected my Arduino via USB and launch Arduino IDE. I opened the Firmata sketch that is available in File > Examples > Firmata > StandardFirmata. The next thing to do was selecting my Arduino board type (e.g. Arduino Uno) via Tools > Board, changing the port to the board by selecting Tools > Serial Port > myArduinoPort and uploading the program by selecting File > Upload.

I then also set up a free mLab account and created an API-Key to send the data to MongoDB. The schema of the data has been defined by saving the temperature and the date to create a history.

Once the schema was designed it was time to define where the data come from that will be applied to the schema. To get the temperature the Board has to be set ready and the output pin and its controller has to be defined. Once the board has loaded it will now save the gathered temperature every 6 seconds.

Output:

{
 '_id': {
 '$oid': '58667d3b6da6f5b23ab00dcb'
 },
 'date': {
 '$date': '2016-12-30T15:28:59.512Z'
 },
 'temperature': 24.218772799999996,
 '__v': 0
}

Once the data is safely stored in the database it is now time to access it using Go and a JSON parser. As soon as the data is parsed and ready it will be transferred to the go-chart function that draws a graph out of it.


package main

import(
'github.com/wcharczuk/go-chart'
)

func drawChart(res http.ResponseWriter, req *http.Request) {

API := new(API)
data, err := API.GetData()
if (err != nil) {
fmt.Println(err)
}

var temps []float64
var dates []time.Time

for _, d := range *data {
temps = append(temps, d.Temperature)
dates = append(dates, d.Date.Date)
}
fmt.Println(temps)
fmt.Println(dates)
//
graph := chart.Chart{
XAxis: chart.XAxis{
Name: 'Time',
NameStyle: chart.StyleShow(),
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64),
FillColor: chart.GetDefaultColor(0).WithAlpha(64),
},
ValueFormatter: chart.TimeHourValueFormatter,
},
YAxis: chart.YAxis{
Name: 'Temperature',
NameStyle: chart.StyleShow(),
Style: chart.Style{
Show: true, //enables / displays the y-axis
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64),
FillColor: chart.GetDefaultColor(0).WithAlpha(64),
},
},
Series: []chart.Series{
chart.TimeSeries{
XValues: dates,
YValues: temps,
},
},
}

res.Header().Set('Content-Type', 'image/png')
graph.Render(chart.PNG, res)
}

By opening http://localhost:8080 we now see the beautifully displayed temperature graph of the collected data.

Conclusion

Based on the high temperature fluctuation we will have to check our heating system. Nevertheless it was a quite funny IoT Weekend Project, very hands on and interesting. We gathered temperature information from Arduino, passed them with Node.js to mLab and accessed these data with Go and created a graph. For sure there are 1000 ways to make this better, faster and cleaner but for a hacky prototype it fits all the requirements.

You can check out the code on Git.

I now just ordered a LCD Touch Screen, MicroSD Card Adapter, VGA Camera Module and WiFi Shield so the hacking will go to the next round.

Kevin Kuhn

Smoothie addicted digital enthusiast with a flair for Algorithms and Digital Business. Managing Partner at Jaywalker Digital