--- /dev/null
+#include <contiki.h>
+#include <contiki-lib.h>
+#include <contiki-net.h>
+#include <stdio.h>
+#include <dev/leds.h>
+#include <dev/button-sensor.h>
+#include <string.h>
+
+////////////////// DEBUGGING STUFF
+#define DEBUG DEBUG_PRINT
+#include "net/uip-debug.h"
+//////////////////////////////////
+
+PROCESS(led_remote_control, "binary LED counter via button over UDP");
+AUTOSTART_PROCESSES(&led_remote_control);
+
+static uip_ipaddr_t ipaddr;
+static uint8_t seq_id;
+static struct simple_udp_connection udp_conn;
+#define UDP_PORT 1337
+
+static void recv(struct simple_udp_connection *c, const uip_ipaddr_t *source_addr,
+ uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port,
+ const uint8_t *data, uint16_t datalen) {
+
+ PRINTF("recv from ");
+ PRINT6ADDR(source_addr);
+ PRINTF(" port %d: %d\n", source_port, *data);
+
+ if((*data) & 0x01) {
+ leds_on(LEDS_GREEN);
+ } else {
+ leds_off(LEDS_GREEN);
+ }
+ if((*data) & 0x02) {
+ leds_on(LEDS_YELLOW);
+ } else {
+ leds_off(LEDS_YELLOW);
+ }
+ PRINTF("toggled LEDs\n");
+}
+
+
+PROCESS_THREAD(led_remote_control, ev, data) {
+ PROCESS_BEGIN();
+
+ uip_create_linklocal_allnodes_mcast(&ipaddr);
+ if(!simple_udp_register(&udp_conn, uip_htons(UDP_PORT), NULL,
+ uip_htons(UDP_PORT), recv)) {
+ printf("o_O could not allocate simple UDP connection!\n");
+ }
+
+ leds_init();
+ leds_off(LEDS_ALL);
+
+ SENSORS_ACTIVATE(button_sensor);
+
+ static struct etimer timer;
+ while(1) {
+ PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
+
+ if(ev == sensors_event) {
+ // debounce for 250 ms
+ etimer_set(&timer, CLOCK_SECOND * 0.25);
+ PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
+
+ seq_id++; // start with 1 for better effect
+
+ PRINTF("Sending message to ");
+ PRINT6ADDR(&ipaddr);
+ PRINTF(": %d\n", seq_id);
+
+ simple_udp_sendto(&udp_conn, &seq_id, 1, &ipaddr);
+ }
+ }
+
+ PROCESS_END();
+}