Search code examples
ccontiki

Simulating different temperatures on Cooja/Contiki


I'm having trouble reading different temperature readings on Cooja. I've done the following:

  • Created a source file that initializes a light- and temperature-sensor.
  • Created a simulation using a Sky mote.
  • Ran the simulation with a network of 8 motes.

Each mote reads the same temperature which is useless for what I want. I've spent the last 8 hours looking through threads, documentation (including the Contiki wiki) and haven't been able to come up with anything.

If I'm misunderstanding the way Cooja/Contiki works I'd also appreciate some help with that but, bottom line, how do I simulate different temperatures that can be read by the mote sensors within the simulation environment?


Solution

  • Cooja/MSPsim does not attempt to simulate realistic sensor readings by default. To do that, you'll need to extend the Java code of MSPsim.

    Reading the temperature sensor on sky simply means reading an ADC port. This means that to simulate custom readings on that port, you need to set up a custom ADCInput on that port.

    Here is a simple example, and here is an advanced example (that also shows how to simulate DAC).

    Code adapted from the first link:

    private SkyMote skyMote;
    
    private final int MY_VALUE = 123;
    
    protected class MyADC implements ADCInput {
       private int fixedValue;
    
       public MyADC(int value) {
           fixedValue = value;
       }
       public int nextData() {
           return fixedValue;
       }
    }
    
    // temperature sensor on sky is on ADC port 10
    IOUnit temperatureADC = skyMote.getCPU().getIOUnit("ADC10");
    if (temperatureADC instanceof ADC12) {
       ((ADC12) temperatureADC).setADCInput(4, new MyADC(MY_VALUE));
    }