Commit e53155b4 authored by PhiBo's avatar PhiBo 🚀

New function to connect to sensor api server

parent 9c8ff466
......@@ -16,7 +16,6 @@ ArduRPC_Serial *rpc_serial;
ArduRPC_SensorNode *rpc_sensor_node;
ESP8266WebServer *server;
WiFiClient *client;
void setup() {
pinMode(16, INPUT);
......
#include "sensor_node.h"
WiFiClient *client = NULL;
bool connectWiFiClient(uint8_t connect_timeout=0)
{
char ssid[65];
......@@ -47,3 +49,30 @@ bool waitWiFiClientConnected(uint8_t connect_timeout)
return false;
}
WiFiClient *connectSensorAPI()
{
/* ToDo: Add SSL/TLS support */
uint16_t port;
char hostname[NODE_EEPROM_API_HOSTNAME_MAX_LENGTH + 1];
if(client == NULL) {
client = new WiFiClient();
}
if(WiFi.status() != WL_CONNECTED) {
return NULL;
}
getAPIHostnameOrDefault(&hostname[0], NODE_EEPROM_API_HOSTNAME_MAX_LENGTH);
hostname[NODE_EEPROM_API_HOSTNAME_MAX_LENGTH] = '\0';
port = getAPIPortOrDefault();
if(client != NULL) {
if(!client->connected()) {
if(client->connect(hostname, port)) {
return client;
}
}
}
return NULL;
}
......@@ -29,6 +29,8 @@
#define NODE_EEPROM_VERSION_MAJOR 0
#define NODE_EEPROM_VERSION_MINOR 0
#define NODE_API_DEFAULT_HOSTNAME ""
class DataString : public Print
{
......@@ -71,6 +73,10 @@ extern WiFiClient *client;
uint8_t ICACHE_FLASH_ATTR readEEPROM_string(uint16_t, char *, uint8_t);
uint8_t ICACHE_FLASH_ATTR writeEEPROM_string(uint16_t, char *, uint8_t);
// Config
uint8_t ICACHE_FLASH_ATTR getAPIHostnameOrDefault(char *, uint8_t);
uint16_t ICACHE_FLASH_ATTR getAPIPortOrDefault();
bool ICACHE_FLASH_ATTR waitWiFiClientConnected(uint8_t);
bool ICACHE_FLASH_ATTR connectWiFiClient(uint8_t connect_timeout);
uint8_t ICACHE_FLASH_ATTR setAPIHostname(char *, uint8_t);
......@@ -84,4 +90,6 @@ uint8_t ICACHE_FLASH_ATTR getNodeConfigStatus();
uint8_t ICACHE_FLASH_ATTR getWiFiSSID(char *ssid, uint8_t max_len);
void ICACHE_FLASH_ATTR initConfig();
WiFiClient ICACHE_FLASH_ATTR *connectSensorAPI();
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment