Created
June 18, 2019 22:21
-
-
Save mcspr/be1a92827f71d9702ebb3d800d82f344 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#include <ESPAsyncTCP.h> | |
#elif ESP32 | |
#include <WiFi.h> | |
#include <AsyncTCP.h> | |
#endif | |
#ifdef ESP8266 | |
#include <PolledTimeout.h> | |
#define TIMEOUT_T esp8266::polledTimeout::periodicFastMs | |
#else | |
class BasicTimeout { | |
public: | |
BasicTimeout(uint32_t interval) : | |
_interval(interval), | |
_last(millis()) | |
{} | |
operator bool() { | |
if (millis() - _last > _interval) { | |
_last = millis(); | |
return true; | |
} | |
return false; | |
} | |
void reset() { this->_last = millis(); } | |
private: | |
uint32_t _interval = 0; | |
uint32_t _last = 0; | |
}; | |
#define TIMEOUT_T BasicTimeout | |
#endif | |
#ifdef NO_SETTINGS | |
#define WIFI_SSID "SSID" | |
#define WIFI_PASSWORD "PASSWORD" | |
#define SERVER_IP IPAddress(192.168.4.1) | |
#define SERVER_PORT 12345 | |
#else | |
#include "settings.h" | |
#endif | |
TIMEOUT_T tick(100); | |
TIMEOUT_T report(1000); | |
AsyncClient client; | |
enum State { | |
IDLE, | |
CONNECTING, | |
CONNECTED | |
}; | |
State state = IDLE; | |
void clientSetup() { | |
client.onDisconnect([](void* arg, AsyncClient* c) { | |
Serial.printf("%lu onDisconnect()\n", millis()); | |
c->free(); | |
state = IDLE; | |
}); | |
client.onTimeout([](void* arg, AsyncClient* c, uint32_t time) { | |
Serial.printf("%lu onTimeout(time=%u)\n", millis(), time); | |
state = IDLE; | |
}); | |
client.onData([](void* arg, AsyncClient* c, void* response, size_t len) { | |
Serial.printf("%lu onData(len=%u)\n", millis(), len); | |
}); | |
client.onConnect([](void* arg, AsyncClient* c) { | |
Serial.printf("%lu onConnect()\n", millis()); | |
state = CONNECTED; | |
}); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println(); | |
Serial.printf("ssid=%s password=%s\n", WIFI_SSID, WIFI_PASSWORD); | |
WiFi.persistent(false); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
clientSetup(); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
tick.reset(); | |
report.reset(); | |
} | |
void loop() { | |
if (tick) { | |
switch (state) { | |
case IDLE: | |
Serial.printf("%lu connect(ip=%s, port=%u)\n", millis(), (SERVER_IP).toString().c_str(), SERVER_PORT); | |
client.connect(SERVER_IP, SERVER_PORT); | |
state = CONNECTING; | |
case CONNECTING: | |
break; | |
case CONNECTED: | |
Serial.print("a"); | |
client.write("a"); | |
} | |
} | |
if (report) { | |
Serial.printf("%lu connected=%u, space=%u\n", millis(), client.connected(), client.space()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment