NodeJS is here with auto Client Update

It was long time ago when I want to make a service where user don't need to refresh their page if there is any update. Currently I am working with a project with RapberryPi wallPad system. SO I decided to install nodeJs+Socket.io... but I need to make it work on virtual machine. So I am using Ubuntu Server but you can use Ubuntu 14

Follow the instruction:

  1. sudo apt-get install npm
  2. npm install -g npm
  3. npm install socket.io
  4. sudo npm install -g n
  5. sudo apt-get install node 
  6. sudo npm install xml2json

Now create file in same folder:

server.js

var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  parser = new require('xml2json'),
  fs = require('fs');

// creating the server ( localhost:8000 )
app.listen(8000);

console.log('server listening on localhost:8000');

// on server started we can load our client.html page
function handler(req, res) {
  fs.readFile(__dirname + '/client.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}
// creating a new websocket to keep the content updated without any AJAX request
io.sockets.on('connection', function(socket) {
  console.log(__dirname);
  // watching the xml file
  fs.watchFile(__dirname + '/example.xml', function(curr, prev) {
    // on file change we can read the new xml
    fs.readFile(__dirname + '/example.xml', function(err, data) {
      if (err) throw err;
      // parsing the new xml data and converting them into json file
      var json = parser.toJson(data);
      // send the new data to the client
      socket.volatile.emit('notification', json);
    });
  });

});
example.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<test>
    <sample>Hello world!</sample>

</test>
 client.html
<html>
    <head>
        <title>Push notification server</title>
    </head>
    <body>
        <time></time>
        <div id="container">Try to change your xml data to update this content</div>
    <script src="socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script>
    // creating a new websocket
    var socket = io.connect('http://10.10.50.136:8000');
    // on every message recived we print the new datas inside the #container div
    socket.on('notification', function (data) {
        // convert the json string into a valid javascript object
        var _data = JSON.parse(data);

        $('#container').html(_data.test.sample);
        $('time').html('Last Update:' + new Date());
    });
    </script>
    </body>

</html>
Please tell me if you get any error...

Attiny85 + 433Mhz transmitter+ DHT11 ----------> Received with Arduino or RaspberryPi

Attiny85 from eBay is really cheap. I paid 5 Attiny85 $8!

433MhZ receiver and transmitter from eBay. Each (both receiver+transmitter)cost $1.

DHT11 also from eBay which cost $1.25.

Transmit code:
#include <dht.h>
#include <VirtualWire.h>
#include <avr/sleep.h>
#include <util/delay.h>

int led=0;
const int transmit_pin = 4;
dht DHT;

#define DHT11_PIN 3

void setup(){
  vw_set_tx_pin(transmit_pin);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000); // Bits per sec
}

byte count = 1;

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  int t=0;
  int h=0;
  //_delay_ms(200);
  t=DHT.temperature;
  h=DHT.humidity;
  char myBigArray[128];
  char temp[4];
  itoa( t, temp, 10);
  //char razib[1]={'k'};
  //char msg[7] = {'H','e','l','l','o',' ','#'};
  myBigArray[0] = '\0';
  //strcat(myBigArray, "T:");
  strcat(myBigArray,temp );
  strcat(myBigArray, ":");
  itoa( h, temp, 10);
  //strcat(myBigArray, razib);
  strcat(myBigArray, temp);
  //msg[6] = count;
  //digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((uint8_t *)myBigArray, strlen(myBigArray));
  vw_wait_tx(); // Wait until the whole message is gone
  //digitalWrite(led_pin, LOW);
//  delay(100);
  count = count + 1;
  //system_sleep();
  //delayMicroseconds(20);
  _delay_ms(1000);
}

WiFi without wicd-curses RaspberryPi

I found a way to connect your RPi without wicd-curses:

nano /etc/network/interfaces
---------------------------------------------------------
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.con$

----------------------------------------------------------
save and exit by pressing ctrl+x

 nano /etc/wpa_supplicant/wpa_supplicant.conf
----------------------------------------------------------------
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="yourNetworkName"
scan_ssid=1
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
psk="yourNetworkPassword"
}
--------------------------------------------------------
save and exit by pressing ctrl+x
restarts all the networks services :
  • /etc/init.d/networking restart
  • or
  • ifconfig wlan0 down
    ifconfig wlan0 up

