Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 01:03 03 May 2024 Privacy Policy
Jump to

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 Kingdom
Posts: 1676
Posted: 01:14pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Republic
Posts: 532
Posted: 02:29pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1676
Posted: 04:00pm 23 Nov 2020
Copy link to clipboard 
Print this post

Python is extremely unforgiving and hard


  Quote  2020-11-23 15:42:09+0000 [-]   File "/home/pi/wfpiconsole/lib/SI7021.py", line 40, in read
2020-11-23 15:42:09+0000 [-]     ser.write((inTemp + ',' + WindSpd).encode('ASCII'))
2020-11-23 15:42:09+0000 [-] NameError: name 'inTemp' is not defined
I then changed the line to ser.write((Temp + ',' + WindSpd).encode('ASCII')) which failed with this [
2020-11-23 15:43:50+0000 [-]   File "/home/pi/wfpiconsole/lib/SI7021.py", line 40, in read
2020-11-23 15:43:50+0000 [-]     ser.write((Temp + ',' + WindSpd).encode('ASCII'))
2020-11-23 15:43:50+0000 [-] TypeError: can only concatenate list (not "str") to list

and again changed to ser.write((Temp).encode('ASCII')) which failed with this
2020-11-23 15:44:18+0000 [-]   File "/home/pi/wfpiconsole/lib/SI7021.py", line 40, in read
2020-11-23 15:44:18+0000 [-]     ser.write((Temp).encode('ASCII'))
2020-11-23 15:44:18+0000 [-] AttributeError: 'list' object has no attribute 'encode'

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
  Quote       ser.write((b Temp).encode('ASCII'))
                    ^
SyntaxError: invalid syntax


Finally I tried and failed again
  Quote       ser.write((b 'Temp').encode('ASCII'))
                      ^
SyntaxError: invalid syntax


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 Republic
Posts: 532
Posted: 04:36pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 76
Posted: 05:16pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 76
Posted: 05:28pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 76
Posted: 05:33pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 76
Posted: 05:37pm 23 Nov 2020
Copy link to clipboard 
Print this post

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


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 Kingdom
Posts: 1676
Posted: 06:14pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 76
Posted: 06:27pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Republic
Posts: 532
Posted: 06:50pm 23 Nov 2020
Copy link to clipboard 
Print this post

  Quote  
and again changed to ser.write((Temp).encode('ASCII')) which failed with this
2020-11-23 15:44:18+0000 [-]   File "/home/pi/wfpiconsole/lib/SI7021.py", line 40, in read
2020-11-23 15:44:18+0000 [-]     ser.write((Temp).encode('ASCII'))
2020-11-23 15:44:18+0000 [-] AttributeError: 'list' object has no attribute 'encode'


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 Kingdom
Posts: 1676
Posted: 07:02pm 23 Nov 2020
Copy link to clipboard 
Print this post

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


I already tried that but I tried it again and got this
  Quote  2020-11-23 19:01:37+0000 [-]     ser.write(Temp.encode('ASCII'))
2020-11-23 19:01:37+0000 [-] AttributeError: 'list' object has no attribute 'encode'
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1676
Posted: 07:53pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 States
Posts: 705
Posted: 08:00pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1676
Posted: 08:15pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Republic
Posts: 532
Posted: 10:02pm 23 Nov 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1676
Posted: 08:02am 24 Nov 2020
Copy link to clipboard 
Print this post

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 Republic
Posts: 532
Posted: 09:42am 24 Nov 2020
Copy link to clipboard 
Print this post

  lew247 said  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


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 Kingdom
Posts: 1676
Posted: 10:18am 24 Nov 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1676
Posted: 03:58pm 29 Apr 2021
Copy link to clipboard 
Print this post

*Deleted
I worked it out myself
Edited 2021-04-30 22:03 by lew247
 
     Page 2 of 3    
Print this page
© JAQ Software 2024