Files
platformio_fensterpiepser_e…/src/MyWifiClient.cpp
T

195 lines
3.0 KiB
C++

/*
* MyWifiClient.cpp
*
* Created on: 24.09.2020
* Author: flori
*/
#include "main.h"
#include "Base32.h"
#include "Seconds.h"
#include "Version.h"
#include "MyWifiClient.h"
Base32 base32;
#if 1
#define MonitorPrint(...) Serial.print(__VA_ARGS__)
#define MonitorPrintln(...) Serial.println(__VA_ARGS__)
#else
#define MonitorPrint(...)
#define MonitorPrintln(...)
#endif
MyWifiClient myWifiClient;
/**
*
*/
MyWifiClient::MyWifiClient()
{
state = IDLE;
bSend = false;
}
/**
*
*/
MyWifiClient::~MyWifiClient()
{
}
/**
*
*/
bool MyWifiClient::canSend(void)
{
return state == IDLE && bSend == false;
}
/* converts dat to base32 and triggers GET request */
void MyWifiClient::send(uint8_t *dat, uint16_t len)
{
strcpy( wifibufMsg , "GET /genericData.php?project=fensterpiepser&dataname=fenster&data=");
size_t pos = strlen(wifibufMsg);
(void) base32.toBase32(dat, len, &wifibufMsg[pos], true);
strcat( wifibufMsg, " HTTP/1.1\r\nHost: fly-with-cats.com\r\nAccept: */*\r\n\r\n");
bSend = true;
}
/**
*
*/
void MyWifiClient::onSetup(void)
{
}
/**
*
*/
void MyWifiClient::onLoop(void)
{
static substate_t substate = ENTRY;
static state_t prevState = IDLE;
if (prevState != state)
{
substate = ENTRY;
prevState = state;
switch(state)
{
case IDLE:
MonitorPrintln("IDLE");
break;
case WAIT_CONNECT:
MonitorPrintln("WAIT_CONNECT");
break;
case WAIT_RECEIVE:
MonitorPrintln("WAIT_RECEIVE");
break;
}
}
switch (state)
{
case IDLE:
switch (substate)
{
static uint32_t tConnectWifiEndSeconds;
case ENTRY:
tConnectWifiEndSeconds = Seconds::Get() + 60;
substate = CYCLIC;
break;
case CYCLIC:
if (WiFi.isConnected())
{
tConnectWifiEndSeconds = Seconds::Get() + 60;
if (bSend)
{
bSend = false;
state = WAIT_CONNECT;
}
}
else
{
if (Seconds::Get() > tConnectWifiEndSeconds)
{
Serial.println("RESET (Wifi)");
ESP.restart();
}
}
break;
}
break;
case WAIT_CONNECT:
switch (substate)
{
static uint32_t tConnectEndSeconds;
case ENTRY:
/* Connect to Server */
this->connect("fly-with-cats.com", 8887, 100);
tConnectEndSeconds = Seconds::Get() + 5;
substate = CYCLIC;
break;
case CYCLIC:
if (this->connected())
{
/* send HTTP request */
this->print(wifibufMsg);
MonitorPrint(wifibufMsg);
state = WAIT_RECEIVE;
}
else if (Seconds::Get() > tConnectEndSeconds)
{
state = IDLE;
}
break;
}
break;
case WAIT_RECEIVE:
switch (substate)
{
static uint32_t tReceiveEndSeconds;
case ENTRY:
tReceiveEndSeconds = Seconds::Get() + 5;
substate = CYCLIC;
break;
case CYCLIC:
if (this->available())
{
int sizeRec = this->read(wifibufRx, WIFI_BUF_SIZE_RX);
wifibufRx[sizeRec] = 0;
//MonitorPrintln((char*)wifibufRx);
state = IDLE;
}
else if (Seconds::Get() >= tReceiveEndSeconds)
{
this->stop();
state = IDLE;
}
break;
}
}
}