Hello World with rapidM2M

The first steps with a new programming language or a new device are usually made with a “Hello World”. With our Developer Package Edition Devices and Machines (rapidm2m M120) we want to start with a simple Hello World”. For the following example, you need the Toolset that can be downloaded from the Microtronics site for free.

The script outputs information about the module after 5 seconds “Hello World” is printed ones. There is no communciation between the module and the server in this srcipt.The script hello.p is delivered with the Toolset.

[code language=”cpp”] /* forward declarations of public functions */
forward public Timer5s();

/* application entry point */
main()
{
new aId[TrM2M_Id];
new iIdx, iResult;

/* Retrieve identification of HW module */
rM2M_GetId(aId); // provides the information of identify the module

/* output module information */
printf("Id: %s\r\n", aId.string); // complete identification string
printf("Module: %s\r\n", aId.module); // module identification
printf("HW: %d.%d\r\n", aId.hwmajor, aId.hwminor); //Main version number and Minor version number

/* init a oneshot 5s Timer */
iIdx = funcidx("Timer5s");
iResult = rM2M_TimerAddExt(iIdx, false, 5000); /* new 5 sec Timer which calls the function Timer5s.
false means that the timer is stopped after
expiry of the intervall */
printf("rM2M_TimerAddExt(%d) = %d\r\n", iIdx, iResult);
}

/* oneshot 5s Timer */
public Timer5s()
{
print("Hello World!\r\n"); // "Hello World!" is printed
}[/code]

You can check the Ouput in the “Console”-Tab.

einmalige Ausgabe von "Hello World"

If you want to print “Hello World” continuously every 5 seconds you have to change

[code language=”cpp”] iResult = rM2M_TimerAddExt(iIdx, false, 5000);
[/code]

to

[code language=”cpp”] iResult = rM2M_TimerAddExt(iIdx, true, 5000); /* true indicates that the timer restarts
after the configured intervall. */
[/code]

Once again check the Output in the “Console”-Tab.
Alle 5 Sekunden wird "Hello World" ausgegeben

Now let’s see how we can send “Hello World!” to the server.

Therefor we need two local constants which defines the record interval and the transmission interval for the wireless data transmission. The data is saved to the internal flash memory in the configured record interval. The transmission interval indicates how often the data will be sent to the server.

[code language=”cpp”] //every 10 seconds a record will occur, every 35 seconds the three data records will be sent to the server
const ciRecItv = 2; //10 sec, as Timer is called every 5 sec; record interval = 2 * 5
const ciTxItv = 7; //35 sec, as Timer is claled every 10 sec; record interval = 7 * 5
[/code]

Then we have to set the connection type. There are three connections types which can be easily addressed by global constants.

  • RM2M_TXMODE_TRIG: Interval; The data connection is established when the “rM2M_TxStart()” function is called up
  • RM2M_TXMODE_WAKEUP: Interval & wakeup: The connection is established in the same way as in “Interval” mode when the “rM2M_TxStart()” function is called up. Additionally, the device can be initiated via the server to immediately establish a connection on request.
  • RM2M_TXMODE_ONLINE: Online; The device does not disconnect the connection and continuously transmits the measurement data in the configured record interval.

We choose the interval connection type, as we can easily control when data is being sent.

[code language=”cpp”] rM2M_TxSetMode(RM2M_TXMODE_TRIG);
[/code]

Last but not least we have to modify the “Timer5s()” function. We surrogate the print (“Hello World!\r\n”); with following code:

[code language=”cpp”] static iTxTimer = ciTxItv;
static iRecTimer = ciRecItv; /* save transmission interval in a static local variable. The value of the
local variable will remain in the memory at the end of the function, but
can only be accessed in the block. */

/* handle transmit timer */
iTxTimer–; /* reduce iTxTimer every second by one until the transmission timer has expired
–> data is send to server */
if(iTxTimer <= 0)
{
/* trigger connection to server */
rM2M_TxStart(); /* initiate a connection to the server and transfer the data from the
internal flash memory */

/* reload timer */
iTxTimer = ciTxItv; // reload transmission timer, the transmission interval starts again

printf("data transmission\r\n"); // debug output to show that data has been transmitted
}

/* handle record timer */
iRecTimer–; /* reduce iRecTimer every second by one until the record timer has expired
–> data record will be saved in the internal flash memory */
if(iRecTimer <= 0)
{
// new array which holds the data for the record
new aRecData{} = {‘H’,’e’,’l’,’l’,’o’,’ ‘,’W’,’o’,’r’,’l’,’d’,’!’};
new size = sizeof aRecData;
printf("%d", size);
/* record data with actual timestamp */
rM2M_RecData(0, aRecData, 12); /* 12 gives the number of bytes to be saved, so the whole aRecData array
are saved into the internal flash memory.
The first parameter indicates that the current system time is used
as the time stamp.
The data wont be transmitted to the server yet. This will take place in
the next transmission interval. */
/* reload timer */
iRecTimer = ciRecItv; // reload record timer, the record interval starts again

printf("data record\r\n"); // debug output to show that data has been stored
}
[/code]

You can the check the functionality with the debug output. Go to the “Console”-Tab and check the proper working script.

Kontrollieren der Debuginformationen

Now switch to the “Server”-Tab and connect to the server by entering the server URL and your user credentials.

Once the Toolset is connected to the server, the incoming data from the module shows up in the window below.

Daten gehen am Server ein

If you’re in the mood to try out the example yourself, you can order your Edition Devices and Machines: Developer Package M120”  in our online shop.
Buy now!

Sabrina