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

Commit

Permalink
Merge pull request #2 from obastemur/device_state
Browse files Browse the repository at this point in the history
[mxchip] Implement device state and event support
  • Loading branch information
obastemur committed May 9, 2018
2 parents ccce369 + ae0b47c commit b6db670
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
11 changes: 10 additions & 1 deletion AZ3166/inc/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@

typedef enum { NORMAL, CAUTION, DANGER } DeviceState;

#define STATE_MESSAGE(state) \
state == NORMAL ? "{\"deviceState\":\"NORMAL\"}" : ( \
state == CAUTION ? "{\"deviceState\":\"CAUTION\"}" : ( \
state == DANGER ? "{\"deviceState\":\"DANGER\"}" : ( \
"{\"deviceState\":\"NORMAL\"}" \
) \
) \
)

class WiFiController;
class SensorController;
class LoopController;
Expand All @@ -59,7 +68,7 @@ struct Globals
#define IOT_CENTRAL_ZONE_IDX 0x02
#define IOT_CENTRAL_MAX_LEN STRING_BUFFER_128
#define AZIOTC_FW_MAJOR_VERSION 1
#define AZIOTC_FW_MINOR_VERSION 1
#define AZIOTC_FW_MINOR_VERSION 2
#define AZIOTC_FW_PATCH_VERSION 0
#define AZIOTC_FW_VERSION TO_STRING(AZIOTC_FW_MAJOR_VERSION AZIOTC_FW_MINOR_VERSION AZIOTC_FW_PATCH_VERSION) "-MSIOTC"

Expand Down
10 changes: 3 additions & 7 deletions AZ3166/src/iotHubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bool IoTHubClient::sendReportedProperty(const char *payload) {
bool retValue = true;

IOTHUB_CLIENT_RESULT result = IoTHubClient_LL_SendReportedState(iotHubClientHandle,
(const unsigned char*)payload, strlen(payload), deviceTwinConfirmationCallback, (void*) payload);
(const unsigned char*)payload, strlen(payload), deviceTwinConfirmationCallback, NULL);

