Sunday, May 19, 2013

Web based AVR Project







          This AVR Project is derived from the idea to control home appliances through web. It is a combination of web application and avr circuit. This project consist  python(flask frame work ) as server and JavaScript with HTML5 as client. The User Interface and avr microcontroller is communicate through serial port of PC.

CIRCUIT DIAGRAM


Circuit is divided into two parts, microcontroller-circuit and serial port interface.

The main circuit consist of atmega8 microcontroller, four leds connected to PORTD (pins-6,11,12,13) and a potentiometer in pin-23(ADC0).The MAX232 used as level converter for communicating the USART of atmega8 with serial port(PC).USART is simply a form of serial data communication.




main circuit


Microcontroller and PC communication using RS232: RS232 is the encoded version of USART.









serial port interface


  This blog post updating now....






Saturday, May 18, 2013

USBasp programmer with avrdude(Linux)


    After success full  implementation of  serial programmer, I turned to make a USBasp programmer .The firmware is downloaded from USBasp official site.In this tutorial we will see how to use AVRdude for burning hex files into AVR microcontroller using USBasp. USBasp is a USB in-circuit programmer for Atmel AVR controllers. It simply consists of an ATMega8 and a couple of passive components. The programmer uses a firmware-only USB driver, no special USB controller is needed. In my circuit only use the six pins(MOSI,MISO,SCK,RESET,GND,+5V) from the usbasp to target microcontroller, It is little different from actual circuit in official site. No need of external +5V power supply to target microcontroller. The circuit is given below


USBasp programmer

NOTE:The fuse bits for 12Mhz crystal  HFUSE=0xc9 and  LFUSE=0xef  burn explicitly to  atmega8 using serial programmer(any programmer).It is very important, the USBasp programmer cannot work with default fuse bits. The command for setting the fuse bit:

avrdude -c ponyser -p m8 -P /dev/ttyS0 -U lfuse:w:0xc9:m -U hfuse:w:0xef:m

For reading the current fuse bit in atmega8:

avrdude -c ponyser -p m8 -P /dev/ttyS0 -U hfuse:r:high.bin:b -U lfuse:r:low.bin:b

Friday, May 17, 2013

AVR Serial Programmer

First I think, how to build a USBasp programmer.But I have n't a usb programmer to flash the firmware to it. Then decided to create a serial programmer and flash the firmware through this programmer to  atmega8. My  AVR serial programmer will transform hex file to most AVR microcontroller. It can build using few readily  available components. This programmer is compatible with popular avrdude software or PonyProg software.

We will be using the serial port for burning. First, we have to develop a burning circuit for it.

REQUIRED COMPONENTS 


Resistors :             10k     - 2
                           4.7k    - 2
                           15k     - 1

Zener diode :         5.1v   - 2


Diode :               1N4148 - 1


Transistor :      BC549/48 -  1


9-PIN RS232 female connector

serial programmer

Monday, April 8, 2013

Unit testing with nose Framework

         Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently tested for proper operation. Unit testing of software applications is done during the development (coding) of an application. Proper unit testing done during the development stage saves both time and money in the end. Unit tests find problems early in the development cycle. Some programming languages directly support unit testing, like Python, java etc. There are a couple of ways to integrate unit tests into your development style. These include Test Driven Development, where unit tests are written prior to the functionality they're testing; during refactoring, where existing code -- sometimes code without any automated tests to start with -- is retrofitted with unit tests as part of the refactoring process; bug fix testing, where bugs are first pinpointed by a targetted test and then fixed; and straight test enhanced development, where tests are written organically as the code evolves. In the end, I think it matters more that you're writing unit tests than it does exactly how you write them.For me, the most important part of having unit tests is that they can be run quickly, easily, and without any thought by developers.

nose

There are many unit test frameworks in Python, and more arise every day. I personally use nose, and it fits my needs fairly well. nose is simpler Unit Testing Framework for python. nose extends unit test to make testing easier. nose comes with a number of builtin plugins to help you with output capture, error introspection, code coverage, doctests, and more. It also comes with plugin hooks for loading, running, watching and reporting on tests and test runs.

Installation

Please refer the nose 1.3.0 documentation for installation of nose on Unix like systems.

simple examples:

Now let's start with a few examples. Here's the simplest nose test you can write:


