Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Created November 20, 2014 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krhoyt/3ce601ecb520229d87bf to your computer and use it in GitHub Desktop.
Save krhoyt/3ce601ecb520229d87bf to your computer and use it in GitHub Desktop.
First Steps with Tessel
// Libraries
var climate = require( 'climate-si7020' );
var parse = require( 'parse' ).Parse;
var tessel = require( 'tessel' );
var wifi = require( 'wifi-cc3000' );
// Constants
var PARSE_APP = '_YOUR_APP_KEY_';
var PARSE_KEY = '_YOUR_JAVASCRIPT_KEY_';
// Parse objects
var Temperature = parse.Object.extend( 'Temperature' );
// Use port
var port = climate.use( tessel.port['A'] );
// Initialize Parse library
// Note Tessel bug requiring change to Parse module
// https://github.com/tessel/runtime/issues/429
parse.initialize( PARSE_APP, PARSE_KEY );
// Port is ready
port.on( 'ready', function () {
console.log( 'Connected to si7020.' );
// Queue execution of port access
setImmediate( function loop() {
// Read Farenheit temperature
port.readTemperature( 'f', function( err, temperature ) {
var storage = null;
// Store in cloud if wireless
if( wifi.isConnected() )
{
// New Parse object
// Values
// Save
storage = new Temperature();
storage.set( 'fahrenheit', temperature );
storage.set( 'celcius', ( temperature - 32 ) / 1.8 );
storage.save( null, {
success: function( result ) {
console.log( 'Saved: ' + result.id );
},
error: function( result, error ) {
console.log( error.message );
}
} );
}
// Log to console
console.log( 'Temperature: ' + temperature.toFixed( 4 ) + 'F' );
// Wait and do it again
setTimeout( loop, 1000 );
} );
} );
} );
// Error accessing port
port.on( 'error', function( err ) {
console.log( 'error connecting module', err );
} );
// Libraries
var climate = require( 'climate-si7020' );
var tessel = require( 'tessel' );
// Use port
var port = climate.use( tessel.port['A'] );
// Port is ready
port.on( 'ready', function () {
console.log( 'Connected to si7020.' );
// Queue execution of port access
setImmediate( function loop() {
// Read Farenheit temperature
port.readTemperature( 'f', function( err, temperature ) {
// Read relative humidity
port.readHumidity( function( err, humidity ) {
console.log(
'Degrees: ', temperature.toFixed( 4 ) + 'F ',
'Humidity: ', humidity.toFixed( 4 ) + '%RH'
);
// Wait and do it again
setTimeout( loop, 1000 );
} );
} );
} );
} );
// Error accessing port
port.on( 'error', function( err ) {
console.log( 'error connecting module', err );
} );
// Libraries
var climate = require( 'climate-si7020' );
var https = require( 'https' );
var tessel = require( 'tessel' );
var wifi = require( 'wifi-cc3000' );
// Constants
var PARSE_APP = '_YOUR_APP_KEY_';
var PARSE_KEY = '_YOUR_REST_KEY_';
// Device port
var port = climate.use( tessel.port['A'] );
// Port is ready
port.on( 'ready', function () {
console.log( 'Connected to si7020.' );
// Queue execution of port access
setImmediate( function loop() {
// Read Farenheit temperature
port.readTemperature( 'f', function( err, temperature ) {
var options = null;
var request = null;
// Store in cloud if wireless
if( wifi.isConnected() )
{
options = {
headers: {
'Content-Type': 'application/json',
'X-Parse-Application-Id': PARSE_APP,
'X-Parse-REST-API-Key': PARSE_KEY
},
hostname: 'api.parse.com',
method: 'POST',
path: '/1/classes/Temperature',
port: 443
};
// Make request
request = https.request( options, function( response ) {
// Got a response
response.on( 'data', function( data ) {
console.log( data.toString().trim() );
} );
} );
request.write( JSON.stringify( {
fahrenheit: temperature,
celcius: ( temperature - 32 ) / 1.8
} ) );
request.end();
// Handle HTTPS error
request.on( 'error', function( err ) {
console.error( err );
} );
}
// Log to console
console.log( 'Temperature: ' + temperature.toFixed( 4 ) + 'F' );
// Wait and do it again
setTimeout( loop, 30000 );
} );
} );
} );
// Error accessing port
port.on( 'error', function( err ) {
console.log( 'error connecting module', err );
} );
// Libraries
var tessel = require( 'tessel' );
// GPIO access
var gpio = tessel.port['GPIO'];
// LED
var led = gpio.pin['G3'];
// Manage state
var led_state = false;
// Set off initially
led.output( led_state );
// Queue to start sampling
setImmediate( function loop() {
// Switch LED state
led_state = !led_state;
// Send state to pin
led.write( led_state );
// Do it again
setTimeout( loop, 1000 );
} );
// Libraries
var parse = require( 'parse' ).Parse;
var tessel = require( 'tessel' );
var wifi = require( 'wifi-cc3000' );
// Thermistor constants
var ANALOG_STEPS = 1023.0;
var B_THERM = 3977.0;
var T_THERM = 298.15;
var RESISTOR = 10000.0;
var THERMISTOR_OHM = 10000.0;
var VOLTAGE = 3.3;
// Parse.com constants
var PARSE_APP = '_YOUR_APP_KEY_';
var PARSE_KEY = '_YOUR_JAVASCRIPT_KEY_';
// Parse objects
var Temperature = parse.Object.extend( 'Temperature' );
// Initialize Parse library
// Note Tessel bug requiring change to Parse module
// https://github.com/tessel/runtime/issues/429
parse.initialize( PARSE_APP, PARSE_KEY );
// GPIO access
var gpio = tessel.port['GPIO'];
// Thermistor
var thermistor = gpio.pin['A5'];
// Queue to start sampling
setImmediate( function loop() {
var analog = null;
var celcius = null;
var fahrenheit = null;
var storage = null;
// Analog reading
// Resulting range is zero to one
analog = thermistor.read();
// Voltage to temperature values
// Multiply by 10-bit steps
celcius = temperature( analog * ANALOG_STEPS );
fahrenheit = celcius * 1.80 + 32.0;
// Store in cloud if wireless
if( wifi.isConnected() )
{
// New Parse object
// Values
// Save
storage = new Temperature();
storage.set( 'fahrenheit', farenheit );
storage.set( 'celcius', celcius );
storage.save( null, {
success: function( result ) {
console.log( 'Saved: ' + result.id );
},
error: function( result, error ) {
console.log( error.message );
}
} );
}
// Show the value
console.log( fahrenheit );
// Do it again
setTimeout( loop, 1000 );
} );
// Calculate celcius temperature
function temperature( analog )
{
var kelvin = 0.0;
var l_therm = 0.0;
var r_therm = 0.0;
var v_out = 0.0;
// Thermistor calculations
v_out = VOLTAGE * analog / ANALOG_STEPS;
r_therm = ( RESISTOR * VOLTAGE / v_out ) - RESISTOR;
l_therm = Math.log( THERMISTOR_OHM / r_therm );
kelvin = ( T_THERM * B_THERM ) / ( B_THERM - T_THERM * l_therm );
// Celcius value
return kelvin - 273.15;
}
// Libraries
var https = require( 'https' );
var tessel = require( 'tessel' );
var wifi = require( 'wifi-cc3000' );
// Thermistor constants
var ANALOG_STEPS = 1023.0;
var B_THERM = 3977.0;
var T_THERM = 298.15;
var RESISTOR = 10000.0;
var THERMISTOR_OHM = 10000.0;
var VOLTAGE = 3.3;
// Parse.com constants
var PARSE_APP = '_YOUR_APP_KEY_';
var PARSE_KEY = '_YOUR_JAVASCRIPT_KEY_';
// GPIO access
var gpio = tessel.port['GPIO'];
// Thermistor
var thermistor = gpio.pin['A5'];
// Queue to start sampling
setImmediate( function loop() {
var analog = null;
var celcius = null;
var fahrenheit = null;
// Analog reading
// Resulting range is zero to one
analog = thermistor.read();
// Voltage to temperature values
// Multiply by 10-bit steps
celcius = temperature( analog * ANALOG_STEPS );
fahrenheit = celcius * 1.80 + 32.0;
// Store in cloud if wireless
if( wifi.isConnected() )
{
// HTTPS request options
// Parse headers
// Parse URL
// Parse path
options = {
headers: {
'Content-Type': 'application/json',
'X-Parse-Application-Id': PARSE_APP,
'X-Parse-REST-API-Key': PARSE_KEY
},
hostname: 'api.parse.com',
method: 'POST',
path: '/1/classes/Temperature',
port: 443
};
// Make request
request = https.request( options, function( response ) {
// Got a response
response.on( 'data', function( data ) {
console.log( data.toString().trim() );
} );
} );
// Send data along with request
request.write( JSON.stringify( {
fahrenheit: fahrenheit,
celcius: celcius
} ) );
// End the request phase
request.end();
// Handle HTTPS error
request.on( 'error', function( err ) {
console.error( err );
} );
}
// Show the value
console.log( fahrenheit );
// Do it again
setTimeout( loop, 1000 );
} );
// Calculate celcius temperature
function temperature( analog )
{
var kelvin = 0.0;
var l_therm = 0.0;
var r_therm = 0.0;
var v_out = 0.0;
// Thermistor calculations
v_out = VOLTAGE * analog / ANALOG_STEPS;
r_therm = ( RESISTOR * VOLTAGE / v_out ) - RESISTOR;
l_therm = Math.log( THERMISTOR_OHM / r_therm );
kelvin = ( T_THERM * B_THERM ) / ( B_THERM - T_THERM * l_therm );
// Celcius value
return kelvin - 273.15;
}
// Library
var tessel = require( 'tessel' );
// Constants
var ANALOG_STEPS = 1023.0;
var B_THERM = 3977.0;
var T_THERM = 298.15;
var RESISTOR = 10000.0;
var THERMISTOR_OHM = 10000.0;
var VOLTAGE = 3.3;
// GPIO access
var gpio = tessel.port['GPIO'];
// Thermistor
var thermistor = gpio.pin['A5'];
// Queue to start sampling
setImmediate( function loop() {
var analog = null;
var celcius = null;
var farenheit = null;
// Analog reading
// Resulting range is zero to one
analog = thermistor.read();
// Voltage to temperature values
// Multiply by 10-bit steps
celcius = temperature( analog * ANALOG_STEPS );
fahrenheit = celcius * 1.80 + 32.0;
// Show the value
console.log( fahrenheit );
// Do it again
setTimeout( loop, 1000 );
} );
// Calculate celcius temperature
function temperature( analog )
{
var kelvin = 0.0;
var l_therm = 0.0;
var r_therm = 0.0;
var v_out = 0.0;
// Thermistor calculations
v_out = VOLTAGE * analog / ANALOG_STEPS;
r_therm = ( RESISTOR * VOLTAGE / v_out ) - RESISTOR;
l_therm = Math.log( THERMISTOR_OHM / r_therm );
kelvin = ( T_THERM * B_THERM ) / ( B_THERM - T_THERM * l_therm );
// Celcius value
return kelvin - 273.15;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment