Update code to work on fabricated PCB
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "connect.h"
|
||||
#include "esp32-hal.h"
|
||||
#include "secrets.h"
|
||||
|
||||
uint64_t start;
|
||||
uint64_t is_connecting = 0;
|
||||
IPAddress host;
|
||||
|
||||
void timerStart()
|
||||
{
|
||||
start = millis();
|
||||
}
|
||||
|
||||
uint64_t timerElapsed()
|
||||
{
|
||||
auto now = millis();
|
||||
uint64_t elapsed = now - start;
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void wifiSetup()
|
||||
{
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setAutoReconnect(false);
|
||||
WiFi.disconnect();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void wifiConnect()
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (is_connecting > 0) {
|
||||
host = WiFi.gatewayIP();
|
||||
Serial.println("Connected to AP!");
|
||||
}
|
||||
is_connecting = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_connecting == 0 || timerElapsed() > is_connecting * 1000) {
|
||||
if (is_connecting == 0) {
|
||||
is_connecting = 10; // start with a 10 second delay
|
||||
}
|
||||
else {
|
||||
is_connecting *= 2; // exponential backoff
|
||||
}
|
||||
|
||||
timerStart();
|
||||
Serial.println("Connecting");
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
IPAddress wifiHost()
|
||||
{
|
||||
return host;
|
||||
}
|
||||
|
||||
// https://docs.espressif.com/projects/arduino-esp32/en/latest/api/wifi.html
|
||||
// https://docs.arduino.cc/libraries/wifi/ (old)
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "IPAddress.h"
|
||||
|
||||
void wifiSetup();
|
||||
void wifiConnect();
|
||||
IPAddress wifiHost();
|
||||
+21
-22
@@ -1,16 +1,14 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
|
||||
#include "secrets.h"
|
||||
#include "connect.h"
|
||||
|
||||
// Default IP address = 192.168.4.1
|
||||
|
||||
#define HOST "192.168.4.1"
|
||||
#define PORT 1234
|
||||
|
||||
#define POWER_BUTTON_PIN 2
|
||||
#define RESET_BUTTON_PIN 3
|
||||
#define POWER_BUTTON_PIN 10
|
||||
#define RESET_BUTTON_PIN 2
|
||||
#define POWER_LED_PIN 3
|
||||
#define RESET_LED_PIN 1
|
||||
|
||||
WiFiClient client;
|
||||
|
||||
@@ -27,25 +25,23 @@ void setup()
|
||||
pinMode(POWER_BUTTON_PIN, INPUT_PULLUP);
|
||||
pinMode(RESET_BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
// Wait for a USB connection to be established
|
||||
while (!Serial)
|
||||
(void)0;
|
||||
delay(1000);
|
||||
Serial.println("Client booted!");
|
||||
pinMode(POWER_LED_PIN, OUTPUT);
|
||||
pinMode(RESET_LED_PIN, OUTPUT);
|
||||
|
||||
// Connect to button server
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
Serial.print("Connecting");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\nConnected to AP");
|
||||
wifiSetup();
|
||||
|
||||
delay(3000);
|
||||
Serial.println("Client booted!");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(20);
|
||||
delay(20); // used for button debounce
|
||||
|
||||
wifiConnect();
|
||||
|
||||
digitalWrite(POWER_LED_PIN, LOW);
|
||||
digitalWrite(RESET_LED_PIN, LOW);
|
||||
|
||||
int powerButtonState = digitalRead(POWER_BUTTON_PIN);
|
||||
int resetButtonState = digitalRead(RESET_BUTTON_PIN);
|
||||
@@ -66,11 +62,13 @@ void loop()
|
||||
previousResetButtonState = resetButtonState;
|
||||
|
||||
if (isButtonPressed) {
|
||||
digitalWrite(RESET_LED_PIN, HIGH);
|
||||
|
||||
String button = (powerButtonState == LOW) ? "power" : "reset";
|
||||
|
||||
Serial.println("Pressed " + button + " button!");
|
||||
|
||||
if (!client.connected() && !client.connect(HOST, PORT)) {
|
||||
if (!client.connected() && !client.connect(wifiHost(), PORT)) {
|
||||
Serial.println("Connection failed");
|
||||
return;
|
||||
}
|
||||
@@ -106,6 +104,7 @@ void ack()
|
||||
// Wait for acknowledgment from the receiver
|
||||
String response = client.readStringUntil('\n');
|
||||
if (response == "ACK") {
|
||||
digitalWrite(POWER_LED_PIN, HIGH);
|
||||
Serial.println("Received ACK");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user