This tutorial refers to a product that has reached its end-of-life status.

Arduino Yún Mailbox Read Message

Send text messages to the Arduino processor using REST API through a browser.

This example for a Yún device shows how to use the Bridge library to send text messages from the Linux to the AVR. It demonstrate how you can create a queue of messages using REST style calls through the browser.

When running this example, make sure your computer is on the same network as the Yún device. Once you have uploaded the sketch on the board you can start to append messages in the Yún mailbox. The Mailbox will be checked every 10 seconds and the available messages displayed on the Serial Monitor. To use the REST APIs you need to insert the password or disable it from the Web panel. You can use a browser with the following URL structure: http://myArduinoYun.local/mailbox/hello

Hardware Required

  • Yún board or shield

  • computer and Yún on the same wireless or wired network

Software Required

  • web browser

Circuit

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

The example code shows how it's possible to make REST requests to the Yún deviceto send messages from the Linux side to the AVR. The messages are stored in a message queue, internal to the Linux side, and read by the AVR only when the readMessage() method is called.

You need to include only the Mailbox library because it automatically include the Bridge library:

1#include <Mailbox.h>

In

setup()
, start serial communication for debugging purposes, and turn the built-in LED on pin 13 high while Bridge begins.
Bridge.begin()
is blocking, and should take about 2 seconds to complete. Once Bridge starts up, turn the LED off.
Mailbox.begin()
starts the Mailbox on the OpenWrt-Yun and on the Arduino processor.

1void setup() {
2
3 pinMode(13, OUTPUT);
4
5 digitalWrite(13, LOW);
6
7 // Initialize Bridge and Mailbox
8
9 Bridge.begin();
10
11 Mailbox.begin();
12
13 digitalWrite(13, HIGH);
14
15 // Initialize Serial
16
17 Serial.begin(9600);
18
19 // Wait until a Serial Monitor is connected.
20
21 while (!Serial);
22
23 Serial.println("Mailbox Read Message\n");
24
25 Serial.println("The Mailbox is checked every 10 seconds. The incoming messages will be shown below.\n");
26}

In

loop()
, you'll create a String where to save the incoming message and call the
Mailbox.messageAvailable()
function to read if there is an available message in the Mailbox.

1void loop() {
2
3 String message;
4
5 // if there is a message in the Mailbox
6
7 if (Mailbox.messageAvailable())
8
9 {

If there is at least one message in the Mailbox, start to read all the messages in the queue and print it on the Serial Monitor.

1// read all the messages present in the queue
2
3 while (Mailbox.messageAvailable())
4
5 {
6
7 Mailbox.readMessage(message);
8
9 Serial.println(message);
10
11 }

The Mailbox in the sketch is checked every 10 seconds using a

delay()
. This is also done to demonstrate the advantage to store data on the Linux processor instead of cluttering the RAM of the Arduino processor.

1Serial.println("Waiting 10 seconds before checking the Mailbox again");
2
3 }
4
5 // wait 10 seconds
6
7 delay(10000);
8}

The full code is below:

1/*
2
3 Read Messages from the Mailbox
4
5 This example for the YunShield/Yún shows how to
6
7 read the messages queue, called Mailbox, using the
8
9 Bridge library.
10
11 The messages can be sent to the queue through REST calls.
12
13 Append the message in the URL after the keyword "/mailbox".
14
15 Example
16
17 "/mailbox/hello"
18
19 created 3 Feb 2014
20
21 by Federico Vanzati & Federico Fissore
22
23 This example code is in the public domain.
24
25 http://www.arduino.cc/en/Tutorial/MailboxReadMessage
26
27 */
28
29#include <Mailbox.h>
30
31void setup() {
32
33 pinMode(13, OUTPUT);
34
35 digitalWrite(13, LOW);
36
37 // Initialize Bridge and Mailbox
38
39 Bridge.begin();
40
41 Mailbox.begin();
42
43 digitalWrite(13, HIGH);
44
45 // Initialize Serial
46
47 SerialUSB.begin(9600);
48
49 // Wait until a Serial Monitor is connected.
50
51 while (!SerialUSB);
52
53 SerialUSB.println("Mailbox Read Message\n");
54
55 SerialUSB.println("The Mailbox is checked every 10 seconds. The incoming messages will be shown below.\n");
56}
57
58void loop() {
59
60 String message;
61
62 // if there is a message in the Mailbox
63
64 if (Mailbox.messageAvailable()) {
65
66 // read all the messages present in the queue
67
68 while (Mailbox.messageAvailable()) {
69
70 Mailbox.readMessage(message);
71
72 SerialUSB.println(message);
73
74 }
75
76 SerialUSB.println("Waiting 10 seconds before checking the Mailbox again");
77
78 }
79
80 // wait 10 seconds
81
82 delay(10000);
83}

Last revision 2016/05/25 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.