Weather forecasts on your NXT (or: communication NXT to PC)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Weather forecasts on your NXT (or: communication NXT to PC)

Post by linusa »

This is a google cache recover of an old nxtasy blog post, hope someone finds it useful.
Weather forecasts on your NXT

or communication between an NXT, a computer and the internet.

Quite some time ago I wanted to give a brief tutorial about communication between an embedded program running on the NXT (for example written in NXC) and a computer. The reasons one would want to send data from an NXC program to a PC or vice versa can be divided into two main categories: Either the NXC program wants the computer to do things it can’t (complex calculations, look up information), or a computer program needs to tell an NXC program what to do (or which information to collect or log).

Weather Demo= In the search for a good example I came across this idea: Wouldn’t it be great to show how the NXT could retrieve data from the internet (although the display is to small for a browser ;) )? The first kind of data I could think of were weather forecasts (or current weather conditions). So here we are: This is how it looks if your NXT can show the current temperature and brief conditions of any place around the world (screenshot taken with J. Hansen’s tool NXTScreen).
Image

Short description: The NXC program (client) can request weather conditions by storing a string in one of its mailboxes. The computer program (server, written in MATLAB) constantly looks for messages there via direct commands. If one is found the according weather conditions will be retrieved from the internet using the Yahoo! Weather RSS service. The most important data are extracted and sent back to the NXT (again by storing a string in a mailbox). The NXC program in term waits until a string can be read from the queue, parses it and finally displays city name, current temperature, and condition.

Left and right buttons cycle through multiple locations (i.e. cities); the orange center button toggles between Fahrenheit and Celsius mode! Interested? Read on!

The main requirements for bi-directional communication between an NXC program and a computer program are functions to send and receive strings. Once we have these, we can define our own protocol and send information back and forth. In NXC, we can use the function SendMessage() to store a string in a mailbox (let’s call this the NXC-Outbox for a moment, number 0 is fine). To retrieve strings from a mailbox (that have been put there by the computer), the function ReceiveRemoteString() works well. This time, we better use a different mailbox queue, e.g. number 1, and name it NXC-Inbox.

Half there! The NXC program can send and receive data, how about the PC side? We need some sort of program or library that supports direct commands (those are commands you can use to remote control your NXT; you can find more details in the Bluetooth Developer Kit). There are many implementations, good overviews are either this Wikipedia table or Steve’s comparison. Even more, we need to find a solution that already supports the direct commands MessageRead and MessageWrite (I’m saying this since some packages are still under development and don’t support the full set of commands). If not, those direct commands would have to be implemented (the Bluetooth Developer Kit has details about the format).

So let’s say we already have MessageRead and MessageWrite. These are the counter-parts of SendMessage() and ReceiveRemoteString() in NXC. We use MessageWrite to send a string to the mailqueue we called NXC-Inbox before (so the PC’s outbox is the NXT’s inbox). Just as well we use MessageRead to see if there’s something waiting for us in the NXC-Outbox (again, this would be the inbox from the PC’s point of view). If you get confused, don’t worry. Just remember that the computer has to receive what the NXT sends and the other way around.

Almost done. We have everything we need. We can send little “orders” (commands would be a better word) from the NXT to the PC, for example. Of course we have to define what those commands mean. The PC could see the incoming strings, react on them and send answers. This is what my weather demo does. I use Yahoo! Weather as actual source, so the program on the PC retrieves data from the internet (it’s actually data in the RSS newsreader format, the API details are described here).

As language I chose MATLAB (and the RWTH - Mindstorms NXT Toolbox, which I happen to be a developer of ;) ). It provides the direct commands mentioned above, as well as a very easy-to-use built-in function to retrieve data from the internet. In theory the server could be implemented in any language. A detail I didn’t mention before is that we of course have to connect the computer to the NXT somehow. This can be a bit complicated if you want to use Bluetooth, it’s usually easier with USB, but always depends on your OS and hardware (as well as the programming package you use). With the MATLAB toolbox, you just have to plug in the USB cable or connect the Bluetooth dongle with the driver software, and you’re ready to go. Other packages are similar easy to use.

So finally, where is the sourcecode? Here’s the NXC client program, and here’s the MATLAB server program. If you don’t have MATLAB installed, this HTML version with syntax highlighting is easier to read. Both programs are heavily commented and can be used freely (GPL license).

If you just wanted to write a program that reacts on certain weather data (or whatever is available on a computer these days), you could have done this without an NXC program of course (just using remote control software like the MATLAB toolbox), resulting in a much simpler program. But then you’re in certain other ways limited (Bluetooth latency, or real time motor control for example). The way I documented it in this little tutorial, you could adapt the NXC program to work without a computer in range, and only use external data if they are available. The main point is that I wanted to illustrate some NXC-to-Computer communication.

So what do you think? What new kind of applications are possible? Any great ideas? If you did anything interesting, useful, or funny with this example, or have any other questions, please don’t hesitate to start a discussion in the forums!
Linus Atorf
Unfortunately the links are gone, but you can find the sourcecode, a screenshot and a syntax-highlighted HTML version of the code here:
http://www.mindstorms.rwth-aachen.de/tr ... therServer

Edit 2011-10-28: Added screenshot of WeatherDemo to this post
Last edited by linusa on 28 Oct 2011, 17:50, edited 1 time in total.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: Weather forecasts on your NXT (or: communication NXT to PC)

Post by linusa »

Please also have a look at this excellent tutorial by Nicolas Schodet, focusing on a similar topic:

Exchange messages with a NXT program
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests