Last active
May 7, 2019 19:58
-
-
Save sandervandevelde/ad55c28fc31c7ea718f6af8d155c1055 to your computer and use it in GitHub Desktop.
ESP8266-iotcentral.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full | |
// license information. | |
// This gist is based on https://github.com/Azure/iot-central-firmware/blob/master/ESP8266/ESP8266.ino | |
#include <ESP8266WiFi.h> | |
#include "src/iotc/common/string_buffer.h" | |
#include "src/iotc/iotc.h" | |
#define WIFI_SSID "[your network WIFI ssid]" | |
#define WIFI_PASSWORD "[your network WIFI password]" | |
const char* SCOPE_ID = "[device connection scope id]"; | |
const char* DEVICE_ID = "[device connection device id]"; | |
const char* DEVICE_KEY = "[device connection primary or secondary key]"; | |
void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo); | |
#include "src/connection.h" | |
void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo) { | |
// ConnectionStatus | |
if (strcmp(callbackInfo->eventName, "ConnectionStatus") == 0) { | |
LOG_VERBOSE("Is connected ? %s (%d)", | |
callbackInfo->statusCode == IOTC_CONNECTION_OK ? "YES" : "NO", | |
callbackInfo->statusCode); | |
isConnected = callbackInfo->statusCode == IOTC_CONNECTION_OK; | |
return; | |
} | |
// payload buffer doesn't have a null ending. | |
// add null ending in another buffer before print | |
AzureIOT::StringBuffer buffer; | |
if (callbackInfo->payloadLength > 0) { | |
buffer.initialize(callbackInfo->payload, callbackInfo->payloadLength); | |
} | |
LOG_VERBOSE("- [%s] event was received. Payload => %s\n", | |
callbackInfo->eventName, buffer.getLength() ? *buffer : "EMPTY"); | |
if (strcmp(callbackInfo->eventName, "Command") == 0) { | |
LOG_VERBOSE("- Command name was => %s\r\n", callbackInfo->tag); | |
} | |
if (strcmp(callbackInfo->eventName, "SettingsUpdated") == 0) { | |
LOG_VERBOSE("- Setting name was => %s\r\n", callbackInfo->tag); | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
connect_wifi(WIFI_SSID, WIFI_PASSWORD); | |
connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY); | |
if (context != NULL) { | |
lastTick = 0; // set timer in the past to enable first telemetry a.s.a.p | |
} | |
} | |
void loop() { | |
if (isConnected) { | |
unsigned long ms = millis(); | |
if (ms - lastTick > 5000) { // send telemetry every 5 seconds | |
char msg[128] = {0}; | |
int pos = 0, errorCode = 0; | |
lastTick = ms; | |
pos = snprintf(msg, sizeof(msg) - 1, "{\"accelerometerX\": %d, \"dieNumber\":%d}", 10 + (rand() % 20), 1 + (rand() % 5)); | |
errorCode = iotc_send_telemetry(context, msg, pos); | |
msg[pos] = 0; | |
if (errorCode != 0) { | |
LOG_ERROR("Sending message has failed with error code %d", errorCode); | |
} | |
} | |
iotc_do_work(context); // do background work for iotc | |
} else { | |
iotc_free_context(context); | |
context = NULL; | |
connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment