You are here

Arduino YUN-Plotter

In this post we will see how to create a simple plotter for Arduino but instead of using the serial port (or Bluetooth) we will use simple TCP/IP ports.

You can install the App from play store: https://play.google.com/store/apps/details?id=com.ArduinoMonitor&hl=es

 

We will use a TimerTask to draw every 300ms (or whatever you want)

1
2
3
4
5
6
7
8
9
10
11
 
private class TickTackRead extends TimerTask {    
public void run() {        
runOnUiThread(new Runnable() {            
public void run() {                
if (actualView == R.layout.<em>main</em>)                
{dataPlotiaps.redraw();                }                 
if(actualView == R.layout.<em>log</em>)                    
SetTextView("Log",datastream.log);            }       
 });    
} }

As you see in my code, when you are in the view that draws we redraw, but when we are in the log view we just write plain text to a textview.

 

We will draw using the XYPlot object from the http://androidplot.com/ libray. We create our view for drawing like this:

1
2
3
4
5
6
7
8
9
 
dataPlotiaps = (XYPlot) findViewById(R.id.<em>dataPlotiaps</em>); 
dataPlotiaps.addSeries(datastream.plenthSeries, new LineAndPointFormatter(Color.<em>rgb</em>(51, 229, 51), Color.<em>TRANSPARENT</em>, Color.<em>TRANSPARENT</em>, null)); 
dataPlotiaps.setGridPadding(0, 0, 0, 0); 
dataPlotiaps.getLayoutManager().remove(dataPlotiaps.getLegendWidget()); 
dataPlotiaps.getGraphWidget().getGridBackgroundPaint().setColor(Color.<em>TRANSPARENT</em>); dataPlotiaps.getGraphWidget().getGridBackgroundPaint().setColor(Color.<em>BLACK</em>); dataPlotiaps.getBackgroundPaint().setColor(Color.<em>TRANSPARENT</em>); 
dataPlotiaps.getGraphWidget().getBackgroundPaint().setColor(Color.<em>TRANSPARENT</em>); 
dataPlotiaps.setRangeStep(XYStepMode.<em>SUBDIVIDE</em>, 5); 
dataPlotiaps.setDomainStep(XYStepMode.<em>INCREMENT_BY_VAL</em>, 128); // 128 muestras por segundo

Before we have created a custom Thread that contains the plethSeries (the points to graph). We created the class TCPStreamReader.

1
2
 
datastream = new TCPStreamReader(port, this); datastream.start();

In the run method we just wait for incoming connection and create a new thread that answers the connection.

1
2
3
4
5
6
7
8
9
10
 
while (running) {    
try {                     
serverSocket = new ServerSocket(port);            
Socket socket = serverSocket.accept();            
SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(socket, this);            socketServerReplyThread.run();        } 
catch (IOException e) {             
AddLog("Error trying to listen in port:" + port + "\n");            
e.printStackTrace();        
}     }

The thread that really listens does the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
public void run() {     
InputStream inputStream;     
AddLog("Input connection received\n");     
try {         
inputStream = hostThreadSocket.getInputStream();        
reader = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-1"));         
while (threadparent.running) {            
s = reader.readLine();            
AddLog(s + ";");             
if (s.startsWith("@")) {                
threadparent.parent.Message(s.substring(1));            }             
try {                
temp = Integer.<em>parseInt</em>(s);                
plenthSeries.addLast(cursor++, temp);                
plenthSeries.removeFirst();            } 
catch (NumberFormatException e) {                 
e.printStackTrace();             }         }         
reader.close();        
inputStream.close();        
hostThreadSocket.close();        
Log.<em>e</em>(<em>TAG</em>,"Thread dying...");    
} catch (IOException e) {        // <em>TODO Auto-generated catch block        </em>e.printStackTrace();    } }

We just use “readline()” and try to parse to an Integer, if possible we add this number to plethSeries, this is a gobal variable of the main thread TCPStreamReader that is readed like a task by the UserInterface.

1
plenthSeries = new SimpleXYSeries("IAPsWaveForm");

I will upload soon the full code to github, meanwhile you can ask me if you like and I will send it to you by email.

 

 

Fernando Setien
Fernando Setien is an electronics and communications engineer enthusiast of technology in general, dungeons and dragons, fencing and lately virtual reality. As Undergraduate professor at Universidad Europea de Madrid he has some experience with Arduino as he taught Sensors and Actuators course in Biomedical Engineer Grade. Right now he lives in Santander an small city of the North of Spain working as a professor and also some times as freelance on the e-health sector.

Leave a Reply

Top