0

absolute newbie to python after years of C/C++. I want to write a Python script to tell my Rasberry Pi to read a smart relay board, calculate a temperature, and write a formatted string to a file. After much googling and newsgroup searching, I may have most of it:

import socket

// open TCP client socket:

IPADDR = '192.168.99.9'


PORTNUM = 2101

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((IPADDR, PORTNUM))



// send command to read A/D value: two hex bytes

tdata = bytes.fromhex('FEA4')

s.send(tdata)



// receive 2-byte (hex) reply

rdata=s.recv(256)



// close socket

s.close()



// convert reply to a voltage reference (unsigned short)

vRef = (rdata[0] * 256)+(rdata[1])



// convert vref to float as degrees Farenheit

degF = vRef * 4930 / 1024

degF = degF / 10

degF = degF - 273.15

degF = degF * 9 / 5 + 32



// open text file

fo = open("\mnt\stuff\temp.txt", "w")



// write formatted string as number only e.g., 32.6

fo.write("{:+.1f}.format(degF)\n")



// Close file

fo.close()

I'm not sure about accessing the received data and creating the unsigned short value. I will receive something like /x02/x55, which is (2*256)+85 = 597.

The floating-point math, not sure here either, but that's how I convert a reading of 597 to a degrees-F value of 57.6

Finally, I need to write the string "57.6" to a file.

Lastly, and not in this code, I will need a way to have the RasPi run this code once per minute to update the file. I have a web server that reads the file and creates HTML text from that.

thanks ANYONE for any help...

1
  • Your comments are going to be syntax errors in Python. In Python, comments start with #. Commented Jan 25, 2014 at 20:47

1 Answer 1

1

I'll assume your socket code is right, and I'll fix the comments:

import socket

# open TCP client socket:
IPADDR = '192.168.99.9'
PORTNUM = 2101
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IPADDR, PORTNUM))

# send command to read A/D value: two hex bytes
tdata = bytes.fromhex('FEA4')
s.send(tdata)
# receive 2-byte (hex) reply
rdata=s.recv(256)
# close socket
s.close()

I'll assume your math is mostly correct, but if you're using Python 2, you might be doing floor division if rdata[0] is an integer (class int).

# convert reply to a voltage reference (unsigned short)
vRef = (rdata[0] * 256)+(rdata[1])

To be safe, first convert vRef to float before the other calculations:

vRef = float(rdata[0] * 256)+(rdata[1])

And then procede:

# convert vref to float as degrees Farenheit
degF = vRef * 4930 / 1024
degF = degF / 10 
degF = degF - 273.15
degF = degF * 9 / 5 + 32

This:

# open text file
fo = open("\mnt\stuff\temp.txt", "w")
# write formatted string as number only e.g., 32.6
fo.write("{:+.1f}.format(degF)\n")
# Close file
fo.close()

Can be replaced with this:

with open("\mnt\stuff\temp.txt", "w") as file:
    file.write("{:+.1f}.format(degF)\n")

The with keyword opens the file as a context manager, and automatically closes the file for you, even if your code raises an exception. Sockets don't work as context managers in Python 2, unless you create a wrapper for it yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.