def test_a():
      assert 'a' == 'a'

Put this in a file called 'test_me.py', and then run nosetest. You will see this output:
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

If you want to see exactly what test was run, you can use nosetest -v

test_me.test_a ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

A fairly common pattern for unit tests is something like this:


def test():
   setup_test()
   try:
      do_test()
      make_test_assertions()
   finally:
      cleanup_after_test()
Here, setup_test is a function that creates necessary objects, opens database connections, finds files, etc.Then do_test and make_test_assertions acually run the test code and check to see that the test completed successfully.Finally, the preconditions are cleaned up.


test discovery and execution

nose is a unit test discovery and execution package. Before it can execute any tests, it needs to discover them. nose has a set of rules for discovering tests, and then a fixed protocol for running them. While both can be modified by plugins, for the moment let's consider only the default rules.nose only looks for tests under the working directory, unless you specify one with the -w command line option.nose can only execute tests that it finds. If you're creating a new test suite, it's relatively easy to make sure that nose finds all your tests.


nose command line

Apart from the plugins, there are only a few options that I use regularly.

-w: Specifying the working directory
nose only looks for tests in one place. The -w flag lets you specify that location; e.g. nosetests -w my_nose_test/.  will run only those tests in the directory "/my_nose_test/"
As of the latest development version you can specify multiple working directories on the command line:
nosetests -w nose_test/ -w sample/

-s: Not capturing stdout
By default, nose captures all output and only presents stdout from tests that fail. By specifying '-s', you can turn this behavior off.

-v: Info and debugging output
nose is intentionally pretty terse. If you want to see what tests are being run, use '-v'.

-p: plugins
Output list of available plugins and exit. Combine with higher verbosity for greater detail

Tools for testing

           nose.tools provides a few convenience functions to make writing tests easier. You don’t have to use them; nothing in the rest of nose depends on any of these methods.

nose.tools.ok_(expr, msg=None)
Shorthand for assert. Saves 3 whole characters!

nose.tools.eq_(a, b, msg=None)
Shorthand for ‘assert a == b, “%r != %r” % (a, b)

nose.tools.make_decorator(func)
Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose’s additional stuff (namely, setup and teardown).

nose.tools.raises(*exceptions)
Test must raise one of expected exceptions to pass.

nose.tools.set_trace()
Call pdb.set_trace in the calling frame, first restoring sys.stdout to the real output stream. Note that sys.stdout is NOT reset to whatever it was before the call once pdb is done!

nose.tools.timed(limit)
Test must finish within specified time limit to pass.

nose.tools.with_setup(setup=None, teardown=None)
Decorator to add setup and/or teardown methods to a test function:

nose.tools.istest(func)
Decorator to mark a function or method as a test

nose.tools.nottest(func)
Decorator to mark a function or method as not a test

Running nose programmatically

nose has a friendly top-level API which makes it accessible to Python programs. You can run nose inside your own code by doing this:


import nose

### configure paths, etc here

nose.run()

### do other stuff here


By default nose will pick up on sys.argv; if you want to pass in your own arguments, use nose.run(argv=args). You can also override the default test collector, test runner, test loader, and environment settings at this level. This makes it convenient to add in certain types of new behavior

For more details please refer the official site.






Friday, March 29, 2013

Graph Colouring

       Now, I am creating a web application, Graph Coloring.The graphs are drowned using nodes and lines. In this Graph Coloring application, the nodes of the graph are colored using different colors for adjacent nodes. My application has two parts, one python script that runs in server and the other, an html file which act as user interface for the users.The html file provides the graphical view representation of my application.This application will be explained in detail, later on.

Graph and Graph Coloring

        Mathematically, a graph is a representation of a set of objects where some pairs of the objects are connected by links. The interconnected objects are represented by mathematical abstractions called vertices, and the links that connect some pairs of vertices are called edges.Vertices are also called nodes or points, and edges are also called lines or arcs.
        In Graph theory, graph coloring is a special case of graph labeling. In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices share the same color; this is called a vertex coloring.

        Now let's going through the codes.In my application, i have used canvas in html file.Canvas is a rectangular portion or area within the html page, and you can control every pixel in it. The canvas element has several methods for drawing paths, boxes, circles, characters, and adding images. For drawing within the canvas, you should need Javascript. The codes are below.
