Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blynk support #1230

Open
wants to merge 36 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8676877
initial commit for blynk support. It supports connection to blynk ser…
Sep 21, 2018
de82bd4
ralay and magnitudes support
Sep 25, 2018
bf41744
added new hardware 'tuya-tywe3s-zx2820' and finalized blynk support
Sep 26, 2018
4f1e491
reset thingspeak support flag to default
Sep 28, 2018
a8a6a8a
bring not mergable files back to e7845ac83dfa86164116e5fa8d67f2c82914…
Oct 2, 2018
ed2f5fb
changed type for relayID in BLYNK_WRITE_DEFAULT to signed char, now v…
Oct 3, 2018
af53669
Merge branch 'dev' into dev
thaeger71 Oct 3, 2018
f7e3043
Update README.md
xoseperez Oct 4, 2018
20a884c
corrected misformatted lines in index.html
Oct 10, 2018
bc65472
Merge pull request #1 from xoseperez/dev
thaeger71 Oct 15, 2018
05425a6
removed tuya_zx2820 wifi plug hardware and changed BLYNK_SUPPORT to d…
Oct 17, 2018
cef4f56
blynk: conditionals for defines
mcspr Nov 18, 2018
d185f77
blynk: reword vpins description in webui
mcspr Nov 18, 2018
f255703
blynk: clean-up
mcspr Nov 19, 2018
5923298
add to travis
mcspr Nov 19, 2018
97d8b4b
blynk in pio lib_deps
mcspr Nov 19, 2018
02d4521
blnkToken
mcspr Nov 19, 2018
cbfeb44
add missing default param
mcspr Nov 19, 2018
9f3f7cd
fixup! add missing default param
mcspr Nov 19, 2018
31426d8
move connection management to class
mcspr Nov 19, 2018
64774e8
lower connection timeout
mcspr Nov 19, 2018
65e13f1
formatting, size
mcspr Nov 19, 2018
9aff49c
check string length
mcspr Nov 19, 2018
c53e842
removed erroneous backoff logging
mcspr Nov 19, 2018
743baa4
use blnkRelayVPin in web
mcspr Nov 19, 2018
552f244
Merge pull request #2 from mcspr/blynk-cleanup
thaeger71 Nov 19, 2018
5201d9e
shorter relay key
mcspr Nov 21, 2018
78d0470
Support BearSSL::WiFiSecureClient and cert pinning
mcspr Nov 21, 2018
9a3300d
fixup! Support BearSSL::WiFiSecureClient and cert pinning
mcspr Nov 21, 2018
bcb047e
crlf -> lf
mcspr Nov 21, 2018
8b85012
adaptation notice
mcspr Nov 21, 2018
7d023b4
spaces
mcspr Nov 21, 2018
1b23194
correct template spec
mcspr Nov 21, 2018
d98f7ae
support core 2.4.2
mcspr Nov 22, 2018
89e5f4c
re-type relay/sensor range functions to 8bit
mcspr Nov 22, 2018
0949414
proper validation, size notice update
mcspr Nov 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ It uses the Arduino Core for ESP8266 framework and a number of 3rd party librari
* Integration via MQTT Discover or copy-pasting configuration code
* [**InfluxDB**](https://www.influxdata.com/) integration via HTTP API
* [**Thingspeak**](https://thingspeak.com/) integration via HTTP API (HTTPS available for custom builds)
* [**Blynk**](https://www.blynk.cc/) integration via native Blynk API.
* **Sonoff RF Bridge** support
* Multiple virtual switches (tested with up to 16)
* MQTT-to-RF two-way bridge (no need to learn codes)
Expand Down
158 changes: 158 additions & 0 deletions code/espurna/blynk.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*

BLYNK MODULE

Copyright (C) 2018 by Thomas Häger <thaeger at hdsnetz dot de>

*/

#if BLYNK_SUPPORT
#include <ESP8266WiFi.h>
#include <BlynkSimpleStream.h>

bool _blnk_enabled = false;
WiFiClient _wific;

signed char _blynkRelay(unsigned int vpin) {
for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
if (getSetting("blnkRelayVpin", relayID, 0).toInt() == vpin) {
return relayID;
}
}
return -1;
}

void _blnkConfigure() {
_blnk_enabled = getSetting("blnkEnabled", BLYNK_ENABLED).toInt() == 1;
if (_blnk_enabled && (getSetting("blnkHost", BLYNK_HOST).length() == 0)) {
_blnk_enabled = false;
setSetting("blnkEnabled", 0);
}
if(!_blnk_enabled) {
Blynk.disconnect();
_wific.stop();
return;
}
}

void _wifi_connect() {

if (_wific.connect(&getSetting("blnkHost", BLYNK_HOST)[0], getSetting("blnkPort", BLYNK_PORT).toInt()))
DEBUG_MSG_P(PSTR("[BLYNK] WiFi Client connected\n"));
else
DEBUG_MSG_P(PSTR("[BLYNK] WiFi could not connect to %s:%s.\n"),&getSetting("blnkHost", BLYNK_HOST)[0],&getSetting("blnkHost", BLYNK_PORT)[0]);
}

BLYNK_WRITE_DEFAULT(){
signed char relayID = _blynkRelay(request.pin);
int value = param[0].asInt();
if (relayID >= 0) {
DEBUG_MSG_P(PSTR("[BLYNK] Received value %d for VPIN %u\n"), value, request.pin);
relayStatus(relayID, value == 1);
} else {
DEBUG_MSG_P(PSTR("[BLYNK] Received value %d for unassigned VPIN %u\n"), value, request.pin);
}
}

BLYNK_CONNECTED(){
DEBUG_MSG_P(PSTR("[BLYNK] Connected to Blynk server\n"));
blynkSendRelays();
}

#if WEB_SUPPORT

bool _blnkWebSocketOnReceive(const char * key, JsonVariant& value) {
return (strncmp(key, "blnk", 4) == 0);
}

void _blnkWebSocketOnSend(JsonObject& root) {
root["blnkVisible"] = 1;
root["blnkEnabled"] = getSetting("blnkEnabled", BLYNK_ENABLED).toInt() == 1;
root["blnkAuthKey"] = getSetting("blnkAuthKey", BLYNK_AUTH_TOKEN);
root["blnkHost"] = getSetting("blnkHost", BLYNK_HOST);
root["blnkPort"] = getSetting("blnkPort", BLYNK_PORT).toInt();

JsonArray& relays = root.createNestedArray("blnkRelays");
for (unsigned char i=0; i<relayCount(); i++) {
relays.add(getSetting("blnkRelayVpin", i, -1).toInt());
}

#if SENSOR_SUPPORT
JsonArray& list = root.createNestedArray("blnkMagnitudes");
for (byte i=0; i<magnitudeCount(); i++) {
JsonObject& element = list.createNestedObject();
element["name"] = magnitudeName(i);
element["type"] = magnitudeType(i);
element["index"] = magnitudeIndex(i);
element["idx"] = getSetting("blnkMagnitude", i, -1).toInt();
}
#endif
}
#endif //WEB_SUPPORT

void blynkSendRelays() {
for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
blynkSendRelay(relayID, relayStatus(relayID) ? 1 : 0);
}
}

void blynkSendMeasurement(unsigned char key, char * payload) {
if (!_blnk_enabled) return;
int vpin = getSetting("blnkMagnitude", key, -1).toInt();
if(vpin>-1) {
//DEBUG_MSG_P(PSTR("[BLYNK] Send sensor data to VPIN %u Data: %s\n"), vpin, payload);
Blynk.virtualWrite(vpin,payload);
}
}

void blynkSendRelay(unsigned char key, unsigned char status) {
if (!_blnk_enabled) return;
DEBUG_MSG_P(PSTR("[BLYNK] Send new status %u to Relay %u\n"),status,key);
unsigned int vpin = getSetting("blnkRelayVpin", key, 0).toInt();
Blynk.virtualWrite(vpin,status);
}

int blynkVpin(unsigned char relayID) {
char buffer[17];
snprintf_P(buffer, sizeof(buffer), PSTR("blnkRelayVpin%u"), relayID);
return getSetting(buffer).toInt();
}



void blynkSetup(){

_blnkConfigure();

#if WEB_SUPPORT
wsOnSendRegister(_blnkWebSocketOnSend);
wsOnReceiveRegister(_blnkWebSocketOnReceive);
#endif

espurnaRegisterLoop(blnkLoop);
espurnaRegisterReload(_blnkConfigure);

}


void blnkLoop(){

if(!_blnk_enabled) return;

if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return;

if(!_wific.connected()) _wifi_connect();

if (!Blynk.connected()) {
char auth[50];
getSetting("blnkAuthKey", BLYNK_AUTH_TOKEN).toCharArray(auth,50);
Blynk.config(_wific, auth);
if (!Blynk.connect(3000))
DEBUG_MSG_P(PSTR("[BLYNK] Could not connect to Bylnk Server. Token[%s]\n"),auth);
}

Blynk.run();

}

#endif
12 changes: 12 additions & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,18 @@
#define THINGSPEAK_TRIES 3 // Number of tries when sending data (minimum 1)
#endif

// -----------------------------------------------------------------------------
// BLYNK
// -----------------------------------------------------------------------------

#ifndef BLYNK_SUPPORT
#define BLYNK_SUPPORT 1
thaeger71 marked this conversation as resolved.
Show resolved Hide resolved
#define BLYNK_ENABLED 0
#define BLYNK_AUTH_TOKEN "insert your blynk auth key here"
#define BLYNK_HOST "blynk-cloud.com"
#define BLYNK_PORT 80
#endif

// -----------------------------------------------------------------------------
// SCHEDULER
// -----------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define BROKER_SUPPORT 0
#define BUTTON_SUPPORT 0
#define DOMOTICZ_SUPPORT 0
#define BLYNK_SUPPORT 0
#define HOMEASSISTANT_SUPPORT 0
#define I2C_SUPPORT 0
#define MDNS_SERVER_SUPPORT 0
Expand Down Expand Up @@ -3025,6 +3026,38 @@
#define NETBIOS_SUPPORT 1
#define SSDP_SUPPORT 1

#elif defined(TUYA_TYWE3S_ZX2820)
thaeger71 marked this conversation as resolved.
Show resolved Hide resolved

// Info
#define MANUFACTURER "Tuya"
#define DEVICE "ZX2820"

// Buttons
#define BUTTON1_PIN 0
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
#define BUTTON1_PRESS BUTTON_MODE_NONE
#define BUTTON1_CLICK BUTTON_MODE_TOGGLE
#define BUTTON1_DBLCLICK BUTTON_MODE_NONE
#define BUTTON1_LNGCLICK BUTTON_MODE_NONE
#define BUTTON1_LNGLNGCLICK BUTTON_MODE_RESET
#define BUTTON1_RELAY 1

// Relays
#define RELAY1_PIN 14
#define RELAY1_TYPE RELAY_TYPE_NORMAL

// LEDs
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0

// HLW8012
#ifndef HLW8012_SUPPORT
#define HLW8012_SUPPORT 1
#endif
#define HLW8012_SEL_PIN 15
#define HLW8012_CF1_PIN 5
#define HLW8012_CF_PIN 4

#endif

// -----------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ PROGMEM const char espurna_modules[] =
#if INFLUXDB_SUPPORT
"INFLUXDB "
#endif
#if BLYNK_SUPPORT
"BLYNK "
#endif
#if IR_SUPPORT
"IR "
#endif
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/espurna.ino
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ void setup() {
#if INFLUXDB_SUPPORT
idbSetup();
#endif
#if BLYNK_SUPPORT
blynkSetup();
#endif
#if THINGSPEAK_SUPPORT
tspkSetup();
#endif
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ void _relayProcess(bool mode) {
domoticzSendRelay(id);
#endif

#if BLYNK_SUPPORT
blynkSendRelay(id,target);
#endif

#if INFLUXDB_SUPPORT
relayInfluxDB(id);
#endif
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,9 @@ void _sensorReport(unsigned char index, double value) {
}
#endif // DOMOTICZ_SUPPORT

#if BLYNK_SUPPORT
blynkSendMeasurement(index, buffer);
#endif //BLYNK_SUPPORT
}

// -----------------------------------------------------------------------------
Expand Down
20 changes: 19 additions & 1 deletion code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function addValue(data, name, value) {
"schEnabled", "schSwitch","schAction","schType","schHour","schMinute","schWDs","schUTC",
"relayBoot", "relayPulse", "relayTime",
"mqttGroup", "mqttGroupInv", "relayOnDisc",
"dczRelayIdx", "dczMagnitude",
"dczRelayIdx", "dczMagnitude","blnkRelayVpin","blnkMagnitude",
"tspkRelay", "tspkMagnitude",
"ledMode",
"adminPass",
Expand Down Expand Up @@ -1404,6 +1404,24 @@ function processData(data) {
createMagnitudeList(value, "dczMagnitudes", "dczMagnitudeTemplate");
return;
}
<!-- endRemoveIf(!sensor)-->

// ---------------------------------------------------------------------
// Blynk
// ---------------------------------------------------------------------

// Blynk - Relays
if ("blnkRelays" === key) {
createRelayList(value, "blnkRelays", "blnkRelayTemplate");
return;
}

// Blynk - Magnitudes
<!-- removeIf(!sensor)-->
if ("blnkMagnitudes" === key) {
createMagnitudeList(value, "blnkMagnitudes", "blnkMagnitudeTemplate");
return;
}
<!-- endRemoveIf(!sensor)-->

// ---------------------------------------------------------------------
Expand Down
Loading