重写 contiki 源代码“example-mesh.c”以根据计时器值发送消息

Rewrite in the contiki source code “example-mesh.c” to send messages based on a timer value

本文关键字:计时器 消息 源代码 contiki example-mesh 重写      更新时间:2023-10-16

Contiki "example-mesh.c"中的示例旨在每次按下按钮时发送消息,但我希望此示例每 5 秒定期发送数据,例如而不是按下按钮,所以我需要重写示例"example-mesh.c"以根据计时器值发送消息。这是代码,但我在微尘输出窗口中运行模拟,只发送 3 条消息,其余的为"数据包超时",我该如何解决这个问题:

#include "contiki.h"
#include "net/rime.h"
#include "net/rime/mesh.h"
#include "contiki-conf.h"
#include "sys/etimer.h"
#include "sys/process.h"
#include "sys/ctimer.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
  printf("packet sentn");
}
static void
timedout(struct mesh_conn *c)
{
     
  printf("packet timedoutn");
}
static void
recv(struct mesh_conn *c, const rimeaddr_t *from, uint8_t hops)
{
  printf("Data received from %d.%d: %.*s (%d)n",
	 from->u8[0], from->u8[1],
	 packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
  packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
  mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
   static struct etimer et;
  PROCESS_EXITHANDLER(mesh_close(&mesh);)
  PROCESS_BEGIN();
  mesh_open(&mesh, 132, &callbacks);
 etimer_set(&et, CLOCK_SECOND +50);
  while(1) {
   rimeaddr_t addr;
 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); 
 
    /* Send a message to node number 1. */
    
    packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
    addr.u8[0] = 1;
    addr.u8[1] = 0;
    mesh_send(&mesh, &addr);
}
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

我知道

这已经有几年的历史了,但这是我的做法。我使用了examples/rime/example-mesh.c程序,并对其进行了修改,以触发计时器过期而不是按钮按下。我把它加载到TelosB(Sky mote)上,它工作了。是的,Contiki 是用 C 而不是 C++ 写的。

#include "contiki.h"
#include "net/rime/rime.h"
#include "net/rime/mesh.h"
#include "sys/etimer.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#define MESSAGE "Hello"
static struct mesh_conn mesh;
/*---------------------------------------------------------------------------*/
PROCESS(example_mesh_process, "Mesh example");
AUTOSTART_PROCESSES(&example_mesh_process);
/*---------------------------------------------------------------------------*/
static void
sent(struct mesh_conn *c)
{
  printf("packet sentn");
}
static void
timedout(struct mesh_conn *c)
{
  printf("packet timedoutn");
}
static void
recv(struct mesh_conn *c, const linkaddr_t *from, uint8_t hops)
{
  printf("Data received from %d.%d: %.*s (%d)n",
     from->u8[0], from->u8[1],
     packetbuf_datalen(), (char *)packetbuf_dataptr(), packetbuf_datalen());
  packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
  mesh_send(&mesh, from);
}
const static struct mesh_callbacks callbacks = {recv, sent, timedout};
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_mesh_process, ev, data)
{
  PROCESS_EXITHANDLER(mesh_close(&mesh);)
  PROCESS_BEGIN();
  mesh_open(&mesh, 132, &callbacks);
  //SENSORS_ACTIVATE(button_sensor);

  static struct etimer et;

  while(1) {
    linkaddr_t addr;
    /* Wait for button click before sending the first message. */
    //PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
    etimer_set(&et, CLOCK_SECOND * 5);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    printf("Timer Expired!nr");
    /* Send a message to node number 1. */
    packetbuf_copyfrom(MESSAGE, strlen(MESSAGE));
    addr.u8[0] = 1;
    addr.u8[1] = 0;
    mesh_send(&mesh, &addr);
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/