Wireless communication, here we come! With a couple of PiicoDev Transceivers we'll create a simple messaging system and control some real hardware. This guide will cover connecting your PiicoDev Transceivers to a dev. board and how to send control messages. From here the sky is the limit - gather data from remote sensors or control distant hardware. It's up to you!

Transcript

Wireless connectivity is a staple of modern technology. We use the radio spectrum to transmit and receive all kinds of data for entertainment, communications, and telemetry. The PiicoDev Transceiver is a data radio you can use in your projects to send and receive simple messages. Use it to create a wireless messaging system, maybe control some distant hardware, or transmit data from a remote weather station.

In this guide, we'll set up two transceivers and send data between them. This video will go over connecting the transceiver to a micro bit. Transceiver features two PiicoDev connectors for daisy-chaining with other hardware, a transmit and receive indicator, a user controllable LED, and ID switches to allow daisy-chaining with other PiicoDev smart modules. Make sure all the ID switches are off for now.

The MicroBit already has a very capable radio on board, so why would we use this PiicoDev Transceiver? The PiicoDev Transceiver will support a much longer range than the onboard radio on the MicroBit and it's also convenient for communicating with other types of Dev boards connected to their own PiicoDev Transceiver. If your MicroBit is going to be communicating with another type of Dev board, then check the guide for help with getting set up for that other Dev board.

To follow along, it's best to have at least two transceivers, two Dev boards, and the hard way to connect everything together. If you only have one Dev board, don't worry. There's an example at the end of this guide where we connect two transceivers to a single MicroBit and send messages between the two.

To assemble one transceiver unit, plug your MicroBit into the MicroBit adapter, making sure that the buttons are facing up. Connect your PiicoDev cable to one end and connect the other to your transceiver. If you're a second transceiver, connect the PiicoDev cable to the transceiver and the other end to the MicroBit adapter. my documents

I have my two transceivers set up. Before we start programming, though, I'm going to change some settings in Thonny to make life a bit easier. Alright, I have my two transceivers set up now. Before we connect these to our computer, we'll change some settings in Funny. Go to Tools > Options and under the General tab, make sure that the "Allow only single Thonny instance" option is unchecked. There's a note that we need to restart Thonny after changing this option, so restart Thonny and now I can connect my first transceiver unit. Click Stop, Restart, and there's my connected micro:bit.

Now open another instance of Thonny connect my second setup, and when I try to connect to my second setup, we can see that there are two candidates to connect to. I'll try the COM port with the highest number first. Go to Run > Select Interpreter, make sure micro:bit is selected, and will connect to COM 10 in this case. If that doesn't work, just try the other COM port and here we have two Thonny windows, each connected to a different micro:bit.

Now things might get a little bit confusing working with two separate Thonny windows, so that we don't get lost, I'm going to color the transmitter window teal (T for transmitter) and I'll color the receiver window red (R for receiver). We'll need to download some driver files. In the guide, find the download section, make sure you have the micro:bit tab selected, and right-click that PICAXE Dev Unified link and save link as. I'll save this to a PICAXE directory in my documents. Do the same thing for PICAXE Transceiver, save link as, and save to that same directory in my documents.

Funny, I've already navigated to that same directory, so for my transmitter I can select both files and upload them. And I'll do the same with my receiver. We have prepared these picots ready for programming. It's time to transmit and receive some data.

Find the transmit and receive section in the article. There's a little documentation about how to use the send and receive methods. For now, scroll down to the example transmit and receive data. You can see there are two code listings for this example. On the left, we have the transmitter code and on the right the receiver code. Copy all that code for the transmitter and paste it into a new script.

Take all the receiver code and copy that into a new script for your receiver. First, run the receiver code and save it if prompted. My receiver is running the code and there's nothing being printed to the Shell because we're not transmitting anything yet. Next, run the transmitter code and save if prompted.

Very interesting! Let's take a closer look at what's just happened. When we ran the transmitter code, the receiver has immediately printed four messages. We have the message "Hello World", another message which looks like a number, and then a couple of more complex messages.

Let's take a closer look at these example scripts. We'll start with the transmitter. We set up the transmitter code much like every other Pica Dev project by importing the device driver and a delay function. Next, we initialize the transceiver and we're calling that radio. So, every time you see the word radio in the transmitter script, we're referring to this PiicoDev transceiver. Next, we send a simple string. We use the send method to send the string "Hello World" and that's exactly what we saw on the receiving end. Look at that! We get capitals, we get lowercase, and we even get some punctuation.

After a short delay, we send a number here. We're sending just a value 123. This could be an integer or a floating point number with a decimal point and you can see on the receiver we got that 123. Now sending just a number by itself can be a little bit confusing, especially if we want to send different types of data and we don't want to mix up those different types of data.

So the next two lines are some labelled values. Here we create a variable called named data and we're assigning that a tuple. This tuple contains a string which is the label, in this case temperature °C, and then after a comma we have the value, in this case 25.0. We call radio.send named data and on the receiver end that tuple comes back out. We have 25.0 with the label temperature °C and we do something similar with humidity percent and 60.0.

