The Internet of Things (IoT) is all about interconnecting of embedded devices to form a local mesh network or connect directly to the internet. The existing networking stacks are not optimal for IoT devices due to constraints like low power (and in most cases we don’t have human intervention to replace the battery) and low processing power. For this type of requirements, the IETF (Internet Engineering Task Force) has drafted a few protocols to accommodate the new network requirements.

To test and debug these new protocols we need to not only consider the functionality of the protocol but also test the properties of the device like battery performance and memory usage. For such requirements the traditional network simulators won’t suffice, so we have simulators especially for these devices. One such simulator for IoT devices is Cooja simulator. In this post, I would like to demonstrate how to use cooja as a network testbed for IoT devices, and integrate with the AWS IoT platform to communicate between two different mesh networks.

Cooja is a simulator initially developed by Contiki-OS developers to simulate Contiki based applications. Contiki is a lightweight and flexible operating system for constrained networked devices. We can also test other IoT OSes like Riot or TinyOS based applications too but it is not fully supported yet. Cooja is based on mspsim which is a java based instruction-level simulator of the MSP430 series microprocessor.

Features in Cooja

  • Emulation of 10 metre grid of physical environment: In Cooja, we can simulate a physical environment as a 10 metre grid as shown below. The benefit of this feature is we can actually place the nodes based on their wireless signal strength.
  • IoT Network Testbed Simulation with Cooja

  • Timeline view of events: The bottom window in the above diagram shows the timeline events happening in any node selected. We can even check events like at what interval LEDs blink.
  • Capture network packets and view in wireshark: After establishing mesh network we can capture the packets from all the nodes and store as pcap file which can later be analysed using wireshark.
  • IoT Network Testbed Simulation with Cooja (1)

  • Capture network packets and view in wireshark: After establishing mesh network we can capture the packets from all the nodes and store as pcap file which can later be analysed using wireshark.
  • IoT Network Testbed Simulation with Cooja (2)

  • Communicate with the nodes from the host machine: Using the existing applications in contiki/examples in the Github repo we can easily connect from host machine to the simulated node using web interface.
  • Emulate thousands of devices based on machine capabilities: We can emulate as many devices as we want as the host machine supports.
  • Emulate thousands of devices based on machine capabilities: We can emulate as many devices as we want as the host machine supports.

Limitations

  • Simulation of limited IoT devices: As I said initially, cooja is based on mspsim which can simulate msp430 microcontrollers but the devices like raspberry Pi, arduino can’t be simulated as of now
  • No physical nodes: We can’t simulate physically connected device to monitor its usage
  • Protocol support: As cooja is still evolving, we cannot see all the protocols supported out of the box. The current protocol support in cooja is limited by the operating system’s network stack that is running on the emulated device. But most of the IoT operating systems supports RPL. RPL is IPv6 routing protocol for Low-Power and Lossy Networks. Cooja doesn’t have support for IPv6 border router(6lbr) out of the box, which limits testing of communication from the simulated device to the external world.
  • Communication from cooja node to external world: Using cooja we can’t directly communicate to the external world. We can integrate with a 6LoWPAN border router for this.

Simulation of two different mesh networks via AWS IoT:

Let’s try to simulate a scenario where IoT devices of two different networks communicates via AWS IoT.
IoT Network Testbed Simulation with Cooja (3)

The source code to simulate the above scenario can be found here. Let me explain how we setup this environment and how this works.

  • Setting up of virtual machines: We have launched two virtual machines on two different networks (host only adapter in VirtualBox). One VM is for publishing the message to AWS IoT (VM1) and another is to subscribe to AWS IoT for a topic. Though the VMs were launched using host only adapter, they can access internet using iptables rules(you can find instructions here)
  • Launching publish app in VM1 using Cooja: In VM1, we launch Cooja with the template and in router mode. The template contains the contiki app which publishes the message to mqtt broker which is running in VM1. The router mode in 6lbr create a bridge and assigns a IPv6 address. Mqtt broker listens on the bridge IPv6 address. But as long as we don’t have Ipv6 support stack, we can’t publish it to AWS IoT. So for this purpose we have a python module which listens on IPv6 address of bridge(to get messages from emulated device) and publish it to AWS IoT using IPv4 connectivity. You can say the emulated device publishing messages to AWS.
  • publish

  • Launching subscribe app in VM2 using cooja: Following the same steps as above, except that we need to open a different template. In this VM, the python module subscribes to AWS IoT and publish the messages to MQTT broker running in VM (which is listening on IPv6 address of br0). You can see below the emulated device in Cooja simulator is receiving those messages.
  • subscribe

Conclusion:

As the IoT world is under active development by introducing new protocols or modifying existing protocols for new requirements, Cooja (and corresponding OS) has to adopt these new modifications accordingly. But if we are developing new applications with the existing protocols, Cooja is a good network testbed for IoT devices.