Arduino and Python (Linux)

Hello, I wr[ch959]te this code in Python for Linux and module PySerial to echo bytes that sends from computer. The program works fine but i want to tell me your opinion ...

Arduino code:

void setup() 
{ 
  Serial.begin(9600);
}

void loop() {
  byte incomingByte;
      if (Serial.available() > 0) {
            incomingByte = Serial.read();
                Serial.println(incomingByte);
      }
}

Python Code:

import serial,sys
ser=serial.Serial('/dev/ttyUSB0',9600, timeout=None)
ser.flushInput()
char = raw_input("Enter String : ")
ser.write(char)
received =[]
for n in range(0, len(char)):
      char_rec = ser.readline()
      received.append(char_rec[0:len(char_rec)-2]) # remove \r\n characters
sys.stdout.write(''.join(received)+'\n')
ser.close()

Screenshot:

Thanks

Gosh they're both remarkably similar aren't they. Small foot print too, can it be called from php? if yes, how?

Gosh they’re both remarkably similar aren’t they. Small foot print too, can it be called from php? if yes, how?

In Linux try to change premissions to “Read/Write” to OTHERS in device /dev/ttyUSB0 (as root) … Maybe this is dangerous but work… In Windows i think you must change line 2 with $f = fopen(“COM1”,“w+”);
You can send data with …

<?php
$f = fopen("/dev/ttyUSB0","w+");
fwrite($f, "Hello Arduino");
fclose($f);
?>

… and check the output in the terminal … I dont know yet how receive and use data …

Screenshot :

I write this little program in Python “Temperature” (modules: wx, serial), it reads the temperature from LM35 sensor and sends data to the computer every 1sec…
More : http://george.22web.net/downloads.html

Code:

Wiring:

int therPin = 2,val=0;       
float cel = 0;
char incomingByte;

void setup() {
  Serial.begin(9600);
  pinMode(therPin, OUTPUT);  
}

void loop() {
  if (Serial.available() > 0) {
  incomingByte = Serial.read();
  if(incomingByte=='t' || incomingByte=='T'){
  val = analogRead(therPin); 
  cel = (5.0 * val * 100.0)/1024.0;
  Serial.println((long)cel);
  }
  }  
}

Python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.5 on Tue Apr  8 05:32:21 2008 from /home/george/Desktop/test
# Title : Arduino Temperature version 1.25
# Author : George Hadjikyriacou - 2008

import wx, serial

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, -1, ".: Temperature (LM35) :.", style=wx.ALIGN_CENTER)
      self.label_2 = wx.StaticText(self, -1, "Celsius\t\t: ---", style=wx.ALIGN_LEFT)
      self.label_3 = wx.StaticText(self, -1, "Fahrenheit\t: ---", style=wx.ALIGN_LEFT)
      self.label_4 = wx.StaticText(self, -1, "Kelvin\t\t: ---", style=wx.ALIGN_LEFT)
      self.label_5 = wx.StaticText(self, -1, "George Hadjikyriacou - 2008", style=wx.ALIGN_CENTER)
        self.button_1 = wx.Button(self, -1, "Start")
      self.button_2 = wx.Button(self, -1, "Stop")
        self.__set_properties()
        self.__do_layout()
        self.button_1.Bind(wx.EVT_BUTTON, self.OnClick_1)
      self.button_2.Bind(wx.EVT_BUTTON, self.OnClick_2)
        # end wxGlade
      
    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Arduino Temperature v1.25")
        self.SetSize((310, 180))
        self.label_1.SetMinSize((310, 17))
      self.label_2.SetMinSize((310, 17))
      self.label_3.SetMinSize((310, 17))
      self.label_4.SetMinSize((310, 17))
      self.label_5.SetMinSize((310, 17))
        self.label_1.SetForegroundColour(wx.Colour(255, 0, 0))
      self.label_2.SetForegroundColour(wx.Colour(0, 0, 255))
      self.label_3.SetForegroundColour(wx.Colour(0, 0, 255))
      self.label_4.SetForegroundColour(wx.Colour(0, 0, 255))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        grid_sizer_1 = wx.FlexGridSizer(6, 1, 10, 0)
      grid_sizer_2 = wx.FlexGridSizer(1, 2, 0, 5)
        grid_sizer_1.Add(self.label_1, 0, 0, 0)
      grid_sizer_1.Add(self.label_2, 0, 0, 0)
      grid_sizer_1.Add(self.label_3, 0, 0, 0)
      grid_sizer_1.Add(self.label_4, 0, 0, 0)
      grid_sizer_1.Add(grid_sizer_2, 0, 0, 0)
        grid_sizer_2.Add(self.button_1, 0, 0, 0)
      grid_sizer_2.Add(self.button_2, 0, 0, 0)
      grid_sizer_1.Add(self.label_5, 0, 0, 0)
        self.SetSizer(grid_sizer_1)
        self.Layout()
        # end wxGlade

    def on_timer(self, event):
      # Set Serial Port and Flush Input ---------------------
      ser=serial.Serial('/dev/ttyUSB0',9600, timeout=None)
      ser.flushInput()
      # Set Serial Port and Flush Input ---------------------
      ser.write("t")
      received =[]
      char_rec = ser.readline()
      cel = float(char_rec)
      far = float((1.8 * cel) + 32)
      kelv = float(cel + 273.15)
      self.label_2.SetLabel("Celsius\t\t: " + "%.2f"%(cel))
      self.label_3.SetLabel("Fahrenheit\t: " + "%.2f"%(far))
      self.label_4.SetLabel("Kelvin\t\t: " + "%.2f"%(kelv))
      ser.close()

    def OnClick_1(self, event): # wxGlade: MyFrame.<event_handler>
      # Start Timer -------------------------------------------
      self.timer = wx.Timer(self, 100)
      self.timer.Start(1000)
      wx.EVT_TIMER(self, 100, self.on_timer)
      # Start Timer -------------------------------------------
        event.Skip()

    def OnClick_2(self, event): # wxGlade: MyFrame.<event_handler>
      # Stop Timer -------------------------------------------
      self.timer.Stop()
      self.label_2.SetLabel("Celsius\t\t: ---")
      self.label_3.SetLabel("Fahrenheit\t: ---")
      self.label_4.SetLabel("Kelvin\t\t: ---")
      # Stop Timer -------------------------------------------
        event.Skip()

# end of class MyFrame

if __name__ == "__main__":
      app = wx.PySimpleApp(0)
      wx.InitAllImageHandlers()
      frame_1 = MyFrame(None, -1, "")
      app.SetTopWindow(frame_1)
      frame_1.Show()
      app.MainLoop()

Screenshot:

Also you can use floatToString.h ( http://www.arduino.cc/playground/Main/FloatToString ) for exactness ....

for example :

#include "floatToString.h"

int therPin = 2;       
float cel = 0;
char incomingByte;
char temperature[10];  
  
void setup() {
  Serial.begin(9600);
  pinMode(therPin, INPUT);

}

void loop() {
  if (Serial.available() > 0) {
  incomingByte = Serial.read();
  if(incomingByte=='t' || incomingByte=='T'){
  cel = (5.0 * analogRead(therPin) * 100.0)/1023.0;
  Serial.println(floatToString(temperature,cel,2));
  }
  }  
}