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...