NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Viewing Temp Data on PC screen

January 19, 2010
by axiom
axiom's Avatar

I've just made my first project, the temp sensor, and the instructions say that it is possible to view the temp data on the pc screen (through serial of course). But how to I actually see this data? Is there a serial terminal where the end temp is outputed and I can view it?

Thanks!

January 20, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Axiom,

The temperature sensor code already has a printf statement that is sending the data back over the serial port to your computer. To read it you just have to use some terminal emulator program that can read the data from the serial port (and send data too, but we are not doing that this time). To set that up on the computer side take a look at our Servo Squirter Tuotorial in the Serial Communications section you will see instructions for setting up Putty to do serial communications to your computer.

Hope that helps. Let us know if you run into any problems.

Humberto

January 20, 2010
by ese
ese's Avatar

The NerdKits folks are using Python. I better use Python too.

For windows xp

Python:

went to http://www.python.org/download/

read this: If you don't know which version to use, start with Python 2.6.4; more existing third party software is compatible with Python 2 than Python 3 right now.

downloaded this: Python 2.6.4 Windows installer (Windows binary -- does not include source)

PySerial:

went to: http://sourceforge.net/projects/pyserial/files/

clicked the download button Multiplatform Serial Port Module for Python (Win32, Jython, Linux, BSD and more)

PyGame:

went here http://www.pygame.org/download.shtml downloaded this pygame-1.9.1.win32-py2.6.msi 3MB

Also put Python and pyserial to my lfs(linux) box but I'm hung up on the pygame dependencies at the moment. So this is on a back burner.

I'm on the Python learning curve.

January 20, 2010
by ese
ese's Avatar

<pre> spaghetti code - has problems - don't click the mouse - probably will run with the NerdKits servoSquirter - Just something to play with. #runs with HyperterminalTalk.c (avr-gcc) - and uses threadin.Lock()-on xp #spaghetti code- kludged from NerdKits import threading import thread import time import serial import sys import pygame import os import string from pygame.locals import * val='' running=True user_input=[] serialPort = None read_write_flag=0 #0-read , 1-write read_list=[] def doWrite(lock1,lock2): global user_input global read_write_flag global serialPort global line_position global read_list content = fixString(user_input,lock2) print 'In doWrite()' + content SerialPort.write(content) lock1.acquire() read_list=[] lock1.release() read_write_flag = 0 def getReading(): global SerialPort x = SerialPort.readline() print 'read' if x != '': return x else: print 'getReading Error' getReading() def reader(string,sleeptime,lock1,*args): #Reader thread global val global running global SerialPort global read_write_flag global read_list count = 0 count0 = 0 str='' # self.s = serial.Serial("/dev/ttyUSB0", 115200) SerialPort = serial.Serial("COM4", 115200) SerialPort.readline() for item in range(32): #loop builds str of none printing characters to put in the upcomming char = chr(item) #strip function to get rid of unwanted characters on display : It str += char #doesn't work while running: if not read_write_flag: k = getReading() # val = k # num = ord('\n') print 'length', len(str) k.strip(str) lock1.acquire() read_list.append(k) lock1.release() def fixString(list,lock2): string='' lock2.acquire() for item in range(len(list)): string += list[item] lock2.release() return string def eventHandler(string,sleeptime,lock1,lock2,*args): global running global user_input global read_write_flag pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN) pygame.event.set_blocked(pygame.MOUSEBUTTONUP) while 1: for event in pygame.event.get(): print event if event.type != KEYUP: lock2.acquire() if event.type == KEYDOWN and event.key == K_DELETE: if len(user_input) > 0 : user_input.pop(len(user_input)-1) elif event.type == KEYDOWN and event.key == K_0: user_input.append('0') elif event.type == KEYDOWN and event.key == K_1: user_input.append('1') elif event.type == KEYDOWN and event.key == K_LEFTBRACKET: user_input.append('[') elif event.type == KEYDOWN and event.key == K_RIGHTBRACKET: user_input.append(']') elif event.type == KEYDOWN and event.key == K_RETURN: print 'got here' read_write_flag=1 lock2.release() doWrite(lock1,lock2) lock2.acquire() user_input=[] elif event.type == KEYDOWN : user_input.append('.') lock2.release() # print string # time.sleep(sleeptime) #sleep for a specified amount of time. def test01(lock1,lock2): global val global user_input global read_write_flag bgcolor = (255,255,255) linecolor = (150,25,0) linewidth = 3 textcolor = (250,0,0) textsize = 30 ctr=0 size = width, height = 640,480 # set up display pygame.init() pygame.font.init() screen = pygame.display.set_mode(size) #background = pygame.Surface(size).convert_alpha() #background.fill(bgcolor) background = pygame.image.load("background.jpg").convert_alpha() font = pygame.font.SysFont("Verdana", textsize) k = 0 while True: ctr += 1 # draw stuff to screen # background screen.blit(background, (0,0)) # data points # text line_position=45 lock1.acquire() for item in read_list: content = item text = font.render(content, True, textcolor) textsize0 = font.size(content) screen.blit(text, ((width - textsize0[0])/2,line_position)) line_position += 30 lock1.release() if not read_write_flag: content = fixString(user_input,lock2) text = font.render(content, True, textcolor) textsize0 = font.size(content) screen.blit(text, ((width - textsize0[0])/2,10)) content = "count %.2d " % ctr text = font.render(content, True, textcolor) textsize2 = font.size(content) screen.blit(text, ((width - textsize2[0])/2,height-textsize2[1]-10)) # done drawing... doublebuffer! pygame.display.flip() # print "test01" if __name__=="__main__": lock1 = threading.Lock() lock2 = threading.Lock() thread.start_new_thread(reader,("reader",2,lock1)) thread.start_new_thread(eventHandler,("eventHandler",3,lock1,lock2)) test01(lock1,lock2) ************************************************************************************ ************************************************************************************ #Kludged from NerdKits #define F_CPU 14745600 #include <stdio.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <inttypes.h> #include "../../code/code/libnerdkits/delay.h" #include "../../code/code/libnerdkits/lcd.h" #include "../../code/code/libnerdkits/uart.h" int main() { int pos = 0; int count = 0; int lineCondition = 0; int y; int interupt_entry_count; // init LCD lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_write_string(PSTR("HyperterminalTalk")); // init serial port uart_init(); FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); stdin = stdout = &uart_stream; char tc; while(1) { printf_P(PSTR("Input one of the following\r\n")); printf_P(PSTR(" 0 , 1 , [ , ]\r\n")); /* * y is an int. If I hit a keyboard character other than a number from * hyperterminal then * the program no longer stops at this scanf */ // scanf_P(PSTR("%d"),&y); lcd_line_two(); fprintf_P(&lcd_stream, PSTR(" servo position: %d "), pos); // Wait for a character to be sent to the serial port. tc = uart_read(); lcd_line_three(); if(tc==']') pos= 1; if(tc=='[') pos=2; if(tc=='0') pos =3; if(tc=='1') pos = 4; if(tc != ']' && tc != '[' && tc != '1' && tc != '0') printf_P(PSTR("Input error\r\n ")); // Print the current servo position to the serial port. printf_P(PSTR("Servo Position %d\r\n"), pos); lcd_home(); lcd_line_four(); lcd_write_string(PSTR("count: ")); lcd_write_int16( count++); } return 0; } </pre>

