For all your music news related needs


Week 8:

Task 8 is a continuation on the work completed in week 7 with greater focus on the permanent storing of data into databases as opposed to live feeds such as server logs. The task entails gathering room temperature data from an electric imp and finding a method for storing this in a database. In terms of getting the temperature readings, pins 8 and 9 must be used to get readings. The initial readings are in voltage and must be converted to degrees centigrade using a small number of mathematical tools. The device code can be seen below.

Device Code:

// Read the internal and external temperature from a thermister.

id <- hardware.getdeviceid();

// Configure Pin

// thermistor connected to pin8(external) and pin9(internal)

external <- hardware.pin8 ;

external.configure(ANALOG_IN);

internal <- hardware.pin9 ;

internal.configure(ANALOG_IN);

// Define the relevant constants for this thermister

const aconst = 65535.0 ;

const bconst = 3988;

const t0const = 298.15;

vconst <- hardware.voltage() ;

// function to read the voltage and convert to degrees Centigrade

function getTemp() {

  // read the value

  local v8 = external.read() ;

  local v9 = internal.read() ;


  // convert the voltage to temperature in centigrade.

  v8 = v8 * vconst / aconst ;

  v9 = v9 * vconst / aconst ;

  local r8 = 10000.0 / ( (vconst / v8) - 1);

  local r9 = 10000.0 / ( (vconst / v9) - 1);

  local ln8 = math.log(10000.0 / r8);

  local ln9 = math.log(10000.0 / r9);

  local t8 = (t0const * bconst) / (bconst - t0const * ln8) ;

  local t9 = (t0const * bconst) / (bconst - t0const * ln9) ;

  local t8 = t8 - 273.15 ;

  local t9 = t9 - 273.15 ;

  // send the value to the server log

  local c8Str = format("%.01f", t8) ;

  local c9Str = format("%.01f", t9) ;

  // send the values to the agent

  local message = {"device" : id ,

    "external" : c8Str ,

    "internal" : c9Str } ;

  agent.send ("chtemp", message) ;

  // get the imp to sleep and wake up every 20 s

  imp.wakeup(20, getTemp);

}

getTemp() ;

As you can see above in the device code, using pin 8 and pin 9, it is possible to gather readings from which can then be converted into a float value representing degrees centigrade. This data is held in a variable called “message” which is sent back to the agent. The agent code is as follows;

Agent Code:

// define the URL of the server code

const url = "https://craig.im/uni/electricimp.php" ;

function log (message) {

  //server.log(message);

  local headers = { "Content-Type" : "application/json"} ;


  local jsonBody = http.jsonencode(message) ;

  // POST the values

  local request = http.post(url, headers, jsonBody);

  local response = request.sendsync();

  server.log(response.statuscode + ": " + response.body);

}

device.on("chtemp", log) ;


From the agent code we can see that we are setting a location for the data to be sent to cont url then we are retrieving the message from the device code. This message is then encoded into a JSON string ready to be sent to the cont url.

The data it has encoded is sent to the url in which should be ready to accept a specific type of data in which the agent code is designed to send. The API code is as follows:

API Code:

<?php

//database connection

include_once 'db_connect.php';

//get data from source loading this page

$rawData = file_get_contents("php://input");

//get a variable for current time

$readingTime = date('Y-m-d H:i:s');

//database insert

$stmt = $con->prepare('INSERT INTO electircimp (readingID, data, readingTime) VALUES (NULL, :data, NOW())');

//bindings

$stmt->execute(array(

  ':data' => $rawData

));

?>

From the php, we can see how the data is listened for and dealt with by setting the receiving JSON string as a variable which is then used along with the current datetime into a prepared insert statement.

This devised setup creates a system which acts as though it is seamless through the fluent transition of data. The usage of JSON ensures that the data is sent in a prepared manner for interchange which is then later used in a prepared statement within the php code to ensures that the statement is parameterised which increases security. Another advantage of using prepared is that they are more efficient as this small section of code may be executed time and time again.

When looking at the database we will be able to see three fields, as depictable from the prepared statement, the first field will be a unique identifier for the specific reading, the second field will hold the JSON string of data, since this is a JSON string, the size and amount of data can vary without need to adjust database fields or columns. The final field holds information on the time of reading.

word count: 436