diff --git a/examples/Blinker_Button/Button_BLE/Button_BLE.ino b/examples/Blinker_Button/Button_BLE/Button_BLE.ino index b999e903..fcef7da5 100644 --- a/examples/Blinker_Button/Button_BLE/Button_BLE.ino +++ b/examples/Blinker_Button/Button_BLE/Button_BLE.ino @@ -2,6 +2,7 @@ #define BLINKER_BLE #define BUTTON_1 "ButtonKey" +#define TAP_EXAMPLE #include @@ -30,7 +31,16 @@ void loop() Blinker.print("millis", BlinkerTime); } +#if defined(TAP_EXAMPLE) if (Blinker.button(BUTTON_1)) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + } // simple tap +#else + if (Blinker.button(BUTTON_1)) { + digitalWrite(LED_BUILTIN, LOW); } + else { + digitalWrite(LED_BUILTIN, HIGH); + } //long press +#endif } \ No newline at end of file diff --git a/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino b/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino index 3bb1ab14..d4c7b56c 100644 --- a/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino +++ b/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino @@ -2,6 +2,7 @@ #define BLINKER_WIFI #define BUTTON_1 "ButtonKey" +#define TAP_EXAMPLE #include @@ -32,8 +33,17 @@ void loop() Blinker.print(BlinkerTime); Blinker.print("millis", BlinkerTime); } - + +#if defined(TAP_EXAMPLE) if (Blinker.button(BUTTON_1)) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + } // simple tap +#else + if (Blinker.button(BUTTON_1)) { + digitalWrite(LED_BUILTIN, LOW); } + else { + digitalWrite(LED_BUILTIN, HIGH); + } //long press +#endif } diff --git a/src/Adapters/BlinkerMQTT.h b/src/Adapters/BlinkerMQTT.h index 82b64d26..b19a3380 100644 --- a/src/Adapters/BlinkerMQTT.h +++ b/src/Adapters/BlinkerMQTT.h @@ -26,7 +26,8 @@ static char DEVICE_NAME[BLINKER_MQTT_DEVICENAME_SIZE]; static char *BLINKER_PUB_TOPIC; static char *BLINKER_SUB_TOPIC; -WiFiClientSecure client; +WiFiClientSecure client_s; +WiFiClient client; Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Publish *iotPub; Adafruit_MQTT_Subscribe *iotSub; @@ -280,10 +281,11 @@ void BlinkerMQTT::connectServer() { MQTT_PORT = BLINKER_MQTT_ALIYUN_PORT; } else if (_broker == BLINKER_MQTT_BORKER_QCLOUD) { + // String id2name = _userID.subString(10, _userID.length()); memcpy(DEVICE_NAME, _userID.c_str(), 12); String IDtest = _productInfo + _userID; strcpy(MQTT_ID, IDtest.c_str()); - String NAMEtest = IDtest + ";12010126;12345"; + String NAMEtest = IDtest + ";" + _userName; strcpy(MQTT_NAME, NAMEtest.c_str()); strcpy(MQTT_KEY, _key.c_str()); strcpy(MQTT_PRODUCTINFO, _productInfo.c_str()); @@ -325,14 +327,14 @@ void BlinkerMQTT::connectServer() { } else if (_broker == BLINKER_MQTT_BORKER_QCLOUD) { uint8_t str_len; - String PUB_TOPIC_STR = String(MQTT_PRODUCTINFO) + "/" + String(MQTT_ID) + "/s"; + String PUB_TOPIC_STR = String(MQTT_PRODUCTINFO) + "/" + String(_userID) + "/s"; str_len = PUB_TOPIC_STR.length() + 1; BLINKER_PUB_TOPIC = (char*)malloc(str_len*sizeof(char)); memcpy(BLINKER_PUB_TOPIC, PUB_TOPIC_STR.c_str(), str_len); #ifdef BLINKER_DEBUG_ALL BLINKER_LOG2("BLINKER_PUB_TOPIC: ", BLINKER_PUB_TOPIC); #endif - String SUB_TOPIC_STR = String(MQTT_PRODUCTINFO) + "/" + String(MQTT_ID) + "/r"; + String SUB_TOPIC_STR = String(MQTT_PRODUCTINFO) + "/" + String(_userID) + "/r"; str_len = SUB_TOPIC_STR.length() + 1; BLINKER_SUB_TOPIC = (char*)malloc(str_len*sizeof(char)); memcpy(BLINKER_SUB_TOPIC, SUB_TOPIC_STR.c_str(), str_len); @@ -341,8 +343,12 @@ void BlinkerMQTT::connectServer() { #endif } - - mqtt = new Adafruit_MQTT_Client(&client, MQTT_HOST, MQTT_PORT, MQTT_ID, MQTT_NAME, MQTT_KEY); + if (_broker == BLINKER_MQTT_BORKER_ALIYUN) { + mqtt = new Adafruit_MQTT_Client(&client_s, MQTT_HOST, MQTT_PORT, MQTT_ID, MQTT_NAME, MQTT_KEY); + } + else if (_broker == BLINKER_MQTT_BORKER_QCLOUD) { + mqtt = new Adafruit_MQTT_Client(&client, MQTT_HOST, MQTT_PORT, MQTT_ID, MQTT_NAME, MQTT_KEY); + } iotPub = new Adafruit_MQTT_Publish(mqtt, BLINKER_PUB_TOPIC); iotSub = new Adafruit_MQTT_Subscribe(mqtt, BLINKER_SUB_TOPIC); diff --git a/src/Blinker/BlinkerApi.h b/src/Blinker/BlinkerApi.h index 31b4a340..7d3ad816 100644 --- a/src/Blinker/BlinkerApi.h +++ b/src/Blinker/BlinkerApi.h @@ -48,13 +48,15 @@ class BlinkerButton void name(String name) { buttonName = name; } String getName() { return buttonName; } - void freshState(bool state) { buttonState = state; } + void freshState(bool state, bool isLong = false) { buttonState = state; isLPress = isLong; } bool getState() { return buttonState; } + bool longPress() { return isLPress; } bool checkName(String name) { return ((buttonName == name) ? true : false); } private : String buttonName; bool buttonState; + bool isLPress; }; class BlinkerSlider @@ -222,7 +224,7 @@ class BlinkerApi _fresh = true; } - if (state == BLINKER_CMD_BUTTON_PRESSED || state == BLINKER_CMD_BUTTON_TAP) { + if (state == BLINKER_CMD_BUTTON_TAP) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { _Button[_bCount] = new BlinkerButton(); @@ -238,6 +240,22 @@ class BlinkerApi _fresh = true; return true; } + else if (state == BLINKER_CMD_BUTTON_PRESSED) { + if( num == BLINKER_OBJECT_NOT_AVAIL ) { + if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { + _Button[_bCount] = new BlinkerButton(); + _Button[_bCount]->name(_bName); + _Button[_bCount]->freshState(true, true); + _bCount++; + } + } + else { + _Button[num]->freshState(true, true); + } + + _fresh = true; + return true; + } else if (state == BLINKER_CMD_BUTTON_RELEASED) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { @@ -265,7 +283,8 @@ class BlinkerApi } bool _state = _Button[num]->getState(); - _Button[num]->freshState(false); + if ( !_Button[num]->longPress() ) + _Button[num]->freshState(false); return _state; } @@ -562,7 +581,7 @@ class BlinkerApi _fresh = true; } - if (state == BLINKER_CMD_BUTTON_PRESSED || state == BLINKER_CMD_BUTTON_TAP) { + if (state == BLINKER_CMD_BUTTON_TAP) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { _Button[_bCount] = new BlinkerButton(); @@ -578,6 +597,22 @@ class BlinkerApi _fresh = true; return true; } + else if (state == BLINKER_CMD_BUTTON_PRESSED) { + if( num == BLINKER_OBJECT_NOT_AVAIL ) { + if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { + _Button[_bCount] = new BlinkerButton(); + _Button[_bCount]->name(_bName); + _Button[_bCount]->freshState(true, true); + _bCount++; + } + } + else { + _Button[num]->freshState(true, true); + } + + _fresh = true; + return true; + } else if (state == BLINKER_CMD_BUTTON_RELEASED) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { diff --git a/src/Blinker/BlinkerConfig.h b/src/Blinker/BlinkerConfig.h index ba883c0f..9d636975 100644 --- a/src/Blinker/BlinkerConfig.h +++ b/src/Blinker/BlinkerConfig.h @@ -49,9 +49,9 @@ #define BLINKER_CMD_BUTTON_TAP "tap" -#define BLINKER_CMD_BUTTON_PRESSED "pressed" +#define BLINKER_CMD_BUTTON_PRESSED "press" -#define BLINKER_CMD_BUTTON_RELEASED "released" +#define BLINKER_CMD_BUTTON_RELEASED "pressup" #define BLINKER_CMD_NEWLINE "\n" @@ -83,7 +83,7 @@ #define BLINKER_MQTT_QCLOUD_HOST "iotcloud-mqtt.gz.tencentdevices.com" - #define BLINKER_MQTT_QCLOUD_PORT 8883 + #define BLINKER_MQTT_QCLOUD_PORT 1883 #define BLINKER_MQTT_PING_TIMEOUT 30000UL @@ -91,7 +91,7 @@ #define BLINKER_MQTT_ID_SIZE 110 - #define BLINKER_MQTT_NAME_SIZE 40 + #define BLINKER_MQTT_NAME_SIZE 80 #define BLINKER_MQTT_KEY_SIZE 50