January 20, 2010
by ese
ese's Avatar

Sorry! I hope that last post can be removed. It posted just like it previewed. I thought maybe it would come out like a program listing.

January 20, 2010
by ese
ese's Avatar

spaghetti code - has problems - don't click the mouse - Runs on xp - Need Python,pyserial and pygame - Runs with hyperterminalTalk.c (below)-probably will run with the NerdKits servoSquirter - Just something to play with. To get the code on this post I dinked with the indent which may cause problems.

    #Kludged from NerdKits
    #runs with HyperterminalTalk.c (avr-gcc) - and uses threadin.Lock()
    #spaghetti code
    import threading
    import thread
    import time
    import serial
    import sys
    import pygame
    import os
    import string
    from pygame.locals import *
    val=''
    running=True
    user_input=[]
    serialPort = None
    read_write_flag=0   #0-read , 1-write
    read_list=[]

def doWrite(lock1,lock2):
    global user_input
    global read_write_flag
    global serialPort
    global line_position
    global read_list
    content = fixString(user_input,lock2)
    print 'In doWrite()' + content
    SerialPort.write(content)
    lock1.acquire()
    read_list=[]
    lock1.release()
    read_write_flag = 0

def getReading():
    global SerialPort
    x = SerialPort.readline()
    print 'read'
    if x != '':
      return x
    else:
      print 'getReading Error'
      getReading()

def reader(string,sleeptime,lock1,*args):    #Reader thread
    global val
    global running
    global SerialPort
    global read_write_flag
    global read_list
    count = 0
    count0 = 0
    str=''
     #    self.s = serial.Serial("/dev/ttyUSB0", 115200)
    SerialPort = serial.Serial("COM4", 115200)
    SerialPort.readline()
    for item in range(32):     #loop builds str of none printing characters to put in  the upcomming
       char = chr(item)    #strip function to get rid of unwanted characters on display : It
       str += char         #doesn't work
    while running:
      if not read_write_flag:
        k = getReading()

#          num = ord('\n')
    print 'length', len(str)
    k.strip(str)
    lock1.acquire()
    read_list.append(k)
    lock1.release()