<canvas id="graph" width="700" height="440" style="border:2px solid #000000;"></canvas>
The above code is used for drawing a canvas in html5. I have choose JavaScript for drawing on canvas.More details on canvas and Javascript are available at: http://www.w3schools.com/html5/html5_canvas.asp. 

       Here in the graph application my aim is use javascript for detecting the mouse movements so that i can get the position of each mouse click. Thus getting the points where mouse is clicked, so that i can draw the vertex and lines. I have assigned different clicks for application, double click for creating a node and two single clicks for drawing a line. My codes for mouse actions are below.

function Point(x,y){
    this.x = x;
    this.y = y;
}

function getMousePos(e){
    var point = new Point(0,0);
    if (e.pageX != undefined && e.pageY != undefined) {
point.x = e.pageX;
point.y = e.pageY;
}
    else{
point.x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
point.y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}

    point.x -= canvas.offsetLeft;
    point.y -= canvas.offsetTop;
    return point;      
}

In the above code, mouse points are returned.the vertex and line are drowned using this points.

function drawcircle(e){
    point=getMousePos(e);
    if(checkNode(point,40)==-1){
Vertex(point,"black");
context.font = "10pt Courier New";
context.fillText(v,point.x+10,point.y-20);
v++;
vertex_list.push(point);
}
    else{ 
        alert("node overlapped");
}
}

function drawline(e){
    var point = getMousePos(e);
    var vertex_no=checkNode(point,40);
    if(vertex_no>=0){
if(start_edge==0){
 start_edge=1;
 selected_vertex=vertex_no;
 context.beginPath();
       context.moveTo(vertex_list[vertex_no].x, vertex_list[vertex_no].y);
}
else{
 start_edge=0;
          if(adj_list[selected_vertex]==undefined)
adj_list[selected_vertex]= new Array();
 if(adj_list[vertex_no]==undefined)
adj_list[vertex_no]= new Array();
 if(selected_vertex!=vertex_no){
adj_list[selected_vertex].push(vertex_no);
adj_list[vertex_no].push(selected_vertex);
 }
 context.lineTo(vertex_list[vertex_no].x,vertex_list[vertex_no].y)
 context.stroke();
 context.closePath(); 
}
}
    else
start_edge=0;
    }

       The functions ‘drawline’ and ‘drawcircle’ gets called when the user clicks, i.e. double or single click, anywhere within the canvas. Its arguments is a MouseEvent object that contains information about where the user clicked. Each functions calls getMousePos(e), where the getMousePos(e) returns the values of mouse coordinates where ever the user clicks within the canvas. The values are returned to a vertex_list.After the values are returned, they are used to draw nodes and lines within the canvas.
       Now for the coloring part, an adjacency list containing the list of nodes is needed for coloring.Since the html file is the client and the adjacency list are sent to the server. For communicating with server, i.e. the python script, jQuery is used.i have used jQuery for sending the adjacent list to the server running the python script where the coloring algorithm is implemented.For more details on jQuery visit http://www.w3schools.com/jquery/default.asp.
The complete the code of Graph coloring application is available at: https://github.com/jaseemkp/Graph-Coloring-Django
You can try the graph coloring application at : http://jastech-graph.herokuapp.com/

Monday, March 25, 2013

MongoDB

           Now Let's going through Simple NoSQL database MongoDB. MongoDB is leading NoSQL database,designed for how we build and run application today. NoSQL is designed for distributed data stores where very large scale of data storing is needed. It is a database system that provides simple lightweight mechanism for storage and retrieval of data.NoSQL has become much popular in the last few years.MongoDB is a general purpose, open-source NoSQL database.
Features of MongoDB:
  • Document data model with dynamic schemas
  • Full, flexible index support and rich queries
  • Auto-Sharding for horizontal scalability
  • Built-in replication for high availability
  • Text search
  • Advanced security
  • Aggregation Framework and MapReduce
  • Large media storage with GridFS
Organizations of all sizes use MongoDB to quickly and easily develop, scale and operate applications. Instead of storing data in rows and columns as one would with a relational database, MongoDB stores a binary form of JSON documents (BSON).

 MongoDB Installation:
      Now let's see how to get started  with MongoDB.This tutorial provide basic installation steps for installing MongoDB on Ubuntu.12.04 Linux system, where we uses  .deb package as the basis of the installation.
