mirror of
https://github.com/sigmasternchen/iot-relay-arduino
synced 2025-03-14 23:48:54 +00:00
initial commit
This commit is contained in:
parent
5b5d062cfa
commit
61fa35878f
3 changed files with 119 additions and 0 deletions
65
IotRelay.cpp
Normal file
65
IotRelay.cpp
Normal file
|
@ -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;
|
||||
}
|
28
IotRelay.h
Normal file
28
IotRelay.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef _IOT_RELAY_H_
|
||||
#define _IOT_RELAY_H_
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#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
|
26
example.ino
Normal file
26
example.ino
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <IotRelay.h>
|
||||
|
||||
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);
|
||||
}
|
Loading…
Reference in a new issue