Homepage Wiki Forum Buy

NetIO

From GNUBLIN

severity (level) requirement Gnublin family
Gnublin logo advanced.png Kernel compile, Toolchain All


Contents


NetIO with Gnublin

Screenshot netio.png

With NetIO you are able to create very fast and easy interfaces for remote controls. You can load the interface on a smartphone or tablet.

As a counterpart to the commands with Python can receive the remote control and evaluate and start simple actions directly very easily on the GNUBLIN.

HowTo

  • Create an account on NetIO webpage http://netio.davideickhoff.de/editor
  • Install app on your smartphone (iPhone or Android)
  • Start UI designer using a browser and open the config: "NetIO with Gnublin" http://netio.davideickhoff.de/editor2?config=6932
  • Enter the current IP address of GNUBLIN in Connection Settings
  • Click Save Online
  • Start Smartphone app
  • Enter the login information and click on the Sync button
  • Select "NetIO with Gnublin"
  • Go on GNUBLIN, copy Python API + server and start
  • GPIO control relay

We control as a demo, the red LED (GPIO 3) on the GNUBLIN board that hangs like our phone on the same network.

Essentially you need simple commands that you've defined in the surface of each button & Co.. These commands are then sent to the small server on GNUBLIN. There you can easily evaluate this.

Functionality

Appflow1.jpg

There is a short Tutorial for getting NetIO running on your Gnublin:

Download UI-Designer config from our project page: [1]

Insert IP of Gnublin in UI-Designer in the connection box. You can find out the IP of the Gnublin with:

root@gnublin:~$ ifconfig

Install NetIO app from the Apple App Store (Google Play). The app builds a TCP socket and then send user-defined strings. You have to synchronize the Online savings with your phone. On a Android, you have to move two fingers over the screen. On a iPhone you have to shake.

Gnublin site:

Now you need a TCP Socket Server, that accepts the connection and react accordingly.

Connect Gnublin with the Network. [2]

Before we can use the Python API, we have to download these files:

root@gnublin:~$ wget https://raw.github.com/embeddedprojects/gnublin-api/master/python-module/gnublin/gnublin.py –no-check-certificate
root@gnublin:~$ wget https://github.com/embeddedprojects/gnublin-api/raw/master/python-module/gnublin/_gnublin.so –no-check-certifica

We download both file directly to our gnublin.

Create a fiele called server_netio.py and copy following content into it. Put it via scp on your Gnublin. [3]

  1. #!/usr/bin/python
  2.  
  3. import asyncore
  4. import socket
  5. import select
  6. import subprocess
  7. import gnublin
  8. import os
  9.  
  10. gpio = gnublin.gnublin_gpio()
  11. gpio.pinMode(3, 'out')
  12. gpio.pinMode(18, 'out')
  13.  
  14. class Client(asyncore.dispatcher_with_send):
  15.     def __init__(self, socket=None, pollster=None):
  16.         asyncore.dispatcher_with_send.__init__(self, socket)
  17.         self.data = ''
  18.         if pollster:
  19.             self.pollster = pollster
  20.             pollster.register(self, select.EPOLLIN)
  21.     def handle_close(self):
  22.         if self.pollster:
  23.             self.pollster.unregister(self)
  24.     def handle_read(self):
  25.         receivedData = self.recv(8192)
  26.         if not receivedData:
  27.             self.close()
  28.             return
  29.         receivedData = self.data + receivedData
  30.         while '\n' in receivedData:
  31.             line, receivedData = receivedData.split('\n',1)
  32.             self.handle_command(line)
  33.         self.data = receivedData
  34.     def handle_command(self, line):
  35.         if line == 'LED1 on':
  36.             self.send('on')
  37.             gpio.digitalWrite(3,1)
  38.         elif line == 'LED1 off':
  39.             self.send('off')
  40.             gpio.digitalWrite(3,0)
  41.         elif line == 'RELAY open':
  42.             self.send('off')
  43.             gpio.digitalWrite(18,0)
  44.         elif line == 'RELAY close':
  45.             self.send('off')
  46.             gpio.digitalWrite(18,1)
  47.         else:
  48.             self.send('unknown command')
  49. class Server(asyncore.dispatcher):
  50.     def __init__(self, listen_to, pollster):
  51.         asyncore.dispatcher.__init__(self)
  52.         self.pollster = pollster
  53.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  54.         self.bind(listen_to)
  55.         self.listen(5)
  56.     def handle_accept(self):
  57.         newSocket, address = self.accept()
  58.         print "Connected from", address
  59.         Client(newSocket,self.pollster)
  60. def readwrite(obj, flags):
  61.     try:
  62.         if flags & select.EPOLLIN:
  63.             obj.handle_read_event()
  64.         if flags & select.EPOLLOUT:
  65.             obj.handle_write_event()
  66.         if flags & select.EPOLLPRI:
  67.             obj.handle_expt_event()
  68.         if flags & (select.EPOLLHUP | select.EPOLLERR | select.POLLNVAL):
  69.             obj.handle_close()
  70.     except socket.error, e:
  71.         if e.args[0] not in asyncore._DISCONNECTED:
  72.             obj.handle_error()
  73.         else:
  74.             obj.handle_close()
  75.     except asyncore._reraised_exceptions:
  76.         raise
  77.     except:
  78.         obj.handle_error()
  79. class EPoll(object):
  80.     def __init__(self):
  81.         self.epoll = select.epoll()
  82.         self.fdmap = {}
  83.     def register(self, obj, flags):
  84.         fd = obj.fileno()
  85.         self.epoll.register(fd, flags)
  86.         self.fdmap[fd] = obj
  87.     def unregister(self, obj):
  88.         fd = obj.fileno()
  89.         del self.fdmap[fd]
  90.         self.epoll.unregister(fd)
  91.     def poll(self):
  92.         evt = self.epoll.poll()
  93.         for fd, flags in evt:
  94.             yield self.fdmap[fd], flags
  95. if __name__ == "__main__":
  96.     pollster = EPoll()
  97.     pollster.register(Server(("",54321),pollster), select.EPOLLIN)
  98.     while True:
  99.         evt = pollster.poll()
  100.         for obj, flags in evt:
  101.             readwrite(obj, flags)

Then execute:

python server_netio.py

Start the NetIO App and control your Gnublin.

Have fun with your projects.

In other languages