if (result != IOTHUB_CLIENT_OK) {
LOG_ERROR("Failure sending reported property!!!");
Expand Down Expand Up @@ -334,7 +334,7 @@ int DeviceDirectMethodCallback(const char* method_name, const unsigned char* pay
void echoDesired(const char *propertyName, const char *message, const char *status, int statusCode) {
JSObject rootObject(message);
JSObject propertyNameObject, desiredObject, desiredObjectPropertyName;
LOG_ERROR("echoDesired is received - pn: %s m: %s s: %s sc: %d", propertyName, message, status, statusCode);
LOG_VERBOSE("echoDesired is received - pn: %s m: %s s: %s sc: %d", propertyName, message, status, statusCode);
const char* methodName = rootObject.getStringByName("methodName");
if (methodName == NULL) {
LOG_VERBOSE("Object doesn't have a member 'methodName'");
Expand Down Expand Up @@ -374,7 +374,7 @@ void echoDesired(const char *propertyName, const char *message, const char *stat
snprintf(*buffer, buffer_size + 1, echoTemplate, propertyName,
(int) value, statusCode,
status, (int) desiredVersion);
LOG_ERROR("Sending reported property buffer: %s", *buffer);
LOG_VERBOSE("Sending reported property buffer: %s", *buffer);

TelemetryController * telemetryController = NULL;
if (Globals::loopController->withTelemetry()) {
Expand All @@ -383,7 +383,6 @@ void echoDesired(const char *propertyName, const char *message, const char *stat

if (telemetryController != NULL && telemetryController->getHubClient() != NULL &&
telemetryController->getHubClient()->sendReportedProperty(*buffer)) {
buffer.makePersistent(); // will be freed under deviceTwinConfirmationCallback
LOG_VERBOSE("Desired property %s successfully echoed back as a reported property.", propertyName);
StatsController::incrementReportedCount();
} else {
Expand Down Expand Up @@ -481,9 +480,6 @@ void deviceTwinGetStateCallback(DEVICE_TWIN_UPDATE_STATE update_state,

static void deviceTwinConfirmationCallback(int status_code, void* userContextCallback) {
LOG_VERBOSE("DeviceTwin CallBack: Status_code = %u", status_code);
if (userContextCallback != NULL) {
free(userContextCallback);
}
}

static void connectionStatusCallback(IOTHUB_CLIENT_CONNECTION_STATUS result,
Expand Down
38 changes: 30 additions & 8 deletions AZ3166/src/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ void TelemetryController::loop() {
return;
}

if ((millis() - lastTimeSync > NTP_SYNC_PERIOD)) {
const uint32_t currentMillis = millis(); // reduce number of times we call this

if ((currentMillis - lastTimeSync > NTP_SYNC_PERIOD)) {
// re-sync the time from ntp
if (SyncTimeToNTP()) {
lastTimeSync = millis();
Expand All @@ -85,24 +87,45 @@ void TelemetryController::loop() {
// look for button A pressed to signify state change
// when the A button is pressed the device state rotates to the next value and a state telemetry message is sent
if (DeviceControl::IsButtonClicked(USER_BUTTON_A) &&
(millis() - lastSwitchPress > TELEMETRY_SWITCH_DEBOUNCE_TIME)) {
(currentMillis - lastSwitchPress > TELEMETRY_SWITCH_DEBOUNCE_TIME)) {

DeviceControl::incrementDeviceState();
DeviceControl::showState();
sendStateChange();
lastSwitchPress = millis();

// SEND State example
const char * stateMessage = (STATE_MESSAGE(DeviceControl::getDeviceState()));
if (iothubClient->sendReportedProperty(stateMessage)) {
LOG_VERBOSE("Device state successfully sent");
StatsController::incrementReportedCount();
} else {
LOG_ERROR("Sending device state has failed");
StatsController::incrementErrorCount();
}
}

// look for button B pressed to page through info screens
if (DeviceControl::IsButtonClicked(USER_BUTTON_B) &&
(millis() - lastSwitchPress > TELEMETRY_SWITCH_DEBOUNCE_TIME)) {
(currentMillis - lastSwitchPress > TELEMETRY_SWITCH_DEBOUNCE_TIME)) {

currentInfoPage = (currentInfoPage + 1) % 3;
lastSwitchPress = millis();
lastSwitchPress = currentMillis;

// SEND EVENT example
// build the event payload
const char * eventString = "{\"ButtonBPressed\": 1}";
if (iothubClient->sendTelemetry(eventString)) {
LOG_VERBOSE("Event successfully sent");
StatsController::incrementReportedCount();
} else {
LOG_ERROR("Sending event has failed");
StatsController::incrementErrorCount();
}
}

// example of sending telemetry data
if (millis() - lastTelemetrySend >= TELEMETRY_SEND_INTERVAL) {
if (currentMillis - lastTelemetrySend >= TELEMETRY_SEND_INTERVAL) {
String payload; // max payload size for Azure IoT

buildTelemetryPayload(&payload);
Expand All @@ -111,8 +134,8 @@ void TelemetryController::loop() {
}

// example of sending a device twin reported property when the accelerometer detects a double tap
if (Globals::sensorController.checkForShake() &&
(millis() - lastShakeTime > TELEMETRY_REPORTED_SEND_INTERVAL)) {
if ((currentMillis - lastShakeTime > TELEMETRY_REPORTED_SEND_INTERVAL) &&
Globals::sensorController.checkForShake()) {

String shakeProperty = F("{\"dieNumber\":{{die}}}");
randomSeed(analogRead(0));
Expand All @@ -124,7 +147,6 @@ void TelemetryController::loop() {
AutoString shakeString(shakeProperty.c_str(), shakeProperty.length());
if (iothubClient->sendReportedProperty(*shakeString)) {
LOG_VERBOSE("Reported property dieNumber successfully sent");
shakeString.makePersistent();
StatsController::incrementReportedCount();
} else {
LOG_ERROR("Reported property dieNumber failed to during sending");
Expand Down

0 comments on commit b6db670

Please sign in to comment.