Wireless Communication¶
Our SensorTag relies on 6LoWPAN technology (IEEE 802.15.4) for wireless communication. However, we don't need to know the details of the technology. Instead, a high-level library is provided for students to communicate wirelessly.
Software Library¶
The library can be found in the laboratory exercise project template, in the
wireless
directory. The header file is comm_lib.h
.- void Init6LoWPAN(void): Initializes the radio. Call this in the main function.
- int8_t StartReceive6LoWPAN(void): Sets the radio to listening mode. This function needs to be called after initialization and every time after sending a message.
- void Send6LoWPAN(uint16_t DestAddr, uint8_t *ptr_Payload, uint8_t u8_length): Sends a message to the specified recipient.
- DestAddr: The destination address, which can be one of the following:
- The constant IEEE80154_SERVER_ADDR provides the server address used in the lab exercise (among other things).
- A 16-bit number that represents your friend's device address.
- A broadcast message with address 0xFFFF.
- *ptr_Payload: A pointer to the char array holding the message. Typically, messages can be created using the
sprintf
function. - length: The length of the message to be sent. The maximum length is 16 bytes in the lab exercise.
- uint8_t GetRXFlag(void): Checks whether the radio has unread messages in its internal buffer.
- Returns true if there is a message waiting.
- Returns -1 if there is an issue.
- int8_t Receive6LoWPAN(uint16_t *senderAddr, char *payload, uint8_t maxLen): Reads a message from the radio's internal buffer.
- *senderAddr: The address of the message sender.
- *payload: A char array to store the message. You need to define this array yourself.
- maxLen: The maximum allowed message length.
- int8_t GetRSSI(void): Returns the received signal strength, RSSI.
Example¶
In this example, a message is sent to the server when the user presses a button, and the program waits for incoming messages.
#include <comm_lib.h>
// Send a message when a button is pressed
void buttonFxn(PIN_Handle handle, PIN_Id pinId) {
// Message is formatted into a char array, making it easy to modify the output
char payload[16];
sprintf(payload, "I am %x", IEEE80154_MY_ADDR);
Send6LoWPAN(IEEE80154_SERVER_ADDR, payload, strlen(payload));
}
// Task to handle communication
Void commTask(UArg arg0, UArg arg1) {
char payload[16]; // Message buffer
uint16_t senderAddr;
// Initialize receiver mode
int32_t result = StartReceive6LoWPAN();
if(result != true) {
System_abort("Wireless receive start failed");
}
// Send a message
Send6LoWPAN(IEEE80154_SERVER_ADDR, "Hello", 5);
// Set radio back to receiver mode
StartReceive6LoWPAN();
while (true) {
// Check if there are messages
if (GetRXFlag() == true) {
// Clean the receive buffer
memset(payload, 0, 16);
// Read the message
Receive6LoWPAN(&senderAddr, payload, 16);
System_printf(payload);
System_flush();
}
} // No Task_sleep here
}
Int main(void) {
...
// Initialize radio
Init6LoWPAN();
/* Communication Task */
Task_Params_init(&commTaskParams);
commTaskParams.stackSize = COMMSTACKSIZE;
commTaskParams.stack = &commTaskStack;
commTaskParams.priority = 1; // Set priority to 1
commTask = Task_create(commTaskFxn, &commTaskParams, NULL);
if (commTask == NULL) {
System_abort("Task creation failed!");
}
...
}
There is one very important rule when using the wireless library: NEVER SEND ANY MESSAGES IN A LOOP as this can flood the channel, rendering it unusable for others. It’s possible to accidentally send hundreds of messages per second. Naturally, we want everyone to finish the exercises successfully, so please don’t make it difficult for others. Always double-check that you are not sending messages in a loop after editing your code.
IMPORTANT!
- It is crucial that:
- You do not use
Task_sleep
in the task that controls the network (where you first call StartReceive6LowPan and thengetRXFlag()
). - You assign a lower priority to the network task: All other tasks should have priority 2, while the network task should have priority 1.
Give feedback on this content
Comments about this material