XBMC_OpenElec_Chromecast

By the way, I have implemented a Chrome plugin too for the same purpose, you can download it from here:StreamToXBMC – Adrian
Note: I have only implemented new servers in this extension-> Stream To XBMC
To install it, you only have to unzip the downloaded file, navigate to Chrome extensions
, expand the developer dropdown menu and click “Load Unpacked Extension” and navigate to local folder. Assuming there are no errors, the extension should load into your browser.
Once installed, we have to click “configure” in the extension and introduce the XBMC ip:

HPC2N job submission

Make.sbatch
***------------------------------------------------------ ***
#!/bin/bash
## Name of the script - unnecessary, but useful to help find the job in a long list
#SBATCH -A 5DV152-VT14
#SBATCH -J mpiCountSort

## Names of output and error files. You can call them what you will. If you
## don't give them a name, the file containing output and any errors, will be called
## slurm-<jobid>.out.
##

## asking for 24 processes. There are 48 cores per node on Abisko.
## If you do not specify processes/cores, you will get the smalles allocatable amount,
## Which is one 'socket' (6 cores, as far as SLURM is concerned). Runtime (walltime)
## in this example is 4 minutes max. If
## the job completes sooner, it will return at that time.
#SBATCH -n 6
#SBATCH --time=01:00:00
# Spread the tasks evenly across two nodes
#SBATCH --ntasks-per-node=48

#SBATCH --output=pN:6&dN:700.out
#SBATCH --error=pN:6&dN:700.err

## Load any modules you need (here OpenMPI for PathScale compilers)
module add openmpi/gcc/1.6.5
echo "Starting at `date`"
echo "Running on hosts: $SLURM_NODELIST"
echo "Running on $SLURM_NNODES nodes."
echo "Running on $SLURM_NPROCS processors."
echo "Current working directory is `pwd`"

srun -n $SLURM_NPROCS ./mpiCountSort 700
echo "Program finished with exit code $? at: `date`"
***-------------------------------------------***
Submit the program with:
sbatch <my_jobscript> 
Help link: http://www.hpc2n.umu.se/batchsystem/examples_scripts

Raspberry Pi change default ssh login message

Changing this message requires editing two different files. The first three sections can be modified by editing the following file:
/etc/motd

To disable the last login message (which I don’t recommend doing), you will need to edit the following file in sudo mode:
/etc/ssh/sshd_config
Find this line in the file and change the yes to no as shown:
PrintLastLog no

Json live temperature update

Get temperature with DS18B20 and save live update with python+php+Json
You can buy DS18B20 from ebay, cost >1$ and you need RaspberryPi.
Full tutorial
temp.py
import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f
   
while True:
    print(read_temp())
    deg_c, deg_f = read_temp()
    msg='<?php echo json_encode(array( "author" => "razib","tweet" => "Temparature is: " . '+str(deg_c)+',"date" => date("l jS \of F Y h:i:s A"))); ?>'
    f = open('/var/www/python/feed.php','w')
    f.write(msg)
    f.close()
    time.sleep(1)

 


index.php
<html>
<head><title>Tweets</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<style>
#tweets {
    width: 500px;
    font-family: Helvetica, Arial, sans-serif;
}
#tweets li {
    background-color: #E5EECC;
    margin: 2px;
    list-style-type: none;
}
.author {
    font-weight: bold
}
.date {
    font-size: 10px;
}
</style>

<script>
jQuery(document).ready(function() {
    setInterval("showNewTweets()", 1000);
});

function showNewTweets() {
    $.getJSON("feed.php", null, function(data) {
        if (data != null) {
            $("#tweets").prepend($("<li><span class=\"author\">" + data.author + "</span> " +  data.tweet + "<br /><span class=\"date\">" + data.date + "</span></li>").fadeIn("slow"));
        }
    });
}
</script>

</head>
<body>

<ul id="tweets"></ul>

</body>
</html>

feed.php
<?php
echo json_encode(array( "author" => "razib",
                        "tweet" => "The time is: " . time(),
                        "date" => date('l jS \of F Y h:i:s A')));
?>

crontab -e

Just add to your crontab
* * * * * for i in {0..59};