KSduino 

 The Device Network
ABOUT | BLOG | FORUM | DOWNLOADS | DEVELOPMENT | DEVICES | DONATE | USER
Your IP Address is:

BLOG

  • Multitasking and real time systems at Arduino

    Posted on Wed March 6, 2013

    image

    to make your sketch more professional

    Real time operating system

    A real-time operating system (RTOS) is an operating system (OS) intended to serve real-time application requests. It must be able to process data as it comes in, typically without buffering delays. Processing time requirements (including any OS delay) are measured in tenths of seconds or shorter.

    It’s not a secret that the Arduino has small system resources. Is it possible to run RTOS at Arduino? Fortunately, yes! There is some RTOS which can run at Arduino. We have used some of them and our choice is ChibiOS.

    ChibiOS/RT is designed for deeply embedded real time applications where execution efficiency and compact code are important requirements. This RTOS is characterized by its high portability, compact size and, mainly, by its architecture optimized for extremely efficient context switching.

    What plus we get when use RTOS

    All examples which you see when study Arduino or KSduino is a Linear program. The basic part of program are placed to Loop function and run from its begin to the end. There is a delay at the end of loop when our Arduino does nothing. This kind of program for similar tasks, for example: we read some sensors one time per second. What should we do if we need read light sensor every ten milliseconds and read temperature sensor one time per five seconds. We can write this program in standard loop using millis function and much if which will perform this task. This program will work but the loops code will very long with much conditions. Programs like this is not readable, heavily modified and may contain difficult to debug errors.

    When we use RTOS we can create separate processes (threads). Each of the processes looks like separate loop function. So we can create a program with much loops each of they will execute its separate task. In our example the first process will read light sensor with quick delay the second process will read temperature sensors with long delay. Both of this process will send information to KSduino.

    Setup RTOS and create sketches

    How the RTOS for Arduino looks and what we will use to create programs? The ChibiOS is a simple library which should be copied to your Arduinos library folder. And the program is simple Arduino sketch created in Arduino IDE.

    OK, Let us download ChibiOS and create your first ChibiOS Arduino sketch!

    We get the ChibiOS from http://code.google.com/p/rtoslibs/ page. This page contain information about other RTOS and link for official ChibiOS website.

    There is direct download link, we use version ChibiOS20130208: http://ksdu.in/o/45

    You can download also this archive from the KSduino website:
    http://ksduino.org/downloads/ChibiOS20130208.zip

    The ChibiOS and KSduino example

    We modify example from previous article: Pseudo multitasking. Using timer in Arduino sketch

    In this example we create process:

    • Thread 1 for sending parameters to KSduino
    • Thread 2 for calculate DS18b20 temperature
    • Thread 3 for read KSduino answers

    To synchronize KSduino tasks we use semafor so in one time moment only one KSduino task can run.
    
    // Includes
    #include "SPI.h"
    #include "EthernetUdp.h"
    #include "OneWire.h"    // one wire library to connect ds18b20
    #include "ksDS18B20.h" 	// subclass to read ds18b20 values
    #include "KSduino.h"	// KSduino Library
    #include "ChibiOS_AVR.h" // ChibiOS library
    
    // DS18B20 address
    uint8_t  devAddr1[] = { 0x28, 0x16, 0x05, 0xB2, 0x03, 0x00, 0x00, 0x00 };
    
    // Define OneWire, DS18B20 & Timer classes
    OneWire ds(7);   // OneWire on pin 7
    ksDS18B20 ds18b20_1 (&ds, devAddr1);
    
    // KSduino User definition -----------------------------------------
    
    // This Arduino ID and Password
    // Change this values to yours
    unsigned int deviceID = 0000;	// set your Device ID
    unsigned int devicePwd = 1111;  // set your Device Password
    
    // This Arduino MAC, IP,DNS, Gateway and port 
    // Change this values to yours
    byte mac[] = { 0x74,0x69,0x69,0x2D,0x00,0x00 };
    byte ip[] =     { 192, 168, 1, 222 };
    //byte dns[] = { 192, 168, 1, 1 };
    //byte gateway [] = { 192, 168, 1, 1 };
    //byte subnet [] = { 255, 255, 255, 0 };  
    unsigned int port = 40000 + deviceID;
    
    // KSduino Server definition ----------------------------------------
    
    // Server address & port
    byte serverIp[] = { 178,63,53,233 };   // Global IP address
    unsigned int serverPort = 9930;        // Port number
    
    // ----------------------------------------------------------------
    
    // Define KSduino class
    KSduino ksd (deviceID, devicePwd, serverIp, serverPort);
    
    // Define some KSduino parameters with KSduinoParameter class
    KSduinoParameter d1 (&ksd, "d1", 0.5, 15*60);
    KSduinoParameter f1 (&ksd, "f1", 0.5, 15*60);
    
    // Semaphore to trigger context switch
    Semaphore sem;
    
    // Idle time counter
    volatile uint32_t count = 0;
    
    
    //------------------------------------------------------------------
    void setup()
    {
      ksd.begin(mac, ip, port);    // start KSduino
      // ksd.begin(mac, ip, dns, gateway, port);
      // ksd.begin(mac, ip, dns, gateway, subnet, port);
    
      // Setup ChibiOS
      chBegin(mainThread);
    }
    
    
    //------------------------------------------------------------------------------
    // thread 1 - this task send KSduino parameters
    // 160 byte stack for KSduino task, beyond task switch and interrupt needs
    static WORKING_AREA (waThread1, 160);
    
    static msg_t Thread1(void *arg) 
    {  
      #define WAIT_FOR_ANSWER 2
    
      for (;;) 
      {
        // Create D1 test value
        int d1_generator = random(1,1000);
        byte d1_value =  d1_generator < 10 ? 1 : 0; // will high ones per second
    
        // Send values to KSduino if something changed
        if (d1.check (d1_value) || ds18b20_1.getTemp () && f1.check(ds18b20_1.celsius))
        {
          chSemWait(&sem); // only one KSduino task should run at this moment
          
          ksd.beginPacket ();
          d1.addParameter (d1_value);
          f1.addParameter( ds18b20_1.celsius);
          ksd.endPacket (WAIT_FOR_ANSWER);
    
          chSemSignal(&sem);
        }
        chThdSleepMilliseconds(10);
      }
      
      return 0;
    }
    
    
    //------------------------------------------------------------------------------
    // thread 2 - calqulate DS18b20 thermometer every second
    // 64 byte stack beyond task switch and interrupt needs
    static WORKING_AREA (waThread2, 64);
    
    static msg_t Thread2(void *arg) 
    {
      
      for (;;) 
      {
        ds18b20_1.readTermometer();
        
        chThdSleepMilliseconds(1000);
      } 
    
      return 0;  
    }
    
    
    //------------------------------------------------------------------------------
    // thread 3 - KSduino update 
    // 160 byte stack for KSduino task, beyond task switch and interrupt needs
    
    static WORKING_AREA(waThread3, 160);
    
    static msg_t Thread3 (void *arg)
    {
      for (;;)
      {
        chSemWait(&sem); // only one KSduino task should run at this moment
        ksd.update ();
        chSemSignal(&sem);
    
        chThdSleepMilliseconds(5);
      }
      
      return 0;
    }
    
    
    //------------------------------------------------------------------------------
    void mainThread() {
      
      // initialize semaphore
      chSemInit(&sem, 1);
    
      // start blink thread
      chThdCreateStatic(waThread1, sizeof(waThread1),
        NORMALPRIO + 1, Thread1, NULL);
        
      // start print thread
      chThdCreateStatic(waThread2, sizeof(waThread2),
        NORMALPRIO + 1, Thread2, NULL);
        
      // start ksduino send thread
      chThdCreateStatic(waThread3, sizeof(waThread3),
        NORMALPRIO + 1, Thread3, NULL);
        
      // Main thread (iadle thread) loop 
      for (;;) 
      {
        // must insure increment is atomic
        // in case of context switch for print
        noInterrupts();
        count++;
        interrupts();
      }
    }
    
    
    //------------------------------------------------------------------
    void loop()
    {
      // not used
    }
    
    To use this example you need one DS18b20 sensor: https://ksdu.in/o/3h
    This sketch is using ksDS18B20 class. You can download this example with ksDS18B20 class at: http://ksdu.in/o/46

    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.
     

    Sea also:

    How to skip repeated parameter values?
    Pseudo multitasking. Using timer in Arduino sketch

  • Comments



  • Pseudo multitasking. Using timer in Arduino sketch

    Posted on Tue February 12, 2013

    image

    to make your sketch more professional

    The reason for using:

    Arduino’s standard sketch is a linear(plain) program. This means that all actions are performed sequentially. When we program the controller, there are situations when one sensor gives its values constantly but other one require our immediate reaction. The sketch may have no time to read the value and lost it. To prevent this we can use pseudo multitasking with timer functions.

    Where to get:

    There is simple and useful timer library called Timer by Simon Monk. It placed at Arduino’s Playground Libraries Timing (the first one):

    This timer library use callback functions which called after some events. We can originate calls every time period or once after needed time. This two simple functions are very useful to organize pseudo multitasking in our sketch and it use few resources of the controller.

    For example:

    We will use DS18b20 temperature sensor F1 parameter and one other test sensor D1 parameter.

    Algorithm of DS18b20:

    • Request to calculate temperature;
    • Read the temperature after 750ms-1000ms.

    Algorithm of D1 test parameter:

    • Most time of the D1 is LOW but some time it going to HIGH. We need read it very often (ones per 10ms) to check this high value.

    Implementation:

    To implement it in our normal sketch we will read D1 inside the one loop with delay 10ms, and will use timers after function to read DS18b20 values. In this case we will get all D1 HIGH values and read DS18b20 according its algorithm: read one value per second.

    
    #include "SPI.h"
    #include "EthernetUdp.h"
    #include "OneWire.h"    // one wire library to connect ds18b20
    #include "Timer.h"       // timer library
    #include "ksDS18B20.h" 	// subclass to read ds18b20 values
    #include "KSduino.h"	// KSduino Library
    
    // DS18B20 address
    uint8_t  devAddr1[] = { 0x28, 0x16, 0x05, 0xB2, 0x03, 0x00, 0x00, 0x00 };
    
    // Define OneWire, DS18B20 & Timer classes
    Timer t;
    OneWire ds(7);   // OneWire on pin 7
    ksDS18B20 ds18b20_1 (&ds, devAddr1);
    
    // KSduino User definition -----------------------------------------
    
    // This Arduino ID and Password
    // Change this values to yours
    unsigned int deviceID = 0000;	// set your Device ID
    unsigned int devicePwd = 1111;  // set your Device Password
    
    // This Arduino MAC, IP,DNS, Gateway and port 
    // Change this values to yours
    byte mac[] = { 0x74,0x69,0x69,0x2D,0x00,0x00 };
    byte ip[] =     { 192, 168, 1, 222 };
    //byte dns[] = { 192, 168, 1, 1 };
    //byte gateway [] = { 192, 168, 1, 1 };
    //byte subnet [] = { 255, 255, 255, 0 };  
    unsigned int port = 40000 + deviceID;
    
    // KSduino Server definition ----------------------------------------
    
    // Server address & port
    byte serverIp[] = { 178,63,53,233 };   // Global IP address
    unsigned int serverPort = 9930;        // Port number
    
    // ----------------------------------------------------------------
    
    // Define KSduino class
    KSduino ksd (deviceID, devicePwd, serverIp, serverPort);
    
    // Define some KSduino parameters with KSduinoParameter class
    KSduinoParameter d1 (&ksd, "d1", 0.5, 15*60);
    KSduinoParameter f1 (&ksd, "f1", 0.5, 15*60);
    
    
    //------------------------------------------------------------------
    void setup()
    {
      ksd.begin(mac, ip, port);    // start KSduino
      // ksd.begin(mac, ip, dns, gateway, port);
      // ksd.begin(mac, ip, dns, gateway, subnet, port);
    
      t.after (0, ksd_loop); 	// start main loop
      t.after (0, readDS18B20);    // start ds18b20 loop
    }
    
    
    //------------------------------------------------------------------
    void loop()
    {
      // Update timers functions
      t.update();
    
      // KSduino update function  
      ksd.update ();
    }
    
    
    //------------------------------------------------------------------
    /*
     * Read D1 value & Send Parameters to KSduino server
     */ 
    void ksd_loop()
    {  
      #define WAIT_FOR_ANSWER 2
    
      // Create D1 test value
      int d1_generator = random(1,1000);
      byte d1_value =  d1_generator < 10 ? 1 : 0; // will high ones per second
    
      // Send values to KSduino if something changed
      if (d1.check (d1_value) || ds18b20_1.getTemp () && f1.check(ds18b20_1.celsius))
      {
        ksd.beginPacket ();
        d1.addParameter (d1_value);
        f1.addParameter( ds18b20_1.celsius);
        ksd.endPacket (WAIT_FOR_ANSWER);
      }
      
      // Loops delay
      t.after (10, ksd_loop); 
    }
    
    
    //------------------------------------------------------------------
    /*
     * Read thermometer value
     */
    void readDS18B20 (void)
    {
      static byte cnt = 0;
      
      ds18b20_1.readTermometer();
      
      t.after (1000, readDS18B20);
    }
    
    To use this example you need one DS18b20 sensor: https://ksdu.in/o/3h
    This sketch is using ksDS18B20 class. You can download this example with ksDS18B20 class at: http://ksdu.in/o/3i

    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.
     

    Sea also:

    How to skip repeated parameter values?

  • Comments



  • Budget Arduino WiFi with TP-LINK WR702N Router

    Posted on Sun January 13, 2013

    The wires is not in fashion now. There is uncomfortable conducting wires in the apartment. It is not clear how to hold the wires in the garden. Yes, There is WiFi but the WiFi modules for Arduino is very expensive: https://ksdu.in/o/2l.

    About TP-LINK TL-WR702N:

    There is a low-cost and excellent performance router TP-LINK TL-WR702N which can be used with Arduino and Ethernet Shield.

    TL-WR702N is the smallest wireless router in the world and very easy to use. A traveler need simply plug a hotel room’s WAN cable into the port provided and enter a default password to instantly create a wireless hotspot in the room.

    The TL-WR702N is designed for use with tablets, smart phones, handheld game consoles and other portable electronic wireless devices. The device, which can be powered by an external power adapter or USB connection to a computer, can conveniently connect to the internet and share the connection around an average sized room at 150Mbps.The device’s tiny size makes it ideal for use on the road and is powerful enough to satisfy almost any basic wireless application requirement.

    See more information at the official TP-LINK website.

    Where to by TP-LINK TL-WR702N:

    You can find it in:

    Or look it in your local shops.

    The price is around $20. Plus the Arduino Ethernet shield w5100: http://ksdu.in/o/2o. The sum will be cheaper than price of one Arduino WiFi module.

    If you’ll take the enc28j60 module it will be even cheaper.
    See price for enc28j60: http://ksdu.in/o/2q.
    See the article: KSduino can connect your Arduino with Enc28j60.

    So if you’ll be use the Enc28j60 Ethernet Shield and the TP-LINK TL-WR702N Router the total WiFI cost will be about $25.

    How to use TP-LINK TL-WR702N:

    The WR702N Router is very easy to use. It has good documentation and intuitive Web interface. To get started:

    • You need to connect the router to your computer and configure it to Client Mode first. After this TP-LINK will be connect to your WiFi network.
       
    • Then you need to connect your Arduinu Ethernet Shield to TP-LINK Ethernet port and control Arduino as it is connected to the regular local network. You do not have to make any changes in the Arduino program to work with TP-LINK TL-WR702N Route.
       
    See the “Enc28J60 and Arduino (11)” article to detail instruction how to connect TP-LINK TL-WR702N.


    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.
     

    Sea also:


  • Comments



  • Using UDP protocol with Arduino

    Posted on Fri January 4, 2013

    The KSduino uses UDP to connect Arduino to Internet to show device parameters at KSduino web site.

    The UDP history:

    The User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without prior communications to set up special transmission channels or data paths. The protocol was designed by David P. Reed in 1980 and formally defined in RFC 768.

    UDP is most quick of basic Internet Protocols. But the UDP protocol does not guarantee 100% packet delivery since UDP has no built-confirmation as TCP for example. Stable connection using UDP releases using software protocols.

    About KSduino UDP send-receive algorithm:

    Sending and receiving UDP packets embedded in KSduino Library that can be downloaded from the DOWNLOADS.

    There are two modes to send values:

    1. synchronous
    2. asynchronous (default)

    See functions where these modes uses: http://ksdu.in/o/22

    In synchronous method we send value and are waiting answer from server. When we use this method we have not lost packets because KSduino waits for answer and repeat packets if it does not get answer during timeout. This method should be used with critical values. But this method takes lot of time from sketch: time minimum = ping to server time 3-4 milliseconds. This is not acceptable for normal operation! So we created another one - asynchronous mode.

    In asynchronous mode we send value, save it packet number to Queue and don’t wait answer after send. It takes only 1 - 3 milliseconds as ksd.getLoopTime() shows. We get a response from servers at next loops and check the Queue. We do not save sent Values because Arduino has not memory for it. So from time to time we have lost packets and wrong packets.

    In this method:

    Lost packet means: we have not got response from server but we don’t sure that server hoes not got this value;

    Wrong packet means: the server (or network) sends something that we are not waiting now! It may be some lost packet which going to long or broadcast or some parasite UDP network packet. We check only packets we are waiting now and drop all other. In most case Wrong packets - is packets which we don’t get during timeout. Arduino has low memory to save packets we send so we decided to have lost packets instead of spending time.

    In most case Lost packet means we don’t got response during timeout. But it’s not real lost packet.


    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.


  • Comments



  • KSduino can connect your Arduino with ENC28J60

    Posted on Sun December 23, 2012

    This allows you to add internet capability to your project. It can also be used to drive relays as only a few pins are used for the ENC28J60.

    KSduino can connect your Arduino with ENC28J60

    About ENC28J60:

    The ENC28J60 Ethernet Module utilizes the new Microchip ENC28J60 Stand-Alone Ethernet Controller IC featuring a host of features to handle most of the network protocol requirements. The board connects directly to Arduino and to most microcontrollers with a standard SPI interface with a transfer speed of up to 20MHz.

    The ENC28J60 contains all the necessary hardware to implement an Ethernet interface including the isolation transformer and LINK/STATUS LEDs. SPI interface makes this one of the easiest 10Base-T ICs yet! 3.3V with 8KB buffer.

    image

    The ENC28J60 is fully compatible with KSduino. ENC28J60 performance is not inferior to the standard Arduinos w5100 Ethernet Shield. One of the biggest factors of using ENC28J60 device is its low cost.

    Where to buy ENC28J60:

    Use online shopping to by it

    etc, there are so many offers. Or look it in your local shops.

    Hardware connection ENC28J60 to Arduino:

    ENC28J60 has 10 pins, but it used only 7 pins to connect ENC28J60 to Arduino. The ENC28J60 use standard SPI interface so you can connect 5 pins to the Arduinos ICSP connector one pin to the 3.3V and another one to the pin 8 (this is EtherCard library default ChipSelect (CS) pin).

    Use this link for additional information: http://ksdu.in/o/1c

    How to connect ENC28J60 to KSduino:

    To connect this device to KSduino we have used KSduino Library for Arduino with ENC28J60 and EtherCart Ethernet library. The EtherCat Ethernet library supports all the functions of ENC28J60 Ethernet Shield and has its own very interesting examples. The KSduino library uses EtherCart UDP stack and allows uses of all functions of KSduino and EtherCart libraries.

    Follow the instructions to download the librarys in the DEVELOPMENT page.

    See also:


    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.

    P.S. The short links was created with our new URL Shortener: http://ksdu.in/o/


  • Comments



  • How KSduino works

    Posted on Sat December 15, 2012

    The KSduino is public open source service, so you can connect your devices for free!

    How KSduino works

    In this article I want to describe how the KSduino works inside. The KSduino consists of four main blocks:

    • KSduino library for Arduino which uses in users sketches
       
    • KSduino server which receives packets from Arduino and sends answers back
       
    • SQL server which gets parameters from KSduino servers, calculate average and saves values
       
    • KSduino web server which displays Arduino parameters in table and graph mode, and processes users requests
       

    We used UDP transport to connect between the Arduino microcontroller and KSduino server. UDP is very fast so we can send a lot of sketches parameters not overloading the microcontroller in a short time. Our protocol allows to send and receive values ​​from any local network and does not require a dedicated ip address.

    KSduino server runs at Linux server. KSduino server is C language server program which uses our own technology to receive, processes and resends data from the microcontroller to SQL server.

    To save, calculate and select data we use MySQL server. Our own technology (as KSduino server) allow gets and save millions parameters received from arduino devices.

    The web site has own kernel writen at PHP. We use JQuery to run in clients part, RGraph library to display graphs, fancyBox to display images and Disqus to proccess comments. The Blog and Help partitions are constructed using Tumblr, the Documentation section built using Doxygen.


    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.


  • Comments



  • Welcome to KSduino

    Posted on Sat December 8, 2012

    We are opened from today so you are Welcome to KSduino device network!

    Welcome to KSduino

    We have designed this service to connect your Arduino microcontrollers to the KSduino device network for free. You may connect your devices and monitor all its parameters. KSduino parameters can handle any Arduinos state. It may be discret or analog pins values, some sensors connected to arduino by supported protocolls like I2C or Onewire values or internal Arduinos values like current milliseconds. In other words you can display any values of your Arduino microcontroller processed at this KSduino web site.

    To send your parameters to KSduino you need to have Arduino Ethernet Shield wl5100 to connect your microcontroller to Internet. And you should be registered to the KSduino web site. When you signed in to KSduino you can add new devices and create any parameters for this device. We have described connection process in the ‘How to connect your device to the KSduino Device Network?' help topic. There are some more helps in the KSduino HELP page. The KSduino continues to develop so we’ll expand HELP page and inform you about new features in this blog.

    The DOWNLOADS page contains latest version KSduino library for Arduino. This library has online documentation placed at the DOCUMENTATION page. In the DEVELOPMENT page you can find some new versions for testing and some aditional advices and offers for developers. You may ask your questions at the CONTACTS page and you can participate in conversations located under articles.

    The DEVICES page contain list of yours and other KSduino members devices, lists and graphs with device parameters. This is main page for monitoring your devices.

    The KSduino project is financed by its members and by donations. You can read more about it at the DONATE page.

    So welcome to KSduino device network!

    Best regards,
    Kirill Scherba
    The founder and main developer of KSduino project.

  • Comments



  • KSduino Library 90 percent ready to Release

    Posted on Thu November 8, 2012

    We are 90% ready to Release KSduino Library for Arduino

    KSduino Library 90 percent ready to Release


    You may look to the Development Page now to test it.

    /**
     * Setup this library
     */
    void KSduino::begin (byte *mac, unsigned int port)
    {
      Ethernet.begin (mac);
      getSocket();
      _port = port;
      Udp.begin (port);
    } 
    
    void main(void)
    {
      printf ("Hello World!\n");
    }
    

  • Comments



  • KSNews support was added to this web site
    Posted on Sun Sep 16, 2012


  • Comments



  • The Development of KSduino Web Server was started
    on Sun September 9, 2012


  • Comments



Created with KSNews

HOME | CONTACTS | INFORMER | SHORTENER | DOCUMENTATION | HELP

Copyright © 2012-2013, The KSduino Project