Enter the following command to import the 10gen public GPG Key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.list file and include the following line for the 10gen repository.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Now Enter the following command to reload your repository:
sudo apt-get update
Issue the following command to install MongoDB:
sudo apt-get install mongodb-10gen
When this command completes, you have successfully installed MongoDB!
You can start the mongod process by issuing the following command:
sudo service mongodb start
As needed, you may stop the mongod process by issuing the following command:
sudo service mongodb stop
You can connect to your MongoDB instance by issuing the following command at the system prompt:
mongo
This will connect to the database running on the localhost interface by default. For more information please refer the link:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/


pymongo

pymongo is python package index. We prefer pip to install pymongo on python:
pip install pymongo
In Python shell, type the following command to import pymongo package.
import pymongo
To connect to the database, we create a database instance- a connection object by including the following in our python shell.
from pymongo import Connection
connection = Connection()
collection = connection.DB_NAME.COLLECTION_NAME
now we are ready to insert database.
collection.insert({"fruit": "Mango", "color": "Orange"})
The above code snippet is enough for inserting a row(document or BSON document in terms of MongoDB) in the table(collection) collection_name.
For retrieving all the data entered into the collection collection_name,
collection.find()
It's also possible to get the specific item in the collection.
collection.find_one({"color": "Orange",})
Reading all the documents of the specified kind, is by,
collection.find({"color": "Orange", })

for more details please click Here.


for sql to mongoDB convertion also refer the link http://docs.mongodb.org/manual/reference/sql-comparison/


Paint application with MongoDB

Now, I have replaced database of My Paint Application from Sqlite3 to MongoDB. The html front end and javascripts are same.Please refer my earlier post about paint application for more info.I will explain how to connect to database and retrive data from it.


from flask import Flask, render_template, request, redirect
from pymongo import Connection

app = Flask(__name__)


connection = Connection()

collection = connection.paintdb.drawings

@app.route('/', methods=['GET', 'POST'])

def paint():
    if request.method == 'GET':
        all_data = {}
        datas = collection.find()
        for data in datas:
            all_data[data["fname"]] = data["image_data"]
        return render_template('paint.html', py_all= all_data)
    elif request.method == 'POST':
        filename = request.form['fname']
        data = request.form['whole_data']
        collection.insert({"fname":filename, "image_data":data})
        return redirect('/')

if __name__ == '__main__':

   app.run()
from pymango module import class "Connection".It is used to connect with database.Then form a collection(table),named as drawings. Datas are inserted directly into collection. 

The complete code is Here

Wednesday, March 20, 2013

Paint Application Using Flask and Sqlite3


     Now, I have rewritten the paint application in Google AppEngine using Flask framework.The html frontend and javascript are same.Please refer my earlier post about paint application for more info.Instead of google datastore, sqlite3 was used for data storage.I will explain how to connect to database and retrive data from it.


def connect_db():
    return sqlite3.connect('paint.db')

@app.before_request

def before_request():
    g.db = connect_db()
    g.db.execute("CREATE TABLE IF NOT EXISTS drawings(fname string primary key, img_data text)")
@app.after_request
def after_request(response):
    g.db.close()
    return response

First we define a function 'connect_db()' for connecting to database before requesting.Then create a table in database file(paint.db) for storing fname and img_data.


@app.route('/', methods=['GET', 'POST'])
def paint():
    if request.method == 'GET':
        py_all = {}
        all_data = g.db.execute("SELECT * FROM drawings")
        for data in all_data:
            py_all[data[0]] = data[1]
        return render_template('paint.html', py_all= py_all)
    elif request.method == 'POST':
        filename = request.form['fname']
        data = request.form['whole_data']
        g.db.execute("REPLACE INTO drawings(fname, img_data) VALUES (?, ?)", (filename, data));
        g.db.commit()
        return redirect('/')

          In GET method the data executed from database is render to  paint.html file.The data taken from data base is stored in 'py_all' object.When user tries to save the image, server gets a POST request.  the image name and its data are saved into a table using the sqlite3 module available in python. Sqlite3 is a basic database interface available in python.
The code can be found Here