Code: Select all
/**
* Supla.org NodeMCU WiFi minimal example
* Author: Programistyk - Kamil Kaminski <[email protected]>
*
* This example shows how to configure SuplaDevice for building for NodeMCU within Arduino IDE
*/
#define SUPLADEVICE_CPP
#include <SuplaDevice.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
int v;
int v1;
WiFiClient client;
// Setup Supla connection
const char* ssid = ".......";
const char* password = ".......";
void setup() {
Serial.begin(9600);
delay(10);
// Replace the falowing GUID
char GUID[SUPLA_GUID_SIZE] = {.....};
// with GUID that you can retrieve from https://www.supla.org/arduino/get-guid
// Ethernet MAC address
uint8_t mac[6] = {.......};
/*
* Having your device already registered at cloud.supla.org,
* you want to change CHANNEL sequence or remove any of them,
* then you must also remove the device itself from cloud.supla.org.
* Otherwise you will get "Channel conflict!" error.
*/
// CHANNEL0 - RELAY
SuplaDevice.addRelay(D1); // 44 - Pin number where the relay is connected
// Call SuplaDevice.addRelay(44, true) with an extra "true" parameter
// to enable "port value inversion"
// where HIGH == LOW, and LOW == HIGH
SuplaDevice.setTimerFuncImpl(&supla_timer);
SuplaDevice.begin(GUID, // Global Unique Identifier
mac, // Ethernet MAC address
"svrX.supla.org", // SUPLA server address
0000, // Location ID
"...."); // Location Password
openLink();
}
void loop() {
SuplaDevice.iterate();
}
void supla_timer() {
v = digitalRead(D1);
if (v != v1){
Serial.println(v);
openLink();
}
v1 = v;
}
void openLink() {
std::unique_ptr<BearSSL::WiFiClientSecure>client1(new BearSSL::WiFiClientSecure);
client1->setInsecure();
HTTPClient https;
if (https.begin(*client1, "https://supla.fracz.com/api/scenes/public/..........")) { // HTTPS
Serial.println("[HTTPS] GET...");
int httpCode = https.GET();
Serial.println(httpCode);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server?
if (httpCode == HTTP_CODE_OK) {
String p = https.getString();
Serial.println(String("[HTTPS] Received: ") + p);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n\r", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n\r");
}
}
// Supla.org ethernet layer
int supla_arduino_tcp_read(void *buf, int count) {
_supla_int_t size = client.available();
if ( size > 0 ) {
if ( size > count ) size = count;
return client.read((uint8_t *)buf, size);
};
return -1;
};
int supla_arduino_tcp_write(void *buf, int count) {
return client.write((const uint8_t *)buf, count);
};
bool supla_arduino_svr_connect(const char *server, int port) {
return client.connect(server, 2015);
}
bool supla_arduino_svr_connected(void) {
return client.connected();
}
void supla_arduino_svr_disconnect(void) {
client.stop();
}
void supla_arduino_eth_setup(uint8_t mac[6], IPAddress *ip) {
// Serial.println("WiFi init");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
//Serial.print("\nlocalIP: ");
//Serial.println(WiFi.localIP());
//Serial.print("subnetMask: ");
//Serial.println(WiFi.subnetMask());
//Serial.print("gatewayIP: ");
//Serial.println(WiFi.gatewayIP());
}
SuplaDeviceCallbacks supla_arduino_get_callbacks(void) {
SuplaDeviceCallbacks cb;
cb.tcp_read = &supla_arduino_tcp_read;
cb.tcp_write = &supla_arduino_tcp_write;
cb.eth_setup = &supla_arduino_eth_setup;
cb.svr_connected = &supla_arduino_svr_connected;
cb.svr_connect = &supla_arduino_svr_connect;
cb.svr_disconnect = &supla_arduino_svr_disconnect;
cb.get_temperature = NULL;
cb.get_temperature_and_humidity = NULL;
cb.get_rgbw_value = NULL;
cb.set_rgbw_value = NULL;
return cb;
}