55 #define DEBUG 0 //Making this 1 will slightly alter command timings
57 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
61 #define DEBUGSERIAL 0 //Making this 1 will significantly alter command timings
64 #include "contiki-lib.h"
65 #include "contiki-net.h"
68 #include "webserver-nogui.h"
69 #include "httpd-cgi.h"
72 #include "raven-lcd.h"
73 #include "lib/sensors.h"
77 #include <avr/pgmspace.h>
78 #include <avr/eeprom.h>
79 #include <avr/sleep.h>
80 #include <dev/watchdog.h>
82 static uint8_t
count = 0;
84 uip_ipaddr_t dest_addr;
86 #define MAX_CMD_LEN 20
88 uint8_t frame[MAX_CMD_LEN];
95 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
96 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
97 #define PING6_DATALEN 16
99 static int battery_value;
100 static int temperature_value;
102 void rs232_send(uint8_t port,
unsigned char c);
110 #define PING_GOOGLE 0
120 }
else if (seqno==2) {
122 }
else if (seqno==3) {
139 count, PING6_DATALEN);
156 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
167 char serial_char_received;
180 void micro_sleep(uint8_t howlong)
182 uint8_t saved_sreg = SREG, saved_howlong = howlong;
183 #if AVR_CONF_USE32KCRYSTAL
185 uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
191 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
192 if (howlong==0) howlong=3;
196 TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));
211 while(ASSR & (1 << TCN2UB));
212 TIMSK2 |= (1 << OCIE2A);
217 serial_char_received=0;
225 if (saved_howlong)
break;
227 if (serial_char_received)
break;
232 #if AVR_CONF_USE32KCRYSTAL
236 TCCR2A = savedTCCR2A;
237 TCCR2B = savedTCCR2B;
242 TIMSK2 &= ~(1 << OCIE2A);
247 #if !AVR_CONF_USE32KCRYSTAL
251 ISR(TIMER2_COMPA_vect)
264 raven_gui_loop(process_event_t ev, process_data_t data)
266 uint8_t i,activeconnections,radio_state;
270 printf_P(PSTR(
"Buffer [%d]="),serialcount);
273 printf_P(PSTR(
" %d"),dbuf[i]);
289 send_frame(REPORT_PING_BEEP, 0, 0);
293 send_frame(REPORT_PING, 1, &seqno);
300 PRINTF(
"\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
306 seqno = cmd.frame[0];
312 web_set_temp((
char *)cmd.frame);
314 temperature_value = atoi((
char *)cmd.frame);
319 web_set_voltage((
char *)cmd.frame);
321 battery_value = atoi((
char *)cmd.frame);
325 if (cmd.frame[0]==0) {
334 if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
336 if (activeconnections) {
337 PRINTF(
"\nWaiting for %d connections",activeconnections);
342 radio_state = NETSTACK_RADIO.off();
343 PRINTF (
"\nsleep %d radio state %d...",cmd.frame[0],radio_state);
346 PRINTF(
"\nSleeping...");
347 micro_sleep(cmd.frame[0]);
349 radio_state = NETSTACK_RADIO.on();
350 if (radio_state > 0) {
353 PRINTF(
"Radio wake error %d\n",radio_state);
358 send_frame(REPORT_WAKE, 0, 0);
378 int raven_lcd_serial_input(
unsigned char ch)
382 if (ch) serial_char_received++;
384 if (serialcount<25) dbuf[serialcount]=ch;
413 if (cmd.ndx >= (MAX_CMD_LEN+3)) {
420 if (cmd.ndx >= cmd.len+3){
438 cmd.frame[cmd.ndx - 3] = ch;
449 raven_lcd_show_text(
char *text) {
450 uint8_t textlen=strlen(text)+1;
451 if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
452 send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
457 lcd_show_servername(
void) {
460 extern uint8_t eemem_server_name[16];
462 char buf[
sizeof(eemem_server_name)+1];
463 eeprom_read_block (buf,eemem_server_name,
sizeof(eemem_server_name));
464 buf[
sizeof(eemem_server_name)]=0;
465 raven_lcd_show_text(buf);
471 value_temperature(
int type) {
472 return temperature_value;
476 value_battery(
int type) {
477 return battery_value;
482 configure(
int type,
int value) {
484 return type = value = 1;
499 SENSORS_SENSOR(temperature_sensor,
"Temperature",
500 value_temperature, configure, status);
501 SENSORS_SENSOR(battery_sensor,
"Battery",
502 value_battery, configure, status);
505 PROCESS(raven_lcd_process,
"Raven LCD interface process");
512 lcd_show_servername();
521 raven_gui_loop(ev, data);
#define ICMP6_ECHO_REPLY
Echo reply.
uip_len
The length of the packet in the uip_buf buffer.
#define EOF_CHAR
End-of-frame character.
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
#define ICMP6_ECHO_REQUEST
Echo request.
#define PROCESS_END()
Define the end of a process.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
uint16_t uip_icmp6chksum(void)
Calculate the ICMP checksum of the packet in uip_buf.
#define UIP_ICMP6_ECHO_REQUEST_LEN
Echo Request constant part length.
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
#define UIP_IP_BUF
Pointer to IP header.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
Source address selection, see RFC 3484.
void rs232_send(uint8_t port, unsigned char c)
Print a character on RS232.
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
#define NULL
The null pointer.
#define PROCESS_YIELD()
Yield the currently running process.
uint8_t icmp6_new(void *appstate)
register an ICMPv6 callback
process_event_t tcpip_icmp6_event
The ICMP6 event.
#define UIP_ICMP_BUF
Pointer to ICMP header.
#define CLOCK_SECOND
A second, measured in system clock time.
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
static volatile clock_time_t count
These routines define the AVR-specific calls declared in /core/sys/clock.h CLOCK_SECOND is the number...
void clock_adjust_ticks(clock_time_t howmany)
Adjust the system current clock time.
uip_ds6_netif_t uip_ds6_if
The single interface.
#define SOF_CHAR
Start-of-frame character.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
#define PROCESS_BEGIN()
Define the beginning of a process.