Hello, World

A number of example applications for Contiki-NG are provided under examples in the root directory. In this tutorial, we will show how the hello-world example works. This classic programming example will just print “Hello, world” to the serial port periodically. Take a look at the hello-world.c file to see the source code. This example illustrates how processes are defined, and how to make sure that the operating system automatically starts a process after booting up.

Running the example as a native node

Contiki-NG can be compiled as a native Unix process. This is useful for instance to implement the RPL native Border Router, and in general for testing: the networking stack of native nodes is reachable through a tun interface they create. One can use this interface to e.g. ping the node (see tutorial:ping) or send CoAP requests (see tutorial:coap).

To run hello-world in native mode, type:

$ cd examples/hello-world
$ make TARGET=native
$ ./hello-world.native

Output

user@7aa37a4ab022:~/contiki-ng$ cd examples/
user@7aa37a4ab022:~/contiki-ng/examples$ cd hello-world/
user@7aa37a4ab022:~/contiki-ng/examples/hello-world$ ls
Makefile  README.md  hello-world.c
user@7aa37a4ab022:~/contiki-ng/examples/hello-world$ make TARGET=native
  MKDIR     build/native/obj/.deps
  CC        hello-world.c
  CC        ../../arch/platform/native/./platform.c
  CC        ../../arch/platform/native/./clock.c
  CC        ../../arch/platform/native/dev/xmem.c
  CC        ../../arch/platform/native/dev/buttons.c
  CC        ../../arch/platform/native/./cfs-posix.c
  CC        ../../arch/platform/native/./cfs-posix-dir.c
  CC        ../../arch/cpu/native/net/tun6-net.c
  CC        ../../arch/cpu/native/./rtimer-arch.c
  CC        ../../arch/cpu/native/./watchdog.c
  CC        ../../arch/cpu/native/dev/eeprom.c
  CC        ../../arch/cpu/native/./int-master.c
  CC        ../../arch/cpu/native/dev/gpio-hal-arch.c
  CC        ../../os/contiki-main.c
  CC        ../../os/dev/button-hal.c
  CC        ../../os/dev/gpio-hal.c
  CC        ../../os/dev/leds.c
  CC        ../../os/dev/nullradio.c
  CC        ../../os/dev/serial-line.c
  CC        ../../os/lib/aes-128.c
  CC        ../../os/lib/assert.c
  CC        ../../os/lib/ccm-star.c
  CC        ../../os/lib/circular-list.c
  CC        ../../os/lib/crc16.c
  CC        ../../os/lib/csprng.c
  CC        ../../os/lib/dbl-circ-list.c
  CC        ../../os/lib/dbl-list.c
  CC        ../../os/lib/heapmem.c
  CC        ../../os/lib/hexconv.c
  CC        ../../os/lib/ifft.c
  CC        ../../os/lib/iq-seeder.c
  CC        ../../os/lib/list.c
  CC        ../../os/lib/memb.c
  CC        ../../os/lib/random.c
  CC        ../../os/lib/ringbuf.c
  CC        ../../os/lib/ringbufindex.c
  CC        ../../os/lib/trickle-timer.c
  CC        ../../os/net/link-stats.c
  CC        ../../os/net/linkaddr.c
  CC        ../../os/net/nbr-table.c
  CC        ../../os/net/net-debug.c
  CC        ../../os/net/netstack.c
  CC        ../../os/net/packetbuf.c
  CC        ../../os/net/queuebuf.c
  CC        ../../os/net/ipv6/ip64-addr.c
  CC        ../../os/net/ipv6/psock.c
  CC        ../../os/net/ipv6/sicslowpan.c
  CC        ../../os/net/ipv6/simple-udp.c
  CC        ../../os/net/ipv6/tcp-socket.c
  CC        ../../os/net/ipv6/tcpip.c
  CC        ../../os/net/ipv6/udp-socket.c
  CC        ../../os/net/ipv6/uip-ds6-nbr.c
  CC        ../../os/net/ipv6/uip-ds6-route.c
  CC        ../../os/net/ipv6/uip-ds6.c
  CC        ../../os/net/ipv6/uip-icmp6.c
  CC        ../../os/net/ipv6/uip-nameserver.c
  CC        ../../os/net/ipv6/uip-nd6.c
  CC        ../../os/net/ipv6/uip-packetqueue.c
  CC        ../../os/net/ipv6/uip-sr.c
  CC        ../../os/net/ipv6/uip-udp-packet.c
  CC        ../../os/net/ipv6/uip6.c
  CC        ../../os/net/ipv6/uipbuf.c
  CC        ../../os/net/ipv6/uiplib.c
  CC        ../../os/net/mac/mac-sequence.c
  CC        ../../os/net/mac/framer/frame802154.c
  CC        ../../os/net/mac/framer/frame802154e-ie.c
  CC        ../../os/net/mac/framer/framer-802154.c
  CC        ../../os/net/mac/framer/nullframer.c
  CC        ../../os/net/mac/nullmac/nullmac.c
  CC        ../../os/net/routing/rpl-lite/rpl-dag-root.c
  CC        ../../os/net/routing/rpl-lite/rpl-dag.c
  CC        ../../os/net/routing/rpl-lite/rpl-ext-header.c
  CC        ../../os/net/routing/rpl-lite/rpl-icmp6.c
  CC        ../../os/net/routing/rpl-lite/rpl-mrhof.c
  CC        ../../os/net/routing/rpl-lite/rpl-nbr-policy.c
  CC        ../../os/net/routing/rpl-lite/rpl-neighbor.c
  CC        ../../os/net/routing/rpl-lite/rpl-of0.c
  CC        ../../os/net/routing/rpl-lite/rpl-timers.c
  CC        ../../os/net/routing/rpl-lite/rpl.c
  CC        ../../os/sys/atomic.c
  CC        ../../os/sys/autostart.c
  CC        ../../os/sys/compower.c
  CC        ../../os/sys/ctimer.c
  CC        ../../os/sys/energest.c
  CC        ../../os/sys/etimer.c
  CC        ../../os/sys/log.c
  CC        ../../os/sys/mutex.c
  CC        ../../os/sys/node-id.c
  CC        ../../os/sys/process.c
  CC        ../../os/sys/rtimer.c
  CC        ../../os/sys/stimer.c
  CC        ../../os/sys/timer.c
  LD        build/native/hello-world.native
  CP        build/native/hello-world.native --> hello-world.native
user@7aa37a4ab022:~/contiki-ng/examples/hello-world$ ./hello-world.native 
[WARN: Tun6      ] Failed to open tun device (you may be lacking permission). Running without network.
[INFO: Main      ] Starting Contiki-NG-develop/v4.9-508-g3754d0088
[INFO: Main      ] - Routing: RPL Lite
[INFO: Main      ] - Net: tun6
[INFO: Main      ] - MAC: nullmac
[INFO: Main      ] - 802.15.4 PANID: 0xabcd
[INFO: Main      ] - 802.15.4 Default channel: 26
[INFO: Main      ] Node ID: 1800
[INFO: Main      ] Link-layer address: 0102.0304.0506.0708
[INFO: Main      ] Tentative link-local IPv6 address: fe80::302:304:506:708
[INFO: Native    ] Added global IPv6 address fd00::302:304:506:708
Hello, world

You first see the Contiki-NG boot messages and then a line with the string “Hello, world”. This line will repeat periodically. Contiki-NG debugging messages will also be printed. Note that the warning regarding opening the tun device can be ignored, as this example does not depend on the networking functionality.

Last updated