Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Clean up a few nits in contrib (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss authored May 27, 2020
1 parent 409b417 commit 1a5e142
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ void handleGatewayCommand() {

void parseInput(String in) {
int x = 0;
char str_array[in.length()+1];
in.toCharArray(str_array,in.length()+1);
char* pars = strtok (str_array,",");
char strArray[in.length()+1];
in.toCharArray(strArray,in.length()+1);
char* pars = strtok (strArray,",");
char* msgArray[3];
while (pars != NULL) {
msgArray[x] = pars;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
Expand All @@ -21,7 +20,6 @@ static String staticBTDeviceID = "";
bool connected;

void setupSerialBT() {

//SerialBT.setPin(pin);
delay(5000);
SerialBT.begin("my-esp32-gateway",true);
Expand All @@ -38,7 +36,7 @@ void setupSerialBT() {
Serial.println("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
}
Expand Down
63 changes: 35 additions & 28 deletions examples/complex/esp32/Gateway/Esp32-gateway/esp32-mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CloudIoTCoreDevice *device;
CloudIoTCoreMqtt *mqtt;
MQTTClient *mqttClient;
unsigned long iss = 0;
String jwt, incoming_payload, incoming_command, input="NOT FOUND";
String jwt, incomingPayload, incomingCommand, input="NOT FOUND";

///////////////////////////////
// Helpers specific to this board
Expand All @@ -45,7 +45,7 @@ void getDeviceID(String payload) {
char *pars = strtok(buf, ",");
staticBTDeviceID = pars;
pars = strtok(NULL, ",");
incoming_command = pars;
incomingCommand = pars;
}


Expand Down Expand Up @@ -87,44 +87,47 @@ void connectWifi() {
}


void detachDelegate(String delegate_id) {
void detachDelegate(String delegateId) {
//subscribe to delegate configuration
mqttClient->unsubscribe("/devices/"+ delegate_id +"/config");
mqttClient->unsubscribe("/devices/"+ delegateId +"/config");

//subscribe to delegate commands
mqttClient->unsubscribe("/devices/"+ delegate_id +"/commands/#");
mqttClient->unsubscribe("/devices/"+ delegateId +"/commands/#");

//attach to delegate device
String dat = "{}";
mqttClient->publish(String("/devices/"+ delegate_id +"/detach").c_str(),dat.c_str(),false,1);
mqttClient->publish(
String("/devices/"+ delegateId +"/detach").c_str(),
dat.c_str(), false, 1);
}


bool attachAndSubscribe(String delegate_id) {
bool attachAndSubscribe(String delegateId) {
//attach to delegate device
String dat = "{}";
mqttClient->publish(String("/devices/"+ delegate_id +"/attach").c_str(),dat.c_str(),false,1);
mqttClient->publish(String("/devices/"+ delegateId +"/attach").c_str(),
dat.c_str(), false, 1);

//subscribe to delegate configuration
mqttClient->subscribe("/devices/"+ delegate_id +"/config",1);
mqttClient->subscribe("/devices/"+ delegateId +"/config", 1);

//subscribe to delegate commands
mqttClient->subscribe("/devices/"+ delegate_id +"/commands/#",0);
mqttClient->subscribe("/devices/"+ delegateId +"/commands/#", 0);
}


// The MQTT callback function for commands and configuration updates
// This is were incoming command from the gateway gets saved,
// to forward to the delegate device
void messageReceived(String &topic, String &payload) {
int size = sizeof(delegate_device_id) / sizeof(delegate_device_id[0]);
int size = sizeof(delegateDeviceId) / sizeof(delegateDeviceId[0]);
Serial.println("incoming: " + topic + " - " + payload);
getDeviceID(payload);
incoming_payload = payload;
incomingPayload = payload;

if(payload == "detach") {
for(int i = 0; i < size;i++) {
detachDelegate(delegate_device_id[i]);
detachDelegate(delegateDeviceId[i]);
mqttClient->loop();
}
}
Expand Down Expand Up @@ -156,24 +159,28 @@ bool publishTelemetry(String subfolder, const char* data, int length) {
}


bool publishDelegateTelemetry(String delegate_id,String data) {
return mqttClient->publish(String("/devices/"+ delegate_id +"/events").c_str(), String(data).c_str(), false, 1);
bool publishDelegateTelemetry(String delegateId,String data) {
return mqttClient->publish(
String("/devices/"+ delegateId +"/events").c_str(),
String(data).c_str(), false, 1);
}


bool publishDelegateState(String delegate_id,String data) {
return mqttClient->publish(String("/devices/"+ delegate_id +"/state").c_str(),String(data).c_str(),false,1);
bool publishDelegateState(String delegateId,String data) {
return mqttClient->publish(
String("/devices/"+ delegateId +"/state").c_str(),
String(data).c_str(), false, 1);
}


// Polls sensor data from the delegate devices and forwards Cloud-to-device messages.
// Message from the delegate device is semicolon terminated and takes the format:
// <deviceid>,<command>,<payload>;
String pollDelegate() {
if (incoming_payload != "") {
if (incomingPayload != "") {
setupSerialBT();
forwardComand(incoming_payload);
incoming_payload = "";
forwardComand(incomingPayload);
incomingPayload = "";

if (Serial.available()) {
SerialBT.write(Serial.read());
Expand All @@ -186,9 +193,9 @@ String pollDelegate() {

input = (SerialBT.readStringUntil(';'));

if(incoming_command == "event") {
if(incomingCommand == "event") {
publishDelegateTelemetry(staticBTDeviceID,input);
} else if(incoming_command == "state") {
} else if(incomingCommand == "state") {
publishDelegateState(staticBTDeviceID,input);
}

Expand All @@ -206,13 +213,13 @@ void connect() {
connectWifi();
mqtt->mqttConnect();

int size = sizeof(delegate_device_id) / sizeof(delegate_device_id[0]);
for(int i = 0; i < size;i++) {
attachAndSubscribe(delegate_device_id[i]);
int size = sizeof(delegateDeviceId) / sizeof(delegateDeviceId[0]);

for(int i = 0; i < size; i++) {
attachAndSubscribe(delegateDeviceId[i]);
mqttClient->loop();
}

delay(500); // <- fixes some issues with WiFi stability
}

Expand Down
2 changes: 1 addition & 1 deletion src/CloudIoTCoreMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) {
onConnect();
}

void CloudIoTCoreMqtt::mqttConnect_nonBlocking(bool skip) {
void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) {
Serial.print("\nconnecting...");

bool result =
Expand Down
2 changes: 1 addition & 1 deletion src/CloudIoTCoreMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CloudIoTCoreMqtt {

void loop();
void mqttConnect(bool skip = false);
void mqttConnect_nonBlocking(bool skip = false);
void mqttConnectAsync(bool skip = false);
void startMQTT();

bool publishTelemetry(String data);
Expand Down

0 comments on commit 1a5e142

Please sign in to comment.