def fixString(list,lock2):
    string=''
    lock2.acquire()
    for item in range(len(list)):
       string += list[item]
    lock2.release()
    return string

def eventHandler(string,sleeptime,lock1,lock2,*args):
    global running
    global user_input
    global read_write_flag
    pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN)
    pygame.event.set_blocked(pygame.MOUSEBUTTONUP)
    while 1:
      for event in pygame.event.get():
        print event
        if event.type != KEYUP:
          lock2.acquire()
          if event.type == KEYDOWN and event.key == K_DELETE:

             if len(user_input) > 0 : user_input.pop(len(user_input)-1)
          elif event.type == KEYDOWN and event.key == K_0:
             user_input.append('0')
          elif event.type == KEYDOWN and event.key == K_1:
             user_input.append('1')
          elif event.type == KEYDOWN and event.key == K_LEFTBRACKET:
             user_input.append('[')
          elif event.type == KEYDOWN and event.key == K_RIGHTBRACKET:
             user_input.append(']')
          elif event.type == KEYDOWN and event.key == K_RETURN:
             print 'got here'
             read_write_flag=1
             lock2.release()
             doWrite(lock1,lock2)
         lock2.acquire()
             user_input=[]
          elif event.type == KEYDOWN :
             user_input.append('.')
          lock2.release()
    #      print string
    #      time.sleep(sleeptime) #sleep for a specified amount of time.

def test01(lock1,lock2):
    global val
    global user_input
    global read_write_flag
    bgcolor = (255,255,255)
    linecolor = (150,25,0)
    linewidth = 3
    textcolor = (250,0,0)
    textsize = 30
    ctr=0
    size = width, height = 640,480

    # set up display
    pygame.init()
    pygame.font.init()
    screen = pygame.display.set_mode(size)

    #background = pygame.Surface(size).convert_alpha()
    #background.fill(bgcolor)
    background = pygame.image.load("background.jpg").convert_alpha()

    font = pygame.font.SysFont("Verdana", textsize)

    k = 0
    while True:
      ctr += 1
      # draw stuff to screen
      #   background
      screen.blit(background, (0,0))
      #   data points

      #   text
      line_position=45
      lock1.acquire()
      for item in  read_list:
        content =  item
        text = font.render(content, True, textcolor)
        textsize0 = font.size(content)
        screen.blit(text, ((width - textsize0[0])/2,line_position))
        line_position += 30
      lock1.release()

      if not read_write_flag:
        content = fixString(user_input,lock2)
        text = font.render(content, True, textcolor)
        textsize0 = font.size(content)
        screen.blit(text, ((width - textsize0[0])/2,10))

      content = "count %.2d " % ctr
      text = font.render(content, True, textcolor)
      textsize2 = font.size(content)
      screen.blit(text, ((width - textsize2[0])/2,height-textsize2[1]-10))

      # done drawing... doublebuffer!
      pygame.display.flip()

    #      print "test01"

if __name__=="__main__":
    lock1 = threading.Lock()
    lock2 = threading.Lock()
    thread.start_new_thread(reader,("reader",2,lock1))
    thread.start_new_thread(eventHandler,("eventHandler",3,lock1,lock2))
    test01(lock1,lock2)

-------------------------------------------------------------------------------------

#HyperterminalTalk.c
#Kludged from NerdKits
#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../../code/code/libnerdkits/delay.h"
#include "../../code/code/libnerdkits/lcd.h"
#include "../../code/code/libnerdkits/uart.h"

int main() {
  int pos = 0;
  int count = 0;
  int lineCondition = 0;
  int y;
  int interupt_entry_count;
  // init LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_write_string(PSTR("HyperterminalTalk"));

  // init serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  char tc;
  while(1) {
   printf_P(PSTR("Input one of the following\r\n"));
   printf_P(PSTR(" 0 , 1 , [ , ]\r\n"));
/*
* y is an int. If I hit a keyboard character other than a number from
* hyperterminal then
* the program no longer stops at this scanf
*/
//   scanf_P(PSTR("%d"),&y);
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR(" servo position: %d "), pos);

    // Wait for a character to be sent to the serial port.
    tc = uart_read();
    lcd_line_three();
    if(tc==']') pos= 1;
    if(tc=='[') pos=2;
    if(tc=='0') pos =3;
    if(tc=='1') pos = 4;
    if(tc != ']' &&
       tc != '[' &&
       tc != '1' &&
       tc != '0') printf_P(PSTR("Input error\r\n "));

    // Print the current servo position to the serial port.
    printf_P(PSTR("Servo Position %d\r\n"), pos);

    lcd_home();
 lcd_line_four();
 lcd_write_string(PSTR("count: "));
 lcd_write_int16( count++);

  }

  return 0;
}

Post a Reply

Please log in to post a reply.

Did you know that a NerdKit can be used to build an iPhone-controlled R/C car? Learn more...