opportunistic-xbee/well/main/main.ino

61 lines
1.3 KiB
Arduino
Raw Normal View History

2018-12-01 17:26:27 +00:00
#include <Wire.h>
#include "lcd.h"
#include "xbee_wrapper.h"
#include "packet.h"
2018-12-01 17:26:27 +00:00
// Peripherals
LCDWrapper screen(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper();
2018-12-01 17:26:27 +00:00
// ACTUAL DATA
uint8_t wave_id = 250;
Packet recv, send;
unsigned long time;
2018-12-01 17:26:27 +00:00
void setup() {
Serial.begin(38400);
Serial.println("+ ready");
2018-12-01 17:26:27 +00:00
screen.begin();
screen.printf("[ well ready ]");
2018-12-01 17:26:27 +00:00
xbee.begin(38400);
time = millis();
2018-12-01 17:26:27 +00:00
}
void loop() {
/* [1] if DISCOVER_TTL reached -> send discover request */
if( millis() - time >= DISCOVER_TTL ){
time = millis();
// increment wave id (will overflow from 255 to 0)
send.setOpcode(0);
send.setWave(++wave_id);
send.setDist(0);
send.setSender(SENDERID);
screen.clear();
screen.printfn(0, "dsc[%3d] %3d/%3d", send.getSender(), send.getDist(), send.getWave());
if( xbee.broadcast(send) == XBWSEND_OK ) screen.printfn(1, "sent");
else screen.printfn(1, "failed");
}
/* [2] Listen for incoming data */
if( xbee.receive(recv) != XBWRECV_OK )
return;
uint8_t opcode = recv.getOpcode();
// ignore DISCOVER requests
if( opcode == 0 )
return;
2018-12-01 17:26:27 +00:00
// log message
screen.clear();
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
screen.printfn_overflow(1, "%s", recv.getData());
2018-12-01 17:26:27 +00:00
}