function periodicActivity() { var buffer = new upmMicrophone.uint16Array(128); var len; while (!len) { len = myMic.getSampledWindow(2, 128, buffer); } var thresh = myMic.findThreshold(threshContext, 30, buffer, len); myMic.printGraph(threshContext); if (thresh) { //if we need it var mappedThresh = mathUtils.map(thresh, 0, 600, 0, 100); var mapColors = Math.abs(mathUtils.map(thresh, 0, 600, 100, 0)); var color = colorUtils.hsvToRgb( mapColors * 0.004, 1, .5); var soundlevel = mappedThresh.toFixed(1); console.log("Threshold is " + thresh); myLcd.setCursor(0,0); myLcd.write("Noise"); myLcd.setColor(color.r, color.g, color.b); myLcd.setCursor(1,2); myLcd.write(soundlevel .toString()); //SEND THE SOUNDLEVEL TO THE CLIENTS io.emit('soundlevel-update', soundlevel); } setTimeout(periodicActivity, 200); }
setInterval(function() { var light_raw_val = light_pin.read(); // calc light // https://github.com/intel-iot-devkit/upm/blob/master/src/grove/grove.cxx#L161-L168 var lux_val = Math.round((10000.0 / Math.pow(((1023.0 - light_raw_val) * 10.0 / light_raw_val) * 15.0, 4.0 / 3.0) * 10)) / 10 var temp_raw_val = temp_pin.read(); // calc temperature // https://github.com/intel-iot-devkit/upm/blob/master/src/grove/grove.cxx#L128-L137 celsius = ((1023.0 - temp_raw_val) * 10000.0) / temp_raw_val temp_val = Math.round((1.0 / ((Math.log(celsius / 10000.0) / 3975.0) + (1.0 / 298.15)) - 273.15) * 10) / 10 var sound_raw_val = sound_pin.read(); // Display sensed analog data on LCD grove_lcd.setColor(0,0,255); grove_lcd.setCursor(0,0); grove_lcd.write("Temp: " + temp_val); grove_lcd.setCursor(1,0); grove_lcd.write("Lux : " + lux_val); var now = new Date(); var message = JSON.stringify({ "deviceid": device_id, "timestamp": now.toISOString(), "temp": temp_val, "lux": lux_val, "sound": sound_raw_val }); device.publish('sensor', message); console.log("Publish: " + message); }, interval);
function setLCDText(txtData) { display.clear(); display.setCursor(0, 0); display.write(txtData.line0); display.setCursor(1, 0); display.write(txtData.line1); }
display.clear = function() { display.setCursor(0, 0); display.write(' ') display.setCursor(1, 0); display.write(' ') display.setCursor(0, 0); }
// Cette fonction va lire les données du capteur TH02 puis affiché la température sur l'écran function readSensor(callback){ temp = th02.getTemperature(); humi = th02.getHumidity(); display.setCursor(0,0); display.write(temp.toFixed(2).toString()); callback(); };
setInterval(function() { var button_val = button_pin.read(); console.log(button_val); // Display sensed analog data on LCD grove_lcd.setColor(255,0,0); grove_lcd.setCursor(0,0); grove_lcd.write("button: " + button_val); if(button_val > 1000 && delay_count === 0){ var now = new Date(); var message = JSON.stringify({ "serialNumber": "some_serial_no", "batteryVoltage": "5000mV", "clickType": "SINGLE" }); device.publish('button', message); console.log("Publish: " + message); delay_count = 2; } else{ if(delay_count > 0){ delay_count--; } } }, interval);
/** * Use the upm library to drive the two line display * * Note that this does not use the "lcd.js" code at all */ function useUpm() { var lcd = require('jsupm_i2clcd'); var display = new lcd.Jhd1313m1(0, 0x3E, 0x62); display.setCursor(1, 1); display.write('hi there'); display.setCursor(0,0); display.write('more text'); rotateColors(display); }
setInterval(function(){ myLcd.setCursor(0,0); // RGB Blue //myLcd.setColor(53, 39, 249); // RGB Red myLcd.setColor(255, 0, 0); myLcd.write('Hello World'); myLcd.setCursor(1,2); },300);
var displayValue = function(callback){ myLcd.setCursor(0,0); myLcd.setColor(255, 255, 255); myLcd.write("Message:"); myLcd.setCursor(1,1); myLcd.write(lcdMessage); if (typeof callback !== "undefined") { callback("Message set to: lcdMessage"); } };
setTimeout(function() { if(r<256 || g<256 || b<256) { // r = r/light.value(); // g = g/light.value(); // b = b/light.value(); } myLcd.setColor(r, g, b); myLcd.setCursor(1,0); myLcd.write(TExp + "Mins" + " UV:" + uv + " "+ tem + "C"); }, 60000);
function marquee(data){ if(data.length>16){ for(var i=0;i<data.length-15;i++){ mylcd.clear(); mylcd.write(data.slice(i,16+i)); sleep(500); } } else mylcd.write(data); }
function useUpm() { var a = myAnalogPin.read(); var resistance = (1023 - a) * 10000 / a; //get the resistance of the sensor; var celsius_temperature = 1 / (Math.log(resistance / 10000) / B + 1 / 298.15) - 273.15;//convert to temperature via datasheet ; var fahrenheit_temperature = (celsius_temperature * (9 / 5)) + 32; display.setCursor(1, 4); display.write(Math.round(celsius_temperature)+" "); rest.postToSurvoy('temperature',Math.round(celsius_temperature)); }
client.comm.setReceivedMessageHandler(function (msg) { console.log("received: " + msg); // to display var show = JSON.parse(msg); display.setCursor(0,0); display.write(show.last_sensor + "=" + show.last_val); display.setCursor(1,0); display.write(show.curr_mean); // to cloud service.comm.send(show.curr_mean); });
setInterval(function () { var Light = LightSensor.read(); var Temp = TempSensor.read(); // バックライト色を指定 myLCD.setColor(255, 255, 255); // 配置(row,clumn)を指定 myLCD.setCursor(0,0); // LCDに書き出し myLCD.write("Light: " + Light); // 配置(row,clumn)を指定 myLCD.setCursor(1,0); // LCDに書き出し myLCD.write("Temp: " + Temp); }, 1000);
function resetDisplay() { display.setColor(0,0,0) display.setCursor(0,0); display.write('<Refreshing..!!>'); display.setCursor(1,0); display.write('*****************'); sleep(500) display.setCursor(0,0); display.write(" "); display.setCursor(1,0); display.write(" "); }
function crash(){ myLed.write(1); display.setColor(255,0,0); setTimeout(function(){ myLed.write(0); display.setColor(0,255,0); },30000); }
function setLCDColor(r, g, b, duration) { display.setColor(r, g, b); if (duration > -1) { setTimeout(function () { setLCDColor(0, 0, 0, -1); }, duration); } }
/** * Use the upm library to drive the two line display * * Note that this does not use the "lcd.js" code at all */ function useUpm() { // display.setCursor(1, 1); display.setColor(0,0,254); display.setCursor(0,0); display.write('Welcome to Intel'); display.setCursor(1,0); display.write('IOT Hackathon 2016'); // crap setTimeout(function(){do_nothing();}, 5000); display.setCursor(0,0); display.setColor(0,254,0); display.write('Presenting AILA'); display.setCursor(1,0); display.write('Auto Inventory n Logistics Analysis'); // display.scroll // display.setColor(0,0,0); }
function displayText(text) { //var l = light.raw_value(); //console.log(l); // mR(); var currentdate = new Date(); var time = "" + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); display.setCursor(0, 0); display.write(time); display.setCursor(1, 0); var tempCelcius = getTemperature(); display.write(tempCelcius + ' C'); if (tempCelcius > FIRE_TEMPERATURE) { myOnboardLed4.write(1); display.setColor(255, 0, 0); buzz(); } else { myOnboardLed4.write(0); display.setColor(255, 255, 255); stopBuzzing(); } sendData(tempCelcius); }
function setColorRGB(r, g, b, opt_duration) { myLcd.setColor(r, g, b); console.log('Setting LCD color to: ', r, g, b); if (opt_duration) { setTimeout(function() { setColorRGB(cur_r, cur_g, cur_b); }, opt_duration); } else { cur_r = r; cur_g = g; cur_b = b; } }
writeText : function(text, opt_line) { if (opt_line == 2) { myLcd.setCursor(1, 0); myLcd.write(' '); myLcd.setCursor(1, 0); } else { myLcd.setCursor(0, 0); myLcd.write(' '); myLcd.setCursor(0, 0); } myLcd.write(text); }
var payment_received = function() { console.log("Payment received!") display.clear(); display.setCursor(0,0); display.write('Payment received!'); var array = fs.readFileSync('api-keys.txt').toString().split("\r\n"); /*** Simplify commerce ***/ client = Simplify.getClient({ publicKey: array[0], privateKey: array[1] }); client.payment.create({ amount : 50, description : "Bitcoin payment", card : { expMonth : "11", expYear : "19", cvc : "123", number : "5555555555554444" }, currency : "GBP" }, function(errData, data){ if(errData){ console.error("Error Message: " + errData.data.error.message); // handle the error return; } console.log("Payment Status: " + data.paymentStatus); }); payed = true }
request('https://blockchain.info/q/addressbalance/'+addr2watch+"?confirmations=0", function (error, response, body) { if (!error && response.statusCode == 200) { var got_money = body; if(got_money != "0") { payment_received() return; } reset_loops += 1 } if (reset_loops >= reset_after) { reset_loops = 0; console.log("Timeout"); return; } display.clear(); display.setCursor(0,0); var n = (reset_after - reset_loops).toString(); display.write(n); _.delay(check_balance, loop_time) // this is like setTimeout(check_balance, loop_Time) })
client.on('message',function(topic,message){ mylcd.clear(); if(topic.toString()==='presence') line1 = message.toString(); else if(topic.toString()=='temp') line2 = message.toString(); else { console.log('here'); marquee(songs[(++index)%songs.length]); } /*if(line1) mylcd.write(line1); mylcd.setCursor(1,0); if(line2) mylcd.write(line2);*/ });
setInterval(function () { // check if button is pressed if(button.read() !== 1) return; // highlight (state) ? LCD.setColor(4,55,21) : LCD.setColor(21,74,7); // remove previous timeout clearTimeout(timeout); // set new timeout timeout = setTimeout(function() { state = !state; LCD.setColor(0,0,0); }, 2000); }, 400);
setInterval(function () { var temperature = getCelsius(temperatureSensor.read()), darkness = getDarknessCoefficient(lightSensor.read()), noise = soundSensor.read(); // save to database sendValues(temperature, noise, darkness); LCD.setCursor(0,0); LCD.write('T:' + temperature); LCD.setCursor(1,0); LCD.write('S:' + noise); LCD.setCursor(1,9); LCD.write('L:' + darkness); }, 60000);
var mqtt = require('mqtt'); var m = require('mraa'); var client = mqtt.connect('mqtt://127.0.0.1'); var LCD = require('jsupm_i2clcd'); var mylcd = new LCD.Jhd1313m1(0); var line1,line2; function sleep(delay){ var start = new Date().getTime(); while(new Date().getTime() < start +delay ); } //file visiter var fs = require("fs"); var path = require("path"); var songs = new Array; var temp = new Array; var index = 0; temp = fs.readdirSync('music/sdcard'); for(var i=0;i<temp.length;i++){ if(temp[i].substr(temp[i].lastIndexOf('.')+1, temp[i].length)==='wav'){ songs.push(temp[i]); } } function marquee(data){ if(data.length>16){ for(var i=0;i<data.length-15;i++){ mylcd.clear(); mylcd.write(data.slice(i,16+i)); sleep(500); } }
// Plug Grove - Temperature&Humidity(High quality) to i2c port // On importe la bibliothèque du capteur de température et d'humidité TH02 puis on le déclare var sensor1 = require('jsupm_th02'); var th02 = new sensor1.TH02(); // On importe la bibliothèque de l'écran LCD puis on le déclare var lcd = require('jsupm_i2clcd'); var display = new lcd.Jhd1313m1(0, 0x3E, 0x62); // On affiche "init" à l'écran display.write("init"); // Client token value returned from thingShadows.update() operation // On importe la bibliothèque de thingShadow const thingShadow = require('./node_modules/aws-iot-device-sdk/thing'); // On importe la bibliothèque AWS IoT var awsIot = require('aws-iot-device-sdk'); // Répertoire contenant les certificats (sur le thing) var rootDir = '/home/root/.node_app_slot/certificat/' // Toutes les explications de la bibliothèque aws-iot-device-sdk // sont données en détail dans le readme du git du package // https://github.com/aws/aws-iot-device-sdk-js var thingShadows = awsIot.thingShadow({ keyPath: rootDir + 'iot.pem', certPath: rootDir + 'cert.pem', caPath: rootDir + 'rootCA.pem', clientId: 'myAwsClientId', region: 'eu-central-1' });
function loop() { /* Started Reading Sensor Data */ tempPinValue = temperatureSensor.read(); tempValue = getTemperature(tempPinValue); lightPinValue = lightSensor.read(); lightValue = Math.round( lightPinValue/1023*100); soundPinValue = soundSensor.read(); soundValue = Math.round( soundPinValue/1023*100); airQualityPinValue = airQualitySensor.read(); airQualityValue = Math.round( airQualityPinValue/1023*100); var touchVal = String(touchSensor.read()); /* Finished Reading Sensor Data */ lcdMessage_temp = "Temp. is @ "+tempValue; lcdMessage_light = "Light is @ "+lightValue+" %"; var lcdMessage_sound = "Sound is @"+soundValue+" %"; var lcdMessage_airQuality = "AirQlty @ "+airQualityValue+" %"; myLCD.setCursor(0,0); myLCD.write(lcdMessage_temp); myLCD.setCursor(1,0); myLCD.write(lcdMessage_light); /* Process Touch Sensor Data */ if(touchVal == '1') { LED.write(1); buzzer.write(1); while(String(touchSensor.read()) == '1') { myLCD.setCursor(0,0); myLCD.write("Touch Detected:)"); myLCD.setCursor(1,0); myLCD.write(getTime()); } } if(touchVal == '0') { buzzer.write(0); } /* Process Light Sensor Data */ if(lightValue > 10) { LED.write(0); } if(lightValue < 10) { LED.write(1); } /* Process Sound Sensor Data */ if(soundPinValue > 200) { if(soundPinValue > 450) { console.log('Clapping! Sound Level @ '+soundPinValue+" at "+getTime()); } else if(soundPinValue > 350) { console.log('Talking/Singing! Sound Level @ '+soundPinValue+" at "+getTime()); } //else //console.log('Fan! Normal Sound Level @ '+soundPinValue+" at "+getTime()); } else if(soundPinValue < 200) { //Normal Value @HomeAlone //console.log('Sound Level @ '+soundPinValue); } /* Process Air Quality Sensor Data */ if(airQualityPinValue > 150) { if(airQualityPinValue > 500) { console.log('Critical Condition!! Air Quality Level @ '+airQualityPinValue+" at "+getTime()); } else if(airQualityPinValue > 350) { console.log('Warning! High CO2 Content! Air Quality Level @ '+airQualityPinValue+" at "+getTime()); } else console.log('Moderate CO2 Content! Air Quality Level @ '+airQualityPinValue+" at "+getTime()); } else if(airQualityPinValue < 150) { //Normal Value @Home //console.log('Air Quality Level @ '+airQualityPinValue); } //console.log('Sound Level @ '+soundPinValue+' Air Quality Level @ '+airQualityPinValue+" at "+getTime()); setTimeout(loop,100); }
timeout = setTimeout(function() { state = !state; LCD.setColor(0,0,0); }, 2000);