Initial - mailbox - Unnamed repository; edit this file 'description' to name the repository.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit f28c3dc2a0d070dbcc402e26d20990ddf84ae34d
 (HTM) Author: Scarlett McAllister <no+reply@roygbyte.com>
       Date:   Mon, 27 May 2024 07:12:18 -0300
       
       Initial
       
       Diffstat:
         A mailbox.ino                         |     200 +++++++++++++++++++++++++++++++
       
       1 file changed, 200 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/mailbox.ino b/mailbox.ino
       @@ -0,0 +1,200 @@
       +#include "user_interface.h"
       +#include <ESP8266WiFi.h>
       +#include <WiFiUdp.h>
       +#include "secrets.h"
       +
       +#define DEBUG false // todo: change macro from DEBUG to SERIAL (i think debug is fucking with things)
       +#define UDP true
       +#define TCP true
       +
       +WiFiUDP Udp;
       +
       +uint32_t wake_up_count = 0;
       +
       +// ap_* variables declared in 'secrets.h'
       +// const char* ap_ssid = "";
       +// const char* ap_pass = "";
       +const char* server_host = "192.168.2.20";
       +const uint16_t server_port = 8008;
       +const uint16_t local_udp_port = 5000;
       +uint32_t max_ap_conn_attempts = 5;
       +uint32_t max_client_conn_attempts = 5;
       +uint32_t reconn_attempt_timeout = 4000;
       +uint32_t ap_conn_attempt_timeout = 20000;
       +
       +const int event_open = 1; // todo: change to enum
       +const int event_close = 2;
       +bool event_mailbox_open = false;
       +char event_open_message[] = "SENSOR\nMAIL 1 OPEN";
       +char event_close_message[] = "SENSOR\nMAILBOX 1 CLOSE";
       +
       +void onWakeUp() {
       +  wake_up_count += 1;
       +#ifdef DEBUG
       +  Serial.println("Callback called");
       +#endif
       +  bool connected = connectToAccessPoint();
       +  if (connected) {
       +    sendEvent(event_open);
       +  }
       +#ifdef DEBUG
       +  Serial.println("Waiting to sleep");
       +#endif
       +  while(digitalRead(D2)) {
       +#ifdef DEBUG
       +    Serial.print(".");
       +#endif
       +    delay(100);
       +  }
       +  if (connected) {
       +    sendEvent(event_close);
       +  }
       +  disconnectFromAccessPoint();
       +  goToSleep();
       +}
       +
       +bool connectToAccessPoint() {
       +#ifdef DEBUG
       +  Serial.println("Connecting to AP");
       +#endif
       +  WiFi.mode(WIFI_STA);
       +  WiFi.begin(ap_ssid, ap_pass);
       +  WiFi.waitForConnectResult(ap_conn_attempt_timeout);
       +  if(WiFi.status() != WL_CONNECTED) {
       +#ifdef DEBUG
       +    Serial.println("> Fail");
       +#endif
       +    return false;
       +  } else {
       +#ifdef DEBUG
       +    Serial.println("> Success");
       +#endif
       +    return true;
       +  }
       +}
       +
       +void disconnectFromAccessPoint() {
       +#ifdef DEBUG
       +  Serial.println("Disconnecting from AP");
       +#endif
       +  WiFi.mode(WIFI_OFF);
       +}
       +
       +void goToSleep() {
       +#ifdef DEBUG
       +  Serial.println("Going to sleep");
       +#endif
       +  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
       +  wifi_fpm_open();
       +  gpio_pin_wakeup_enable(D2, GPIO_PIN_INTR_HILEVEL);
       +#ifdef DEBUG
       +  digitalWrite(LED_BUILTIN_AUX, HIGH);
       +#endif
       +  wifi_fpm_do_sleep(0xFFFFFFF);
       +  // the CPU will only enter light sleep on the next idle cycle, which
       +  // can be triggered by a short delay()
       +  delay(100);
       +#ifdef DEBUG
       +  digitalWrite(LED_BUILTIN_AUX, LOW);
       +  Serial.println("Woke up from sleep");
       +#endif
       +  onWakeUp();
       +}
       +
       +void sendUdpMessage(char* message) {
       +#ifdef DEBUG
       +  Serial.println("Sending UDP");
       +#endif
       +  Udp.begin(5000);
       +  delay(100);
       +  int packet_began = Udp.beginPacket(server_host, server_port);
       +#ifdef DEBUG
       +  if(packet_began) {
       +    Serial.println("> Packet begun");
       +  } else {
       +    Serial.println("> Packet not begun");
       +  }
       +#endif
       +  Udp.write(message);
       +  int packet_end = Udp.endPacket();
       +#ifdef DEBUG
       +  if(packet_end) {
       +    Serial.println("> Packet end");
       +  } else {
       +    Serial.println("> Packet not end");
       +  }
       +#endif
       +}
       +
       +void sendTcpMessage(char* message) {
       +#ifdef DEBUG
       +  Serial.println("Sending TCP");
       +#endif
       +  WiFiClient client;
       +  for(uint32_t i = 0; i < max_client_conn_attempts; i++) {
       +    if (!client.connect(server_host, server_port)) { // TODO: Change to UDP
       +#ifdef DEBUG
       +      Serial.print(".");
       +#endif
       +      delay(reconn_attempt_timeout);
       +    } else {
       +      break;
       +    }
       +  }
       +  if(!client.connected()) {
       +#ifdef DEBUG
       +    Serial.println("> Failed to connect client to server");
       +#endif
       +    // This will be a missed event. Could add event to a running log,
       +    // and transmit the log next time there is successful connection.
       +    return;
       +  }
       +#ifdef DEBUG
       +  Serial.println("> Sending string");
       +#endif
       +  client.println(message);
       +  client.abort();
       +}
       +
       +void sendEvent(int event) {
       +#ifdef DEBUG
       +  Serial.println("Connecting to client");
       +#endif
       +  char* message;
       +  switch(event) {
       +  case event_open:
       +    message = event_open_message;
       +    break;
       +  case event_close:
       +    message = event_close_message;
       +    break;
       +  }
       +#ifdef UDP
       +  sendUdpMessage(message);
       +#endif
       +#ifdef TCP
       +  sendTcpMessage(message);
       +#endif
       +}
       +
       +void setup() {
       +#ifdef DEBUG
       +  Serial.begin(115200);
       +  delay(1000);
       +  Serial.println();
       +  Serial.println();
       +  pinMode(LED_BUILTIN_AUX, OUTPUT);
       +  digitalWrite(LED_BUILTIN_AUX, LOW);
       +#endif
       +  pinMode(D2, INPUT_PULLUP);
       +  digitalWrite(D2, LOW);
       +#ifdef DEBUG
       +  Serial.println("Setup complete");
       +#endif
       +  delay(1000);
       +  goToSleep(); // fix: false event triggered on program boot.
       +}
       +
       +void loop() {
       +  delay(1000);
       +}