Created
February 27, 2021 12:54
-
-
Save sivar2311/43204ef7c0f0f3d9a1594803c0fba3b0 to your computer and use it in GitHub Desktop.
SinricProMQTTExample
This file contains hidden or 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
#define SSID "WIFI_SSID" | |
#define PASS "WIFI_PASS" | |
#define MQTT_SERVER "MQTT-SERVER-ADDRESS" | |
#define MQTT_PORT 1883 | |
#define MQTT_USERNAME "MQTT-USER-NAME" | |
#define MQTT_KEY "MQTT-KEY" | |
#define MQTT_TOPIC "MQTT-TOPIC" | |
#define APP_KEY "SINRIC-PRO-APPKEY" | |
#define APP_SECRET "SINRIC-PRO-APPSECRET" | |
#define DEVICE_ID "SINRIC-PRO-DEVICEID" |
This file contains hidden or 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
//#define ENABLE_DEBUG | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <SinricPro.h> | |
#include <SinricProSwitch.h> | |
//edit credentials.h and put in your credentials | |
#include "credentials.h" | |
WiFiClient wifiClient; | |
PubSubClient client(wifiClient); | |
SinricProSwitch &mySwitch = SinricPro[DEVICE_ID]; | |
bool sentBySinric; //helper - used in onPubSub() | |
bool onPowerState(const String &deviceId, bool &state) { | |
Serial.printf("SinricPro: Switch turned %s\r\n", state?"on":"off"); | |
client.publish(MQTT_TOPIC, state?"on":"off"); | |
sentBySinric = true; | |
return true; | |
} | |
// helper function to convert non-nullterminated byte array into String | |
String bufToString(byte *data, unsigned int length) { | |
char *str = new char[length+1]; | |
memcpy(str, data, length); | |
str[length] = 0; // Null termination. | |
String temp(str); | |
delete[] str; | |
return temp; | |
} | |
void onPubSub(char* topic, byte * data, unsigned int length) { | |
if (sentBySinric) { // ignore incomming commands that have been published by onPowerState-function | |
sentBySinric = false; | |
return; | |
} | |
String command = bufToString(data, length); | |
if (command == "on") mySwitch.sendPowerStateEvent(true); | |
if (command == "off") mySwitch.sendPowerStateEvent(false); | |
Serial.printf("onPubSub: Switch turned %s\r\n", command.c_str()); | |
} | |
void handlePubSub() { | |
static unsigned long lastTry; | |
unsigned long currentMillis = millis(); | |
if (!client.connected() && currentMillis - lastTry > 5000) { | |
lastTry = currentMillis; | |
Serial.printf("Attempting MQTT connection..."); | |
if (client.connect("ESP8266-Test", MQTT_USERNAME, MQTT_KEY)) { | |
Serial.printf("connected\r\n"); | |
client.subscribe(MQTT_TOPIC); | |
} else { | |
Serial.printf("failed, rc=%d try again in 5 seconds\r\n", client.state()); | |
} | |
} | |
client.loop(); | |
} | |
void setupWiFi() { | |
Serial.printf("[WiFi]: connecting to \"%s\"", SSID); | |
WiFi.begin(SSID, PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
Serial.printf("connected\r\n"); | |
} | |
void setupSinricPro() { | |
mySwitch.onPowerState(onPowerState); | |
SinricPro.onConnected([](){ Serial.printf("SinricPro: connected\r\n");}); | |
SinricPro.onDisconnected([]() { Serial.printf("SinricPro: disconnected\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setupPubSub() { | |
client.setServer(MQTT_SERVER, MQTT_PORT); | |
client.setCallback(onPubSub); | |
} | |
void setup() { | |
Serial.begin(115200); | |
setupWiFi(); | |
setupPubSub(); | |
setupSinricPro(); | |
} | |
void loop() { | |
handlePubSub(); | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment