For this project, we recommend using VS Code with the CircuitPython extension

Once you've configured your coding workspace, grab the Azure Cost Monitor program code by cloning the GitHub repo (direct link here) onto your computer:

git clone https://github.com/microsoft/azure-cost-monitor.git

Create a secrets.py file

This is where we'll store our WiFi and Azure credentials (all that stuff we copied from earlier!)

1. Create a new file called secrets.py

2. Fill secrets.py with the following code: 

secrets = {
  "ssid" : "YOUR_WIFI_SSID",
  "password" : "YOUR_WIFI_PASSWORD",
  "appId": "AZURE_APP_ID",
  "clientSecret": "PASSWORD",
  "tenant": "TENANT_ID",
  "subscription": "SUBSCRIPTION_ID
}

3. Replace all of the variables in quotes (e.g. "YOUR_WIFI_SSID") with the appropriate credentials for your WiFi SSID and password, and the Azure credentials you copied from the "Get Azure Credentials" section.

Keep your secrets.py private! Do not upload it to GitHub, etc.

If you create a repo or otherwise share your project, be sure to leave out this file.

Load files onto the MagTag!

We're ready to go! To finish up, we just need to load three files onto the MagTag:

Note: azure.py and code.py are in the src folder of the GitHub repo

  • secrets.py
    • This holds our credentials
  • azure.py
    • This interfaces with Microsoft Azure Cost API
  • code.py
    • This is the main program loop!

Wait.. what's happening??

The azure.py file is a mini-library for interfacing with the Azure Cost API. It creates a class called 'azure' which takes in your Azure credentials (which we pull from the secrets.py file). The cost_forecast function grabs and returns your Azure daily cost forecast. If you want to see more info, use the following command at the bottom of the cost_forecast function:

print(json_resp)

The code.py file is the main program loop: it loads the secrets.py file, connects to your local WiFi network, and then connects Azure and returns the daily cost forecast once every 24 hours. 

If you want to see more info or if you need to debug, use a Serial Monitor (e.g. PuTTY). Here's a great overview on how to do this on Windows.

Going Further: Modify or add to other projects

One of the main reasons we created the azure.py file was to make it easier to add cost tracking to other projects.

The Azure Cost API allows you to monitor a subscription or, with a bit of modification, a specific resource group. See this resource for assistance on changing the API request to monitor a resource group.

Other helpful modifications might be:

  • Add a datestamp to the print output
  • Create an estimate of aggregated cost forecast
    • Keep in mind that your forecasted cost will likely be different than actual billed costs
  • Set the NeoPixels to change color based on the cost forecast
    • For example, red could indicate that cost is above an expected threshold (e.g. if you're expecting less than $0.20/day, turn NeoPixels red if forecast cost is above that)
  • Other thoughts? Let us know by opening an issue in the GitHub repo!

This guide was first published on Feb 15, 2021. It was last updated on Mar 28, 2024.

This page (Program the MagTag!) was last updated on Mar 08, 2024.

Text editor powered by tinymce.