in mxdevice/Device/MXDeviceDemo.ino [114:189]
void setup()
{
Screen.init();
Screen.print(0, "IoT Device Demo");
Screen.print(2, "Initializing...");
Screen.print(3, " > Serial");
Serial.begin(115200);
// Initialize the WiFi module
Screen.print(3, " > WiFi");
hasWifi = false;
initWifi();
if (!hasWifi)
{
return;
}
// Get MAC address and populate device id
byte mac[6];
WiFi.macAddress(mac);
sprintf(device_id, "%02x%02x%02x" , mac[3], mac[4], mac[5]);
// HTTP call to device endpoint
char endpoint[100];
sprintf(endpoint, "%s%s" ,DEVICE_CREDENTIAL_ENDPOINT, device_id);
Serial.println(endpoint);
HTTPClient client = HTTPClient(HTTP_GET, endpoint);
client.set_header("Authorization", DEVICE_CREDENTIAL_ENDPOINT_AUTH);
const Http_Response *response = client.send(NULL, 0);
if (response != NULL)
{
const char *message = response->body;
JSON_Value *root_value = json_parse_string(message);
JSON_Object *root_object = json_value_get_object(root_value);\
device_conn_string = json_object_get_string(root_object, "ConnectionString");
Serial.println("Get HTTP succeeded");
Serial.println(device_conn_string);
Serial.println("after");
}
else
{
Serial.println("Get HTTP failed");
return;
}
// Write device connection in eeprom
EEPROMInterface eeprom;
uint8_t *connString = (uint8_t *) device_conn_string;
int resp = eeprom.write(connString, AZ_IOT_HUB_MAX_LEN, AZ_IOT_HUB_ZONE_IDX);
if (resp == 0)
{
Serial.println("Successfully wrote device connection string to eeprom");
}
else
{
Serial.println("Error writing device connection string to eeprom");
return;
}
Screen.print(3, " > Sensors");
sensorInit();
Screen.print(3, " > IoT Hub");
DevKitMQTTClient_Init(true);
DevKitMQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "MXDeviceDemo");
DevKitMQTTClient_SetSendConfirmationCallback(sendConfirmationCallback);
DevKitMQTTClient_SetMessageCallback(messageCallback);
DevKitMQTTClient_SetDeviceTwinCallback(deviceTwinCallback);
DevKitMQTTClient_SetDeviceMethodCallback(deviceMethodCallback);
send_interval_ms = SystemTickCounterRead();
}