Using the Flux VS Code Extension for IoT Application Development

5 minutes

InfluxData prides itself on its effort to prioritize developer happiness. This included providing developers with a variety of tools to interact with InfluxDB v2 OSS or InfluxDB Cloud, so they can pick the development style that works best for them.  This article assumes you’re using the InfluxDB Cloud Free tier, which is the easiest way to get started and maintain InfluxDB. You can use any of the following tools for your IoT application development:

The Visual Studio Code Flux extension in combination with the CLI is the preferred tool for most developers. The CLI offers an easy approach to InfluxDB database management, and the Visual Studio Code Flux extension provides an easy way to query and write Flux scripts (including Flux tasks). If you haven’t given it a try already, I encourage you to install the Flux extension for Visual Studio Code. To query your InfluxDB Cloud account with the Flux extension, you must first configure it and connect to your cloud account.

An example of editing a Task in VS Code with the Flux extension
An example of editing a Task in VS Code with the Flux extension. Easily switch between and select the InfluxDB organization you want to use from the existing configurations in the left-hand panel. The corresponding output appears in the tab to the left. I have configurations for my personal InfluxDB Cloud account and organization. I’m also a user on my coworker John’s organization.

How developers are using the Flux VS Code Extension

I recommend checking out this three-part blog series on building a house plant monitoring application on top of InfluxDB with the CLI and VS Code Flux extension for an example of how to leverage those tools for IoT application development.

  1. Part One: Prototyping IoT with InfluxDB Cloud
  2. Part Two: Queries, Tasks and Dashboards
  3. Part Three: Building an IoT App with InfluxDB Cloud, Python and Flask 
A screenshot from the development of Plant Buddy
A screenshot from the development of Plant Buddy, an IoT application built on top of InfluxDB. Here the developer is using the VS Code Flux extension to query their InfluxDB Cloud Free Tier account and visualize the time series data on their application.

I also enjoy using the Flux VS Code extension for debugging complicated Flux scripts and optimizing the performance of my Flux scripts because I can save my Flux scripts and easily compare Flux performance with the Flux Profiler.

Using the Flux VS Code extension to compare the outputs of the Flux profiler
Using the Flux VS Code extension to compare the outputs of the Flux profiler for variations of the same Flux query in an effort to optimize the query.

Exciting new features in the Flux VS Code Extension

Today I want to take a moment to highlight the new features that were included in the latest release. They include the ability to:

  1. Create and delete buckets
  2. Create and delete tasks

To create a bucket, right-click on the Buckets dropdown and select Create bucket.

Exciting new features in the Flux VS Code Extension

This will bring you to a configuration tab where you can name your bucket and specify the retention period of your bucket. Click Create to create your bucket.

Create your bucket

To delete a bucket, right-click on any bucket in the Buckets dropdown and select Delete bucket. Delete tasks in the same way you delete buckets.

To delete a bucket

To create a task, right click on the Tasks dropdown and select Create task, which will bring you to the following tab where you configure your task options.

How to Create task

Click Save and continue to create a new task. A new tab is populated with the task options and Flux boilerplate. Write your Flux query after the task options. Right-click on the task tab to Run Query and verify that your Flux is transforming your data correctly. The output of your task will be populated in a tab to the right.

Run Query

It’s best practice to use task options in your task script. To ensure that your script  queries data from the last time the task was run, use the task.every option in your range() function, |> range(start:-task.every). Additionally, make sure to append the to() function to your query to write your new data to a new destination bucket or measurement. Finally, save the task script to create an active task in InfluxDB Cloud.

Final thoughts on the  Flux VS Code Extension

I hope this post inspires you to take advantage of the Visual Studio Code Flux extension. If you are using the extension or Flux and need help, please ask for some in our community site or Slack channel. If you’re developing a cool IoT application on top of InfluxDB, we’d love to hear about it, so make sure to share your story! Additionally, please share your thoughts, concerns or questions in the comments section. We’d love to get your feedback and help you with any problems you run into!

Further reading

While this post aims to provide a comprehensive overview of how to optimize the performance of your Flux queries, the following resources might also interest you:

  1. TL;DR InfluxDB Tech Tips – Optimizing Flux Performance in InfluxDB Cloud: This post describes how to optimize your Flux performance with the Flux Profiler and the Flux VS Code extension.
  2. Use the Flux VS Code extension: The documentation page on how to install and set up the Flux VS Code extension.

Related Blog Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top