0

How to toggle Pin 1?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: How to toggle Pin 1?

by danhalbert on Sat Mar 10, 2018 9:06 pm

Most programming languages these days are case-sensitive, including Python, so yes, case definitely does matter.

Below is a rewritten version (not tested!). Note that I don't use `print("AT")`, because you need to send "AT" to the HC-12, not to the serial output. I used `uart.write(..)` instsead. Also note the check for "OK" and breaking out of the loop when it's read. I hope this will help get you started.

Code: Select all | TOGGLE FULL SIZE
from digitalio import DigitalInOut, Direction
import board
import busio
import time

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

# Create a new UART using pins 3(Rx) and 4(Tx)
# You can also use board.TX and board.RX for prettier code!
uart = busio.UART(board.D4, board.D3, baudrate=9600)
time.sleep(3)    # wait 3 sec for power up

d1 = DigitalInOut(board.D1)   # D1 is the SET pin on the HC12
d1.direction = Direction.OUTPUT
d1.value = False   # set pin LOW

led.value = True  # set LED 13 On

time.sleep(0.3)
# Send
uart.write("AT\n")

# Loop until we get "OK"
while True:
    data = uart.read(32)  # read up to 32 bytes   # Expect "OK"
    print("Data =", Data) # print what we read
    if data.startswith("OK"):
        # If we read "OK", then break out of loop. Otherwise try reading again.
        break

d1.value = True    # Set pin 1 HIGH

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Mon Mar 12, 2018 9:02 pm

Thanks Dan! We are making progress. Slow on my part but moving ahead.
Your code works, thank you. It looks like the communication is good with the HC-12
as I am getting back an OK (plus other characters).
Here is the exact line. Please explain what the extra text means.

Data = b'OK/r/n'

What does the b mean before the string? What does the /r mean?
I know the /n means Newline as you added one of those when sending the AT.
Thanks for correcting me on how to send serial data over the port. Is there a link to those
serial port commands?

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Mon Mar 12, 2018 10:19 pm

b'...' means it's a bytes object, which is just a bunch of 8-bit bytes than can be treated as characters or 8-bit numbers. There is no encoding happening. A string can be a Unicode string, which might have multibyte characters (which would not be true under in most CircuitPython applications). You can treat it as a string, and you can convert it to string if you need to by doing something like str(b'abc', "ASCII").

Re b'OK\r\n' : Notice those are backslashes, not forward slashes. `\r` is carriage return, `\n` is newline. Devices that do AT-style commands often return \r\n for historical reasons and compatibility with all operating systems.

As for the HC-12 commands, I'm not that familiar with the device, but there is documentation here: https://www.elecrow.com/download/HC-12.pdf, and there are a number of tutorials (not all of which say that much).

You mentioned that you've been looking at some Python books. I've not found the perfect book for beginners. Python for Kids is good for intro stuff, but doesn't cover things like bytes objects. I like Python in a Nutshell (3rd edition covers Python 3), but it may assume more background than you might have. You might start here: https://wiki.python.org/moin/BeginnersGuide and look at the resources for both programmers and non-programmers.

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Tue Mar 13, 2018 4:16 pm

Thanks for the hints on decoding the returned bytes. If those bytes are numbers can I use them directly in a calculation?
If not how do I convert them to numbers? Also, I am planning on sending numbers that I read from an Adafruit BME680 sensor.
Example: humidity at 46.2564%, temperature at 22.1368 degrees C. Do I need to do any conversion before sending floating point numbers over the UART?

You wrote: "As for the HC-12 commands, I'm not that familiar with the device, but there is documentation here: https://www.elecrow.com/download/HC-12.pdf, and there are a number of tutorials (not all of which say that much)."

I was not inquiring about the HC-12 commands, but the CircuitPy UART commands such as write(buffer).
Let me know if there is a link to a detailed document on the UART commands. Thanks Dan.

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Tue Mar 13, 2018 4:25 pm

You're welcome. The BME680 library returns floating-point numbers which you can do arithmetic on in CircuitPython. To send those over serial the easiest way would be to do `str(value)`, which will convert value (in whatever form) to a string. You could convert it back on the other side if you need to do further computation.

The UART functions are described here: http://circuitpython.readthedocs.io/en/ ... /UART.html

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Sun Mar 18, 2018 9:25 pm

Dan, Thanks for the UART library stuff, just want I wanted.
I am looking at the BME680 CircuitPy code example. The line that reads the sensor is
combined with the print command like so:

print('Temperature: {} degrees C'.format(sensor.temperature))

It is really hard for me to see what the {} is doing and what the format command is doing.
How can I just read the temperature into a floating point variable so I can convert it to a string before
sending it out over the UART?

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Mon Mar 19, 2018 9:12 am

You can do this:
Code: Select all | TOGGLE FULL SIZE
temperature = sensor.temperature
temperature_string = str(temperature)
#or, shortcut:
temperature_string = str(sensor.temperature)


The `format()` call is not so complicated: the `{}` is replaced by the string value of `sensor.temperature`. Here are a couple of explanatory links:
https://www.digitalocean.com/community/ ... n-python-3
https://pyformat.info/

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Wed Mar 21, 2018 7:02 pm

Dan, having trouble with the silly indentation protocol in Python.
What is the purpose of that? Nevermind, not your fault...
Error says Unexpected Indent in Line 26. But there is NO new indent that I can see.
Attachments
Full wingtip code.txt
(1.31 KiB) Downloaded 62 times

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Wed Mar 21, 2018 7:36 pm

The problem is that you have a mix of tabs and spaces doing indentation. You have to use one or the other consistently. We prefer spaces. Line 25 is indented with spaces, but line 26 uses a tab.

A number of editors will insert the proper number of spaces when you press the tab key, so check the preferences in yours.

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Wed Mar 21, 2018 8:15 pm

OK, changed all the tabs to 4 spaces.
It is not talking to the serial monitor again and the colored LEDs are blinking but I can't decode those at all.
Finally got it into REPL mode and hit cntrl Z and then cntrl D and it says ValueError: Line 18 has invalid pins.
I copied those lines right out of my successful communications code (shown earlier) that worked with the HC-12.
Why is it now not liking the pins?

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Wed Mar 21, 2018 11:18 pm

Is this the same board that you used with the HC-12, or a different one? The pins available for UART can vary depending on the board.

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Thu Mar 22, 2018 9:38 am

This is the same Trinket M0 board, same laptop.

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by danhalbert on Thu Mar 22, 2018 10:34 am

I tried the code you posted above and it works on my Trinket running CircuitPython 2.2.4. Could you repost the code giving the error?

How are you running this code? Is it is in main.py, or are you importing a file from the REPL?

danhalbert
 
Posts: 802
Joined: Tue Aug 08, 2017 12:37 pm

Re: How to toggle Pin 1?

by Crossette on Thu Mar 22, 2018 1:49 pm

I am running it as code.py and it is imported here as code.txt because the forum does not allow .py files.
Just tried it again and still getting the same error in line 18.
Attachments
code.txt
(1.34 KiB) Downloaded 63 times

Crossette
 
Posts: 44
Joined: Wed Dec 13, 2017 8:50 pm

Re: How to toggle Pin 1?

by Amrat on Fri Mar 23, 2018 5:38 am

I use Flowcode and able to toggle Pin 1. But when Paul out power (usb cable) and restore power again, program will be lost !!! I mean if to toggle LED on Pin 1 have to re-program !!
Please advice
Thanks
Amrat

Amrat
 
Posts: 2
Joined: Mon Mar 19, 2018 11:20 am

Please be positive and constructive with your questions and comments.