Skip to content

Commit

Permalink
Merge pull request #11 from blinker-iot/dev
Browse files Browse the repository at this point in the history
update codes
  • Loading branch information
i3water authored May 9, 2018
2 parents aa9c0be + 4cf545e commit 3d18ace
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
10 changes: 10 additions & 0 deletions examples/Blinker_Button/Button_BLE/Button_BLE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BLINKER_BLE

#define BUTTON_1 "ButtonKey"
#define TAP_EXAMPLE

#include <Blinker.h>

Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion examples/Blinker_Button/Button_WiFi/Button_WiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BLINKER_WIFI

#define BUTTON_1 "ButtonKey"
#define TAP_EXAMPLE

#include <Blinker.h>

Expand Down Expand Up @@ -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
}
18 changes: 12 additions & 6 deletions src/Adapters/BlinkerMQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down
43 changes: 39 additions & 4 deletions src/Blinker/BlinkerApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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 ) {
Expand Down
8 changes: 4 additions & 4 deletions src/Blinker/BlinkerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -83,15 +83,15 @@

#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

#define BLINKER_MQTT_HOST_SIZE 50

#define BLINKER_MQTT_ID_SIZE 110

#define BLINKER_MQTT_NAME_SIZE 40
#define BLINKER_MQTT_NAME_SIZE 80

#define BLINKER_MQTT_KEY_SIZE 50

Expand Down

0 comments on commit 3d18ace

Please sign in to comment.