Laurent Ellerbach

Internet of Things, IoT, Arduino, Netduino, Galileo, Spark.IO, NETMF, .NET Microframework and other embedded gadgets :-)

  • Laurent Ellerbach

    How to connect Lego Mindstorms NXT ev3 to Azure IoT Hub using node.js

    • 0 Comments

    Recently, I’ve played with node.js and Azure IoT Hub. You can see my previous blog posts here, here and here. And as I’m a huge fan of Lego, I’ve decided to connected my Lego Mindstorms ev3 (the new version of NXT) to Azure IoT Hub. Well, at the end of the day, the ev3 is just a 32-bit ARM9 processor, Texas Instrument AM1808 cadenced at 300MHz. The main OS is Linux and it does have 1 USB port and 1 SD card reader. It does allow to boot on an SD card another OS so you don’t have to flash the main one. and on the USB port, you can plus a wifi dongle.

    Setup the Mindstorms ev3

    I already used the excellent monobrick to run C# code on the brick and it was working perfectly. Now my challenge was to run node.js and connect it to Azure IoT Hub. So I looked quickly at various available images and found quickly the ev3dev one.

    So I’ flashed a 4Gb SD card and booted on it. Just follow the steps on the ev3dev site to flash the SD card. I’m using a very cheap wireless dongle, an Edimax. It does cost less than 10$/€ and it’s very small, so easy to add to the brick. It does connect at 150Mb max but you really don’t need to have more on the brick!

    Time to boot the brick using ev3dev. I’m using an external power supply so I’m not consuming batteries and I do recommend to do it while running all those tests. It is very convenient during the development and test phase. Later you can of course run on batteries.

    WP_20151224_16_08_03_ProWP_20151224_16_08_08_Pro

    Once you’ve booted, connect to the wifi, you can as well use a wired dongle if you prefer. Careful as you only have 2 minutes to enter your key, so you better have to speed up to enter it Sourire I had to redo it 3 times the first time as I have a quite long key and it’s not that easy to enter using the buttons and screen from the ev3.

    Once connected to wifi, the IP address will display on the screen. Time to connect to the brick. I’m using PuTTY to connect to the brick.

    login as: root
    root@192.168.1.20's password:
                 _____     _
       _____   _|___ /  __| | _____   __
      / _ \ \ / / |_ \ / _` |/ _ \ \ / /
    |  __/\ V / ___) | (_| |  __/\ V /
      \___| \_/ |____/ \__,_|\___| \_/

    Debian jessie on LEGO MINDSTORMS EV3!

    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.

    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Wed Dec 23 18:49:39 2015 from pc10.home
    root@ev3dev:~#

    Follow the instruction of the e3vdev website to update the brick. It will take some time to update and upgrade all packages.

    At the end of the day, when running a node –v, you’ll get v0.10.29 and npm is 1.4.21. And it’s the best you can get on this armel distribution so far.

    Setup the development environment

    I’ve decided to go for Visual Studio. You can get a free version of Visual Studio Community here. I’ve installed as well the needed tools as explained in my blog post here. If you prefer you can go as well for Visual Studio Code which you’ll find here. It does work perfectly as well.

    Once all ready, create a new node.js project, I went for the simple web server one. Again, follow the steps from my previous article to get it all setup. I’m of course using TypeScript as it makes it easy to maintain the code. It does generate automatically at build time the javascript files.

    Add the azure-iot-device and ev3dev packages.

    image

    Up to this point, it was quite straight forward and I didn’t had any issue. The next steps are where I started to face issues.

    Deploy the solution on the brick

    To deploy the solution, it’s just about creating a directory and copying the needed files on it. This time I used WinSCP for this. I’ve created a nodeNXT directory in /home and deployed the files there. Just copy the generated server.js and package.json file.

    image

    On the device, in the /home/nodeNXT directory, run an “npm install” command. It will install the missing packages. Be patient, it does take a while. And it’s where the problems starts.

    As the needed version of node.js for the Azure IoT SDK is 0.12, and as the system can’t get more recent build than 0.10.29, some of the packages do not get deploy correctly. The main faulty one is the websocket one.

    If you try to create in your code a “var device = require('azure-iot-device');” you’ll get an error message on the device. I have to say I didn’t had any idea on how to get thru this. So I’ve asked Pierre Cauchois who is working in the Azure IoT team for advices. And he told me to remove the websocket part and just use classes which I’ll need. In my case I just needed the http way to publish in Azure IoT Hub.

    For this, you’ll need to edit on the device “./node_modules/azure-iot-device/device.js”, in the last part of the file, just keep this:

    var common = require('azure-iot-common');

    module.exports = {
        Client: require('./lib/client.js'),
        ConnectionString: require('./lib/connection_string.js'),
        Http: require('./lib/http.js'),
        Message: common.Message,
        SharedAccessSignature: require('./lib/shared_access_signature.js'),
    };

    And you’ll need as well to edit the file on the device  “./node_modules/azure-iot-device/lib/http_receiver.js”, comment the following lines:

    //var util = require('util');

    //util.inherits(HttpReceiver, EventEmitter);

    It looks like there is an issue as well in the util package. For what I’l do later, looks like those 2 lines have no issue. My goal is just to send data to Azure IoT Hub from the NXT. I haven’t tested to receive data from the Azure IoT Hub, so this may have an impact.

    I’ve spend a lot of time fixing this issue due to the outdated version of node.js running on the brick but if you do this, you’ll be able to upload data in the Azure IoT Hub.

    I found another issue, not related to the Azure IoT SDK but to the ev3dev version. The version deployed thru npm is the 0.9.2 and is quite basic. You can find a new version on github which is 0.9.3. So download and install this version in your node modules, add it as a project in Visual Studio, and use this one. There is a bug in the index.ts file. Some references are missing for sensors.

    In the sensor.ts file, make sure you reference all sensors from the sensor.ts file:

    // Sensors
    export var Sensor = sensors.Sensor;
    export var I2CSensor = sensors.I2CSensor;
    export var TouchSensor = sensors.TouchSensor;
    export var ColorSensor = sensors.ColorSensor;
    export var UltrasonicSensor = sensors.UltrasonicSensor;
    export var GyroSensor = sensors.GyroSensor;
    export var InfraredSensor = sensors.InfraredSensor;
    export var SoundSensor = sensors.SoundSensor;
    export var LightSensor = sensors.LightSensor;

    Note that the code is coming in TypeScript, so you’ll need to compile it and deploy it manually to the device. I did it in a very basic way, once compiled, I’ve just copied all files from the directory. In theory, you just need the js ones and the package.json and the scprits folder.

    image

    OK, now we’re really done on the device side Sourire Good because it takes me a lot of time to figure out all those issues.

    Some code to upload data

    Now here is some code to play with. This is a very basic code but shows all what you need to get data from sensors and upload them in Azure IoT Hub:

    import http = require('http');
    var url = require('url');
    var ev3dev = require('./node_modules/ev3dev-lang/index.js');
    var device = require('azure-iot-device');
    var connectionString = 'HostName=XXX.azure-devices.net;DeviceId=NXT;SharedAccessKey=XXXXXXXXXXXX';


    var port = process.env.port || 1337
    http.createServer(function (req, res) {
        var request = url.parse(req.url, true);
        var action = request.pathname;
        var tablee = [];
        var i = 0;

        if (action == '/battery') {
            var battery = new ev3dev.PowerSupply();
            var str = '';

            if (battery.connected) {
                str += '  Technology: ' + battery.technology + '\n';
                str += '  Type: ' + battery.type + '\n';
                str += '  Current (microamps): ' + battery.measuredCurrent + '\n';
                str += '  Current (amps): ' + battery.currentAmps + '\n';
                str += '  Voltage (microvolts): ' + battery.measuredVoltage + '\n';
                str += '  Voltage (volts): ' + battery.voltageVolts + '\n';
                str += '  Max voltage (microvolts): ' + battery.maxVoltage + '\n';
                str += '  Min voltage (microvolts): ' + battery.minVoltage + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'PowerSupply',
                    technology: battery.technology,
                    type: battery.type,
                    measuredCurrent: battery.measuredCurrent,
                    currentAmps: battery.currentAmps,
                    measuredVoltage: battery.measuredVoltage,
                    voltageVolts: battery.voltageVolts,
                    maxVoltage: battery.maxVoltage,
                    minVoltage: battery.minVoltage
                });
                i++;
            }
            else {
                str = '  Battery not connected!';
                tablee[i] = JSON.stringify({
                    Sensor: 'PowerSupply',
                    error: 'not connected'
                });
                i++
            }
            sendmsg(JSON.stringify({
                tablee
            }));
            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.end(str + JSON.stringify(battery));
        } else if (action == '/sensor') {
            var touchSensor = new ev3dev.TouchSensor();
            var colorsensor = new ev3dev.ColorSensor();
            var UltrasonicSensor = new ev3dev.LightSensor();
            var InfraredSensor = new ev3dev.InfraredSensor();
            var SoundSensor = new ev3dev.SoundSensor();
            var str = '';

            if (touchSensor.connected) {
                str += 'touch pressed: ' + touchSensor.isPressed + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'TouchSensor',
                    isPressed: touchSensor.isPressed
                });
                i++;
            }
            if (colorsensor.connected) {
                str += 'color sensor: \n   reflectedLightIntensity: ' + colorsensor.reflectedLightIntensity + '\n';
                str += '   ambientLightIntensity: ' + colorsensor.ambientLightIntensity + '\n';
                str += '   color: ' + colorsensor.color + '\n';
                str += '   red: ' + colorsensor.red + '\n';
                str += '   green: ' + colorsensor.green + '\n';
                str += '   blue: ' + colorsensor.blue + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'ColorSensor',
                    reflectedLightIntensity: colorsensor.reflectedLightIntensity,
                    ambientLightIntensity: colorsensor.ambientLightIntensity,
                    color: colorsensor.color,
                    red: colorsensor.red,
                    green: colorsensor.green,
                    blue: colorsensor.blue
                });
                i++;
            }
            if (UltrasonicSensor.connected) {
                str += 'UltrasonicSensor: \n   distanceCentimeters' + UltrasonicSensor.distanceCentimeters + '\n';
                str += '   otherSensorPresent: ' + UltrasonicSensor.otherSensorPresent + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'UltrasonicSensor',
                    distanceCentimeters: UltrasonicSensor.reflectedLightIntensity,
                    otherSensorPresent: UltrasonicSensor.ambientLightIntensity
                });
                i++;
            }
            if (InfraredSensor.connected) {
                str += 'InfraredSensor: \n   proximity: ' + InfraredSensor.proximity + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'InfraredSensor',
                    proximity: InfraredSensor.proximity
                });
                i++;
            }
            if (SoundSensor.connected) {
                str += 'SoundSensor: \n   soundPressure: ' + SoundSensor.soundPressure + '\n';
                str += '   soundPressureLow: ' + SoundSensor.soundPressureLow + '\n';
                tablee[i] = JSON.stringify({
                    Sensor: 'SoundSensor',
                    soundPressure: SoundSensor.soundPressure,
                    soundPressureLow: SoundSensor.soundPressureLow
                });
                i++;
            }
            sendmsg(JSON.stringify({
                    tablee
                }));

            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.end(str);
        } else
    {
            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.end('Try /battery /sensor \n');
        }
    }).listen(port);

    function sendmsg(data) {
        var client = device.Client.fromConnectionString(connectionString);
        var message = new device.Message(data);
        message.properties.add('NXTsensors', 'sensorData');
        console.log("Sending message: " + message.getData());
        client.sendEvent(message, printResultFor('send'));
    }

    function printResultFor(op) {
        return function printResult(err, res) {
                 if (err) console.log(op + ' error: ' + err.toString());
                 if (res && (res.statusCode !== 204)) console.log(op + ' status: ' + res.statusCode + ' ' + res.statusMessage);
           
        };
    }

    The code is quite simple, I do return, as text, in the page, the sensors and their states. At the same time I’m building a JSON table containing the sensors data.

    The function sendmsg is sending the data to Azure IoT Hub. This part of the code coming from the sample code you can find in the excellent SDK (it’s the simple http one). In the code, replace the XXX in the connection string by your IoµT Hub, make sure the name of your device is NXT or replace NXT by the name of device you want to use. Don’t forget to create a device before if needed. And of course, the primary key of the device.

    Now, compile everything, deploy the server.js file to the device, run “node server.js” and wait 30 seconds. In a browser, you can now access the NXT brick on port 1337 like http://ipaddress:1337/ (replace ipaddress by the IP address of your ev3). It will return: Try /battery /sensor

    the /sensor returns in my case:

    image

    /battery returns:

    image

    On the device side, you can check that the data has been sent:

    image

    What about Azure IoT Hub? Well, just use the Device Explorer (see my previous article on where to get it and how to use it). Run the Device Explorer and connect it to your IoT Hub before trying /sensor and /battery.

    image

    And voilà Sourire we made it Sourire The Lego Mindstorms ev3 is now connected to Azure IoT Hub using node.js and able to send data to Azure.

    Bottom line: when you have sources of and SDK, don’t be afraid to adapt, change and modify what you need for your own platform. I did it with the Azure IoT SDK because my node.js version was too old to fully run it. And I did it with the ev3dev package as it was outdated and there were couple of bugs in it.

    As always, feedbacks welcome!

  • Laurent Ellerbach

    How to deploy a node.js site into Azure Web App to create a Website

    • 0 Comments

    There is quite a good documentation on the Azure website on how to deploy an Azure WebApp when using node.js and Git repository from a Mac. Documentation is here.

    I wanted to deploy the code thru FTP and deploy selected part of my project. My idea is to deploy the Device Explorer site I’ve build in my previous post. So for the rest of the article, I will assume you’ve already created a node.js website using either the simple http listener, either an express, jade template like in my example.

    Step 1: create an Azure WebApp

    I’ll assume you already have an Azure subscription. If you don’t, just open a trial account, it’s quite fast and easy. You’ll need your credit card but it won’t be used. It’s just in case you’ll go over the free costs. And in my example, as I don’t need this website to be all the time online and I will only use it sporadically, I’ll use free hosting.

    In the Azure portal, select New, then Web + Mobile, then Web App

    image

    Then create the site, the App Service Name is the name of your site.

    image

    Allow couple of minutes for it to be created.

    Step 2: allow FTP uploads in the WebApp

    Now, you need to allow FTP deployment.

    In the portal, select your newly created site, go into All Settings then deployment credentials, add a username and a password.

    image

    Once done, you’ll get this in the summary view. The blue boxes include information to connect to the ftp deployment with the user name to use.

    image

    Now, you can check that you have access using any FTP tool like the Explorer in Windows or anything else.

    Step 3: creating the web.config file

    Now, we have to go into the project and add a web.config file. This is necessary to be able to have the node.js project running correctly.

    Right click on your project, select Add then New Item

    image

    Just write web.config and click on OK

    image

    Then place this XML code into the web.config file:

    <?xml version="1.0" encoding="utf-8"?>

      <configuration>

        <system.webServer>        

          <handlers>

            <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>

          </handlers>

          <rewrite>

            <rules>

                <rule name="DynamicContent">

                     <match url="/*" />

                     <action type="Rewrite" url="app.js"/>

                </rule>

           </rules>

          </rewrite>

        </system.webServer>

      </configuration>

     

    Important part is on the <handlers> as it tells Azure Web App that it’s a node.js app and need to use the node engine. The path="app.js" tells which js file to use as startup.

    The other important part is the <rewrite> section as it will make sure the app.js page will be called as default page. If you don’t do that, you’ll have to access your site like http://mysite.net/app.js and you just want to access it like http://mysite.net/

    Step 4: deploying the files to the Web App

    To deploy, I’m simply using the Windows Explorer as an FTP client. For copying files, it’s just working perfectly. Connect to the FTP host, you’ll be ask for login and password, that’s the one you’ve created at step 2.

    image

    Once connected, you can go to the /site/wwroot directory for example. but it can be any directory, you’ll just need to set it up in the Web App properties. In order to make it simple and as it’s the default directly, I’ll use this one.

    Now, very important, copy all the files and directories that do contains the code you’ve produced. Make sure you deploy the newly created web.config, your package.json. Do not deploy the node_modules directory.

    Once compiled with Visual Studio, if you’re creating your site using TypeScript, it will create the js file. You don’t need to deploy the ts file, you just need the JavaScript generated js files.

    image

    Step 5: making sure all node modules are installed

    Now you’ve copy all the right files, you can make sure the node.js packages are deployed. In theory, when you’ll run for the first time your app, packages will be downloaded and installed. It is better to do it manually by connecting to the Kudu console.

    So connect to http://nameofsite.scm.azurewebsites.net where nameofsite is the name of the site you’ve created, use the login and password you’ve created.

    Navigate to your main directory, select Debug console and CMD

    image

    In the console type “npm install”, this will install al your packages.

    This will create the node_modules directory and deploy all the packages in it:

    image

    Step 6: you’re good to go

    Now you can connect to your web site and test it! It will just work Sourire 

    And big thanks to Julien Corioliand for his help.

  • Laurent Ellerbach

    Creating an Azure IoT Device Explorer in node.js, express and jade

    • 0 Comments

    After playing a bit with Azure IoT hub and building a webcam system with a RaspberryPi 2 running Linux in my previous article, I’ve decided to continue developing a bit in node.js to build a simple equivalent of the Device Explorer but in node.js. I’m not a node.js expert so there may be more efficient way to write some of the code.

    Code is available on GitHub: https://github.com/Ellerbach/nodejs-webcam-azure-iot/tree/master/DeviceExplorer. The code on GitHub does include more than what is explained in this article.

    Setup the dev environement

    I’ll use Windows as my development environment. Visual Studio 2015 can support node.js development and debugging. So first I needed to setup Visual Studio 2015:

    • You can get for free Visual Studio Community 2015 here.
    • Then you need to the free node.js tools for Visual Studio, download them here.
    • And you need of course to install the node.js framework from here.

    Once those 3 steps done, you’re good to go!

    Creating a node.js project in Visual Studio

    This is quite straight forward, you have new project type which is node.js. I’ve created a Start Node.js Express 3 Application project. It does come with a simple MVC project containing couple of example pages which makes it easy to learn and understand how all is working.

    image

    Views are using jade. This is a part where I had quite some difficulties to use. I do recommend to read the Language Reference as well as the examples from http://jade-lang.com/. The most important is to keep in mind the indentation must be respected and this is what makes groups and makes all the code logic. When you got that, the rest is quite easy and really nice to use.

     

    It will create a full web site. I do recommend to create TypeScript file and not Javascript. this will allow to have a better support and maintainability over time. Javascript file are generated once compiled. And you can choose which version of Javascript you want to generate.

     

    Listing devices

    The idea is to have a page that will allow to enter the connection key:

    image

    Once the key is entered, it will list the devices plus their keys and couple of other info (in real, keys are displayed instead of the blue box):

    image

    So to build this, we will need to:

    • Create a view which is about adding a jade file in the views directory
    • Add a function to handle requests on the page
    • Add a route so the traffic will be correctly redirected to the page

    I will first explain how the principle of views and code is working.

    Adding the view

    Right click on Views then Add and New Items…

    image

    select jade file and create one call devices.jade. Let start by replacing what is generated by this code:

    extends layout

    block content
      h2 #{title}
      h3 #{message}

    div.connbox
      p please enter your connection string like HostName=XXX.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=XXX
      form(name="connection", action="/devices", method="post")
        input(type="text", name="constr")
        input(type="submit", value="Connect")

    In jade, it will create a page where the #{title} will be replaced by the default text rendering of the title object which will be given to the page. It can be a string or any object. This will allow to manipulate those objects and we will see later how to do that.

    The second part will create a form which will post the value of the text box to the page names /devices

    Adding a function to handle the code

    In the routes/index.js file, add this code:

    export function devices(req: express.Request, res: express.Response) {
        var cnxstr = req.body['constr'];
       res.render('devices', { title: 'Devices', message: 'this is the connection string: ‘ + cnxstr });
    };

    This simple code just find the value of ‘constr’ which has been send by the post form and ask to render the page by sending back the info in the message object. Now, we still don’t have everything as the function has not been declared as a route when the page /devices is called.

    Adding the route for a page

    Find the file app.ts and add the lines after the line “app.get(‘/’, routes.index);”

    app.get('/devices', routes.devices);
    app.post('/devices', routes.devices);

    Basically, the function we’ve just wrote is now linked to a get or post request on the /devices page.

    All together, if you click F5, go on the /devices pages, you’ll be able to fill the text box, send the data to the page and see what you’ve posted. This is a very basic example but it does allow to understand how express and jade are working together.

    Requesting Azure IoT Hub devices list in node.js

    You’ll need to add the ‘azure-iothub”"’ module.  Using Visual Studio, makes it super easy. Just right click on “npm” in your project then select “Install new npm Packages…”

    image

    This will open this window where you can search for the packages.

    image

     

    Here is the code to request all devices present in the IoT hub. It is quite straight forward as the node.js SDK is really nicely done:

    var iothub = require('azure-iothub');
    var cnxString = '';

    export function devices(req: express.Request, res: express.Response) {
        var cnxstr = req.body['constr'];
        if ((cnxstr != undefined) || (cnxString)) {
            if (cnxString) {
                if ((cnxstr == undefined)||(cnxstr ==''))
                    cnxstr = cnxString;
            }    
            var registry = iothub.Registry.fromConnectionString(cnxstr);
            registry.list(function (err, deviceList) {
                if (!err) {
                    cnxString = cnxstr;
                    res.render('devices', { title: 'Devices', year, message: 'Getting list of devices', devicelist: deviceList });
                } else
                    res.render('devices', { title: 'Devices', year, message: 'Error getting list of devices', devicelist: null });
            });
        } else
            res.render('devices', { title: 'Devices', year, message: 'Please give a valid connection key', devicelist: null });   
    };

    First part of the code is really here to check if a connection string has already been provided. If yes, it will reuse the connection string. The key 2 lines are:

             var registry = iothub.Registry.fromConnectionString(cnxstr);
            registry.list(function (err, deviceList) {

    this will return in devicelist an array of devices. If no error, then it is passed to the devices view. So very straight forward.

    Modifying the jade view to render devices list

    Go back to the devices.jade file and add the following code:

    div.text
    if(devicelist!=null)
      table
        thead
          tr
            th
              | DeviceId
            th
              | Prim Key
            th
              | Sec key
            th
              | Last upd
            th
              | Status
            th
              | Msg waiting
        tbody
          each device in devicelist
            tr
              td
                a(href='/devicedetail/' + device.deviceId) #{device.deviceId}
              td
                | #{device.authentication.SymmetricKey.primaryKey}
              td
                | #{device.authentication.SymmetricKey.secondaryKey}
              td
                | #{device.lastActivityTime}
              td
                | #{device.status}
              td
                | #{device.cloudToDeviceMessageCount}
      p
      a(href='/adddevice/') Add a new device

    This part took me quite a lot of time. The reason is jade and the way indentation is working. It is really super important to respect it and the alignment almost drives the behavior of what will be generated. the good news is that you can add code in the jade file like testing if you have a devicelist object. and do for each in the code. The code will generate a simple table which contains some of the device properties. I’ve created as well a page for details as well as a page to add a new device.

    This code will render exactly as in the screen capture from the first part of this article. Now, let see how to generate the detailed page for devices as well as creating a new device.

    Listing devices properties

    Similar to the previous part, add a devicedetail jade, here is the code very similar to the previous page to generate the details in a table:

    extends layout

    block content
      h2 #{title}
      p #{message}

      div.text
      if(device!=null)
        table
          thead
            tr
              th
                | Details
              th
                | Values
          tbody
              tr
                td
                  | Device Id
                td
                  | #{device.deviceId}
              tr
                td
                  | Primary Key
                td
                  | #{device.authentication.SymmetricKey.primaryKey}
              tr
                td
                  | Secondary Key
                td
                  | #{device.authentication.SymmetricKey.secondaryKey}
              tr
                td
                  | Last Activity
                td
                  | #{device.lastActivityTime}
              tr
                td
                  | Generation Id
                td
                  | #{device.generationId}
              tr
                td
                  | Messages waiting
                td
                  | #{device.cloudToDeviceMessageCount}
              tr
                td
                  | etag
                td
                  | #{device.etag}
              tr
                td
                  | Status
                td
                  | #{device.status}
              tr
                td
                  | Status Reason
                td
                  | #{device.statusReason}
              tr
                td
                  | Connection State
                td
                  | #{device.connectionState}
              tr
                td
                  | connectionStateUpdatedTime
                td
                  | #{device.connectionStateUpdatedTime}
              tr
                td
                  | statusUpdatedTime
                td
                  | #{device.statusUpdatedTime}

    We’ll need to create a function that will handle the request and return the device object:

    export function devicedetail(req: express.Request, res: express.Response) {
        var devId = req.params.deviceId;
        var strcnx = getHostName(cnxString);
        if (strcnx == '')
            res.render('devicedetail', { title: 'Device detail', year, message: 'Error getting device details. Connection string was: ' + cnxString + ' and deviceId: ' + devId });
        strcnx += ';DeviceId=' + devId;
        var registry = iothub.Registry.fromConnectionString(cnxString);
        var msg = 'No device found';
        registry.get(devId, function (err, device) {
            if (!err) {
                strcnx += ';SharedAccessKey=' + device.authentication.SymmetricKey.primaryKey;
                res.render('devicedetail', { title: 'Device detail', year, message: 'Those are the device details. Connection string: ' + strcnx, device: device });
            } else
                res.render('devicedetail', { title: 'Device detail', year, message: 'Error connecting' });
        });
    };

    function getHostName(str)
    {
        var txtchain = str.split(';');
        for (var strx in txtchain) {
            var txtbuck = txtchain[strx].split('=')
            if (txtbuck[0].toLowerCase() == 'hostname')
                return txtchain[strx];
        }
        return '';
    }

    As you’ll see in the jade page, the link to the page is /devicedeatil/name_of_a_device. In order to catch it, we’ll need to declare it in the route and it will allow to have it thru the req.params function. I will name it deviceid. so add this line in the app.ts file:

    app.get('/devicedetail/:deviceId', routes.devicedetail);

    First part of the code is about getting the list of devices and making sure the device exists. then it’s about getting the device and returning it. As sometimes you need the device connection string, this string is built and returned as well. As a result, you’ll get a detailed page like:

    image

    Those properties are the ones available for every device. The status shows is the device is allow or not to connect. If not, you’ll have a reason (128 bit max) displayed in the Status reason. If you send messages to your device, you’ll see as well if messages are waiting.

    Adding a device to Azure IoT hub

    Very similar to the previous part, we’ll just add a jade file adddevice. Here is the code:

    extends layout

    block content
      h2 #{title}
      p #{message}

      div.connbox
      if(deviceId==null)
        p please enter your device name
        form(name="adddevice", action="/adddevice", method="post")
          input(type="text", name="deviceId")
          input(type="submit", value="Add")

    Simple code, very similar to the first example. For the main function code, it’s quite easy as well:

    export function adddevice(req: express.Request, res: express.Response) {
        var devId = req.params.deviceId;
        if (devId == undefined) {
            devId = req.body['deviceId'];
            if (devId == undefined)
                res.render('adddevice', { title: 'Add device', year, message: 'Error, no device ID' });
        }
        if (cnxString == '')
            res.render('adddevice', { title: 'Add device', year, message: 'Error, no connection string' });
        else {
            var registry = iothub.Registry.fromConnectionString(cnxString);
            //create a new device
            var device = new iothub.Device(null);
            device.deviceId = devId;
            registry.create(device, function (err, deviceInfo, response) {
                if (err)
                    res.render('adddevice', { title: 'Add device', year, message: 'Error, creating device' + err.toString() })
                else
                    if (deviceInfo)
                        res.render('adddevice', { title: 'Add device', year, message: 'Device created ' + JSON.stringify(deviceInfo) });
                    else
                        res.render('adddevice', { title: 'Add device', year, message: 'Unknown error creating device ' + devId });
            });
        } 
    }

    All up, first part of the code is just to check if there is a device name either thru get as a param, either thru post. Second part is about adding a device:

             var registry = iothub.Registry.fromConnectionString(cnxString);
            //create a new device
            var device = new iothub.Device(null);
            device.deviceId = devId;
            registry.create(device, function (err, deviceInfo, response) {

    Again, very simple, very straight forward. we’re connecting to the Azure IoT Hub registry, then create an empty device, set the deviceId name and ask for creation.

    Don’t forget to add the route as well. the “?” in “/adddevice/:deviceId?” is to make the param as optional.

    app.get('/adddevice/:deviceId?', routes.adddevice);
    app.post('/adddevice', routes.adddevice);

    Once created, if there is no error, the device is sent back. I’m just converting it into a JSON and display it in the message:

    image

    You can do a very similar code to delete a device. You can as well send a message to the device and monitor the results. I have to say the Azure IoT SDK in node.js is really great and working perfectly. And please note that this website can be deployed on Azure, Windows or Linux or anything else that can run one of the latest node.js version (see restriction in the Azure Iot SDKs on GitHub here)

    More examples on my GitHub! Enjoy and feedback welcome.

  • Laurent Ellerbach

    Azure IoT Hub uploading a Webcam picture in an Azure blob with node.js on Windows and Linux

    • 0 Comments

    In my garden, I have couple of sensors and a greenhouse. In order to play with different technologies I’m using a RaspberryPI v1 (RPI) under Linux with an Atmel328 (same as in Arduino) for analogic data. And I wanted to test the new Azure IoT Hub. As I didn’t know anything on node.js, I‘ve decided to go for node.js as well using this RPI under Linux. So many new technologies for me Sourire As I just wrote, I’m not a node.js king neither a Linux expert, just a beginner but I want to share my experience. And there may be best ways to code all this, so feedbacks welcome.

    Setup the dev environement

    I’ll use Windows as my development environnement. Visual Studio 2015 can support node.js development and debugging. So first I needed to setup Visual Studio 2015. You can get for free Visual Studio Community 2015 here. Then you need to the free node.js tools for Visual Studio, download them here. And you need of course to install the node.js framework from here.

    And you’re all done for the dev side! so fast, so quick on the Windows side!

    Setup the RaspberryPI

    I have to say, this part was a bit more difficult for me. First I’m not a Linux guy, second, it’s not that easy to install node.js when you don’t really know what you need. So here are the steps to install the latest version. I have to say I’ve spend quite a lot of time trying to install the right version. The one by default in the raspian repository is not the latest version. Finally, after some time I found the way to properly install it from the Azure IoT SDK github page here. The version I’m running on the RPI is a Debian (wheezy). So if you’re running the Jessie or Wheezy version, to get node.js installed correctly, here are the steps:

    sudo apt-get install curl
    sudo curl --silent --location https://deb.nodesource.com/setup_0.12 | bash –
    sudo apt-get install --yes nodejs

    Most likely because of previous/bad versions installed before it didn’t worked right away. After a reboot, it did finally (who said, no reboot is necessary in Linux? Sourire). This will install as well “npm” which will allow later to download some needed module packages.

    Creating a node.js project in Visual Studio

    this is quite straight forward, you have new project type which is node.js. I’ve created a Web project (this will allow easily to test couple of functions)

    image

    It will create a server.ts file. I do recommend to create TypeScript file and not Javascript. this will allow to have a better support and maintainability over time. Javascript file are generated once compiled. And you can choose which version of Javascript you want to generate.

    You can hit F5 and run your code Sourire by default, it will create a project with a simple web server, you can try to debug, put break point. Quite magic Sourire

    How to run all this on Raspberry?

    the only 2 files you’ll need are the “server.js” and the “package.json” one. this second one contains the dependencies with other modules. So far, no real dependencies. It’s needed as well if you want to publish and share back your code as a package. So you’ll make sure that you’ll get all the right dependencies for your package and that it will always work.

    to lunch the server, just type:

    node server.js

    The server will run as long as you close the console or stop it. Access it thru a browser with http://ipaddress:port/ the ipaddress is the ip address of the RPI and the port, the port you’ve chosen in your code.

    If at this point it does not work, then it’s maybe because you have a node.js problem. Try to type “node –v”, this is supposed to give you the installed version. The one I’ve installed is v0.12.7

    Installing packages on dev environment

    With node.js you’ll need to install couple of packages. Both on the dev environment and on the production environment.

    Using Visual Studio, makes it super easy. Just right click on “npm” in your project then select “Install new npm Packages…”

    image

    This will open this window where you can search for the packages.

    image

    We’ll need the following ones: “azure” and “azure-iot-device”. So install them. this will install all the packages and dependencies needed. It makes it very easy to see what are the available versions, if a package is already installed locally.

    Installing packages on the RPI

    it’s about the same except it’s in command line. Thanks to the npm utility in Visual Studio, I know the packages I’ll need to install Sourire 

    sudo npm install azure
    sudo npm install azure-iot-device

    Note: if you need a specific version, you can install it by giving the version like: sudo npm install azure-iot-device@1.0.0-preview5

    Connecting a device to Azure IoT Hub

    The first thing to do is to create an Azure IoT Hub. Step by step here. This is great step by step, very easy. I do recommend to do all the tutorial and create all the codes in C#. This will allow you to understand how Azure IoT Hub is working. You’ll need this to continue this article.

    The most important is to create at least 1 device identity. If you don’t, you will not be able to access the IoT Hub, send and receive messages.

    The other tool I recommend you to use is part of the Azure IoT SDK and named DeviceExplorer. Follow the tutorial to create a device, launch this tool, put the connection string from your Azure IoT Hub in it (connection string look like: HostName=youriothub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=thekey), and you should be able to see the device:

    image

    From here, you’ll need for later the device ConnectionString. this connection string is different from the main hub connection string! it is formed the same way as the previous one but is contains DeviceId (here calle LaurelleRPI) which is the name of your device instead of SharedAccessKeyName.

    HostName=youriothub.azure-devices.net;DeviceId=LaurelleRPI;SharedAccessKey=thedevicekey

    In the code, you’ll need this to get connected to the IoT Hub:

    var device = require('azure-iot-device');
    // must match the deviceID in the connection string
    var connectionstring = HostName=youriothub.azure-devices.net;DeviceId=LaurelleRPI;SharedAccessKey=thedevicekeyI';
    var iotHubClient = new device.Client(connectionstring, new device.Https());

    And yes, that’s all what you need! How to test it? We can modify a bit the web server code to have a function which will send data to the IoT Hub. We will check with the Device Explorer if data arrive. So add this to the previous code and change the server function.

    var url = require('url');
    var deviceID = 'LaurelleRPI';
    var port = process.env.port || 1337

    http.createServer(function (req, res) {
        var request = url.parse(req.url, true);
        var action = request.pathname;

         if (action == '/senddata') {
            var payload = '{\"deviceid\":\"' + deviceID + '\",\"wind\":42 }';
            var message = new device.Message(payload);
            iotHubClient.sendEvent(message, function (err, res) {
                if (!err) {
                    if (res && (res.statusCode !== 204)) console.log('send status: ' + res.statusCode + ' ' + res.statusMessage);
                }
                else
                    console.log('no data send, error ' + err.toString());
            });
            res.end('data sent');
        } else {
            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.end('Linux RPI working perfectly, try /status /postimage /senddata and /image.jpg \n');
        }

    }).listen(port);

     

    Run the code and access to the http://ipaddress:1337/senddata

    this will post data to the Azure IoT Hub and you’ll see them in the Device Explorer, You’ll need to monitor the device you’ve just created and you’ll use in your node.js code. At this point, you should see this:

    image

    if not, you’ll most likely get an error message. Most of the error I got was because I made a mistake in the connection string or in my Typescript.

    Now, the next step is to be able to receive messages from the IoT Hub. In order to see if we have received a message or not, we’ll need to listen all the time to check is a message is arrived. The function bellow check is a message is arrived and place it into messageFromIoTHub

    var messageFromIoTHub = "";

    function pushtoblob()
    { //do nothing

    }

    function isMessage()
    {
        iotHubClient.receive(function (err, res, msg) {
            if (!err && res.statusCode !== 204) {
                console.log('Received data: ' + msg.getData());
                // process the request
                messageFromIoTHub = msg.getData();
                if (messageFromIoTHub === "picture")
                    pushtoblob();
                iotHubClient.complete(msg, function (err, res) {
                    if (err) console.log('complete error: ' + err.toString());
                    if (res && (res.statusCode !== 204)) console.log('complete status: ' + res.statusCode + ' ' + res.statusMessage);              
                });
                return true;
            } else if (err) {
                console.log('receive error: ' + err.toString());
            }
        });
        return false;
    }

    var isWaiting = false;
    function waitForMessages() {
        isWaiting = true;
        isMessage()
        isWaiting = false;
    }

    // Start messages listener
    setInterval(function () { if (!isWaiting) waitForMessages(); }, 1000);

    The hub will be checked every second. Up to this point, you can test the code. Place a break point on the line “messageFromIoTHub = msg.getData();” and run the code.

    From Device Explorer, send the message “picture”, if all geos well, you’ll receive back a feedback.

    image

    In terms of code, it’s quite straight forward, the Azure SDK IoT is very well done and easy to use. Most of the function to access the IoT hub are super well designed and only 1 or 2 functions are needed.

    Uploading the picture to an Azure Blob storage

    As for the previous part, you’ll need a storage setup. Follow the very well done step by step here. You’ll need to go thru to continue this code.

    Blob storage are very easy as well to access in node.js. They can get access like http://mystorageaccount.blob.core.windows.net/mycontainer/myblob.

    In terms of code, as for the IoT Hub, you need the connection string and create the access to the blob service.

    var azure = require('azure');

    //need to change AccountName and AccountKey
    var connectionblob = 'DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=thelongkey';
    var blobSvc = azure.createBlobService(connectionblob);

    From the previous code, replace

    function pushtoblob()
    {
        blobSvc.createContainerIfNotExists('webcam', function (error, result, response) {
            if (!error) {
                // Container exists
                if (result == true)
                    console.log('blob created');
                else
                    console.log('blob existing');
            }
        });
        //create the picture named image.jpg
        //webcam.run();  
        // this will upload the picture named image.jpg which needs to be in the same directory as the js file
        blobSvc.createBlockBlobFromLocalFile('webcam', 'picture', 'image.jpg', function (error, result, response) {
            if (!error) {
                // file uploaded
                console.log('file uploaded :-) ');
            } else {
                console.log('error uploading picture');
            }
        });
    }

    the code is very straight forward as well. I do create a blob called “webcam” and upload an existing picture from the disk called “image.jpg”. We’ll see right after how to generate this picture from the webcam. In the meantime, make sure you add a picture called “image.jpg” in the same directory as your server.js file.

    You can as well add the following code into the ‘'”http.createServer” function, after the “action =”.

    if (action == '/image.jpg') {
            //webcam.run();
            var img = fs.readFileSync('./image.jpg');
            res.writeHead(200, { 'Content-Type': 'image/jpeg' });
            res.end(img, 'binary');
        }

    Hit F5 in VS and test the code by accessing http://ipaddress:1337/image.jpg

    You should be able to see the picture you’ve placed in the folder. now, go back to the Device Explorer, and send again the “picture” message. You will see a confirmation in the Device Explorer that the message has been received.

    So how to check if the blob picture has been uploaded correctly?

    One of the way is to check in the Azure portal is the container has been created. And you can change the properties to make it public for example, so you can directly from the browser check your image. You’ll access it like https://yourblobstorage.blob.core.windows.net/webcam/picture

    image

    You can use as well the excellent CloudBerry Explorer for Azure which you can download from here. Once setup (see my previous post where I show how to move one Azure VM from one subscription to another to have more info on how to setup this tool). If all went correctly, you should be able to see the “webcam” container created

    image

    if you double click in it, you’ll see the picture blob:

    image

    and if you double click on the “picture” blob, you can open it and download your picture.

    Saving a picture from the webcam

    I have to say, I did quite a lot of research to see if there is an efficient way to do this on a RPI. And there are not that many nice, easy and straight forward way to do it. First, not all webcam are supported. I had to check couple of webcams to find an old Lifecam-VX-6000 supported.

    I finally went for some command line tool that generate images from the webcam entry (/dev/video0 for the default webcam). I’ve installed fswebcam

    sudo apt-get install fswebcam

    To create an image, just run “fswebcam image.jpg” and it will generate an image. Worked quite well on my Raspberry.

    Then I’ve created in my project a webcam.ts file and put the following code:

    'use strict'

    var child = require('child_process');

    module.exports = {
        run: function () {
            child.execSync('fswebcam image.jpg', function (err, stdout, stderr) {
                if (err !== null) {
                    console.log('error ' + err);
                } else
                {
                    console.log('image saved');
                }
            }); 
        }
    };

    and in the main server.js file:

    var webcam = require('./webcam');

    You can un comment the 2 lines you’ll find with this “//webcam.run();” This code just create the image from the webcam. Now, if you test this code on Windows, it will not create the image as the command “fswebcam” does not exist. But it will work on the RPI where you’ve install the command. Note that you need to use execSync to create the external process. It will wait for the external process to finish before continuing. If you're using the normal exec, the picture won't have time to be saved before the next steps.

    Deployment on the Raspberry and all together

    You’ll need to deploy only the 3 files: “server.js”, “webcam.js” and “package.json”. Use the method you want (I personnaly use the old smb way, so installing and setting up smb…). I have to say I like as well to access my Raspberry with Remote Desktop. Just install “xrdp”  and you’re good to go for this Sourire

    And voilà, you can get it from your blob storage:

    For sure all this can be improved, especially with the webcam part.

    <edit> I've placed the webcam on the RPI which is in the garden and the picture can be found here. </edit>

    Conclusion

    Visual Studio 2015 + node.js tools + Azure IoT Hub + Azure Storage = happiness

    So easy to develop, so easy to debug, so easy to find the right module with the npm tools in VS. All the Azure resources are really easy and simple to use with node.js.

    The great news is that my solution is working the same way under Windows and Linux. Only difference is about the webcam part but I really didn’t need it on the Windows side.

    I’ve learned a lot in this project and will for sure continue it!

    https://portalvhdskb2vtjmyg3mg.blob.core.windows.net/webcam/picture

  • Laurent Ellerbach

    How to move Azure VM between subscriptions

    • 7 Comments

    I recently needed to move an Azure Virtual Machine from one subscription to another one. I read a LOT on how to do that and it looks super complicated. At the end of the day, I found an easy 3 steps way to make it, so sharing on this blog Sourire

    Step 1: Move the Azure VM from one Blob storage to another one

    In Microsoft Azure, when you have a blob  storage, it can be attached only to 1 subscription. You can of course have multiple storage attached into a subscription. So first step is to move the VHD which is used by the VM.

    For this, I used the excellent CloudBerry Explorer for Azure which you can download for free here. After installation, just register for free and you’re good to go.

    You’ll need to add your 2 blob storage, the one you want to move the VHD from and the one you want to move the VHD to.

    image

    To find the name of the storage and the key, just go into the Azure management console and select Manage Access Keys, you’ll get the info you need to setup both accounts.

    image

    Once setup, you can now have a view like this:

    image

    Stop your VM and you’re good to copy/paste your VM from one storage to another.

    Step 2: Create a Disk from VHD

    In the management console, go to Virtual Machine then Disks

    image

    then Create

    image

    fill a name, select the VHD from the storage you just moved your VHD file to.

    image

    Step 3: Create the VM from the Disk

    Go to the Virtual Machine instances

    image

    then create a New

    image

    select From Gallery

    image

    and go to Disks to select the disk you just created.

    image

    And you’re good to go to run your VM!

  • Laurent Ellerbach

    ESP8266 and Posting on Windows Azure using Mobile Services

    • 0 Comments

    The ESP8266 is a nice, cheap wifi board which does include multiple GPIO, SPI, I2C. It is usually integrated in small boards which does contains 2 GPIO + 1 UART for the basic 01 version.

    You can find them for few $, usually around 3$. It’s a low consumption board which can be powered by a battery and charged with a solar panel without draining the battery too fast when not connected tot eh wifi. So it makes it an interesting element for Internet of Things (IoT).

    As there is an UART, you can as well use it as a super cheap wifi board for Arduino, Netduino and other boards like this. Full documentation for AT commands is here.

    The other great news is that the Arduino community has been working on making a compatible version so you can use the wiring framework to program those boards. You can follow the steps here to install the components in the Arduino IDE. More info here.

    And as good news are not coming alone, if like me, you’re a use fan of Visual Studio, you can even use the excellent Visual Micro complement to program it. And more info on Visual Micro and ESP8266 support here.

    The most complicated part with this board is that it is only 3.3V, so be careful when alimenting it. I did a mistake and used 5V for couple of hours, the module is still alive but made the board reboot any time I wanted to drive a bit of current out of a GPIO. And that’s how when controlling I figured it out! To flash the board, you must put the GPIO0 to ground and reset the board. The UART is 5V tolerant so if you have an FTDI cable or equivalent which you can use to upload the code, make sure you will use only the Ground, RX and TX from the FTDI. Alimentation have to be 3.3V for the board. And once flashed, don’t forget to remove the GPIO0 from the ground.

    And as I explained in this previous post with the Spark.io (renamed Particle.io), you can then directly post to Azure Mobile Services with this 3$ chip! I’m planning to use it to measure temperature and humidity plus level of water from my cellar (I have to manually empty a bucket of water from my cooling system). And this 3$ chip plus 1 DHT11/22 plus a water level sensor (using the digital output only as no analog input) will be perfect! And I’ll still need to see if I can use the UART as something else.

    Enjoy :-)

  • Laurent Ellerbach

    Create a DHT11 C library using WiringPI on RaspberryPI and use it in Mono C#

    • 0 Comments

    I’m using various boards like RaspberryPI (1 and 2) as well as Arduino and Netduino. I have to say I’m a big fan of C#, so I try to use C# as much as I can. Based on the excellent WiringPI framework, I’ve ported equivalent of .NET Microframework classes to RaspberryPI (see previous post here, code not fully updated with latest Wiring PI version, just need to find some time to update it). All is using Mono on Linux.

    But as .NET is a managed code runtime, there are some limitations when it come to do some very short timing operations as managed code can’t guaranty those easily. In order to have those operations working, you’ll need to build some native C code. But wait, I know you’re a fan of C# like me and you don’t want to build your full project in C! It’s like the old time when we had the excellent Visual Basic to build all the graphical interfaces, fast development for anything related to interfaces but lack of performance. At this time, most of the code were don in a C/C++ dll and imported with the famous dllimport into the VB code.

    Well, we will do exactly the same here but replacing the old VB by the modern C# and for anything that need to be hard real time, we’ll put that into a C dll and import it into C#. But wait, here, our RaspberryPI is running Linux. So how to make that possible? In Linux as well, there are equivalent of dll, those are called library. They start with lib and have .so extensions. They are the exact same as dll but in the Linux world with the same advantages and same weaknesses.

    Then, let start by the library first. I wanted to find some code that already worked with the WiringPI framework as I’m using it (see this article on how to install it on a RaspberryPI). And I fond this great example which was almost what I wanted. I just needed to adapt it a bit to make it working.

    So I created (using my preferred tool Visual Studio), a normal C++ project. My main idea is to use it as an editor. I need 2 files: the library header and the core code. Both are important, and if you want to build a library you must have both. Let start with the header (called DHT11library.h in my code), here is the code:

    #ifndef _DHTLIB

    #define _DHTLIB

     

    #ifdef __cplusplus

    extern "C" {

    #endif

           extern bool InitDHT(int pinval);

           extern float getTemp();

           extern float getHumidity();

           extern bool dht11_read_val();

    #ifdef __cplusplus

    }

    #endif

     

    #endif

     

    What is the most important here is to have the entry point of the library properly declared in this header with extern. Now, the rest of the code in the file DHT11library.cpp:

    #include <wiringPi.h> 

    #include <stdio.h>

    #include <stdlib.h>

    #include <stdint.h>

    #include "DHT11library.h"

    #define MAX_TIME 85

    int DHT11PIN;

    int dht11_val[5] = { 0, 0, 0, 0, 0 };

    bool isinit = false;

     

    bool InitDHT(int pinval)

    {

           if (wiringPiSetup() == -1)

           {

                  isinit = false;

                  return isinit;

           }

           DHT11PIN = pinval;

           // initialize pin

     

           isinit = true;

           return isinit;

    }

     

    float getTemp()

    {

           return (float)(dht11_val[2] + dht11_val[3] / 10);

    }

     

    float getHumidity()

    {

           return (float)(dht11_val[0] + dht11_val[1] / 10);

    }

     

    bool dht11_read_val()

    {

           if (!isinit)

                  return false;

           uint8_t lststate = HIGH;

           uint8_t counter = 0;

           uint8_t j = 0, i;

           float farenheit;

           for (i = 0; i < 5; i++)

                  dht11_val[i] = 0;

           pinMode(DHT11PIN, OUTPUT);

           digitalWrite(DHT11PIN, LOW);

           delay(18);

           digitalWrite(DHT11PIN, HIGH);

           delayMicroseconds(40);

           pinMode(DHT11PIN, INPUT);

           for (i = 0; i < MAX_TIME; i++)

           {

                  counter = 0;

                  while (digitalRead(DHT11PIN) == lststate){

                         counter++;

                         delayMicroseconds(1);

                         if (counter == 255)

                               break;

                  }

                  lststate = digitalRead(DHT11PIN);

                  if (counter == 255)

                         break;

                  // top 3 transistions are ignored 

                  if ((i >= 4) && (i % 2 == 0)){

                         dht11_val[j / 8] <<= 1;

                         if (counter>16)

                               dht11_val[j / 8] |= 1;

                         j++;

                  }

           }

           // verify cheksum and print the verified data 

           if ((j >= 40) && (dht11_val[4] == ((dht11_val[0] + dht11_val[1] + dht11_val[2] + dht11_val[3]) & 0xFF)))

           {

                  if ((dht11_val[0] == 0) && (dht11_val[2] == 0))

                         return false;

                  return true;

           }

           return false;

    }

    As you can see, the code is very similar to the example code I found. I just added 3 functions to initialize the pin I’ll use and return both the temperature and humidity. Every time I’ll need to read the DHT11, I’ll call the dht11_read_val and then call both functions to return the temperature and humidity.

    Now, this is where you have to pay attention to make sure you’ll build the library correctly. You need to copy both file the DHT11library.cpp and .h into the Raspberry (I’m using a Samba share for this), then using PyuTTY or another way, connect to the Raspberry, go to the directory where you have placed the 2 files and compile the library:

    sudo gcc -o libDHT11.so DHT11library.cpp -L/usr/local/lib -lwiringPi –shared

    -o command will allow you to define the name of the library, on Linux, name need to start with lib and finish with so. At least that’s the only way I managed to make it working. I’m not a Linux expect so I may have miss something.

    -L and –lwinringPI reference the rest of the library. You must have compiled and installed the WiringPI framework before of course.

    -shared is the command to tell gcc to build a library

    If everything goes well, you’ll get your libDHT11.so compiled and ready to use. So let move to C# now. You can create a normal C# project using Visual Studio, .NET 4.0 type is recommended to work on Mono.

    I have created a class that embedded the C library and then allow to be used like a normal class:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.InteropServices;

    using System.Text;

    using RaspberryPiNETMF;

    using Microsoft.SPOT.Hardware;

    using System.Timers;

     

    namespace SerreManagement

    {

        public class NewDataArgs

        {

            public NewDataArgs(float temp, float hum)

            { Temperature = temp; Humidity = hum; }

            public float Temperature { get; private set; }

            public float Humidity { get; private set; }

        }

     

        class DHT11

        {

            //bool InitDHT(int pinval)

            [DllImport("libDHT11.so", EntryPoint = "InitDHT")]

            static extern bool InitDHT(int pinval);

            //float getTemp()

            [DllImport("libDHT11.so", EntryPoint = "getTemp")]

            static extern float getTemp();

            //float getHumidity()

            [DllImport("libDHT11.so", EntryPoint = "getHumidity")]

            static extern float getHumidity();

            //bool dht11_read_val()

            [DllImport("libDHT11.so", EntryPoint = "dht11_read_val")]

            static extern bool dht11_read_val();

     

            // private values

            private Cpu.Pin mPin;

            private int mSec;

            private Timer mTimer = new Timer();

     

            public delegate void NewData(object sender, NewDataArgs e);

            public event NewData EventNewData;

     

            // to get temperature and humidity

            public float Temperature { get; internal set; }

            public float Humidity { get; internal set; }

     

            public DHT11(Cpu.Pin pin, int seconds = 0)

            {

                mPin = pin;

                mSec = seconds;

                if (!InitDHT((int)pin))

                    throw new Exception("Error initalizing DHT11");

                mTimer.Elapsed += mTimer_Elapsed;

            }

     

            void mTimer_Elapsed(object sender, ElapsedEventArgs e)

            {

                if (dht11_read_val())

                {

                    Temperature = getTemp();

                    Humidity = getHumidity();

                    if (EventNewData != null)

                        EventNewData(this, new NewDataArgs(Temperature, Humidity));

                }

            }

     

            public void Start()

            {

                if (mSec != 0)

                {

                    mTimer.Interval = mSec * 1000;

                    mTimer.Start();

                }

            }

     

            public void Stop()

            {

                mTimer.Stop();

            }

     

            public bool ReadDHT11()

            {

                return (dht11_read_val());

            }

     

        }

    }

    The way to import the library is simple, it’s like for a Windows dll, it’s just the name changing. Be very careful if you build more complex library because the type conversion as well as pointers conversion may not be strait forward, you’ll need to use marshaling:

    //bool InitDHT(int pinval)

    [DllImport("libDHT11.so", EntryPoint = "InitDHT")]

    static extern bool InitDHT(int pinval);

    This is working with any kind of already existing Linux library, the only thing you need is the header file with the definition in order to find the right entry points as well as the exact types used.

    Then usage of the class is really simple:

    DHT11 mDHT;

    mDHT = new DHT11(Cpu.Pin.Pin_P1_18, 30);

    mDHT.EventNewData += mDHT_EventNewData;

    mDHT.Start();

    void mDHT_EventNewData(object sender, NewDataArgs e)

    {

        Console.WriteLine("Temp: " + e.Temperature + ", Hum: " + e.Humidity);

    }

     

    In our case, the DHT11 is linked to the Pin 18 on a Raspberry and the call for temperature and humidity is done every 30 seconds. The event is raised and the data can be read. You can then create an exe with Visual Studio, and run it on the RPI. Don’t forget to copy all the dll you’ll need as well as the libDHT11.so one. It is a dll so it has to be present with the exe. If not present, you’ll get an exception

    Results looks like:

    Temp: 23, Hum: 34

    The DHT11 is very simple and the temperature and humidity is not precise, you don’t have anything after the decimal point. the DHT22 is much better, it’s very easy to adapt this code to it, the difference with DHT11 is very tinny.

     

     

     

  • Laurent Ellerbach

    Internet of Things: installing Mono/Linux and Windows Embedded on a BeagleBone Black board (BBB)

    • 2 Comments

    The BeagleBone Black, also called Beagle Bone Black or BBB is an interesting board. It’s an AM335x 1GHz ARM Cortex-A8 with 512M of DDR3, 4GB of flash and does have an HDMI, USB client and host interface. But what I prefer are the 2 times 46 pins to have tons of IO available. It does reduce a bit the number if you are using HDMI. But still, it’s an interesting board. On the not as good aspect the analogic entries are only 1.8V max and not protected (so you need to add a 1.8V Zener diode to protect all analogic entries.

    clip_image001

    The board does natively run Linux on it on the embedded Flash. And you can flash it with another Linux or even Windows Embedded see here.

    I’m a C# guy (ok, sometimes I do some C++ too) so I like anything running C#. And I always try to find a way to develop using C# on any board. Sometimes it’s possible like for the RaspberyPi first version, see my blog post here. C” with either the full Microsoft .Net framework or the open source Mono one is a great way to be super productive in terms of development. Even in the embedded world if you don’t need hard code real time. And in the case you need some, then switch to C++ and create your own dll which you’ll be able to use later with C# for the rest.

    In this article, I’ll explain how to install a new Linux compatible to install Mono as well as a Windows Embedded image.

     

    The Linux way

    I’m not a Linux guy and I have to admit, I had to spend quite a lot of time for this step. I realized not all SD cards were compatible and I had no real way to check this on Linux before really booting on the SD. So make sure you have recent SD card and looks like it will work fine.

    Step 1: find a version that is BBB compatible

    After researches using Bing, I found a link explaining almost everything. The project was about developing using Mono including classes to access the GPIO. You’ll find the full site here. So I learned I need to install on the board an armhf version of Linux. The one present is just arm and Mono does not work on it.

    So after spending time figuring out that some of my SD card were not compatible while booting but compatible for flashing under Linux, I’ve finally found one recent compatible one. And to make sure the image was correct, I went for the Windows way of flashing. So I’ve downloaded an img file from here. The BB-ubuntu-14.04.2-console-armhf-2015-02_19-2gb.img.xz is working perfectly. And as recommended, I used the Win32DiskImager to flash the image. Other equivalent tools are also working perfectly.

    Once flashed, put the SD card into the SD card reader, press the button which is up to the SD card, plug the power and release the button. This will make the BBB boot on the SD card. If none of the blue led is flashing, then you’ve most likely have the same issue as me with none compatible SD card. Try with another one! If it’s blinking, then everything should be all good.

    You can easily test but using PuTTY or equivalent to connect to the board using SSH on port 22 with the IP address the board will get. If you don’t know or if you are not sure, just plug the board with the USB cable on your PC, use the address 192.168.7.2 to connect.

    If everything goes correctly, you’ll be able to log with “ubuntu” as username and “temppwd” as the password.

    clip_image003

    At this point, you can go to the next step. If you don’t have this screen, it does mean you’ve most likely miss something or your SD is not compatible.

    Step 2: installation Mono

    As explained in the RaspberyyPi first version article, steps are the same. Conect with PuTTY or equivalent, make sure this time, you’re plug to a network cable and have internet access.

    sudo apt-get update

    The password is the same as for the login so “temppwd”. Now, you are downloading all updates, it can take few minutes.

    sudo apt-get install mono-runtime

    this will get the mono runtime and will allow you to further develop using Visual Studio and C#. Careful as this image is just a console one. If you want to add a desktop, you need to install one. You can for example install Gnome like this:

    sudo apt-get install ubuntu-desktop

    Step 3: accessing the GPIO thru C#

    That’s where the first project I found is super useful as it’s a great, efficient implementation of OutpuPort and other well know classes from .Net Microrfamework (NETMF). You can download it directly from here. From this point, you can extract the source and directly use them in your project.

    You’re now ready to do a C# development using the BBB and some common NETMF classes J For the deployment, either copy your build, or use a network deployment or anything equivalent. I may write as well how to setup the debugger and environment to include a full debugger.

     

    The Windows Embedded way

    Step 1: prepare an SD Card with Windows Embedded

    Download the Windows image from Codeplex here. The download contains an example image.

    You’ll need to prepare your SD card. For this, use diskpart with an admin command prompt:

    clip_image005

    Then find your SD card, it should be easy to find with the size. Here it’s a 8Gb one, so it’s the Disk 2. Select it by typing

    Select disk 2

    You can then list the partitions with “List part” make sure there is just one partition, if it’s not the case, you may have done a mistake with your disk. If you are not sure, it’s better to exit the tool, format your SD card with Windows the normal way and make sure you just have one partition. Make sure the original partition is formatted in FAT32 or exFAT.

    Then select the particition with

    Select part 1

    Now the partition is selected, you need to make it active. This will allow the boot loader on the board to find the SD card and boot from there. This is done by using the command

    active

    clip_image007

    Exit from diskpart now. Now you need to copy the Windows Embedded files to the bootable SD card you’ve prepared. Launch the TI SDCard boot utility from the tools directory from the downloaded files. Select the MLO file. It will be copied first on the SDCard, then select the other elements so the EBOOTSD.nb0, the logo.bmp and NK.bin files.

    clip_image008

    Click proceed, skip the formatting of the SD card (should have been done previously and the partition is now active). One done, you’re ready!

    clip_image009

    Place the SD card in the SD card reader on the board, press the button which is up the SD card reader, plug the power and release the button.

    If everything went correctly, you are now running Windows Embedded Compact version on the BeagleBone Black.

    You can test by trying to find your board on the network. I’m using Advanced IP Scanner. If you’ve used the board previously, the IP address your DHCP server will give will be the same.

    clip_image010

    You can now access the board with Telnet or thru FTP. If you’ve done everything correctly, then you’ll be able to see this from the ftp address:

    clip_image012

    And from Telnet:

    clip_image014

    From here, you can now play a bit and try to connect remotely to the device with a tool called “Remote Display Control for Windows CE”. You’ll find the tool in the tools directory. Just type in the telnet session

    Cerdisp -c

    And launch the Host version on your desktop. You will see the board and you can now play with it.

    clip_image016

    Step 2: installing .NET and using GPIO

    OK, for this step, it’s a bit more complicated as it does require to rebuild totally a Windows CE image for this board. The platform elements are included in the BeagleBone WEC7 SDK file. Once you’ve installed the full Windows Embedded Compact 7, you then install the SDK. And you can create a project to include the .NET framework.

    I haven’t done it yet but I used Windows CE quite a long time ago and there is a lot of fun building your own OS with component. The god news is that Windows EC is free for individuals:

    Individual Non-Commercial Use. If you are an individual (that is, a natural person, and not an organization, business entity, or unit of government), then you may flash or download the Runtime Image to your device for your own Non-Commercial use.

    From full license here.

    Access to GPIO is then a bit of development as you’ll need to build a DLL to be able to do it or see if there is an existing one in the PAL.

    If you do an image, please let me know! Enjoy and have fun.

  • Laurent Ellerbach

    Servo motor tester in NETMF (.NET Micro Framework) with Netduino

    • 0 Comments

    I rencently bought new servo motor as I needed some to pilot new switches in my Lego train. The problem is I didn’t found the same as the previous one. Another problem is that I needed to replace one which didn’t worked properly.

    And here came the main issue: find the boundaries of those servo motors. There are databases online but my servo was not existing (or I didn’t found it). My new servo is a Motorcraft MC-410 sold by Connard. So I had to found the boundaries myself. Rather than testing fully blind, I’ve decided to make it in a flexible way. I have a Netduino Plus 2 available for my dev. I also developed a light WebServer. So why not build a simple webpage where I’ll be able to change the key settings of the servo and see the impact for real.

    In order to pilot correctly  servo motors, we need to use PWM. It’s quite straight forward to use for servo motors. It’s about having a period and a duration. This duration is from a minimum pulse to a maximum pulse. this will make move the servo motor from its base angle to its maximum angle.

    Usually servo are using a 20000 period and then the minimum pulse vary a lot around 800 microseconds and the maximum one around 2000, sometimes more. But it’s get quite complicated to find the right numbers especially because most of the time, the makers are not providing them as they are usually calibrated with a bit of analogic.

    I build a solution where I have a simple web page and I can change the settings of the servo motor and test them right away. The web page is super simple:

    image

    The way it’s working is quite simple, I start with some default settings, put the position at 50% and then try to change the min pulse to 700, then put 0 in the Position and see if it’s moving. If it’s moving, then the boundary is lower, if not, the boundary is higher. and the other way for the higher boundary. The period is usually 20K but some may be lower or higher. This allow also to test up to which value the servo still operate.

    And as a bonus, when the lower and higher boundaries are found, it does allow to measure the full angle the servo can give. In my case, close to 200° which does correspond to the spec.

    On the hardware side, it’s super simple, I’m using the pin D5 for the pilot cable to the servo, it’s a PWM on the Netduino. And then I use 5V and ground for the two other pins. The middle one in most servo is the +5V, the brown/black is the ground, the other one (can be yellow, white, orange, etc) is then the pilot one.

    Here is how it does looks like on my Lego test switch.

    WP_20150109_002

    In terms of software, I’m using my micro Web Server which I’ve developed. I’m using it because it’s light and I don’t need anything complicated. You can find the source code here and more explanation here.

    The full code source is here:

    using System;

    using System.Net;

    using System.Net.Sockets;

    using System.Threading;

    using Microsoft.SPOT;

    using Microsoft.SPOT.Hardware;

    using SecretLabs.NETMF.Hardware;

    using SecretLabs.NETMF.Hardware.Netduino;

    using NetduinoLibrary.Toolbox;

     

    namespace Servo_test

    {

        public class Program

        {

     

            static private SecretLabs.NETMF.Hardware.PWM pwm = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D5);

            private static WebServer server;

     

            //string constant

            private const string strOK = "OK";

            private const string strProblem = "Problem";

            private const string pageReq = "req.aspx";

            private const string paramMinPulse = "mi";

            private const string paramMaxPulse = "ma";

            private const string paramPeriod = "pe";

            private const string paramPosition = "po";

            //servo info

            static private uint MinPulse = 800;

            static private uint MaxPulse = 2200;

            static private uint Period = 20000;

            static private uint Position = 0;

     

            public static void Main()

            {

                server = new WebServer(80, 10000);

                server.CommandReceived += new WebServer.GetRequestHandler(ProcessClientGetRequest);

                server.Start();

                Thread.Sleep(Timeout.Infinite);

            }

     

            private static bool DecryptRequest(string strDecrypt)

            {

                // decode params

                WebServer.Param[] Params = WebServer.decryptParam(strDecrypt);

                int mMin = -1;

                int mMax = -1;

                int mPeriod = -1;

                int mPosition = -1;

                bool isvalid = true;

                if (Params != null)

                {

                    for (int i = 0; i < Params.Length; i++)

                    {

                        //on cherche le paramètre strMonth

                        int j = Params[i].Name.ToLower().IndexOf(paramMinPulse);

                        if (j == 0)

                        {

                            mMin = Convert.ToInt32(Params[i].Value);

                            if (mMin<0)

                                isvalid = false;

                        }

                        j = Params[i].Name.ToLower().IndexOf(paramMaxPulse);

                        if (j == 0)

                        {

                            mMax = Convert.ToInt32(Params[i].Value);

                            if (mMax<0)

                                isvalid = false;

     

                        }

                        j = Params[i].Name.ToLower().IndexOf(paramPeriod);

                        if (j == 0)

                        {

                            mPeriod = Convert.ToInt32(Params[i].Value);

                            if (mPeriod<0)

                                isvalid = false;

                        }

                        j = Params[i].Name.ToLower().IndexOf(paramPosition);

                        if (j == 0)

                        {

                            mPosition = Convert.ToInt32(Params[i].Value);

                            if (mPosition<0)

                                mPosition = 0;

                            if (mPosition > 100)

                                mPosition = 100;

                        }

                    }

                }

                // check if all params are correct

                if (isvalid)

                {

                    if (!Microsoft.SPOT.Hardware.SystemInfo.IsEmulator)

                    {

                        if(mMin > 0)

                        {

                            MinPulse = (uint)mMin;

                        }

                        if (mMax > 0)

                        {

                            MaxPulse = (uint)mMax;

                        }

                        if(mPeriod>0)

                        {

                            Period = (uint)mPeriod;

                        }

                        if(mPosition>=0)

                        {

                            Position = (uint)mPosition;

                        }

                        uint duration = (uint)(MinPulse + (MaxPulse - MinPulse)/100.0f*Position);                

                        //pwm.SetDutyCycle(duration);

                        pwm.SetPulse(Period, duration);

                    } 

                    else

                        Debug.Print("MinPulse " + mMin.ToString() + " MaxPulse " + mMax.ToString() + " Period " + mPeriod.ToString() + " Position " + mPosition.ToString());

                }

                else

                    isvalid = false;

     

                return isvalid;

            }

     

            private static void ProcessRequest(Socket response, string rawURL)

            {

                string strResp = "";

                if (DecryptRequest(rawURL))

                    strResp = strOK;

                else

                    strResp = strProblem;

                WebServer.OutPutStream(response, strResp);

            }

     

            private static void ProcessClientGetRequest(object obj, WebServer.WebServerEventArgs e)

            {

                Socket response = e.response;

                string strResp = "";

                string strFilePath = e.rawURL;

     

                //HTP header

                strResp = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n";

                strResp = WebServer.OutPutStream(response, strResp);

     

                if (strFilePath.Length >= pageReq.Length)

                {

                    if (strFilePath.Substring(0, pageReq.Length).ToLower() == pageReq)

                    {

                        ProcessRequest(response, strFilePath);

                        return;

                    }

                }

     

                strResp += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

                strResp += "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Servo Motor Discover</title>";

                strResp += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";

                strResp += "<meta http-equiv=\"Cache-control\" content=\"no-cache\"/>";

                //create the script part

                strResp += "<script language=\"JavaScript\">var xhr = new XMLHttpRequest();function btnclicked(boxMSG, cmdSend) {";

                strResp += "document.getElementById('status').innerHTML=\"waiting\";";

                strResp += "xhr.open('GET', cmdSend + boxMSG.value);";

                strResp += "xhr.send(null); xhr.onreadystatechange = function() {if (xhr.readyState == 4) {document.getElementById('status').innerHTML=xhr.responseText;}};}";

                strResp += "</script>";

                strResp = WebServer.OutPutStream(response, strResp);

                //body

                strResp += "</head><body><table >";

                strResp += "<tr><td>Min pulse</td><td><input id=\"MinPulse\" type=\"text\" value=\""+MinPulse.ToString()+"\" /></td><td><input id=\"MinPulseBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('MinPulse'),'req.aspx?mi=')\"  /></td></tr>";

                strResp += "<tr><td>Max pulse</td><td><input id=\"MaxPulse\" type=\"text\" value=\""+MaxPulse.ToString()+"\" /></td><td><input id=\"MaxPulseBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('MaxPulse'),'req.aspx?ma=')\" /></td></tr>";

                strResp += "<tr><td>Period</td><td><input id=\"Period\" type=\"text\" value=\"" + Period.ToString() + "\" /></td><td><input id=\"PeriodBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('Period'),'req.aspx?pe=')\" /></td></tr>";

                strResp += "<tr><td>Position %</td><td><input id=\"Position\" type=\"text\" value=\"" + Position.ToString() + "\" /></td><td><input id=\"PositionBtn\" type=\"button\" value=\"Update\" onclick=\"btnclicked(document.getElementById('Position'),'req.aspx?po=')\" /></td></tr>";

                strResp += "</table><div id=\"status\"></div></body></html>";

                strResp = WebServer.OutPutStream(response, strResp);

            }

     

        }

    }

     

    The Main function just create the Web Server.

    ProcessClientGetRequest received all the Get requests from the Web Server. I do analyze the path. If the called page is “req.aspx” I do then call a sub function which will change the servo motor settings. If not, then the main page.

    Let start with the main page source code, I’m using background http requests to change the servo settings. This avoid reloading the page and make it light in terms of discussions with the board, only a light header is sent with a simple status. This is a very simple a typical script that all web dev knows.

    <script language="JavaScript">

    var xhr = new XMLHttpRequest();

    function btnclicked(boxMSG, cmdSend) {

    document.getElementById('status').innerHTML="waiting";

    xhr.open('GET', cmdSend + boxMSG.value);

    xhr.send(null);

    xhr.onreadystatechange = function() {

    if (xhr.readyState == 4) {

    document.getElementById('status').innerHTML=xhr.responseText;}};}

    </script>

     

    Then it’s a simple tables which does contains the various settings and a button which does call the req.aspx page to update the settings.

    <tr><td>Min pulse</td>

    <td><input id="MinPulse" type="text" value="800" /></td>

    <td><input id="MinPulseBtn" type="button" value="Update" onclick="btnclicked(document.getElementById('MinPulse'),'req.aspx?mi=')"  /></td></tr>

     

    I pass the object so in the code I can read the value. I can do it also in the call but this allow to do other modification if needed from the script. At the end, there is status div to pub the status sent back by the req.aspx page.

    The code to build the page has nothing complicated. The code of the page is build dynamically with predefined values for the text box.

    The function DecryptRequest decrypt the request and the settings. It’s rest API based with parameters in the URL. I would even be able to build a Windows Phone, Windows 8, Android or iOS App if I want Sourire Well, let say this test page is enough for me Rire

    In the function, after analyzing the parameters and validating few of them, in a very basic, it’s time to change the settings of the servo. The pulse is then calculated and the order to make the PWM work sent with the period.

    uint duration = (uint)(MinPulse + (MaxPulse - MinPulse)/100.0f*Position);                

    pwm.SetPulse(Period, duration);

     

    So all up, it’s a good reuse of my Web Server code. And good use of basic javascript in a page. It’s already something I’ve used in various projects and it’s really saving some time. So all up, it took me 5 minutes to find the right settings of the servo and will save me time in the future.

  • Laurent Ellerbach

    Manage my wine cellar with QR codes and Microsoft Azure

    • 2 Comments

    I have quite “few” bottles of wine. I really do like wine a lot. Yes, I’m French Sourire As for any resource you use a lot with lots of new items in and out almost every day, you start to have mistakes in your inventory. That’s what happen naturally to any inventory. And you need to rebuild it time to time to check if it’s still correct or not.

    For a very long time, I was using a simple Excel file to manage my wine cellar. With manually decreasing by handwriting the new number of bottles on each line. You can imagine how the line looks like when I have 12 bottles and drink them all between 2 inventory periods. Aalso adding new bottles manually on the paper was quite a challenge. So I needed to do the inventory at least 2 to 3 times a year.

    I was thinking of doing something with barcode and a simple barcode reader. Finally, I never took the time to do it. But recently, I got the idea of using QR Codes, smartphones to recognize the QR Codes and a Cloud website to store everything.

    I went naturally for Microsoft Azure for both the Website using ASP.NET + MVC + EntityFramework and a SQL Azure database. I would have been able to use PHP, Java or any web technology like this but my core skills are more on .NET and C# Sourire Note that for the usage I’m doing, the project hosting and database are fully free in Azure. So if you want to do the same, it will be fully free. So let see in 8 simple steps how to make this app.

    Step 1: Create the Azure Web Site from Visual Studio 2013

    Visual Studio 2013 is the best development tool I’ve ever used (yes, I do use to test, competitive ones, time to time). Visual Studio have multiple versions and some are for free like Visual Studio Community.

    You will need a Microsoft Azure subscription. To do this application, including the database, you won’t have to pay anything at all. The free hosting and free database is largely enough. So first, click on Trial on the Azure page and follow the steps. You'll have to use an existing Microsoft Account (ex Live ID), then you’ll have to put your credit card. This is for verification only, after the trial, you won’t be charged, you’ll be able to continue to use the free website and free database.

    To create an Azure Website, you can follow the step by step documented here. What is important is to keep the Authentication mechanism into the website, so to select “Individual User Accounts”. This is the only difference from the tutorial. I will use authentication in the web application to make sure not everyone can access some specific parts.

    image

    The project is now ready and you already have the ability to add new users. The AspNet* tables are used to manage the users and roles.

    image

    Every time a user is registered, the AspNetUsers is filled. As the basic pages does only contains email and password, those 2 are created. Note that the password is not stored, only the Hash code is for security reason. It’s a good habit not to share any password in a database but either a crypted one or just the hash.

    image

    Step 2: create the Wine database

    I made it super simple and straight forward: I’ve only created 1 table called “ListeVin” (WineList in English). Go to the Server management, select your Azure database and select manage it in the server explorer (you may get a message to update the firewall rules on Azure). Right click on Table from the database you’ve created with the website, select Add New Table.

    image

    You can then start creating new columns. In my case, I’ve created an Id which will be my primary key and can’t be null. Then a region, description and placement (“rangement”) which are text, a year (“annee”), quantity (“quantitee”) and years to keep (“Agarder”) which are integers. Yes, it’s simple, I should have add the creation date, the last update, another table to keep track of the modifications, etc. But I’m more interested for this version to just the necessary.

    image

    Once done, click on Update, and the database will the create.

    Step 3: create the Model associated to this table

    MVC means Model View Controller. The Model is the data part, the View is the page and the Controller is in the middle of the view and the data to perform advanced operations, manage some security, etc. So we need to add a Model first.

    On Models in your project, right click, select “Add” then “New Item…” and “ADO.NET Entity Data Model”.

    image

    Name it, then Add and select “EF Designer from database”.

    image

    If you haven’t setup the connection to the database, you’ll need to do it selecting “New Connection”

    image

    You can get the name of your database in the database properties or directly in the Azure portal. Select your database from the drop down list. The next step looks like this, you can include or not the login and password in the code or in the connection string. I’m adding it in the connection string.

    image

    In the next step, you select the table just created. Note you can select the other tables as well as views, store procedures and functions if you’ve created some.

    image

    Just click on Finish and you’ll get now your new entity created. The C# files has been created automatically and will make our life easier later on. There is no need to change anything to the generated code. You can have a look if you want to see all what you’ve avoided to do to access the database Sourire

    image

    Rebuild the full project. It’s important to make sure the Entity is recognize correctly.

    Step 4: Create the Controller and the View

    Right click on “Controllers” in the project, select “Controller…” and then “MVC 5 Controller with views, using Entity Framework” and click “Add”

    image

    Select the created Model in the previous step as well as the entity in the drop down list. Name it “ListeVinsController”, select “Generate views” at least and click “Add”. You can also select the layout of the project or do it later in the code of the pages.

    image

    You’re done for this step. It’s the moment to test the app! Hit F5. You’ll arrive on the default web page.

    Change the URL by replacing “/home/index” by “/ListeVins”. Please note that this name is the Controller name. It’s automatically done by default by removing the “Controller” part of the name. When we’ve created it, I’ve named it “ListeVinsController”. If you had names if “WineListController”, you’ll be able to access the page by putting “/WineList”.

    image

    At this moment, you can create new entries, edit then and delete them without any security. And you didn’t had to write any code either to have those pages! This is the beauty of those ASP.NET MVC default pages. And yes, of course, you’ll be able to modify the code later to adapt to your needs. I’ll show an example later.

    Step 5: Adding security

    As I wrote in the first step, few tables are created for the users. One is important, it’s called AspNetRoles. This is the one which contains the roles of users. The way the security is working in this MVC model is thru users and or roles. Users can be part of roles. I will use this notion of roles in the code.

    image

    I’ve create 2 roles in the previous table. I will use them later in the code. Be careful, they are case sensitive.

    The AspNetUsersRoles is the table linking a user with roles. I’ve linked a use to those 2 roles.

    image

    I do this manually but of course, yes, we can also build simple pages to manage all this. But I’ll have something like 5 users so I’ll do it directly in the database Sourire Please note that in development mode, you have also a local database. You have to do this modification as well in the local base to be able to use the roles and the users.

    The way to use this security in the code is working either by declaration either by code or both. I’ll use only the declarative way. And it’s very straight forward by adding [Authorize(Roles = “Admin”)] to give access to the admin roles. By default, all anonymous connections are allowed.

            // POST: ListeVins/Create

            // To protect from overposting attacks, please enable the specific properties you want to bind to, for

            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.

            [HttpPost]

            [ValidateAntiForgeryToken]

            [Authorize(Roles="Admin")]

            public ActionResult Create([Bind(Include = "Id,Region,Description,Annee,Quantite,Rangement,Agarder")] ListeVin listeVin)

            {

                if (ModelState.IsValid)

                {

                    db.ListeVin.Add(listeVin);

                    db.SaveChanges();

                    return RedirectToAction("Index");

                }

     

                return View(listeVin);

            }

     

    You can define one or multiple roles, just separate them with a coma. You can do the same with users for example. But the notion of role is much more flexible than users.

            [Authorize(Roles = "Admin,CanEdit")]

            public ActionResult Edit(int? id)

            {

                if (id == null)

                {

                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

                }

                ListeVin listeVin = db.ListeVin.Find(id);

                if (listeVin == null)

                {

                    return HttpNotFound();

                }

                return View(listeVin);

            }

    I did it on the Create, Delete and Edit functions which are the key feature to protect.

    So if you are not registered or if you don’t have the right to access a specific part of the code, you’ll be redirected to the login page:

    image

    Once logged with the correct role, you can access to the Create page and create new set of bottles.

    image

    Step 6: Removing 1 bottle from the inventory

    I’ve create a specific function in the controller to decrease the number of bottles on a specific set of bottle.

            // GET: ListeVins/Decrease/5

            [Authorize(Roles = "CanEdit")]

            public ActionResult Decrease(int? id)

            {

                if (id == null)

                {

                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

                }

                ListeVin listeVin = db.ListeVin.Find(id);

                if (listeVin == null)

                {

                    return HttpNotFound();

                }

                listeVin.Quantite -= 1;

                db.SaveChanges();

                return RedirectToAction("Index");

            }

     

    The code is very similar to the delete one but I just decrease the quantity, save the changes and redirect on the full wine list. I don’t test anything, quantity can be negative. Again, this is very simple but largely enough for my needs. I should add a nice message sayaing everything has been updates, etc, etc. But hey, I’ll teach everyone at home. To call this function, I just have to go to“http://yoursite/listevins/decrease/1” to decrease by 1 the number or bottle of the bottle set numer 1 (replace yoursite by the url of your site).

    This is not very user friendly as none at home will remember what to call in the url, the id of a specific set of bottles. And that’s where QR Codes arrived Sourire

    Step 7: Adding QR codes with a redirect on the good URL

    I want everyone at home to be able to scan a RQ code sticked on a bottle with their Windows Phone (yea, it’s also working with other smartphones as well but everyone has Windows Phone at home Rire) and that’s it!

    But I’ll need to generate the QR Codes dynamically when I’ll create the set of bottles to be then able to print them and stick them on the bottles. Ho yes, this will take some time as I have quite a lot of bottles. But it’s the price to pay to make it simple on the other side. And once it’s done, it’s done! No need to change them even if I keep the bottles for 20 years or more Clignement d'œil

    I found a good QR Code project to generate QR Code on CodePlex: http://qrcodenet.codeplex.com/

    Once downloaded and referenced in the project. The usage is quite easy:

            public string GetQrCode(int? id)

            {

                return "<img src=\"/ListeVins/QrCode?id=" + id + "\" />";

            }

     

            public FileResult QRCode(int? id)

            {

                QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);

                QrCode qrCode;

                MemoryStream ms = new MemoryStream();

                encoder.TryEncode("https://yoursite/listevins/decrease/"+id, out qrCode);

     

     

                var render = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two));

                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);

                return File(ms.GetBuffer(), @"image/png"); ;

     

            }

     

    The GetQrCode function is just returning a simple string containing the call on the second function which does return a file, in our case an image. The image is generate dynamically by encoding the the URL. The image is generated in memory in a stream, the stream is saved as an image in the memory. In order to generate the right URL, change “yoursite” to the right URL.

    Now, I have to modify the “Details.cshtml” by adding this QR Code in the list, at the end. GetQrCode will be called, this will create the image tag. The image tab contains a call on the QRCode image creation and the image will be displayed.

    <dd>
        @Html.Action("GetQrCode", "ListeVins", new { id = Model.Id });
    </dd>

    image

    And as a result, the details page shows now the QR Code.

    image

    All what I need to do is copy/paste it in Word in a template that I’m using to print stickers, print them and stick them on the bottle!

    As you can also see, I’ve customized the main menu of the website as well as the footer. You can do all this by modifying the cshtml files from the home folder as well as the Shared one.

    Step 8: Scan and drink Sourire

    The last step is the user step. When anyone from the familly at home goes to the basement and take a bottle, he/she just have to scan the bottle using a QR Code app if they’re not using a Windows Phone or directly the visual search (Visual Bing) using a Windows Phone. And if they’ve installed Cortana, they can use QR for Cortana for example and launch the filter which does the QR Code recognition.

    wp_ss_20141226_0002

    The first time they scan a QR code, they just have to put their login and password in the website (like in Step 5), click “remind me” and they’re done for the other times. So very simple. And even if they forget to do it from the basement or they forgot their phone or whateverotherfakereasontheywillfind, I/they still can do it when they have the bottle upstairs.

    Now I can enjoy a great bottle of wine Sourire Feedback welcomes on the article as well on wine of course Sourire

  • Laurent Ellerbach

    Connect Arduino, Spark.IO, Netduino (NETMF .Net Microframework), to Microsoft Azure Mobile Services, create IoT (Part 3)

    • 2 Comments

    In the 2 previous articles, we’ve created a local network of objects which communicate sensor information to a more smarter object which will post the information somewhere in the Cloud. Idea is to create a full Internet of Things (IoT) infrastructure. Those sensors are temperature, humidity (air and soil), wind direction as well as rain. Some have been just reused from Oregon Scientific and some have been developed from scratch using simple Arduino or equivalent like Spark.IO.

    It is now time to look at how to post the sensor information in the Cloud. There are multiple offers to post data in the Cloud. I prefer the Microsoft Azure offer for multiple reasons. First, it’s free for a low usage like my hobbyist usage. Yes, it’s just free. Yes, really free. I will use for this the Azure Mobile Services as well as an Azure SQL database for the storage. The Azure SQL database is free up to 1Gb of data. And in one of the next article, we’ll see how to consume those data, also for free in an Azure Web site.

    There are also good technical reasons to use Azure: first it’s totally open, easy to access, based on the Internet protocols. All what you need is simply an IP stack and sockets. It can work on a low speed connection like GPRS or high speed like WiFi, using HTTPS or HTTP. So it’s very flexible and very easy to use. Let go deeper in the code. As a summary, Azure Mobile Services are:

    • Fully REST API, can be access HTTP or HTTPS, in our case, as the processors are very low end, it will be HTTP only
    • Dynamic table auto setup, no need to be SQL guru
    • Access thru application key, this is a simple but easy and simple security mechanism
    • Can customize insert, update and all other functions, I’ll use this to simplify a bit the data returned by Azure
    • Can create custom API, I’ll use this feature for the last part to connect my Sprinkler to the Azure data

    First step is to open an Azure subscription: http://azure.microsoft.com/pricing/free-trial/. Your credit card will be required but won’t be used if you decide to stay with free offer. The credit card is used for verification mainly. You’ll get 30 days also with real $$ to use for free if you want to test other part of Azure which I of course encourage you to do.

    First step is to create a Mobile Service. For this, just follow the excellent step by step you’ll find here. http://azure.microsoft.com/en-us/documentation/services/mobile-services/. It’s just 3 easy steps and you’re done for the server side.

    image

    Now the service is creates, to access it, it’s super simple: connect to the port 80 (stand HTTP port) of the Azure Mobile Service you’ve just created. And send the following data:

    POST /tables/weather/ HTTP/1.1

    X-ZUMO-APPLICATION: 123456789abcdef123456789abcdef12

    Host: nomduservice.azure-mobile.net

    Content-Length: 88

    Connection: close

    {"sensorID":22, "channel":5, "instSpeed":12,"averSpeed":5,"direction":2,"batterylife":90}

     

    The server will return the following text:

    HTTP/1.1 201 Created

    Cache-Control: no-cache

    Content-Length: 133

    Content-Type: application/json

    Location: https://nomduservice.azure-mobile.net/tables/weather//931CFDDE-AB7F-4480-BA28-F1D5C611398B

    Server: Microsoft-IIS/8.0

    x-zumo-version: Zumo.master.0.1.6.3803.Runtime

    X-Powered-By: ASP.NET

    Set-Cookie: ARRAffinity=da4a9f7437a690e3c1a799d3a6c3ddf3ee0cbb9f5a67008d3c919f0149f34ee3;Path=/;Domain= nomduservice.azure-mobile.net

    Date: Sun, 31 Aug 2014 15:40:12 GMT

    Connection: close

     

    {"sensorID":22,"channel":5,"instSpeed":12,"averSpeed":5,"direction":2,"batterylife":90,"id":"931CFDDE-AB7F-4480-BA28-F1D5C611398B"}

     

    yes, it’s HTTP, so it’s just simple text. What we send is a POST request on a specific table, here /tables/weather/ and we send information in a JSON format. See JSON as a simplified XML still readable by a human Sourire So basically a bit less text to send with still a structured way.

    What we get in return from the server is a standard header containing quite lots of information as well as the data and the unique ID returned. Wait, a unique ID? Yes, it’s part of what is generated automatically when you are using the Azure Mobile Services. All the database is created for you, all the mechanism to generate the data in the database is totally hidden for you. So clearly no need to be the king of SQL. It’s fully transparent. And yes, you can personalize, you can customize the received data, you can do check and all this. We'll have a look later on how to simply do it.

    OK, time to see code. Here is the full Arduino code necessary to post the example above:

    TCPClient client;

    byte AzureServer[] = { 12, 34, 56, 78 };

     

    String writeJsonWind(struct wind wd) {

      // Create a simple JSON;

      String datastring = "{\"sensorID\":";

      datastring += String(wd.ID);

      datastring += ",\"channel\":";

      datastring += String(wd.channel);

      datastring += ",\"instSpeed\":";

      datastring += String(wd.instantSpeed);

      datastring += ",\"averSpeed\":";

      datastring += String(wd.averageSpeed);

      datastring += ",\"direction\":";

      datastring += String(wd.direction);

      datastring += ",\"batterylife\":";

      datastring += String(wd.bat);

      datastring += "}";

      return (datastring);

    }

     

    void sendData(String thisData) {

      // create a connection to port 80 on the server

      // IP is your Mobile Services address

      if (client.connect(AzureServer, 80))

      {

      //Serial.println("Connected to Azure Server");

      // create the REST request using POST

      // Nomdelatable is name of the table

      client.print("POST /tables/weather/");

      client.println(" HTTP/1.1");

      // use the application key

      client.println("X-ZUMO-APPLICATION: 123456789abcdef123456789abcdef12");

      // host name is name of your Azure Mobile Service

      client.println("Host: nomdumobileservice.azure-mobile.net");

      client.print("Content-Length: ");

      client.println(thisData.length());

      client.println("Connection: close");

      client.println();

      // and finally data!

      client.println(thisData);

      }

      else {  // in case of error, stop connection

      client.stop();

      }  }

    // Sending data is simple, create a JSON, and send it on port 80!

    String dataString = writeJsonWind(myWind);

    sendData(dataString);

    That’s it? Yes, that’s it. Nothing more is needed. As I wrote before, you need an IP stack and sockets. You have them with the TCPClient object. I’m using a Spark.IO which is based on the same kind of processor you find in normal Arduino but with a WiFi chip on top. See previous articles for more information.

    Rest of the code is quite straight forward, the writeJsonWind function create the Json string. sendData is first connecting to the Azure Mobile Services server on port 80. Then a socket connection is created, it write in the socket the header and then the Json data. And that’s all! The data will be stored automatically in the SQL Azure database and you’ll be able to access them.

    It’s possible to personalize and create your own Azure Mobile Services API. You can do it either in Javascript directly in the Azure management console or in .NET using the excellent and free Visual Studio, either the Express version and installing the Azure SDK, either the Visual Studio Community edition. As an example, I’ll use Javascript here. The idea is to reduce number of data sent back to the client. No need to send the generated data, it won’t be use. The header will be largely enough.

    function insert(item, user, request) {

    request.execute({

    success: function(results) {

    request.respond(statusCodes.OK);  },

    error: function(results) {

    request.respond(statusCodes.BAD_REQUEST); }  }); }

    This is a simple modification which return OK if the data has been successfully entered into the database or bad request if not. You can of course do more. I encourage you to follow the project my friend Mario Vernari is doing here and here. Lots of great and cool stuff too with example of personalized API.

    As always, you can raise the question: what about security?

    It’s a critical part of Mobile Services. The default access is HTTPS which offers a reasonable level of security with the application key. Here, we are using very cheap processors which are not powerful enough to run HTTPS, so we are using basic HPPT. Of course, for a professional project or a project which require more sensible data, we’ll clearly have to use more robust processors. On top, you can use Azure Directory federation and make user/device authentication. When you’ll have to manage thousands of devices, when you’ll have them randomly in the wild and not physically secured, you’ll be happy to use those kind of mechanism to exclude specific devices. You have on top an easy federation with Microsoft ID, Google, Facebook and Twitter. And of course the Azure SQL database is secured with login/pwd, you can control which server/PC/device can have access by IP as well as user/pwd.

    What are the other way to post data in Azure? 

    There are other ways of course. You can access directly the SQL database but it’s not the easiest method to manage authentication, validate data. You better want to use the Azure Event Hubs to connect millions of devices across platforms and which allow the support for AMQP and HTTP. It does have native client libraries also exist for popular platforms

    You can also use framework like Intelligent Systems Service, more oriented to consume the data. It’s based on Azure, provide additional tools for analyze and data consumption.

    So all up, we’ve seen how to post data from a 1$ chip with access to an IP stack and Socket to Azure and store them in a SQL Azure database. It is simple, straight forward and flexible solution. It’s free for low usage so it makes it the best solution for simple projects and hobbyists.

  • Laurent Ellerbach

    Connect your Arduino, Spark.IO, Netduino (NETMF .Net Microframework), Galileo to Microsoft Azure (Part 2) to create an Internet of Things (IoT)

    • 0 Comments

    In the previous article, I’ve explained that I’ll build an Internet of Things (IoT) infrastructure to pilot automatically with sensors and a Microsoft Azure backend my Sprinkler system at home. I’ve also explained a bit the full architecture. In this post, I will explain how I have connected existing objects and make them communicating with the rest of the world as well as how I’ve created my own objects.

    I have decided to connect the existing Oregon Scientific sensors to the Cloud and Microsoft Azure Sourire In order to do that, without doing any modification on the existing sensors, I had to analyze how they’re build and how to be able to interface with them.

    image

    Those sensors are using a 433MHz band to communicate wireless to a base which can gather the information. Some are even connectable with a serial port. In my case, I didn’t had such a base. So I needed to basically decode the protocol and decrypt it. After a quick search on the internet, I found and excellent article from Olivier Lebrun in French (yes I’m French Sourire) here: http://connectingstuff.net/blog/decodage-protocole-oregon-arduino-1/. Olivier is using another article which is this time in English and which you can find here: http://jeelabs.net/projects/cafe/wiki/Decoding_the_Oregon_Scientific_V2_protocol

    The good news for me is that it’s easy and cheap to find a 433MHz demodulator and that the protocol used is simple and non crypted. In order to decode the protocol, I’ve decided to use an Arduino based processor. the reason is the very low cost and the fact that the existing code to decode is existing for Arduino. When searching a bit more, I found an Arduino compatible based processor which has an embedded wifi chip: Spark.IO. Good news is that it is still cheap and can be connected to the Internet. So with the Spark.IO, I can decode the protocol as well as posting information in Azure cloud.

    The wireless protocol use Manchester encoding. It’s a very common protocol used when doing wireless communications.

    image

    Source Wikipedia http://en.wikipedia.org/wiki/Manchester_encoding

    When a message is sent, it is transformed using the Manchester encoding and modulated at 433MHz. The graphic explains vey well how the information is sent. So in order to decode the signal, you can do it with the states changes and the timing between 2 changes. All this is very well explained in the 2 articles I4ve pointed.

    And as I can decode the protocol, I can also do the opposite and use a transmitter to send a signal. By doing this, I can have my own sensors. I’ll post an example of code later.

    The architecture I will use for my sensors and to connect them to Internet is a quite common one. It is used for years in alarms for example. Most sensors are only one direction, they use a close wireless protocol and send them information to a smart base. All what the open door detection sensor has to do is send time to time that he is alive and in case the door open send the signal it is open. It does not need to know if his signal has arrived or not and who did listen to him. And most sensors in the world are like that. But the smart base has to know all the sensors and has to monitor them. It has to make sure that they are all alive, or it will raise an alert. It aslo has to make sure that when the open door signal is raised it will also raise an alert. Today the only thing which is really connected to the internet and a another central point in an alarm system is the smart base.

    So I’ll use the same architecture for my project. Only the Spark.IO is connected to Internet thru its wifi. The other sensors are using the 433MHz wireless protocol to send their information. And they have no idea is it will arrive or not.

    image

    So the role of the Spark.IO is crucial. It will have the task to decode the messages send over the air as well as posting the information in Azure.

    The sensors I’ve developed are using ATmega 328 which is the processor used in the basic Arduino. it is very cheap, 1€, consume very low energy as well as possibility to sleep it to even more reduce the energy used. I wanted my sensors to be outside and fully autonomous. So I had to take case of this.

    Another great news is the possibility to use Visual Studio with the Visual Micro complement to develop for Arduino. Visual Studio Community Edition is free and have much more feature than the Arduino tools. Writing code is then super easy, it does support all the Ardunio flavor as well as a great debugger.

    imageimage

    Here is the example of the wind speed, wind direction and pluviometer. It is quite simple electronic. I bought the anemometer, wind vane and rain gauge sensors here. This was what cost me the most in the all project (70$)!

    image

    this is how it does look like as a prototype:

    clip_image002

    and integrated as a beta version fully autonomous in the garden

    WP_20141111_002

    inside the transparent box, there is Li-ion battery (3$), a Li-ion charger (2$) and a solar panel (6$) on top of what you have in the prototype picture.

     WP_20141111_004

    The power consumption is quite low, it can run in the dark on the battery for multiple days and few hours of a direct and intense sun is enough to fully recharge the battery.

    Here is a part of the code needed for the anemometer. It is quite straight forward:

    #define PIN_ANEMOMETER  2     // Digital 2

    #define PIN_ALIMRF   5      //Digital 5

     

    // How often we want to calculate wind speed or direction

    #define MSECS_CALC_WIND_SPEED 10

     

    volatile int numRevsAnemometer = 0; // Incremented in the interrupt

    ulong nextCalcSpeed;                // When we next calc the wind speed

    ulong time;                         // Millis() at each start of loop().

     

    // Buffer for Oregon message max size

    byte OregonMessageBuffer[12];

    byte numByteToSend = 12;

     

    Wind *myWind = new Wind();

     

    unsigned long lastSensorCheck = now();  

    const unsigned int interval = 5; // every 3 minutes = 3*60 sec

     

     

    void setup()

    {

           pinMode(PIN_ALIMRF, OUTPUT);

           pinMode(PIN_ANEMOMETER, INPUT);

           // set internal pull up

           digitalWrite(PIN_ANEMOMETER, HIGH);

           //set interuption

           attachInterrupt(0, countAnemometer, FALLING);

           nextCalcSpeed = now() + MSECS_CALC_WIND_SPEED;

           //other initis

    }

     

    void setWinMessage(){

           // Create the Oregon message for an Anemometer (WTGR800)

           byte ID[] = { 0x1A, 0x99 };

           setType(OregonMessageBuffer, ID);

           setChannel(OregonMessageBuffer, 0x20);

           setId(OregonMessageBuffer, 0x12);

           setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high

           setWind(OregonMessageBuffer, myWind->getInstantSpeed(), myWind->getAverageSpeed(), myWind->getDirection());

           byte numByteToSend = 10;

           csWind(OregonMessageBuffer);

           Serial.print("inst speed: ");

           Serial.print(myWind->getInstantSpeed());

           Serial.print(" aver speed: ");

           Serial.print(myWind->getAverageSpeed());

           Serial.print(" dir :");

           Serial.println(strVals[myWind->getDirection()]);

    }

     

     

    void sendMessage()

    {        // Show the Oregon Message

           digitalWrite(PIN_ALIMRF, HIGH);

           // wait 1 second that RF switch on

           delay(1000);

           for (byte i = 0; i < numByteToSend; ++i)   {

                  Serial.print(OregonMessageBuffer[i] >> 4, HEX);

                  Serial.print(OregonMessageBuffer[i] & 0x0F, HEX);

           }

           // Send the Message over RF

           sendOregon(OregonMessageBuffer, numByteToSend);

           // Send a "pause"

           SEND_LOW();

           delayMicroseconds(TWOTIME * 8);

           // Send a copie of the first message. The v2.1 protocol send the

           // message two time

           sendOregon(OregonMessageBuffer, numByteToSend);

     

           // Wait for 30 seconds before send a new message

           SEND_LOW();

           digitalWrite(PIN_ALIMRF, LOW);

    }

     

    void loop()

    {

           time = now();

     

           if (time >= nextCalcSpeed) {

                  calcWindSpeed();

                  setWinMessage();

                  sendMessage();

                  nextCalcSpeed = time + MSECS_CALC_WIND_SPEED;

           }

    // other treatment and other time related check

    }

     

    void countAnemometer() {

           numRevsAnemometer++;

    }

     

    And there are also functions used to send the message. Here, idea is to build few macros and then use them to send the low or high message:

    #define TX_PIN       4      //Digital 4

    const unsigned long TIME = 512;

    const unsigned long TWOTIME = TIME * 2;

     

    #define SEND_HIGH() digitalWrite(TX_PIN, HIGH)

    #define SEND_LOW() digitalWrite(TX_PIN, LOW)

    inline void sendZero(void)

    {

           SEND_HIGH();

           delayMicroseconds(TIME);

           SEND_LOW();

           delayMicroseconds(TWOTIME);

           SEND_HIGH();

           delayMicroseconds(TIME);

    }

    inline void sendOne(void)

    {

           SEND_LOW();

           delayMicroseconds(TIME);

           SEND_HIGH();

           delayMicroseconds(TWOTIME);

           SEND_LOW();

           delayMicroseconds(TIME);

    }

    inline void sendQuarterMSB(const byte data)

    {

           (bitRead(data, 4)) ? sendOne() : sendZero();

           (bitRead(data, 5)) ? sendOne() : sendZero();

           (bitRead(data, 6)) ? sendOne() : sendZero();

           (bitRead(data, 7)) ? sendOne() : sendZero();

    }

    inline void sendQuarterLSB(const byte data)

    {

           (bitRead(data, 0)) ? sendOne() : sendZero();

           (bitRead(data, 1)) ? sendOne() : sendZero();

           (bitRead(data, 2)) ? sendOne() : sendZero();

           (bitRead(data, 3)) ? sendOne() : sendZero();

    }

    It is quite straight forward to then send an information thru the 433MHz emitter. It’s just then about sending the bits one by one.

    I’m using a Spark.IO for the decoding part. The Spark.IO can be programmed over the air (OTA) and you need to use the development tools in a browser provided by the Spark.IO. The tools are ok to use, clearly much more limited than Visual Studio. It is still possible to use Visual Studio as for the Arduino but then you’ll need to be plugged in to program the chip. What I4m doing is I’m using Visual Studio for the code, the syntax and then copy/paste my code into the online editor to program the chip.

    It comes also with what they call a cloud. In reality, it’s just a server which provide access to information you can publish. But it is very limited and can’t be control in a smart way. So I will use Microsoft Azure instead. And I will explain it in the next part.

    Now on the Spark.IO, the way the decoding is working is this way:

    • First disconnect Spark.IO from it’s own server. If you don’t do it, you will never be able to decode correctly anything, the servers are taking lots of resources and interrupt the processes not in a needed time. So use Spark.disconnect(); in the main void setup() function. I am allowing e to reconnect it to make my life easier when I want to reprogram it over the air by using one of the entry. When I change the state of the entry, the server is reconnected so I can reprogram it over the air
    • A length is calculated between 2 state changes on the 433MHz receptor
    • if this length is between 200 and 700 milliseconds, then it’s a short, if it’s between 700 and 1200 it’s a long. Others are not valid and reinit the decoder
    • As soon as the impulsions are still valid:
      • The Manchester decoding is done for every new impulsion
      • The message is decoded on the flight
      • When the synchro is found (32 impulsions of 1 so 0xFF 0xFF received after the first 32 impulsions has been decoded), the sensor type is checked and what kind of data are expected.
      • if at some point data are not valid the decoder is reinitialyzed
    • When all the message is decoded, it is stored and it’s ready for the next one to be decoded

    This decoding part is well documented in the 2 articles I was referring before.

     

    This is how look like the Spark.IO with the receptor. The 433MHz demodulator is under the paper. In fact it’s a layer of paper plus few layers of aluminum and a layer of paper so like an aluminum sandwich. And the aluminum is then put to the ground. This simple method allow to isolate a bit the very cheap receptor I bought and increase drastically the decoding performances Sourire

    image

     

    There is a key question that can be raised here: the security one. In fact the data that are send by the sensors are not crypted and they are not secured. The initial temperature and humidity sensors are not protected. This is for a cost/sensibility reason. Those are not considered as sensitive. Of course, if you have to deal with sensitive data and send them with those kind of short range wireless protocols, do crypt them. This will increase of course the cost of your solution. But it’s the price to pay for security. And some of my neighbor is using also Oregon Scientific sensors and I can decode them and have access to them.

     

    So that’s it for this article. It did cover the sensor part, how to make existing sensor connected, how to create your own sensor, what kind of architecture to use and how to build a simple solution to decode everything. We will see next time how to post the data on Azure.

  • Laurent Ellerbach

    Connect your Arduino, Spark.IO, Netduino (.Net Microframework), Galileo to Microsoft Azure (Part 1)

    • 1 Comments

    Internet of Things (IoT) is exploding. Cost of processors and cost of electronic components is falling, electrical consumption is also getting lower and lower for more and more power. It’s now more than ever becoming to be super easy to create our own objects with some intelligence. Add to this technologies like Bluetooth Low Energy (BLE), some Wifi or GPRS low cost and low consumption, you can also add some connectivity, between each others and/or with Internet.

    What is the Internet of Things? “The network of physical objects that contain embedded technology to communicate and interact with their internal states or the external environment.” Gartner

    Being connected is nice but does not bring any value if the generate data are not used or if the object don’t take any action. For few years, the Cloud infrastructure like Microsoft Azure makes it easy to store and analyze those produced data or make it easy to consume data exposed by those infrastructures. So our objects can publish data in the Cloud as well as consume data that the Cloud host.

    IoT is more complex than most people thing about. And there as lots of questions that must be answered: create a brand new object or just add connectivity to an existing one? How to manage the object remotely? How to deploy new firmware to those new objects? How to secure access to those objects both physically as well as thru their communications and software? How to manage the generated data? How to analyze them? What to analyze? How to send back the data generated to some smartest objects? How to make an object adapt to data generated by others? What kind of hardware? What tools to use to develop on those new platforms?

    And those are just few examples of questions, there are many more that come in an IoT project.

    image

    To help answering those questions, we will separate the problem in multiple layers. Even if IoT is a fashion word those days, the full process behind and the way projects are managed is quite old and working for years. We will use the framework above in order to segment the problem. The first part will be related to the devices and objects we have. How to architecture them, why, how to connected them to the next layer which is the storage and cloud. We will then move to the data analytics, try to understand the data and get insights. With this, we will then go back the chain up to the objects themselves.

    In order to make it concrete, I will use a real example: my home and my sprinkler system.

    What I need: automate my sprinkler system based on elements like the temperature, the air and soil humidity, the sun light, the wind, the pluviometer.

    What I already have: I have my sprinkler system using .NET Microframework (NETMF), you’ll find previous article on how it is build in my blog here and here for example, you’ll find more in the full blog. This is already a big part of the system as it allows me to control manually and in a programmatically way my sprinkler. but it’s not fully automated based on external conditions. This sprinkler is based on Netduino Plus board using an ATMEL AT91SAM7X512 processor. NETMF is fully open source and as the name said, it’s mini .NET running directly on the metal without any OS. It can be develop with C# using Visual Studio.

    image

    I do also have existing Oregon Scientific sensors. My idea is to reuse them for the temperature and air humidity. I will make them communicating with the Internet. We will see how to do it in a next article.

    image

    What I’ve decided to do: I’ve decided to reuse the Oregon Scientific sensors, make them connected to the Internet. For this, I’ve decided to use Arduino and Spark.IO. Both are based on the cheap ATMEL ATmega328 processor. Both implement the open source Wiring.org framework. This framework is easy to use based on C/C++ and it does make easy to build simple devices. You’ll find tons of do it yourself (DIY) projects based on Arduino. I also wanted to show that it is super easy to connect those objects to Microsoft Azure Cloud infrastructure. Keep in mind that a simple ATMEL ATmega328 cost 1€. So it’s a very cheap solution. The good news with the Wiring.org framework is that it can also run on top of an OS like Linux or Windows. As an example, it’s the case for the Arduino YÚN or Galileo. Galileo boards can run Linux or Windows and have the Wiring.org layer on top. See http://dev.windows.com/en-us/featured/Windows-Developer-Program-for-IoT for more info as well as the sources https://github.com/ms-iot for the Windows version. The Windows version is free to run on those boards.

    Having an OS below those frameworks allow to have more security, capacity to encrypt data, do authentication and more. But at the same time, you are increasing the cost of your solution. We will discuss a bit the security further as it’s an important point. But it’s like in any project: it has a cost. So up to you to place the cursor where you think it must be. The more security you’ll need, the more costly it will be.

    So my final architecture will look like this:

    image

    I will have sensors, my Oregon Scientific existing sensors, new ones I will create using the same wireless 433MHHz protocol as the existing Oregon Scientific sensors. A Spark.IO will be decrypting the data and posting them on Microsoft Azure using the Azure Mobile Services. The data will be stored in a SQL Azure database. I’ve build a full Azure Website using ASP.NET + MVC + Entity Framework + jquery. And my Sprinkler will connect also to the Azure Mobile Services to consume the data produced and take decision to sprinkler or not.

    I will go step by step to describe the full architecture in the next parts. This was just the intro to explain the full architecture. Stay tune!

  • Laurent Ellerbach

    .NET Microframework on RaspberryPi (Part 2)

    • 1 Comments

    I realize I haven’t posted on my blog the direct link to the porting of .NET Microframework (NETMF) to the RaspberryPi. See previous article.

    So code is available here: https://raspberrypinetmf.codeplex.com/. This is not a full port of NETMF natively on RaspberryPi. It’s a port of specific NETMF classes on Mono running on Linux. It can be useful to quickly port existing NETMF projects with very little or no modifications. Note also that some classes are missing. So don’t hesitate to participate.

    Enjoy!

  • Laurent Ellerbach

    Building an Internet of Things garden with Arduino/Netduino/Azure

    • 0 Comments

    For those who follow this blog, it’s been a while I haven’t posted anything. Reason of time missing and also because I worked on a new project so documentation of the project was not the priority.

    But now it’s time to start the documentation and sharing. I gave recently a conference in Kiev to explain how the project is working and all the technologies involved.

    Presentation for the day as well as full presentation with more details can be found on my OneDrive here.

    So you’ll find in the coming months quite detailed article to explain how to publish information from an Arduino or any thing with an IP stack/Socket. Stay tune Sourire

  • Laurent Ellerbach

    .NET Microframework on RaspberryPi (Part 1)

    • 7 Comments

    I’m a bit fan of .NET Microframework (NETMF). NETMF is an open source technology originally developed by Microsoft based on .NET. It directly run on metal, without any OS. All sources and portability kit are freely available On Codeplex: http://netmf.codeplex.com/.

    And as a fan of Embedded devices I had to buy this inexpensive RaspberryPi http://www.raspberrypi.org/. I bought the version second revision with the network adaptor. Advantage is clearly the price and having access to couple of GPIO like SPI,k i2c, PWM, serial and couple of other digital IO. Now it’s a slow device when using the user interface, there is no analog input or output, PWM is limited to only 1 hardware PWM and not really flexible for setting the frequencies. But overall, for the price, it’s Worth playing a bit with it.

    In term of environment, it runs under Linux, it was a very long time I did not had to setup and make a Linux working. I was just reminding couple of commands. And I’m far to be a Linux developer… so I’ve started to look at what to put that looks like C# or .NET on this board and more specifically .NET Microframework.

    I was disappointed to see that NETMF is not on the platform. But happy to see a good implementation of Mono http://www.mono-project.com/Main_Page. So at least I was able to run some .NET code on the Raspberry and take the advantage of the display if needed with Windows forms.

    So what for access like we do in NETMF to the GPIO? I started to search on the web for what is possible to do. And I found a first project RaspberryPi.Net: https://github.com/cypherkey/RaspberryPi.Net/. This project is using one library to access the BCM2835 chipset where the GPIO are managed. I found the last version of the library here: http://www.airspayce.com/mikem/bcm2835/

    And I just followed the steps explained on the RaspberryPi.Net project page. Here are the steps. Open an LXTerminal and type:

    sudo apt-get update

    sudo apt-get install xrdp

    this second one will allow you to access your Raspberry thru RDP Sourire So you can do remote desktop on your Raspberry as you do with any other Windows machine.

    sudo apt-get install mono-runtime

    this will install Mono and all what is needed to run a .NET program.

    sudo apt-get install git

    Very useful if you need to download Git projects. I’ll explain how to use it in the next part of this article as I’ve used it.

    Now we go thru the download of the BCM2835 library, the compilation, the installation:

    wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.25.tar.gz

    tar -zxvf bcm2835-1.25.tar.gz

    cd bcm2835-1.25

    ./configure

    make

    sudo make install

    cd src

    cc -shared bcm2835.o -o libbcm2835.so

    cd

    rm bcm2835-1.25.tar.gz

    Now, you have to copy the libbcm2835.so library with the executable you’ll produce at the end. And for the moment you’re done with the RaspberryPi.

    Go on Windows, download the code from https://github.com/cypherkey/RaspberryPi.Net/, open the project in Visual Studio, if like me you’re using the 2012 version, you may have to change the target to .Net Framework 4.0, change the pin you want to use to put a led, compile the project, put the exe and the so on a USB stick for example, or use any other way to copy it on your Rapsberry.

    image

    I’ve used GPIO 7 (pin 11 on the board), ground on pin 9, I put a Led and a 60 ohms resistor.

    Use this site as a reference to know which pin does what: http://elinux.org/Rpi_Low-level_peripherals. Very well done, with all the details.

    image

    On the Raspberry, run the following command line from the directory you’ve copied the exe and the so file:

    Sudo mono yourprogramm.exe

    of course, yourprogramm here represent the name of the program you’ve done. And in my case I made the link blinking.

    The way it’s working is quite simple. Like in a normal .Net program, you can import external DLL like the system ones. Here, you import the same way but of course not DLL but object. See the following example:

     

    [DllImport("libbcm2835.so", EntryPoint = "bcm2835_spi_transfernb")]
    static extern void bcm2835_spi_transfernb(
    [MarshalAs(UnmanagedType.LPArray)]byte[] tbuf, 
    [MarshalAs(UnmanagedType.LPArray)]byte[] rbuf, UInt32 len);
    

    In this case, it’s interesting as you’ll have to use the Marshaling. And you’ll have to do in a similar way for all the array, strings, pointers and anything what is not a very simple as byte, uint or other. Be also very careful on the length of the parameters, make sure you match them correctly.

    And I’ve quickly implemented a compatible class for Cpu.Pin, SPI, OutputPort and PWM by importing the various functions from the BCM2835 library.

    My main goal was to reuse the maximum of code from an SPI example piloting a 3.5$ Nokia 5110 LCD screen. I reused the code found on the Netduino wiki page: http://wiki.netduino.com/Nokia-5110-LCD.ashx?HL=nokia.

    I did it in couple of hours, exporting the definition from the NETMF code to a new project. As a result, I manage to make it work perfectly on my RaspberryPi!! Cool Sourire 

    image

    Now, I quickly found lots of limitation in the BCM2835 library I’ve used. PWM was not implemented, I found bugs in the SPI part, no support of interruptions, and others.

    So I’ve looked at another library and I found the WiringPi http://wiringpi.com/. This library was a bit more complete. Still not the perfect one but more complete. It does implement SPI, i2c, soft and hard PWM and interruption support for the digital IO. It does support also advanced implementations like the helpers we can find in NETMF.

    And from this points, I’ve used the NETMF portability kit with all the definitions, all the sources to make it as much compatible as possible with NETMF. See my next posts to find more about it and find the Codeplex project associated.

    Ultimately, I’d love to port the full NETMF on RaspberryPi but for this, I’ll need much more time than I have or other very motivated persons.

  • Laurent Ellerbach

    Ultrasound sensor and .NET Microframework (NETMF)

    • 0 Comments

    I recently bought a cheap ultrasound sensor because I want to build a robot. Yes, after the sprinkler management system and the automation of my Lego city, it’s time to play with a robot Sourire. This sensor can measure the distance from 2 cm to approximately 50 cm. Cost is about 5$.

    There are 4 pins:

    • VCC = 5V
    • Trig = send an impulsion and you’ll get a signal back in the Echo pin
    • Echo = the signal back from the impulsion
    • Gnd = ground

    So the usage is very simple, just use 2 pins in the Netduino board, 1 for Trig and 1 for Echo. In terms of code, it will be simple: output the value 1 (high) on the trig pin, then put it back to 0 (low), wait for an answer on Echo pin. Then you measure the time between the value sent and the way back. It will give you the distance. Best is to use the Ticks generated by DateTime. The difference will give 2 times the distance which you’ll have to divide by the celerity of the air (so approx 340 m per second). And that’s for the theory Sourire

    When you look at the reality, it’s always very different Rire. I’ve done measurement and he is the graph with points and ticks every centimeter:

    image

    What we can clearly see is a distortion when the object is close, the distance is not very well evaluated and it’s a pretty much linear curve for the rest. I’ve removed the 1cm point and made a linear regression. The initial point is at 4000 ticks.

    Then the step is about 636 per centimeter. And with those 2 values I can now easily calculate a distance.

    I’ve look at the web to search some existing code and there are existing classes. I pick one which was well written by John E. Wilson and posted in the Netduino forum. I’ve simplified a bit the code and change values for calibration. The simplify code looks like:

        public class HC_SR04
        {
            private OutputPort portOut;
            private InterruptPort interIn;
            private long beginTick;
            private long endTick;
            private long minTicks = 0;  // System latency, 
    subtracted off ticks to find actual sound travel time
    
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="pinTrig">Netduino pin connected to the HC-SR04 Trig pin</param>
            /// <param name="pinEcho">Netduino pin connected to the HC-SR04 Echo pin</param>
            public HC_SR04(Cpu.Pin pinTrig, Cpu.Pin pinEcho)
            {
                portOut = new OutputPort(pinTrig, false);
                interIn = new InterruptPort(pinEcho, false, 
    Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
                interIn.OnInterrupt += new NativeEventHandler(interIn_OnInterrupt);
                minTicks = 4000L;
    
            }
    
            /// <summary>
            /// Trigger a sensor reading
            /// 
            /// </summary>
            /// <returns>Number of mm to the object</returns>
            public long Ping()
            {
                // Reset Sensor
                portOut.Write(true);
                Thread.Sleep(1);
    
                // Start Clock
                endTick = 0L;
                beginTick = System.DateTime.Now.Ticks;
                // Trigger Sonic Pulse
                portOut.Write(false);
    
                // Wait 1/20 second (this could be set as a variable instead of constant)
                Thread.Sleep(50);
    
                if (endTick > 0L)
                {
                    // Calculate Difference
                    long elapsed = endTick - beginTick;
    
                    // Subtract out fixed overhead (interrupt lag, etc.)
                    elapsed -= minTicks;
                    if (elapsed < 0L)
                    {
                        elapsed = 0L;
                    }
    
                    // Return elapsed ticks
                    return elapsed * 10 / 636;
                    ;
                }
    
                // Sonic pulse wasn't detected within 1/20 second
                return -1L;
            }
    
            /// <summary>
            /// This interrupt will trigger when detector receives back reflected sonic pulse       
            /// </summary>
            /// <param name="data1">Not used</param>
            /// <param name="data2">Not used</param>
            /// <param name="time">Transfer to endTick to calculated sound pulse travel time</param>
            void interIn_OnInterrupt(uint data1, uint data2, DateTime time)
            {
                // Save the ticks when pulse was received back
                endTick = time.Ticks;
            }
        }

    The value returned is in milimeter. Usage of the class is extremely simple:

            public static void Main()
            {
                HC_SR04.HC_SR04 mUS = new HC_SR04.HC_SR04(Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5);
                while (true)
                {
                    Debug.Print(mUS.Ping().ToString());
                    Thread.Sleep(500);
                }
    
            }

    That’s it for the Ultra Sound sensor. Next sensor will probably be a presence sensor.

    Enjoy!

  • Laurent Ellerbach

    Adding Bluetooth support to a NETMF board (.NET Microframework)

    • 2 Comments

    I recently bought a very cheap Bluetooth adaptor for my Netduino. I wanted to test how easy/hard it is to support Bluetooth. I see lots of advantages with Bluetooth for a near field communication like piloting easily a robot with a Phone without the need of other network or Infrared. Also Bluetooth is a secured communication with a peering.

    So I bought this cheap Bluetooth adaptor for $8.20. It does expose itself to the world with a serial port on one side and as a normal Bluetooth device on the other side. Communication is supported with a serial port from one side to the other. On a PC, Phone or whatever device, it creates a serial port. So communication is basically very transparent and assimilated to a serial port from end to end.

    JY-MCU Arduino Bluetooth Wireless Serial Port ModuleJY-MCU Arduino Bluetooth Wireless Serial Port Module

    When I received it, I was impatient to test it. First step was to peer it with a PC. I looked at the documentation and found the default name for this device was “linvor” and found out the passkey was 1234. After cabling it with 3.3V (my board support 3.3V to 12V alimentation) and the ground, and approximately 1 minutes, I peered it!

    New step was to write a bit of code to test all this. I decided to do a very basic echo program. So whatever it will receive, it will send it back to the calling program. On the netduino board, I’ll use the COM1 (pins D0 and D1). I found also in less than 1 minute that the default configuration was 9600 bauds, 8 bits, no parity and 1 bit stop. So I wrote this very simple code for the test, very hard to do more basic than that:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using Microsoft.SPOT;
    using Microsoft.SPOT.Hardware;
    using SecretLabs.NETMF.Hardware;
    using SecretLabs.NETMF.Hardware.Netduino;
    using System.Text; 
    using System.IO.Ports;   
    
    namespace Bluetooth
    {
        public class Program
        {
            static SerialPort serial;
            public static void Main()
            {
                // initialize the serial port for COM1 (pins D0 and D1)             
                serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);             
                // open the serial-port, so we can send and receive data             
                serial.Open();             
                // add an event-handler for handling incoming data             
                serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);       
                //wait until the end of the Universe :-)
                
                Thread.Sleep(Timeout.Infinite);         
            }            
            static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)         
            {             
                // create a single byte array             
                byte[] bytes = new byte[1];                
                // as long as there is data waiting to be read             
                while (serial.BytesToRead > 0)             
                {                 
                    // read a single byte                 
                    serial.Read(bytes, 0, bytes.Length);                 
                    // send the same byte back                 
                    serial.Write(bytes, 0, bytes.Length);             
                }         
            }       
            
        }
    }
    

    I launch a simple serial port program like the old Hyper terminal on the PC where I peered the Bluetooth device and ran the test. Good surprise, I selected the port created on my PC (was port 6), open the port. The Bluetooth device went from a red blinking led to a always on led showing the device was correctly peered. Sounds good so far! So I typed “bonjour” and send it. instantly I get the “bonjour” back.

    So cool it’s working! I wanted to know more about the cheap and what can be setup, changes like the name of the device, the pin, the baud rate, etc. I used my preferred search engine Bing and quickly found out that it’s possible to change lots of things by sending couple of AT command. Those commands were used at the old age of modems Sourire It just remembered me that!

    Even if there are lots of cheap existing like the one I bought, most support exactly the same commands. I found a good documentation there. It’s not the same cheap and the AT commands are a bit different but I quickly found out that most were working. So I’ve decided to test if it was working. All what you have to do is send the commands when the device is not peered. You can do it either with a USB to serial FTDI cheap or directly from the board. I did it directly from the Netduino by modifying  a bit the code to send the commands. I found the most interesting commands were the following:

    • AT+NAMEnewname\r\n to change the device name, you get an answer
    • AT+PINxxxx\r\n to change the pin code, default is 1234
    • AT+BAUDx\r\n where X goes from 1 to 8 (1 = 1200 to 8 = 115200) to change the baud rate

     

    I send couple of commands to test and it worked just perfectly Sourire So I renamed the device to LaurelleBT instead of linvor. As the device was already peered, Windows did not had to reinstall drivers or cut the communication, it was just about changing the displayed name:

    image 

    So that’s it! In 5 minutes I had a working Bluetooth module on my board. I was positively surprised and I’ll buy more for sure! Next step it to mount it on a robot and pilot it from a Windows Phone or Windows 8 device.

  • Laurent Ellerbach

    .NET Microframework (NETMF) Web Server source code available

    • 3 Comments

    So as I got lots of asks to get the code of my Web Server, I’ve decided to create a Codeplex project. You’ll find the source here: https://netmfwebserver.codeplex.com/

    It does include the following features:

    • Start, stop, Pause the Web Server
    • Creation of a Web Server on any port
    • Fully functional multithread Web Server supporting GET only
    • Downloading any file present on a SD (or any other physical storage)
    • A full function to get all the parameters of a URL

    Examples of usage can be found on this blog. Working to publish a fully functional example.

    Feedback welcome!

  • Laurent Ellerbach

    Web Server and CSS files in NETMF (.NET Microframework)

    • 2 Comments

    It’s been a long time I did not write anything on my blog. Not that I haven’t developed anything but just because I did not take the time to write proper articles. I’ve continue to add features to my Lego city by piloting the trains but also the switches. And I’ll try to write articles to explain how to do that for all the features.

    But I will start with the modification of my Web Server to support CSS file. I did couple of demonstration of my development and each time I show the interface people were telling to me I need to work with a designer. And that’s what I finally did Sourire I worked with Michel Rousseau who is designer at Microsoft in the French team. And I gave him a challenge: “Design this simple web page without changing the code too much and keep it less than couple of K without any image”. Michel is used to design Windows 8 and Windows Phone apps but not very very simple page like the one I had.

    And he has done an excellent job! Here is the view before and after:

    imageimage

    Now I had to implement this in my code. As the brief was to have minimal effect on the code, I was expecting to implement this quickly. Reality was a bit different. It took me a bit more time than expected for the following reasons:

    • I had to implement in my basic web server a function to be able to download a file (the CSS one)
    • To read and download a file from an SD, you have to do it by chunk as the buffer size is limited (in the case of my Netduino 1024 bit)
    • Modify the main code to care about downloaded file and also add the lines of code to support CSS
    • But the main issue was that I’ve discovered that to be able to have a CSS file, you need to have the specific type “text/css”. This is to avoid cross domain fishing and other hacking

    So let see how to implement this step by step. So let start with the reading part of the file and how to send it. As explained in the last point, a CSS file has to have the correct mime type in the header. In fact, Internet Explorer and most of the other browsers such as Chrome and Firefox does not need the mime type to determine what kind of fire you are downloading. They do it with the mime type and/or with the extension. Most of the time, it’s just with the extension and reading the header of the file. But for security reason, it’s better if you have to determine correctly the type matching with the extension and the header of the file. And for CSS, it is forced like this to reinforce the security in Internet Explorer 8, 9 and 10.

    So as I had to implement this feature for CSS, I made a simple function to support some types I’ll use in other creation:

            public static void SendFileOverHTTP(Socket response, string strFilePath)
            {
                string ContentType = "text/html";
                //determine the type of file for the http header
                if (strFilePath.IndexOf(".cs") != -1 ||
                    strFilePath.IndexOf(".txt") != -1 ||
                    strFilePath.IndexOf(".csproj") != -1
                )
                {
                    ContentType = "text/plain";
                }
    
                if (strFilePath.IndexOf(".jpg") != -1 ||
                    strFilePath.IndexOf(".bmp") != -1 ||
                    strFilePath.IndexOf(".jpeg") != -1
                  )
                {
                    ContentType = "image";
                }
    
                if (strFilePath.IndexOf(".htm") != -1 ||
                    strFilePath.IndexOf(".html") != -1
                  )
                {
                    ContentType = "text/html";
                }
    
                if (strFilePath.IndexOf(".mp3") != -1)
                {
                    ContentType = "audio/mpeg";
                }
                if (strFilePath.IndexOf(".css") != -1)
                {
                    ContentType = "text/css";
                }
    
                string strResp = "HTTP/1.1 200 OK\r\nContent-Type: "
     + ContentType + "; charset=UTF-8\r\nCache-Control: 
    no-cache\r\nConnection: close\r\n\r\n";
                OutPutStream(response, strResp);

     

    So very simple and straight forward code. I do determine the extension of the file I want to read and create a ContentType variable with the right format. Then I build the HTTP header and send the header. Very simple, efficient and straight forward code. We are doing Embedded code, it’s not the code I would do in a normal development of a real web server. But it does the job there!

    Next step is reading the file and outputting it to the Socket. And I do this in the same function, right after this first part:

                FileStream fileToServe = null;
                try
                {
                    fileToServe = new FileStream(strFilePath, 
    FileMode.Open, FileAccess.Read);
                    long fileLength = fileToServe.Length;
                    // Now loops sending all the data.
    
                    byte[] buf = new byte[MAX_BUFF];
                    for (long bytesSent = 0; bytesSent < fileLength; )
                    {
                        // Determines amount of data left.
                        long bytesToRead = fileLength - bytesSent;
                        bytesToRead = bytesToRead < MAX_BUFF ? bytesToRead : MAX_BUFF;
                        // Reads the data.
                        fileToServe.Read(buf, 0, (int)bytesToRead);
                        // Writes data to browser
                        response.Send(buf, 0, (int)bytesToRead, SocketFlags.None);
    
                        System.Threading.Thread.Sleep(100);
                        // Updates bytes read.
                        bytesSent += bytesToRead;
                    }
                    fileToServe.Close();
                }
                catch (Exception e)
                {
                    if (fileToServe != null)
                    {
                        fileToServe.Close();
                    }
                    throw e;
                }
    
            }

    First step is to create a FileStream and create the Stream with the path of the file and read the length of the file. MAX_BUFF = 1024 and is the maximum size of a buffer. It depends on the .NET Microframework Platform. And we will start a loop to read part of the file and send it.

    The System.Threading.Thread.Sleep(100) is necessary to allow some time for the system and other tasks. If you don’t put it and have other tasks, the risk is that the memory will get full very quickly and you’ll block all the code.

    In my