So you can see this labelled data can be useful if we want to separate our data so it doesn't get mixed up. You can imagine this being used for say a weather station where we wouldn't want to mix up the two different types of data that we're sending.

So those are the three ways that we can use send. We can send a simple string, we can send a value all by itself, or we can send a value with a label in the form of a tuple.

So what's the receiver doing? We initialize it in exactly the same way with the same three setup lines and the receiver is running an infinite loop. We call if radio.receive which will return true if a new message has been received. We can access the contents of that message by reading from the radio.message attribute. So we read that into a variable called simply message and finally we print message and we're running this on a pretty fast loop which means that this is constantly polling the radio to see if a new message has been received. on the receiver

We have received the message. This polling rate is faster than the rate at which we're sending messages, and that's pretty important if you don't want to miss any messages. Okay, so we can send and receive simple messages. Let's actually control some real hardware using these messages. You might recall that the PiicoDev transceiver has a user controllable LED on it. This LED more or less behaves like a power LED; it always turns on when the device gets power, but you can turn it on and off if you wish.

Let's remix this example code to create a simple remote controller. We'll have our transmitter periodically send on/off commands. The receiver will listen for these commands and turn its LED on or off depending on what message it gets. Let's start with the transmitter. Starting at the bottom, I'll delete all of this code up to send "hello world". I know that I want an infinite loop, so I'll create a while true loop and I'll put all of that code inside the loop.

Let's create a command to send. I propose that we send "LED: on" to turn the LED on and "LED: off" to turn the LED off. Radio.send("LED: off"). Now we have to decide which of these messages to send. We'll just continuously send "on/off, on/off" over and over again. For that, I'll need a state and we'll set that to false to begin with. Then, every loop we can check if state is equal to false; then we want to turn the LED on, so we send the command for "on". We can also check if state is equal to true, and that's when we want to turn the LED off, so we send the "off" command. Then, at the end of our loop, we can say state is equal to not state; that will turn true to false and false to true. And we can delay sleep ms 1000.

So what we have just done is write an infinite loop that loop will continuously send on/off commands every one second. I'm just going to run this code before working on the receiver.

My receiver was still running, so from the example I can actually see those commands are coming in on the receiver - that's pretty cool! We're periodically getting LED on/LED off. Now let's do something with that. This part's pretty easy. In the receiver, we can just check the contents of the message variable. After we print the message, we can just insert a couple of if statements. If message is LED on, we want to turn the LED on the board on, and that is Radio.LED equals true. Likewise, if message is equal to LED off, then Radio.LED equals false. And that's it.

If we run this code again, we're getting our messages printed to the Shell as before, and we can also see that that LED is blinking at exactly the same time. Very cool - we can control Hardware over the air! This is a very simple example, but from here you can do whatever you like. Here's another remix idea - the MicroBit has a beautiful 5x5 LED display. Maybe you can remix this controller example so that when you press a button on one micro bit, an image will display or change on the other micro bit.

Now for projects with many transceivers, you may want to manage radio traffic using addresses and groups. This will allow you to send messages that are only received by the intended recipients. Transceivers can be configured with a radio address - by default, this is zero, but it can be any whole number up to 127. To send a private message to a specific transceiver, include the address argument when using the send command. When the address argument is not included, send just broadcasts to address zero, which all transceivers listen to. So you can broadcast to everyone with address 0, but then if you want to send a private message, you use the address of that receiver. A group is a collection of PiicoDev transceivers that may...

Communicate the PiicoDev transceiver will only ever send and receive messages within its own group. The group parameter is set at initialization and is a number between 0 and 255. The default is zero. Now it's also possible to run multiple transceivers on the same PK Dev bus. You might want to do this if you're experimenting and you only have one Dev board or to create a bridge between two transceiver groups. Recall that we're already working with a transceiver that has all its ID switches off. To add a second transceiver, you first need to set a unique ID. Do this by setting the first ID switch on and then the rest can remain off. Then daisy-chain the new transceiver onto the PK Dev bus.

Now in the code, we can initialize the two different transceivers. The first instance I'm calling transmitter and it's the unit with all the ID switches off. So I've included the ID argument with four zeros. For the second instance called receiver, the first switch is on so there is a one in the first place of the ID argument. The remaining three switches are off so there are three zeros that follow. When I run the code, the transmitter will continuously transmit new values and the receiver will receive the values and print them to the shell.

And so there you have it, getting started with the PiicoDev transceiver. I hope you're as excited by this as me. This is a really powerful tool we can send simple text messages from one device to another and you could use that to create some kind of remote control system or remote data Gathering system. You can do a lot by transmitting data over the air. Some fireflies blink their light and certain species of fireflies will actually synchronize their blinking over time. We can create a small project that works kind of to this effect where we can set up each MicroBit to Blink its display.

Slowly at first, all the blinking lights are out of sync. But slowly over time, as the blinking continues, they begin to synchronize. And once they're synchronized, they stay pretty locked on together. This is a pretty cute demo of what you can do with wireless communication.

I can't wait to see what you make with your PiicoDev transceiver. If you make anything cool or you just have some questions, let us know on our forums. We're full-time makers and happy to help. Until next time, happy making!

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.