From 61fa35878ffcf0b544bc5bb9fae5f1870be49702 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sun, 14 Nov 2021 14:45:12 +0100 Subject: [PATCH] initial commit --- IotRelay.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ IotRelay.h | 28 ++++++++++++++++++++++ example.ino | 26 +++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 IotRelay.cpp create mode 100644 IotRelay.h create mode 100644 example.ino diff --git a/IotRelay.cpp b/IotRelay.cpp new file mode 100644 index 0000000..1387ad7 --- /dev/null +++ b/IotRelay.cpp @@ -0,0 +1,65 @@ +#include "IotRelay.h" + + +void IotRelay::begin(String addr, int port, String id, String loc) { + this->addr = addr; + this->port = port; + this->id = id; + this->loc = loc; + this->size = 0; +} + +void IotRelay::connect(String ssid, String password) { + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); +} + +void IotRelay::add(String key, String value) { + keys[size] = key; + values[size] = value; + + size++; +} + +int IotRelay::send() { + client.connect(addr, port); + client.print("id="); + client.print(id); + client.print("\n"); + client.print("loc="); + client.print(loc); + client.print("\n"); + for(int i = 0; i < size; i++) { + client.print(keys[i]); + client.print("="); + client.print(values[i]); + client.print("\n"); + } + client.print("\n"); + + size = 0; + + unsigned long startTime = millis(); + while (client.available() < 3) { + if (millis() - startTime > CONNECTION_TIMEOUT) { + client.stop(); + return -1; + } + delay(CHECK_TIMEOUT); + } + + if (client.read() != 'o' || client.read() != 'k') { + client.stop(); + return -1; + } + + client.stop(); + return 0; +} diff --git a/IotRelay.h b/IotRelay.h new file mode 100644 index 0000000..c3f26aa --- /dev/null +++ b/IotRelay.h @@ -0,0 +1,28 @@ +#ifndef _IOT_RELAY_H_ +#define _IOT_RELAY_H_ + +#include + +#define MAX_DATA_VALUES (10) +#define CONNECTION_TIMEOUT (2000) +#define CHECK_TIMEOUT (500) + +class IotRelay { + public: + void begin(String addr, int port, String id, String loc); + void connect(String ssid, String password); + void add(String key, String value); + int send(); + + private: + WiFiClient client; + String addr; + int port; + String id; + String loc; + int size; + String keys[MAX_DATA_VALUES]; + String values[MAX_DATA_VALUES]; +}; + +#endif diff --git a/example.ino b/example.ino new file mode 100644 index 0000000..eb81bf2 --- /dev/null +++ b/example.ino @@ -0,0 +1,26 @@ +#include + +const char* ssid = "wifi-ssid"; +const char* password = "wifi-password"; + +const char* addr = "ip to iot relay server"; +int port = 20159; +const char* id = "main-sensor"; +const char* loc = "living-room"; + +IotRelay relay; + +void setup() { + Serial.begin(9600); + + relay.connect(ssid, password); + relay.begin(addr, port, id, loc); +} + +void loop() { + relay.add("temp", String(31.415, 3)); + relay.add("humid", String(42)); + relay.send(); + + delay(5000); +}