2 #include <contiki-lib.h>
3 #include <contiki-net.h>
6 #include <dev/button-sensor.h>
9 ////////////////// DEBUGGING STUFF
10 #define DEBUG DEBUG_PRINT
11 #include "net/uip-debug.h"
12 //////////////////////////////////
14 PROCESS(led_remote_control
, "binary LED counter via button over UDP");
15 AUTOSTART_PROCESSES(&led_remote_control
);
17 static uip_ipaddr_t ipaddr
;
18 static uint8_t seq_id
;
19 static struct simple_udp_connection udp_conn
;
22 static void recv(struct simple_udp_connection
*c
, const uip_ipaddr_t
*source_addr
,
23 uint16_t source_port
, const uip_ipaddr_t
*dest_addr
, uint16_t dest_port
,
24 const uint8_t *data
, uint16_t datalen
) {
27 PRINT6ADDR(source_addr
);
28 PRINTF(" port %d: %d\n", source_port
, *data
);
38 leds_off(LEDS_YELLOW
);
40 PRINTF("toggled LEDs\n");
44 PROCESS_THREAD(led_remote_control
, ev
, data
) {
47 uip_create_linklocal_allnodes_mcast(&ipaddr
);
48 if(!simple_udp_register(&udp_conn
, uip_htons(UDP_PORT
), NULL
,
49 uip_htons(UDP_PORT
), recv
)) {
50 printf("o_O could not allocate simple UDP connection!\n");
56 SENSORS_ACTIVATE(button_sensor
);
58 static struct etimer timer
;
60 PROCESS_WAIT_EVENT_UNTIL(ev
== sensors_event
&& data
== &button_sensor
);
62 if(ev
== sensors_event
) {
63 // debounce for 250 ms
64 etimer_set(&timer
, CLOCK_SECOND
* 0.25);
65 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer
));
67 seq_id
++; // start with 1 for better effect
69 PRINTF("Sending message to ");
71 PRINTF(": %d\n", seq_id
);
73 simple_udp_sendto(&udp_conn
, &seq_id
, 1, &ipaddr
);