我正在尝试在 Contiki-ng 上使用 Cooja 模拟器,并且我有如下传感器:
#include "contiki.h"
#include <stdio.h>
#include <stdlib.h>
#include "sys/etimer.h"
static int humidity = 60; // Initial humidity value (percentage)
int generate_humidity_data(void); // Function declaration
PROCESS(humidity_sensor_process, "Humidity Sensor Process");
AUTOSTART_PROCESSES(&humidity_sensor_process);
PROCESS_THREAD(humidity_sensor_process, ev, data)
{
PROCESS_BEGIN();
static struct etimer et_hum;
etimer_set(&et_hum, 5*CLOCK_SECOND ); // Sample humidity every 20 seconds
while (1)
{
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et_hum));
if (ev == PROCESS_EVENT_TIMER && data == &et_hum)
{
humidity = generate_humidity_data();
printf("Humidity: %d\n", humidity);
etimer_restart(&et_hum);
}
}
PROCESS_END();
}
int generate_humidity_data(void)
{
// Simulate humidity variation
humidity += (rand() % 5) - 2; // Random change between -2% and 2%
return humidity;
}
每次我在模拟器中添加一个带有其中一个传感器的微尘时,模拟都会在 30 秒后停止并出现以下错误:
FATAL [Thread-3] (Simulation.java:293) - Simulation stopped due to error: Event is already scheduled: DELAY
[java] java.lang.IllegalStateException: Event is already scheduled: DELAY
[java] at org.contikios.cooja.EventQueue.addEvent(EventQueue.java:55)
[java] at org.contikios.cooja.EventQueue.addEvent(EventQueue.java:49)
[java] at org.contikios.cooja.Simulation.scheduleEvent(Simulation.java:195)
[java] at org.contikios.cooja.Simulation$2.execute(Simulation.java:218)
[java] at org.contikios.cooja.Simulation.run(Simulation.java:281)
[java] at java.lang.Thread.run(Thread.java:748)