App Notes

Download zip archive

Key Ideas

  • Connecting two .NOWs with a serial port

Description

This app note uses the eMote .NOW's second serial port to connect to another eMote .NOW. Both motes run a simple ping-pong program where each chooses randomly a number and sends it to the other. When received, the receiver chooses the larger of it's number and the one received, increments it, and sends it back. Hence both motes agree on value and increment it by one on each exchange.

eMote .NOW's come with two serial (COM) ports. COM1 is used for system and debug messages, and sometimes for program data, and uses a serial-USB interface. COM2 is for program data only and uses the COM2 transmit & receive pins on the .NOW. See the .NOW Layout & Pinouts for more information. COM2 can be used to connect two .NOWs or to connect to another device such as a terminal emulator.

Compatibility

eMote.NOW 1.0, eMote version 4.3.1.14 and Visual Studio 2013.

Complexity Level

Fairly introductory. There might be some complexity getting the .NOWs ready for serial communication.

Setup

You'll need two eMote .NOWs for this app note. Make sure that both are setup for COM2. Otherwise, the setup is the same as for the Hello World app note.

Getting Started

Connect both of your eMote .NOWs to your PC. Open this app note in Visual Studio and make sure it builds correctly. When all is well, deploy to the first mote, then change the deployment device to the second mote and deploy to it.

Running the Programs

If you want to see system and debug messages, use a terminal emulator to connect to either or both motes. You cannot use MFDeploy; see setup for COM for details. Press the Reset button on both. On the LCD display of each mote you'll see the number that they have converged to. You can Reset any time you want and force a mote to choose a new number; both will again converge.

Discussion

The basic idea behind this app note is pretty simple. Both motes are running the same program. When one starts, it chooses a number at random as its current value and sends it to the other via the radio. When a mote gets a message from the other, it uses the maximum of the current & received value, adds 1, and sends the new value back. This way, both converge on the same value and take turns incrementing it.

In the program,  note the use of timer ReplyTimer to delay sending the message. It would be tempting to use a Thread.Sleep in the ReceiveCallback method. However, this method is called as part of an message-received interrupt call-back, which should complete as quickly as possible. So rather than delay it by sleeping in the method, a timer, which runs in a separate thread, assumes responsibility for the transmission and the ReceiveCallback method can exit without further delay.

Note that in what we've described so far, a mote will send it's value out once and then wait for the other to receive it and respond. What happens if the other mote is not available? In that case, when the mote has decided that the other one is not responding, it will send out another message, and continue to do so periodically until it hears back. Thus you can start one mote and after a while, start the other one, and they will get in step with each other. So a second timer, NoResponseDelayTimer, with a longer interval, is used in case the other mote is unresponsive. When it fires, the current value is resent and the timer is restarted. That timer gets reset whenever the mote hears from the other, so in the normal case when the motes are communicating, it will never fire. But if either mote becomes unresponsive, it will fire periodically until it again hears from the other mote.

Suggestions

Try incorporating a switch (see the On-Off Switch app note). When the switch is pressed, choose a new random number. Watch the motes re-converge to the correct value.

Designate one of the motes as a base station and send the current value to a PC. See the On-Off Switch with Serial Link app note for an example of how to communicate with the PC.