Laurent Ellerbach

Ce blog est principalement destiné à publier des informations relatives à Microsoft, à ses technologies, aux outils Visual Studio et à ses versions Express notamment Visual Basic

February, 2012

  • Laurent Ellerbach

    Using 2 identical I2C device on the same I2C bus

    • 0 Comments

    If you know a bit about I2C bus, it is impossible to use 2 identical devices with the same address on the bus. Read my previous article to understand more on how it’s working. But as always, you can find trick to make it works.

    In my case, I’m using a TC74 I2C temperature sensor from Microchip. Their bus address is the same (0x4D). Plugging 2 on the same bus will create a redundancy but that’s all what you’ll get. If you want to place them in 2 different locations, you’ll never be sure which one will give you the right temperature.

    I get the idea of powering on one sensor and powering the other one off to make sure only one of the device will be on and will respond to the requests. To do that I’m using one digital IO. In the state high (1), one of the sensor will be on and on state low (0) the other will be on.

    So I’ve decided to do the following hardware implementation:

    image

    and how it looks like for real Sourire

    WP_000164

    When D0 will be high (1), the TC74 #1 will be alimented and when it will be low (0), the TC74 #2 will be alimented. Both transistors are playing the role of switch there. Each TC74 need approximately 300 ms to get fully initialized. So in the code, before accessing any of the sensor, right after switching, we will have to wait a bit. But overall, this simple and smart solution will work with more than 2 sensors, if you need a third or a fourth one, just add another IO and do a bit of logic. And that’s it!

    The .NET Microframework (NETMF) code is very simple, based on the same example as the previous post, it will looks like this:

    public static void Main()
    {
        TC74Device MyTC74 = new TC74Device(0x4D); //0x4D
        OutputPort MySelect = new OutputPort(Pins.GPIO_PIN_D0, false);
        Thread.Sleep(1000);
        byte MyData;
        sbyte MyTemp;
        MyTC74.Init();
        MySelect.Write(!MySelect.Read());
        Thread.Sleep(1000);
        MyTC74.Init();
        while (MyTC74.IsReady())
        {
            MyTemp = MyTC74.ReadTemperature();
            Debug.Print("Temperature :" + MyTemp);
            MyData = MyTC74.ReadRegister();
            Debug.Print("Register :" + MyData);
            Thread.Sleep(1000);
            MySelect.Write(!MySelect.Read());
            Thread.Sleep(1000);
            //MyTC74.Standby(true);
        }
    }

    So nothing really different from the previous post except that a digital IO is created and the state is changed every time in the infinite loop. And there are a 1s sleep before access any of the sensor. As for the netduino, the component is the same, it is declared one time. But in terms of programming, we know we have 2 different sensors Sourire Up to you to create a class to select which sensor you want to measure and play with the sleep if needed.

    I did put the sensors outside (I’m on vacations in the mountains) and run the program. I put my fingers on one of the sensor so the temperature get higher. And the result is the following:

    Temperature :-4
    Register :64
    Temperature :21
    Register :64
    Temperature :-4
    Register :64
    Temperature :23
    Register :64
    Temperature :-4
    Register :64
    Temperature :24
    Register :64
    Temperature :-4
    Register :64
    Temperature :23
    Register :64
    Temperature :-4
    Register :64
    Temperature :21
    Register :64

    So as you can see, the outside temperature is –4 and my fingers warmed up the sensor to 21-23 degrees. It was cold so I did not wait the full time to get to the 37 degrees or so it should be Sourire

    I hope you’ll enjoy the trick Sourire Feedback from electronic guys welcome.

  • Laurent Ellerbach

    Using one temperature sensor with I2C protocol and .NET Micro framework on netduino board

    • 2 Comments

    I wanted to play with a temperature sensor. And when the time came to choose one, I was amaze to see how many of those sensor exists. Some were simple resistor like the light sensor I used in one of my previous example, some were more like transistors, and couple integrated more advanced features. And I choose a TC74 from Microchip as it includes an I2C communication protocol and was extremely cheap (less than 1€ for the cheap). And they were sold by 2 so I get 2 of them Sourire My main idea was to be able to get the temperature of both of them.

    So I started to understand how I2C was working. The basic idea is simple: you have a clock going from the master (the netduino board in my case) to slaves (the TC74) and a line with data which is bidirectional. So the master can speak to the slave and the slave to the master.

    Good explanation on how this bus works in details in Wikipedia for example. The main difficulty with this protocol is to understand that you are sending information and can continue to send or receive some depending on what you’ve asked. But I’ll explain this later. Every device has an address on the bus and will respond when this address is send on the bus. That’s the ACK below.

    This table is coming from the TC74 documentation and explain how to write, read and receive a byte from the TC74.

    image

    There are simple commands and more complex one. The more complex one are usually accessing registers to setup and tweak a bit the device. In the case of the TC74, the register can be read and write. But it’s extremely simple as there are only 2 registers. One to see if a temperature is ready to read and one to put the device in standby mode or read if it is standby.

    image

    And the associated value to the register is simple also. D[7] is the high bit and D[0] the lowest one.

    image

    Then the read function return the temperature in a sbyte according the to table bellow:

    image

    Last but not least, here is how to connect the pins:

    image

    You don’t have to forget to put a resistor between the SDA and SCL lines like in the schema here. I used 10KΩ resistors and it’s working perfectly. I need to run more tests to see how long the cables cans be. I guess that if I need long cables, I’ll need to lower the value of this resistor.

    That’s it for the hardware part. Now, on the soft part, I started to search using bing and found couple of good articles to explain how to use I2C. This first one gives you an overall example and this second one a class to be used with multiples slaves. What I liked with the second one is that it’s easy to use it with multiples slaves. And in the future, I may want to add other sensors like a barometer and humidity sensor using I2C. Or even create my own I2C sensor as there are existing chip to be the interface.

    On top of this code, I’ve implemented a class called TC74 which implement all features of this sensor and calling the I2C class. So the overall code is quite simple.

    namespace TC74
    {
        //Command Code Function
        //RTR 00h Read Temperature (TEMP)
        //RWCR 01h Read/Write Configuration
        //(CONFIG)
        public enum TC74Command: byte
        {
            ReadTemperature = 0x00,
            ReadWriteRegister = 0x01
        };
        
        public enum TC74Config: byte
        {
            READY = 0x40,
            STANDBY = 0x80
        };
        /// <summary>
        /// This is an I2C temperature sensor.
        /// </summary>
        public class TC74Device
        {
            private I2CDevice.Configuration _slaveConfig;
            private const int TransactionTimeout = 3000; // ms
            private const byte ClockRateKHz = 100;
            public byte Address { get; private set; }
    
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="address">I2C device address 
    of the TC74 temperature sensor
    </param> public TC74Device(byte address) { Address = address; _slaveConfig = new I2CDevice.Configuration(address,
    ClockRateKHz); } public sbyte ReadTemperature() { // write register address I2CBus.GetInstance().Write(_slaveConfig, new byte[]
    { (byte)TC74Command.ReadTemperature }, TransactionTimeout); // get the byte result byte[] data = new byte[1]; I2CBus.GetInstance().Read(_slaveConfig, data,
    TransactionTimeout); //force the convertion to a signed byte return (sbyte)data[0]; } public byte ReadRegister() { // get the Register byte[] data = new byte[1]; I2CBus.GetInstance().ReadRegister(_slaveConfig,
    (byte)TC74Command.ReadWriteRegister, data, TransactionTimeout); return data[0]; } public void Init() { byte[] data = new byte[2] { (byte)TC74Command.ReadWriteRegister, 0x00 }; I2CBus.GetInstance().Write(_slaveConfig, data, TransactionTimeout); I2CBus.GetInstance().Write(_slaveConfig, new byte[]
    { (byte)TC74Command.ReadTemperature }, TransactionTimeout); } public bool IsReady() { bool bready = false; byte ret = ReadRegister(); if ((ret | (byte)TC74Config.READY) == (byte)TC74Config.READY) bready = true; return bready; } public void Standby(bool stdby) { byte[] data = new byte[2] { (byte)TC74Command.ReadWriteRegister, 0x00 }; if (stdby) data[1] = (byte)TC74Config.STANDBY; I2CBus.GetInstance().Write(_slaveConfig, data, TransactionTimeout); } } }

    Starting with the constructor, the address need to be stored. This address is b1001101 (0x4D) as I have a TC74A5-5.0VCT. We will use it later in a sample code. And this device works very well at 100KHz.

    Then the function Init is there to initialize the device. First it write in the internal register the value 0 to make sure it is not in standby mode. And then it write ReadTemperature to make sure we’ll be able to read the temperature.

    The register function read the register and return the byte value.

    The IsReady function read the register to check if the device is ready. It is only ready when power is up for enough time and before shut down. It is also not ready when the device is on standby mode.

    Standby set or unset the standby mode. It write in the register the STANDBY value which is 0x80 (b10000000).

    So pretty straight forward code and simple as well.

    public static void Main()
    {
        TC74Device MyTC74 = new TC74Device(0x4D); //0x4D
        byte MyData;
        sbyte MyTemp;
        Thread.Sleep(1000); 
    MyTC74.Init(); while (MyTC74.IsReady()) { MyTemp = MyTC74.ReadTemperature(); Debug.Print("Temperature :" + MyTemp); MyData = MyTC74.ReadRegister(); Debug.Print("Register :" + MyData); Thread.Sleep(1000); //MyTC74.Standby(true); } }

    The basic example to use this sensor is also quite easy. The device is initialized with the 0x4D address. Then the device is initialized. And the temperature and register are ready every second, if you want to test the Standby function, just unhide the last line, it will put the device in the standby mode and the device won’t be ready so the code will return.

    If you’ve done something wrong, exception will be raised and your code will stop.

    Now that’s how to pilot one sensor. The question is what can be done to read 2 identical sensors with the same address? I did it Sourire and it will be the topic of the next post. Stay tune!

  • Laurent Ellerbach

    Some hard to pilot a Sprinkler with .NET Microframework

    • 0 Comments

    In previous post, I’ve explained I want to pilot my sprinklers with a netduino board. I’ve already write couple of articles around it, including how to create a HTTP web server, set up the date and time, manage parameters, launch timers, securing the access, pilot basic IO. I’ve also shown couple of examples including this Sprinkler solution during the French TechDays. The video is available. I just love .NET Microframework (NETMF) Sourire so good to have no OS such as Linux or Windows, just a managed .NET environment!

    During the TechDays, I get questions on the electronic part of this demo. So in this post, I’ll explain how I did it and show code example to make it happen. Back to my Sprinklers, the brand is Gardena. The electro valves I have to pilot are bi valves. They need a positive 9V pulse to open and a 9V negative one to close. Gardena do not publish any information regarding there valves but that is what I found with couple of tests.

    The netduino board have a 3.3V and a 5V alimentation and the intensity is limited if alimented with the USB port. So not really usable to generate a 9V pulse. Plus I don’t want to mix the netduino electric part and the valve one. So I will use simple photosensitive octocouplers. The way it’s working is simple, you have a led and a photosensitive transistor, when lighted, the transistor open. The great advantage is you have a very fast switching totally isolated circuit.

    I pick a cheap circuit with 4 octocouplers (ACPL-847-000E) as I will need 4 per valves.

    image

    The basic idea is to be able to be able to send some current in one way to open the valve and in the other to close it. And to pilot it, I will use the digital IO from the netduino. I will need 2 IO per vavle. One to pilot the “Open” and one to pilot the “Close”. I just can’t use only one IO as I will need to send short pulses to open and short pulses to close. I want to make sure I’ll close the valve as well as opening it. and not only one single pulse. One IO won’t be enough as I need to have 3 states: open, close and “do nothing”.

    When I will have the first IO open (let call it D0) at 1, I will open the valve. When the second one (D1) will be set at 1, I will close the valve. And of course when both will be at 0, nothing will happen as well as when both will be at 1. So I will need a bit of logic with the following table:

    D0 D1 Pin On Pin Off
    0 0 0 0
    0 1 0 1
    1 0 1 0
    1 1 0 0

    So with a bit of logic, you get quickly that Pin On = D0 && !D1 and Pin Off = !D0 && D1 (I’m using a programming convention here). So I will need couple of inverters and AND logical gates. I’ve also choose simple and cheap ones (MC14572UB and CD74HC08EE4). They costs couple of euro cents. Those components have all what I need.

    image

    For the purpose of this demo, I will use 2 inverted led (one green and one red) and will not send pulse but a permanent current. So it will be more demonstrative in this cold winter where I just can’t test all this for real with the sprinklers! I’ll need a new post during spring Sourire

    Now, when I put everything, here is the logical schema:

    image

    I will have to do this for each of my sprinklers. I have 3 sprinklers in total. And here is a picture of a real realization:

    WP_000160

    You can also see a push button in this picture (on the left with white and blue wires). I’m using it to do a manual open and close of the sprinklers. I’m using here the IO D10. When I’ll push the switch, it will close the valve if it is open and open it if it is closed.

    I’m done with the hardware part! Let see the code to pilot all this. The overall code for the Sprinkler class looks like this:

    public class Sprinkler {
        private bool MySpringlerisOpen = false;
        private int MySprinklerNumber;
        private bool MyManual = false;
        private OutputPort MySprOpen;
        private OutputPort MySprClose;
        private Timer MyTimerCallBack;
        private InterruptPort MyInterPort;
        private long MyTicksWait;
    
        public Sprinkler(int SprNum)
        {
            MySprinklerNumber = SprNum;
            MyTicksWait = DateTime.Now.Ticks;
            switch (SprNum)
            {
                case 0:
                    MySprOpen = new OutputPort(Pins.GPIO_PIN_D0, false);
                    MySprClose = new OutputPort(Pins.GPIO_PIN_D1, true);
                    MyInterPort = new InterruptPort(Pins.GPIO_PIN_D10,
    false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh); break; case 1: MySprOpen = new OutputPort(Pins.GPIO_PIN_D2, false); MySprClose = new OutputPort(Pins.GPIO_PIN_D3, true); MyInterPort = new InterruptPort(Pins.GPIO_PIN_D11,
    false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh); break; case 2: MySprOpen = new OutputPort(Pins.GPIO_PIN_D4, false); MySprClose = new OutputPort(Pins.GPIO_PIN_D5, true); MyInterPort = new InterruptPort(Pins.GPIO_PIN_D12,
    false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh); break; } if (MyInterPort != null) MyInterPort.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt); } // manual opening based on an interupt port // this function is called when a button is pressed static void IntButton_OnInterrupt(uint port, uint state, DateTime time) { int a = -1; switch (port) { case (uint)Pins.GPIO_PIN_D10: a = 0; break; case (uint)Pins.GPIO_PIN_D11: a = 1; break; case (uint)Pins.GPIO_PIN_D12: a = 2; break; } if (a >= 0) { //wait at least 2s before doing anything if ((time.Ticks - MyHttpServer.Springlers[a].MyTicksWait) > 20000000) { if (!MyHttpServer.Springlers[a].MySpringlerisOpen) { MyHttpServer.Springlers[a].Manual = true; MyHttpServer.Springlers[a].Open = true; } else { MyHttpServer.Springlers[a].Open = false; } MyHttpServer.Springlers[a].MyTicksWait = DateTime.Now.Ticks; } } } // open or close a sprinkler public bool Open { get { return MySpringlerisOpen; } set { MySpringlerisOpen = value; //do harware here if (MySpringlerisOpen) { MySprOpen.Write(true); MySprClose.Write(false); } else { MySprOpen.Write(false); MySprClose.Write(true); MyManual = false; } } } public bool Manual { get { return MyManual; } set { MyManual = value; } } //read only property public int SprinklerNumber { get { return MySprinklerNumber; } } public Timer TimerCallBack { get { return MyTimerCallBack; } set { MyTimerCallBack = value; } } }

    Have a look at the previous posts to understand how to use it thru a web server. This part, is only the class to pilot the sprinklers. I know I only have 3 sprinklers so there are many things hardcoded. It’s embedded and no one else will use this code. It’s more efficient like this. The size of the program has to be less than 64K (yes K and not M or G!). The netduino board has only 64K available to store the program.

    The initialization of the class will create 2 OutputPort per valve. As explain in the hardware part, one to open and one to close the valve. It will also create one InterruptPort to be able to manually open and close the valve. In order to understand how those ports are working, please refer to this post.The initialization will setup to port with default values. False for the pin D0 which pilot the “open” valve and True for the pin D1 which pilot the “close” valve.

    The IntButton_OnInterrupt function will be called when a switch will be pressed. Depending on the pin, it will close or open the valve linked to the specific pin.

    The Open property will open or close the valve. In my project, I’ll use pulse to open the valve, for this demo, I’m using continued output so the led will be either red (close) or green (open). The 2 leds are mounted in an opposite way so when the current is in one way it will be red and in the other it will be green.

    The TimerCallBack function is used when a Sprinkler need to be switch off. The associated code is:

    static void ClockTimer_Tick(object sender)
    {
        DateTime now = DateTime.Now;
        Debug.Print(now.ToString("MM/dd/yyyy hh:mm:ss"));
        //do we have a Sprinkler to open? long initialtick = now.Ticks;
        long actualtick;
        for (int i = 0; i < SprinklerPrograms.Count; i++)
        { 
            SprinklerProgram MySpr = (SprinklerProgram)SprinklerPrograms[i];
            actualtick = MySpr.DateTimeStart.Ticks;
            if (initialtick>=actualtick)
            { // this is the time to open a sprinkler Debug.Print("Sprinkling " + i + " date time " + now.ToString("MM/dd/yyyy hh:mm:ss"));
                Springlers[MySpr.SprinklerNumber].Manual = false;
                Springlers[MySpr.SprinklerNumber].Open = true;
                // it will close all sprinkler in the desired time of sprinkling. Timer will be called only once. //10000 ticks in 1 milisecond Springlers[MySpr.SprinklerNumber].TimerCallBack = 
    new Timer(new TimerCallback(ClockStopSprinkler), null, (int)MySpr.Duration.Ticks/10000, 0); SprinklerPrograms.RemoveAt(i); } }

    The ClockTimer_Tick fonction is called every 60 seconds. It check if a sprinkler need to be switch one. If yes, a timer is created and associated with the TimerCallBack timer. And this timer will be called after the amount of time programmed to be open.

    static void ClockStopSprinkler(object sender)
    {
        Debug.Print("Stop sprinkling " + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));
        //close all sprinklers if automatic mode for (int i = 0; i < NUMBER_SPRINKLERS; i++)
        {
            if (Springlers[i].Manual == false)
            {
                Springlers[i].Open = false;
                Springlers[i].TimerCallBack.Dispose();
            }
        }
    }

    The function is quite simple, it just call the Open property to close all the spinklers. I’ve decided to do this as in any case, I don’t have enough pressure to have all them open. Of course, to be complete, all timers will be close. The Manual check will not close the sprinkler.

    So that’s it for this post. I hope you’ll enjoy it! And this time, I’m not in a plane to write this post, I’m on vacation Sourire

  • Laurent Ellerbach

    Using basic IO with .NET Microframework

    • 0 Comments

    Here is the code from my first French TechDays demo. The video is available here. During this first demo, I explained how to use the IO in a simple way: OutputPort, InterruptPort, InputPort and Analogic input ports. So those ports are really the basic one you can use in a .NET Microframework (NETMF) boards like the netduino one I’m using. All boards do also includes more advances IO like UART (serial), SPI, I2C and more. I’ll probably do other posts to explain how to use more advance ports.

    Here is the structure of the code and the global variables. I’ll use this to explain each function later in this code.

    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.NetduinoPlus;
    
    namespace ButtonBlinking
    {
        public class Program {
            static OutputPort LED;
            static InterruptPort IntButton;
            static InputPort Button;
            static bool IsBlinking = false;
    
            public static void Main()
            public static void Blinking()
            public static void LightWhenOpturated()
            static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
            public static void Blink()
            public static void ButtonPressedLight()
            public static void ReadAnalogic()
        }
    }

    As their name are very explicit, an OutputPort is a digital IO port to do an outpout. Main functions are Write and Read. And you write or read a boolean. Write(true) will outpout a high signal (1) and write(false) will output a low signal (0).

    An InputPort is also very explicit Sourire. You can read it. It can be high (true, 1) or low (false, 0).

    An InterruptPort is an input port but it can raise interruption when there is a change in the status of the the port.

    Now you get the bases, lets go for more. To execute this code, you’ll need couple of hardware things:

    • leds (1 or 2 is quite enough)
    • resistors (65 ohms)
    • a press button or a switch
    • a light sensor resistor or temperature sensor resistor

    For the main function, the code is simple, it just call the various sub function we will review in detail. So unhide the function you want to execute:

    public static void Main()
    {
        //Demo 1: blinking a led //Blinking(); //Demo 2: lighting a led when button pressed //ButtonPressedLight(); //Demo 3: blinking a led when a sensor is opturated //LightWhenOpturated(); //Demo 4: Analogic input //ReadAnalogic(); Thread.Sleep(Timeout.Infinite);
    }

    So we will start with the most basic demo you can do with IO on a NETMF board like netduino: blinking a led. This this the hello world of hardware Sourire

    The code is very simple:

    public static void Blinking()
    {
        //Open an OutputPort LED = new OutputPort(Pins.GPIO_PIN_D0, true);
        while (true)
        {
            //write the opposite value read in the LED status LED.Write(!LED.Read());
            //wait some time Thread.Sleep(200);
            //effect = blinking }
    }

    You create an OutputPort one of of the digitial IO. Here the 0. And in an infinite loop, you write the opposite of the value you read on this port. And wait couple of milliseconds before doing this operation.

    image

    On the hardware part, all what you have to know is a simple rule: U = RxI

    The output port is a 3.3V on a netduino. And reading the documentation of a led, the average voltage is 2V (vary a bit from green, red and orange) and the current it normally need to receive is 0.02A.

    So applying this to calculate the needed resistor will give R = (3,3 – 2)/0,02 = 65 Ohms

     

     

    Plug the resistor on pin D0 of the board, and then the led and connect it to the ground.

    image

    Run the code. And the magic happen Sourire you have a lighting led. Wow!!!! Congratulations, you’ve achieve level 1: blinking the led. Good! Want more? OK, you’ll get more.

    Now let control the led regarding the state of a press button (or interrupter/switch).

    The code is also very simple and straight forward:

    public static void ButtonPressedLight()
    {
        LED = new OutputPort(Pins.GPIO_PIN_D0, true);
        Button = new InputPort(Pins.GPIO_PIN_D5, false, Port.ResistorMode.PullUp);
        while (true)
        {
            LED.Write(Button.Read());
            Thread.Sleep(10);
        }
    
    }

    We are creating an outpout port, an input port and in an infinite loop, we aligned the state of the button with the state of the switch. On the electronic part, also very simple:

    image

    So when you will close the switch, the state of the input port will go to the ground so to 0 and it will switch off the Led. When the switch will be open, the state is high so 1 and the led will be lighted.

    The next one is about the same. The idea is to blink the led 5 times when the switch is closed and 5 times when it is open again. As you can imagine, it’s not easy to do in an in finite loop like previously. Plus, you will probably use your board to do something while you are waiting for the user to do an action or to get a state change on a pin. For this, there is then interrupt port. The idea is to raise an event when the state change. And as you will do with a regular .NET code, you’ll handle it and do something in this event handler.

    Here is the code:

    public static void LightWhenOpturated()
    {
        LED = new OutputPort(Pins.GPIO_PIN_D0, false);
        // the pin will generate interrupt on high and low edges IntButton = new InterruptPort(Pins.GPIO_PIN_D12, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
        // add an interrupt handler to the pin IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
    }
    
    static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
    {
        if (IsBlinking == false)
            Blink();
    }    
    
    public static void Blink()
    {
        IsBlinking = true;
        int i = 0;
        while (i<5)
        {
            LED.Write(!LED.Read());
            Thread.Sleep(200);
            i++;
        }
        IsBlinking = false;
    }

    The main “difficulty” is to declare correctly the interrupt port and the event handler. You can choose with the last parameter to generate the interruption only when you change from state low to high or high to low or both direction. Here it’s bi directional so on both edges. The function IntButton_OnInterrupt will be called when the sate will change. And from there the function to blink. I’ve voluntarily added some “control” code to not call the blinking function when it’s already in use.

    On the hardware part, it’s like the previous one except that I’m using here pin 12. This is because I used a more sophisticated switch. See this post.

    When you will close the switch, the light will blink 5 times so if if was off, it will finish on. Now open it again, it will blink 5 times and go back to off. So what happen if you close it and open it before you light finish to blink? will the IntButton_OnInterrupt function be called right away and because of the test to see if we are blinking or not, it will stay on the on mode after blinking 5 times? Or not?

    Answer is not! The interruption are serialized and it wait for the previous one to finish. So it will wait for the blink function to return to call the IntButton_OnInterrupt one. And that’s the mane reason why you have as a parameter the DateTime parameter. So if you did not have time to handle an interruption and want to skip it you can do it.

    Still there? Wow, that’s cool, you want some more? yes? OK, so let go for the last part: the analogic input. To illustrate this, we will need to use a temperature or light resistor. It cost couple of euros cents. They act like a resistor. The resistor vary regarding the temperature or the light.

    image

    To change a bit, let start with the hardware part. In my case I’m using a very simple light sensor acting like a resistor from 1M Ω (dark) to 100 Ω (very bright).

    In order to use it and use the most of the input range (in netduino 1024 points), you need to know the average resistance which can be calculated like this: R = √(MaxR x MinR) = √(1M x 100) = 10K Ω

     

    image

    We will use the following simple electronic schema to measure the variance of the light sensor. Again, all what you need as an electronic knowledge is U = RxI

    The analogic input Voltage = 3.3/(1+R/RL).

    So it will vary from 0.0323V (very bright) to 3.23V (dark). The medium value will be 1.5V and will be attained when the light sensor will be at its mid point resistance our famous 10K ohms.

     

     

     

    Now, lets go for the code:

    public static void ReadAnalogic()
    {
        SecretLabs.NETMF.Hardware.AnalogInput lightSensor 
    = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); //lightSensor.SetRange(0, 100); int lightSensorReading = 0; while (true) { lightSensorReading = lightSensor.Read(); Debug.Print(lightSensorReading.ToString()); Thread.Sleep(500); } }

    We create an AnalogicInput object. All those classes are different depending on the board you are using. But they are working almost the same way. The idea is to open a pin as analogic, setup a range for the values so it makes more easy to read a data and avoid you to do the transformation. So rather than having a 0 to 1024 value, you can directly get a –25 to 120 value if you want (for a temperature sensor for example).

    Reading the value is quite easy, just use the Read property and you’ll get the transformed value. In my case, for the example, I’m just doing a loop. In reality, it will be much better to use a timer for example. To know how to use timer, just read one of my previous article.

    This is it for this long explaining how to to use the basics IO in NETFM with a netduino board. Enjoy the electronic part! More to come Sourire I had a bit of time to write this one on my way back from Dubai in a nice A380.

  • Laurent Ellerbach

    Securing web server with login and password on .NET Microframework

    • 1 Comments

    If you want to expose your .NET Microframework web server on the Internet you better have to make sure it is protected. Even if you keep the URL secret, it will not stay secret Sourire it’s not a good way to protect it! A better way if you have to expose it is to use a login and a password. Still not the best way but reasonably ok. To add more security, use https on your board. But if you are using a netduino board like me, the https web server is a big too big and will not fit. So I’ve decided to use login/password.

    the good news is that you already have classes to be able to implement a login and password very easily. First read this post to understand how to implement a web server. In the main function ProcessClientGetRequest, just add at the beginning of the function the following code:

    string strResp = "<HTML><BODY>netduino sprinkler<p>";
    bool IsSecured = false;
    if (request.Credentials != null)
    { // Parse and Decode string.
        Debug.Print("User Name : " + request.Credentials.UserName);
        Debug.Print("Password : " + request.Credentials.Password);
        // if the username and password are right, then we are authenticated
        if (request.Credentials.UserName == MyLogin && 
    request.Credentials.Password == MyPassword) { IsSecured = true; } } //if not secured display an error message. And return the function if (!IsSecured) { strResp += "<p>Authentication required<p>Invalid login or password"; response.StatusCode = (int)HttpStatusCode.Unauthorized; response.Headers.Add("WWW-Authenticate: Basic realm=\"netduino sprinkler\""); strResp += "</BODY></HTML>"; byte[] messageBody = Encoding.UTF8.GetBytes(strResp); response.ContentType = "text/html"; response.OutputStream.Write(messageBody, 0, messageBody.Length); return; }

    The IsSecured boolean is there to check if the login and password are valid. by default it is set at false. Then the request.Credentials contains the login credentials. If they are not null, you can just check if they are the right ones. UserName contains the user name and guess what, Password contains the password. Not rocket science there Sourire just compare them with the one you want to use. By default the authentication type is basic. So login and password are send in clear mode. Not the most secure but unfortunately like most of the internet protocol still use for mail like POP and other protocols like that. Again, to get more protection, use https if you can on your board.

    .NET Micro framework web server object also support Windows Live authentication. As I’m doing this code in a plane from Moscow to Dubai, quite hard to test it!

    request.Credentials.AuthenticationType = AuthenticationType.Basic;
    request.Credentials.AuthenticationType = AuthenticationType.WindowsLive;

    Those are the 2 modes. If I have the opmportunity, I’ll test the Windows Live one. If anyone do the test, please let me know!

    The rest of the code is pretty straight forward, it’s just about displaying an error message with the HttpStutusCode as Unauthorized. And just return the function before the end. Very efficient to protect all site and all pages. The good news is that you will not have to authenticate to access all pages, it will be carry on from one page to another up to when you’ll close your browser and come back. I do not recommend to store any login/password on any of your pc, or device. Always type them!

    If you want to store somewhere the login and password, you can store it on the SD card of your board. If your board is store in a secure location in your house and not accessible, you may not need to crypt the login and password. And anyway if the person has access to your board controlling some equipment, it’s probably too late. But if you need, crypt it on the storing location.

    Here is a code example how to read this login and password from a file.

    if (Microsoft.SPOT.Hardware.SystemInfo.IsEmulator)
        strDefaultDir = "WINFS";
    else
        strDefaultDir = "SD";
    
    FileStream fileToRead = null;
    try
    {
        fileToRead = new FileStream(strDefaultDir + "\\" 
    + strFileProgram, FileMode.Open, FileAccess.Read); long fileLength = fileToRead.Length; Debug.Print("File length " + fileLength); //file length has to be less than 1024 otherwise, it will raise an exception byte[] buf = new byte[fileLength]; string mySetupString = ""; // Reads the data. fileToRead.Read(buf, 0, (int)fileLength); // convert the read into a string mySetupString = new String(Encoding.UTF8.GetChars(buf)); int mps = mySetupString.IndexOf(ParamSeparator); MyLogin = mySetupString.Substring(0, mps); MyPassword = mySetupString.Substring(mps + 1, mySetupString.Length - mps-1); fileToRead.Close(); } catch (Exception e) { if (fileToRead != null) { fileToRead.Close(); } //throw e; Debug.Print(e.Message); }

    strFileProgram contain the file name in which you ware storing the login and password. And ParamSeparator is a char containing a separator character between the login and password stored. So it’s a character that you will not allow to use in a login or password. So use a character like return. As it can’t be used in a login and password.

    The first couple of lines are to identify if we are running in the emulator or not. In the emulator storing access is by default in the WINFS directory. And on the netduino board in SD. Then the code is very similar to what I’ve already shown in this article. The login and password will have to be stored in a global string of the http server class. As explain in the mentioned article, you can also save those data quite easily using the same file. And you can also build a simple page to allow the user (usually you) to change this login and password. In my case I’m not doing it as I’m the only user. And if I need to change it, I can do it directly on the SD card. Of course, use a long login and a complex and long password. At least 10 character, a majuscule, a miniscule, a numeric and a non alphanumeric character. And you can consider you may be ok. But again, it’s not the most secure solution. So if you have to expose directly your board to the internet and you’ll need to access it thru the web sever, make sure you are not piloting any critical part of your house or resources.

    As always, any feedback welcome.

Page 1 of 1 (5 items)