Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

iIve asked you few weeks ago about solution on my python's scripy problem.

I just started my project again, and still got a problem.

My Arduino is working fine, command sudo screen /dev/ttyACM0 is working perfect, and I'm getting:

T:  52.80%  23.80 15% 92% N 
T:  52.80%  23.80 15% 92% N 
T:  52.80%  23.80 15% 92% N 
  • T - letter is separator between next row
  • First number is Humidity
  • Second is temperature
  • Third is photoresistor
  • Next one is soil moisure
  • and last one is fan working state (N - not working, Y - working)

I would like to use Python's script with cron to write a text file with results for every single sensor data.

For example I'll use cron to save 4 text files (temp.txt, humi.txt, soil.txt, photo.txt) every 5 minutes, 30 minutes, 1 hour, 3 hours, 12 hours, 24 hours.

Next I'll use a php script to show data as diagrams on my website.

But the problem is with my python script. I've got a solution here, and at the moment I'm using the following script (temperature's example):

#!/usr/bin/python

import serial
import time

buffer = bytes()
ser = serial.Serial('/dev/ttyACM0',9600, timeout=10)
while buffer.count('T:') < 2:
    buffer += ser.read(30)
ser.close();
# Now we have at least one complete datum. Isolate it.
start = buffer.index('T:')
end = buffer.index('T:', start+1)
items = buffer[start:end].strip().split()
print time.strftime("%Y-%m-%d %H:%M:%S"), items[2]

But in my text file I've got incorrect info, which looks like:

2013-05-10 19:47:01 12%
2013-05-10 19:48:01
2013-05-10 19:49:01 N
2013-05-10 19:50:01 24.10
2013-05-10 19:51:01 24.10
2013-05-10 19:52:01 7%
2013-05-10 19:53:01 24.10

but it should be 2013-05-10 19:47:01 24.10 all the time.

What's wrong with it?

share|improve this question
1  
Maybe you could change the last line to print time.strftime("%Y-%m-%d %H:%M:%S"), items[2], repr(buffer) to see what exactly you are parsing each time? – Martijn Pieters 2 days ago

1 Answer

I suspect that instead of

items = buffer[start:end].strip().split()

you want

items = buffer[start:end].split().strip()

or maybe

items = buffer[start:end].split()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.