Będę wdzięczny jeśli ktoś ma większą wiedzę w tej kwestii.
Kod: Zaznacz cały
/**
* Supla.org NodeMCU WiFi minimal example
* Author: Programistyk - Kamil Kaminski <kamil@programistyk.pl>
*
* This example shows how to configure SuplaDevice for building for NodeMCU within Arduino IDE
*/
#include <srpc.h>
#include <log.h>
#include <eh.h>
#include <proto.h>
#include <IEEE754tools.h>
// We define our own ethernet layer
#define SUPLADEVICE_CPP
#include <SuplaDevice.h>
#include <lck.h>
#include <WiFiClient.h>
#include <ESP8266WiFiType.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiServer.h>
#include <ESP8266WiFiGeneric.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiSTA.h>
#include <WiFiUdp.h>
WiFiClient client;
// Setup Supla connection
const char* ssid = "xxxx";
const char* password = "xxxx";
// Pushover settings
char pushoversite[] = "api.pushover.net";
char apitoken[] = "your30characterapitokenhere12";
char userkey [] = "your30characteruserkeyhere123";
int length;
byte pamiec;
void setup() {
Serial.begin(115200);
delay(10);
// Replace the falowing GUID
char GUID[SUPLA_GUID_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x00,0x000,x00,0x00,0x00,0x00};
// with GUID that you can retrieve from https://www.supla.org/arduino/get-guid
// Ethernet MAC address
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/*
* 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(4, true); // 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
// CHANNEL1 - RELAY
SuplaDevice.addRelay(5, true); // 45 - Pin number where the relay is connected
SuplaDevice.addSensorNO(2);
SuplaDevice.begin(GUID, // Global Unique Identifier
mac, // Ethernet MAC address
"svr1.supla.org", // SUPLA server address
0, // Location ID
""); // Location Password
}
void loop() {
SuplaDevice.iterate();
if ((digitalRead(2)==LOW) and (pamiec==0)) {
pushover("Brama wjazdowa otwarta");
Serial.println("Brama wjazdowa - otwarta");
pamiec=1;
}
if (digitalRead(2)==HIGH) pamiec=0;
}
// --------------
// Pushover settings
byte pushover(char *pushovermessage)
{
String message = pushovermessage;
length = 81 + message.length();
if(client.connect(pushoversite,80))
{
client.println("POST /1/messages.json HTTP/1.1");
client.println("Host: api.pushover.net");
client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(length);
client.println("\r\n");;
client.print("token=");
client.print(apitoken);
client.print("&user=");
client.print(userkey);
client.print("&message=");
client.print(message);
while(client.connected())
{
while(client.available())
{
char ch = client.read();
Serial.write(ch);
}
}
// client.stop();
}
}
// -----------------
// 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;
}
A oto co wychodzi z terminala :
Not connected
Register in progress
Registered and ready.
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 19 Nov 2017 17:52:45 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, X-Prototype-Version, Origin, Accept, Content-Type, X-CSRF-Token, X-Pushover-App
Access-Control-Max-Age: 1728000
X-Limit-App-Limit: 7500
X-Limit-App-Remaining: 7484
X-Limit-App-Reset: 1512108000
ETag: W/"85115b670bbe1ca3be7bb01f337ddc70"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 8f08bea2-c7d7-4c16-9d31-ab0083508213
X-Runtime: 0.021830
X-Frame-Options: DENY
3d
{"status":1,"request":"8f08bea2-c7d7-4c16-9d31-ab0083508213"}
0
Brama wjazdowa - otwarta
Not connected
Register in progress
Registered and ready.