Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1183506%22%20slang%3D%22en-US%22%3ESetting%20an%20Azure%20IoT%20Hub%20connection%20string%20in%20code%20on%20an%20MXChip%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1183506%22%20slang%3D%22en-US%22%3E%3CP%3EI%20was%20recently%20asked%20if%20there%20was%20a%20way%20to%20set%20the%20Azure%20IoT%20Hub%20connection%20string%20for%20an%20MXChip%20board%20in%20code.%20Normally%20you'd%20push%20this%20to%20the%20EEPROM%20using%20the%20tooling%20in%20VS%20code%2C%20or%20from%20a%20terminal%20using%20SSH%20as%20described%20%3CA%20href%3D%22https%3A%2F%2Fmicrosoft.github.io%2Fazure-iot-developer-kit%2Fdocs%2Fuse-configuration-mode%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%20In%20this%20situation%2C%20this%20was%20for%20students%20and%20was%20needed%20for%20two%20reasons%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20students%20would%20be%20sharing%20lab%20PCs%20and%20MXChip%20boards%20so%20would%20need%20to%20constantly%20log%20in%20and%20out%20of%20the%20Azure%20extensions%20in%20VS%20Code%20and%20re-configure%20boards%20-%20something%20they%20would%20probably%20forget%20to%20do%20every%20now%20and%20again%20causing%20a%20support%20headache.%3C%2FLI%3E%0A%3CLI%3EThe%20lecturer%20would%20need%20to%20be%20able%20to%20take%20their%20code%20and%20run%20it%20on%20their%20own%20board%20to%20test%2C%20so%20would%20need%20their%20connection%20string.%20To%20save%20time%20configuring%20boards%20and%20looking%20up%20connection%20strings%2C%20it%20would%20be%20better%20to%20have%20it%20in%20code.%20Again%2C%20it%20could%20easily%20be%20forgotten!%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CFONT%20color%3D%22%23808080%22%3E%3CEM%3E%3CSTRONG%3ENOTE%3C%2FSTRONG%3E%20-%20this%20is%20potentially%20a%20%3CSTRONG%3Every%20bad%20thing%3C%2FSTRONG%3E%20as%20you%20can%20end%20up%20essentially%20putting%20secrets%20in%20code.%20%3CSTRONG%3EDO%20NOT%3C%2FSTRONG%3E%20do%20this%20for%20public%20code%2C%20code%20then%20ends%20up%20on%20GitHub%20or%20anything%20like%20this%2C%20this%20only%20makes%20sense%20for%20private%20code%20submitted%20internally%20for%20something%20like%20a%20students%20assessment%20using%20a%20hub%20on%20a%20free%20tier%20so%20cannot%20cause%20any%20cost%20if%20it%20gets%20flooded.%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOut%20of%20the%20box%20there%20are%20no%20APIs%20available%20to%20do%20this.%20However%2C%20there%20is%20a%20way!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20connecting%20to%20Azure%20IoT%20Hub%20over%20MQTT%2C%20you%20call%20%3CSTRONG%3EDevKitMQTTClient_Init%3C%2FSTRONG%3E%20and%20this%20loads%20the%20connection%20string%20from%20EEPROM%20and%20uses%20it%20for%20the%20connection.%20As%20it%20turn%20out%2C%20as%20well%20as%20being%20able%20to%20read%20from%20EEPROM%20in%20code%2C%20you%20can%20also%20%3CA%20href%3D%22https%3A%2F%2Fmicrosoft.github.io%2Fazure-iot-developer-kit%2Fdocs%2Fapis%2Feeprom-interface%2F%23write%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewrite%20to%20the%20EEPROM%3C%2FA%3E%2C%20meaning%20you%20can%20set%20the%20value%20before%20it%20is%20read.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUsing%20this%2C%20it%20wasn't%20too%20hard%20to%20write%20the%20code%20to%20set%20this%20value%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3E%23include%20%22EEPROMInterface.h%22%0A%23include%20%22SerialLog.h%22%0A...%0Avoid%20setup()%20%7B%0A%20%20...%0A%20%20if%20(WiFi.begin()%20%3D%3D%20WL_CONNECTED)%0A%20%20%7B%0A%20%20%20%20%2F%2F%20Write%20the%20connection%20string%20to%20EEPROM%20as%20an%20array%20of%20uint8_t%0A%20%20%20%20EEPROMInterface%20eeprom%3B%0A%20%20%20%20char%20connString%5B%5D%20%3D%20%22%3CMY%20connection%3D%22%22%20string%3D%22%22%3E%22%3B%0A%20%20%20%20int%20ret%20%3D%20eeprom.write((uint8_t*)connString%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20strlen(connString)%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AZ_IOT_HUB_ZONE_IDX)%3B%0A%0A%20%20%20%20%2F%2F%20Check%20the%20write%20worked%20-%200%20means%20it%20was%20written%0A%20%20%20%20%2F%2F%20Less%20than%200%20is%20an%20error%0A%20%20%20%20if%20(ret%20%26lt%3B%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LogError(%22Unable%20to%20set%20the%20connection%20string%20in%20the%20EEPROM.%22)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20Connect%20as%20normal%2C%20this%20will%20read%20the%20new%20value%0A%20%20%20%20%2F%2F%20for%20the%20connection%20string%0A%20%20%20%20DevKitMQTTClient_Init()%3B%0A%20%20%20%20...%0A%7D%3C%2FMY%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EReplace%20%3CSTRONG%3E%3CMY%20connection%3D%22%22%20string%3D%22%22%3E%3C%2FMY%3E%3C%2FSTRONG%3E%20in%20the%20above%20code%20with%20your%20connection%20string.%20It%20will%20then%20be%20written%20to%20the%20EEPROM%20before%20the%20call%20to%20%3CSTRONG%3EDevKitMQTTClient_Init%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20read%20the%20%3CA%20href%3D%22https%3A%2F%2Fmicrosoft.github.io%2Fazure-iot-developer-kit%2Fdocs%2Fapis%2Feeprom-interface%2F%23write%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEEPROM%20write%20documentation%3C%2FA%3E%2C%20you%20will%20see%20%3CEM%3Ezones%3C%2FEM%3E%20listed.%20These%20are%20defined%20areas%20in%20the%20EEPROM%20and%20you%20can%20use%20these%20to%20write%20the%20WiFi%20SSID%20and%20Password%20as%20well%20as%20the%20connection%20string.%20This%20is%20useful%20if%20you%20want%20to%20build%20a%20solution%20that%20downloads%20new%20WiFi%20details.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWant%20more%20IoT%20Content%3F%20Check%20out%20the%20%3CA%20href%3D%22https%3A%2F%2Fchannel9.msdn.com%2FShows%2FInternet-of-Things-Show%2F%3FWT.mc_id%3Dmxchip-blog-jabenn%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIoT%20Show%20on%20Channel%209%3C%2FA%3E%2C%20or%20work%20through%20one%20of%20our%20hands-on%20self%20guided%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Flearn%2Fbrowse%2F%3Fterm%3DIOT%26amp%3BWT.mc_id%3Dmxchip-blog-jabenn%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIoT%20learning%20paths%20at%20Microsoft%20Learn%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1183841%22%20slang%3D%22en-US%22%3ERe%3A%20Setting%20an%20Azure%20IoT%20Hub%20connection%20string%20in%20code%20on%20an%20MXChip%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1183841%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20Sharing%20with%20the%20Community%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fcool_40x40.gif%22%20alt%3D%22%3Acool%3A%22%20title%3D%22%3Acool%3A%22%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

projects-devkit-get-started-th.jpg

 

I was recently asked if there was a way to set the Azure IoT Hub connection string for an MXChip board in code. Normally you'd push this to the EEPROM using the tooling in VS code, or from a terminal using SSH as described here. In this situation, this was for students and was needed for two reasons:

 

  • The students would be sharing lab PCs and MXChip boards so would need to constantly log in and out of the Azure extensions in VS Code and re-configure boards - something they would probably forget to do every now and again causing a support headache.
  • The lecturer would need to be able to take their code and run it on their own board to test, so would need their connection string. To save time configuring boards and looking up connection strings, it would be better to have it in code. Again, it could easily be forgotten!

NOTE - this is potentially a very bad thing as you can end up essentially putting secrets in code. DO NOT do this for public code, code then ends up on GitHub or anything like this, this only makes sense for private code submitted internally for something like a students assessment using a hub on a free tier so cannot cause any cost if it gets flooded.

 

Out of the box there are no APIs available to do this. However, there is a way!

 

When connecting to Azure IoT Hub over MQTT, you call DevKitMQTTClient_Init and this loads the connection string from EEPROM and uses it for the connection. As it turn out, as well as being able to read from EEPROM in code, you can also write to the EEPROM, meaning you can set the value before it is read.

 

Using this, it wasn't too hard to write the code to set this value:

 

 

#include "EEPROMInterface.h"
#include "SerialLog.h"
...
void setup() {
  ...
  if (WiFi.begin() == WL_CONNECTED)
  {
    // Write the connection string to EEPROM as an array of uint8_t
    EEPROMInterface eeprom;
    char connString[] = "<my connection string>";
    int ret = eeprom.write((uint8_t*)connString, 
                           strlen(connString), 
                           AZ_IOT_HUB_ZONE_IDX);

    // Check the write worked - 0 means it was written
    // Less than 0 is an error
    if (ret < 0)
    {
        LogError("Unable to set the connection string in the EEPROM.");
        return;
    }

    // Connect as normal, this will read the new value
    // for the connection string
    DevKitMQTTClient_Init();
    ...
}

 

 

Replace <my connection string> in the above code with your connection string. It will then be written to the EEPROM before the call to DevKitMQTTClient_Init.

 

If you read the EEPROM write documentation, you will see zones listed. These are defined areas in the EEPROM and you can use these to write the WiFi SSID and Password as well as the connection string. This is useful if you want to build a solution that downloads new WiFi details.

 

 

Want more IoT Content? Check out the IoT Show on Channel 9, or work through one of our hands-on self guided IoT learning paths at Microsoft Learn.

 

1 Comment

Thanks for Sharing with the Community :cool: