diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/device/main.ino | 118 |
1 files changed, 103 insertions, 15 deletions
diff --git a/src/device/main.ino b/src/device/main.ino index 8794ef5..eb5d122 100644 --- a/src/device/main.ino +++ b/src/device/main.ino @@ -19,45 +19,133 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include <ESP8266WiFi.h> +#include <WiFiUdp.h> #include <stdlib.h> +#include <stddef.h> +static char udppacketbuffer[32] = {0}; +static char *udppacketcursor = NULL; static const unsigned int internal_led = 2; +static unsigned int led_state = 0; +static const char servermagic[] = "I love coffee!"; +static const char clientmagic[] = "I love tea!"; +IPAddress ip; +WiFiUDP Udp; -unsigned int toggle_led(void); +unsigned int toggle_led(const int ip); +static void discover_client(void); +static int wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin); +static void discover_client(void); +static void udp_init_packet(IPAddress ip, const int port); +static void udp_push(const void * const data, const size_t size); +static int udp_flush(void); +static void blink_led(const int pin, const int ontime, const int offtime); void setup(void) { pinMode(internal_led, OUTPUT); - - /* don't set pin mode here, it will get toggled at the very start of - * the logic loop, either way. - */ + toggle_led(internal_led); + wifi_connect("ssid", "password", 1, internal_led); + discover_client(); } /* the logic is a placeholder right now */ void loop(void) { /* sleep length to use */ - static unsigned int delta = 100; + static unsigned int delta = 2000; - /* progresivelly grow the time delta while changing state*/ - delta += delta >> (6 + toggle_led()); - - /* sleep */ delay(delta); + udp_init_packet(ip, 6996); + Udp.begin(6996); + udp_push(clientmagic, sizeof(clientmagic)); } /* toggle the bult-in led and return current state */ -unsigned int toggle_led(void) +unsigned int toggle_led(const int pin) { - static unsigned int state = 0; - state = !state; + led_state = !led_state; /* as the cathode of the builtin diode is connected to the MCU's pin, * while the anode is connected to Vcc, to turn it off, we need to set * the pin to HIGH. */ - digitalWrite(internal_led, state ? LOW : HIGH); + digitalWrite(pin, led_state ? LOW : HIGH); + + return led_state; +} + +static int wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin) +{ + size_t i = 30; + + WiFi.forceSleepWake(); + yield(); + WiFi.persistent(0); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + do { + if (doblink) { + blink_led(ledpin, 250, 250); + } else { + delay (500); + } + } while (WiFi.status() != WL_CONNECTED); + + return 0; +} + +static void discover_client(void) +{ + IPAddress bcastip(255, 255, 255, 255); + static const char servermagic[] = "I love coffee!"; + static const char clientmagic[] = "I love tea!"; + char buffer[32] = {0}; + size_t done = 0; - return state; + do { + udp_init_packet(bcastip, 6996); + udp_push(servermagic, sizeof(servermagic)); + udp_flush(); + delay(5); + while (Udp.parsePacket()) { + if (Udp.available() >= sizeof(clientmagic)) { + Udp.read(buffer, sizeof(clientmagic)); + if (!(strcmp(clientmagic, buffer))) { + ip = Udp.remoteIP(); + ++done; + } + } + } + delay(95); + } while (!done); +} + +static void udp_init_packet(IPAddress ip, const int port) +{ + Udp.beginPacket(ip, port); + memset(udppacketbuffer, 0, sizeof(udppacketbuffer)); + udppacketcursor = udppacketbuffer; +} + +static void udp_push(const void * const data, const size_t size) +{ + memcpy(udppacketcursor, data, size); + udppacketcursor += size; +} + +static int udp_flush(void) +{ + Udp.write((const uint8_t *) udppacketbuffer, udppacketcursor - udppacketbuffer); + return Udp.endPacket(); +} + +static void blink_led(const int pin, const int ontime, const int offtime) +{ + toggle_led(pin); + delay(ontime); + toggle_led(pin); + delay(offtime); } |