-
-
Save yarogniew/6ca190a72090e0d9ab42605e152c55e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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> | |
#define Pin_D1 5 | |
#define Pin_D2 4 | |
#define Pin_D6 12 | |
#define Pin_D7 13 | |
WiFiClient client; | |
// Setup Supla connection | |
const char* ssid = "XXXXX"; // MyWiFiNetwork | |
const char* password = "XXXXX"; // MyWiFiPassword | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
// Replace the falowing GUID | |
char GUID[SUPLA_GUID_SIZE] = {0x3C,0x53,0x1E,0x87,0x58,0x95,0xDF,0xAE,0x68,0xE9,0x82,0x1E,0x8B,0xC2,0x99,0xD9}; | |
// with GUID that you can retrieve from https://www.supla.org/arduino/get-guid | |
// MAC adres urządzenia | |
// Ethernet MAC address [84:F3:EB:73:5A:A4] | |
uint8_t mac[6] = {0x84, 0xF3, 0xEB, 0x73, 0x5A, 0xA4}; | |
/* | |
* 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(Pin_D1); // 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(Pin_D2); // Pin number where the relay is connected | |
// CHANNEL2 - RELAY | |
SuplaDevice.addRelay(Pin_D6); // Pin number where the relay is connected | |
// CHANNEL3 - RELAY | |
SuplaDevice.addRelay(Pin_D7); // Pin number where the relay is connected | |
// CHANNEL4 - Opening sensor (Normal Open) | |
//SuplaDevice.addSensorNO(A0); // A0 - Pin number where the sensor is connected | |
// Call SuplaDevice.addSensorNO(A0, true) with an extra "true" parameter | |
// to enable the internal pull-up resistor | |
// CHANNEL5 - Opening sensor (Normal Open) | |
//SuplaDevice.addSensorNO(A1); // A1 - Pin number where the sensor is connected | |
// CHANNEL6 - DHT22 Sensor | |
// SuplaDevice.addDHT11(); | |
// SuplaDevice.addAM2302(); | |
// SuplaDevice.addDHT22(); | |
SuplaDevice.begin(GUID, // Global Unique Identifier | |
mac, // Ethernet MAC address | |
"svr17.supla.org", // SUPLA server address | |
38, // Location ID | |
"9ffe"); // Location Password | |
} | |
void loop() { | |
SuplaDevice.iterate(); | |
} | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment