Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Anyone know Python?
Page 2 of 3 | |||||
Author | Message | ||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Unfortunately that didn't do it as adding the missing opening bracket didnt The error seems to be that the serial data needs to be sent as bytes and the string is unicode and needs to be sent as utf-8 |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
ser.write expect bytes, so try ser.write((inTemp + ',' + WindSpd).encode('ASCII'))... Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Python is extremely unforgiving and hard I'm open to any suggestions I know the serial has to be bytes and it has to be in ascii so I changed the line because I know a small b means bytes and got Finally I tried and failed again I know that because Obs['inTemp'] = Temp then I can send Temp instead of inTemp and I know inTemp is a string so it should be easy but its really not |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
It looks like Temp or WinSpd is not string but list (of strings?)... I need to know, how are this variables used. When A is list of strings (A = ['0', '1', '2']), you need to use just one specific element, for example A[1] (which is '1'), so A is list and A[1] is string. Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
markboston36 Regular Member Joined: 27/10/2020 Location: United StatesPosts: 76 |
python is very finicky and you have to have all your parenthesis, brackets and indentations correct or it won't work. https://docs.python.org/3/tutorial/datastructures.html |
||||
markboston36 Regular Member Joined: 27/10/2020 Location: United StatesPosts: 76 |
you could also use a dictionary and then pick out the parts you need to use. check the link i posted. |
||||
markboston36 Regular Member Joined: 27/10/2020 Location: United StatesPosts: 76 |
also i can see you have put a lot of work into this but something to check out would be https://learn.adafruit.com/adafruit-si7021-temperature-plus-humidity-sensor/circuitpython-code. circuitpython is designed specifically for what you are trying to do and it might be easier to work with then using the full python stack and trying to figure things out. im actually looking into learning this as well for doing things with my rspi. |
||||
markboston36 Regular Member Joined: 27/10/2020 Location: United StatesPosts: 76 |
there are list's dictionaries tuples and strings. a tuple is a group of strings basically, a dictionary is just like what it sounds you give a variable and a definition and then are able to pull out different parts. lists and strings are the same as any other language. |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Unfortunately I don't know exactly if it's a string or list of strings? This is the main program and it's for this I'm trying to extract some of the values to send to a Micromite to drive a stepper motor based gauge on the wall to display physically some of the elements like outdoor and indoor temp and so on I added the SI7021.py module myself and the python code to get it working as I don't have a second Weatherflow air sensor to use for the indoor temperatures I cannot use circuitpython because python is already installed and used by the main program Hope that made sense |
||||
markboston36 Regular Member Joined: 27/10/2020 Location: United StatesPosts: 76 |
check out the first link i posted its a link to the python reference on data types. |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
When you have just simple variable, write: ser.write(Temp.encode('ASCII')) without quotes. With Temp with quotes you are doing from string list (or tuple) with one member Temp... Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
I already tried that but I tried it again and got this |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
I think I found the issue I used class to find out what class and both Temp and Obs['inTemp'] are lists If I print them I get the following Now to try and figure out how to convert lists to bytes |
||||
hitsware2 Guru Joined: 03/08/2019 Location: United StatesPosts: 705 |
I have no idea if it might help , but here is something with a slight similarity that works : import busio; import math; import time import board; import random fo = busio.UART(board.TX, board.RX,baudrate=31250) while True: midinote_on=[144,60,127] midinote_off=[144,60,0] fo.write(bytes(midinote_on)) time.sleep(0.3) fo.write(bytes(midinote_off)) time.sleep(0.7) my site |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
If I import Json then use ser.write(json.dumps(Temp) + '\\n') it gets more hopeful I get 2020-11-23 20:11:54+0000 [-] raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) 2020-11-23 20:11:54+0000 [-] TypeError: unicode strings are not supported, please encode to bytes: '["20.6", "\\\\u2103"]\\n' I think it might be telling me how but I don't understand Python enough to get it Following on if I try this print(Temp) Temp = Temp[0].encode() print(Temp) I get Edited 2020-11-24 06:38 by lew247 |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
First you need to chose list element to write, looks like Temp = ['value', 'unit'], so has 2 elements, Temp[0] and Temp[1], both elements of Temp list are strings. So if you need to write it to serial, the easiest way is make on string from both: strTemp = Temp[0] + '_' + Temp[1], you will get string strTemp = value_temp. And this you can encode into bytes and send: ser.write(strTemp.encode('ASCII'). Hopefully I'm clear... Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Thanks That did make sense and I understood it However once I do that the program fails on the last line (next line) that was working before Clock.schedule_once(partial(read,Obs), 60) I can use Obs['inTemp'] instead because this only contains one set of data temperature for example 21.7 I therefore tried strTemp = Obs['inTemp'] ser.write(strTemp.encode('ASCII') and I get the same result File "/home/pi/wfpiconsole/lib/SI7021.py", line 51 Clock.schedule_once(partial(read,Obs), 60) ^ SyntaxError: invalid syntax |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
I think you are again one level higher... Obs is dictionary, Obs['inTemp'] is list ('value', 'unit') and you need the strings, so you need (for inTemp): string for value is Obs['inTemp'][0] string for unit is Obs['inTemp'][1] So complete string for it (you can then print(strInTemp) to check it): strInTemp = Obs['inTemp'][0] + Obs['inTemp'][1] print(strInTemp) #for test and send it (you need bytes from string): ser.write(strInTemp.encode('ASCII')) Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Getting very close if I use as you suggested strInTemp = Obs['inTemp'][0] + "," + Obs['inTemp'][1] print(strInTemp) #for test ser.write("test".encode()) ser.write(strInTemp.encode('ASCII')) It sends the word test via the serial port but I then get 2020-11-24 10:11:20+0000 [-] ser.write(strInTemp.encode('ASCII')) 2020-11-24 10:11:20+0000 [-] UnicodeEncodeError: 'ascii' codec can't encode character '\u2103' in position 4: ordinal not in range(128) If I use ser.write(strInTemp.encode()) instead of ser.write(strInTemp.encode('ASCII')) it works but sends this and if I add a comma to seperate the values I still get the garbage instead of item 2 strInTemp = Obs['inTemp'][0] + "," + Obs['inTemp'][1] returns The issue is character U+2103 the degree symbol Is there a way to strip the symbol out so it's not sent? Edit: ignore I just don't send the 2nd section Edited 2020-11-24 20:57 by lew247 |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
*Deleted I worked it out myself Edited 2021-04-30 22:03 by lew247 |
||||
Page 2 of 3 |
Print this page |