Notifications
Clear all

Visual Studio IDE for the Dronebot

8 Posts
2 Users
2 Reactions
456 Views
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Introduction

For a long time I labored over how to write this post; trying to present the technique without drifting into excessive explanation. I finally gave up and decided to leave the explainations to follow-up posts (if asked). If I have to argue for the concept then, as Ben Saks says, "you're losing."

Goal

On my Windows 11 system, I use Visual Studio IDE (n.b., not Visual Code) as my primary development tool. I'd like to use Visual Studio to build Arduino/ESP32 programs that mimic the Arduino IDE; that is, edit, compile, link, upload, and monitor the program on the board. I'd like to use the Visual Studio solution/project manager, editor, and source code browser as well. Everything else is a bonus.

Prerequisites

Arduino IDE installed

Visual Studio IDE installed

Arduino-cli

You have to download the Arduino's command line tool (arduino-cli) and install it on the system.  Arduino provides an installation file (Windows msi) to do this. See Installation | Arduino Documentation

fig 1

Syntax Highlighting

You can get the benefits of the Visual Studio editor even if you don't build the project with it. Open Visual Studio and select Tools - Options item. This brings up the Options dialog. Expand the "Text Editor" item and select the "File Extensions" item.

fig 5

Enter the 3-letter extension (ino) and select the editor to use (Microsoft Visual C++) and press the Add button.

fig 4

Now when you open an *.ino file in Visual Studio you see the desired syntax highlighting.

fig 6

Project Settings

I assume you know how to setup an empty project in Visual Studio. We want to able to build the program in both Visual Studio and the Arduino IDE. The Arduino IDE requires that the sketch files are in a folder with the same name as the sketch source file. Visual Studio will create the folder when we create the project using the project name. I'll use the project vsBlink to demonstrate.

Open the Arduino IDE and open the Blink example. Save the file to the same folder as in Visual Studio vsBlink project and rename Blink.ino to vsBlink.ino.

In Visual Studio, right click on the project and select Add - Existing item and select the vsBlink.ino file and the text file Blink.txt

project

You want to change all the source file properties to exclude them from the build. Yes, exclude. Everything is done in the pre and post build events

We will edit two build events: Pre-Build Event and Post Build Event

Right click the project and select Properties to bring up the project properties. dialog:

fig 8

For the Pre-Build Event command line, add the following:

arduino-cli compile --no-color --warnings=all --clean -e -b esp32:esp32:esp32

You may need to change the fqbn to match the board you're using.

For the Post Build Event, add the command line

if NOT errorlevel 1  (
    arduino-cli upload --no-color -b esp32:esp32:esp32 -p COM3
    if NOT errorlevel 1  (
        echo Monitor %(Filename) ...
        start /d "$(ProjectDir)" /wait arduino-cli monitor -p COM3 -c baudrate=115200,bits=8,parity=none,stop_bits=1
    )
)
fig 9

You may need to change the fqbn and com port to match the board and port you're using.

That's all you need to build the project using the Visual Studio "Build" command. When you do you should see something like:

Build started at 10:24 PM...
1>------ Build started: Project: vsBlink, Configuration: Debug x64 ------
1>Compile vsBlink ...
1>...\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\cores\esp32\esp32-hal-uart.c: In function 'uartSetPins':
1>...\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\cores\esp32\esp32-hal-uart.c(153,9): warning GDBCCDD98: 'return' with no value, in function returning non-void
1>         return;
1>         ^~~~~~
1>...\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\cores\esp32\esp32-hal-uart.c:149:6: note: declared here
1> bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
1>      ^~~~~~~~~~~
1>Sketch uses 237117 bytes (18%) of program storage space. Maximum is 1310720 bytes.
1>Global variables use 21048 bytes (6%) of dynamic memory, leaving 306632 bytes for local variables. Maximum is 327680 bytes.
1>Upload vsBlink ...
1>esptool.py v4.5.1
1>Serial port COM3
1>Connecting.....
1>Chip is ESP32-D0WD-V3 (revision v3.1)
1>Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
1>Crystal is 40MHz
1>MAC: e4:65:b8:48:3e:64
1>Uploading stub...
1>Running stub...
1>Stub running...
1>Changing baud rate to 921600
1>Changed.
1>Configuring flash size...
1>Flash will be erased from 0x00001000 to 0x00005fff...
1>Flash will be erased from 0x00008000 to 0x00008fff...
1>Flash will be erased from 0x0000e000 to 0x0000ffff...
1>Flash will be erased from 0x00010000 to 0x00049fff...
1>Compressed 18992 bytes to 13110...
1>Writing at 0x00001000... (100 %)
1>Wrote 18992 bytes (13110 compressed) at 0x00001000 in 0.4 seconds (effective 400.6 kbit/s)...
1>Hash of data verified.
1>Compressed 3072 bytes to 146...
1>Writing at 0x00008000... (100 %)
1>Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.1 seconds (effective 452.3 kbit/s)...
1>Hash of data verified.
1>Compressed 8192 bytes to 47...
1>Writing at 0x0000e000... (100 %)
1>Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 711.3 kbit/s)...
1>Hash of data verified.
1>Compressed 237488 bytes to 130711...
1>Writing at 0x00010000... (12 %)
1>Writing at 0x0001d273... (25 %)
1>Writing at 0x00024369... (37 %)
1>Writing at 0x00029537... (50 %)
1>Writing at 0x0002eb93... (62 %)
1>Writing at 0x00037102... (75 %)
1>Writing at 0x0003f145... (87 %)
1>Writing at 0x000447c4... (100 %)
1>Wrote 237488 bytes (130711 compressed) at 0x00010000 in 2.0 seconds (effective 957.9 kbit/s)...
1>Hash of data verified.
1>
1>Leaving...
1>Hard resetting via RTS pin...
1>New upload port: COM3 (serial)
1>Monitor vsBlink ...
1>Done building project "vsBlink.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 10:25 PM and took 54.030 seconds ==========

If the compilation fails, the prebuild event will fail and stop the build.

If you're not connected to the board. the post build event will fail.

If you're connected to the board then a monitor window will appear showing the program output:

Monitor port settings:
  baudrate=115200
  bits=8
  dtr=on
  parity=none
  rts=on
  stop_bits=1

Connecting to COM3. Press CTRL-C to exit.
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13964
load:0x40080400,len:3600
entry 0x400805f0

Press CTRL-C to exit the monitor.

Conclusion

I find this easy to do. It's not perfect but is more than sufficient for my needs. Its benefits far outweigh its deficiencies. I get the best of both worlds with it.

YMMV


The one who has the most fun, wins!


   
robotBuilder reacted
Quote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Correction:

I misattributed the quote "you're losing" to Ben Saks. It actually comes from his father, Dan Saks, from his CppCon talk "CppCon 2016: Dan Saks “extern c: Talking to C Programmers about C++” . Dan spent many years writing about embedded programming and knows what he's talking about. The talk above is well worth watching.


The one who has the most fun, wins!


   
robotBuilder reacted
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2493
 

@tfmccarthy

Dan Saks' CppCon talk is worth watching for the insights into the psychological make up of human beings.

The hobby level programming I do is only as a part time adjunct to all my other interests and have found I need to keep it simple and readable. Learning to use a complicated IDE is not fit for my purposes when a simple language and a text editor does the job.

I find this easy to do.

I would suggest you find what you do easy because you have spent a lot of time learning advanced C++ programming techniques and using complex development environments?

 



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

@robotbuilder 

Learning to use a complicated IDE is not fit for my purposes when a simple language and a text editor does the job.

That's what the "YMMV" was for. If the Arduino IDE fits your needs, then you're all set.

However, I think there's interest in this forum for other IDEs. Enough interest that Bill made a video on using the PlatformIO alternative (see below). I counted 10 separate discussions on PlatformIO and 7 pages of posts that discuss it.

I think an argument can be made that the complexity of IDE for VS Code + PlatformIO and Visual Studio IDE are comparable.

The dronebot who already has Visual Studio IDE and the Arduino IDE installed might feel a bit neglected. That's what my post addresses. I've leveraged my experience using that IDE to show others how to easily build their Arduino/ESP32 projects using the "other" VS IDE. As a bonus, it's all free. Including an AI too!

DroneBot Workshop Forum Posts on PlatfromIO

  1. Getting Started with PlatformIO – 2021 Videos – DroneBot Workshop Forums
  2. Adding Libraries in PlatformIO (VS Code) – Tools – DroneBot Workshop Forums
  3. How to setup debugging in VScode using PlatformIO for the various Arduino boards – Archived Suggestions – DroneBot Workshop Forums
  4. PlatformIO fejl – Components & Programming – DroneBot Workshop Forums
  5. Please help me resolve this platformio error – General Discussions – DroneBot Workshop Forums
  6. test code errors in platformIO – Arduino – DroneBot Workshop Forums
  7. C++ / Platformio library – C++ – DroneBot Workshop Forums
  8. Arduino vs. PlatformIO – Arduino – DroneBot Workshop Forums
  9. I2C Part One tutorial and Slave demo sketch for PlatformIO – Arduino – DroneBot Workshop Forums
  10. using nodeemcuv2 board in PlatformIO, problem with monitoring speed – ESP32 & ESP8266 – DroneBot Workshop Forums

The one who has the most fun, wins!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2493
 

@tfmccarthy

However, I think there's interest in this forum for other IDEs.

Indeed there is! Thus the reason I liked your posts. Both valuable inputs. It was a lament I wasn't able to achieve such high levels with C++ and using those IDEs. It was all too time consuming and perhaps I lacked the smarts but despite that and within my limited free time and skill levels I still have had decades of  fun programming and playing with electronic hardware.

Actually I didn't know what the acronym YMMV meant and tend to ignore them.

 



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Posted by: @robotbuilder

I still have had decades of  fun programming and playing with electronic hardware.

That's the most important part.

Also, point taken about acronyms. I will try to be more careful with them in future.


The one who has the most fun, wins!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2493
 

@tfmccarthy

Acronyms are useful and common within a community to show that you are one of them but it helps outsiders if the full word version be given as a reference at the start of a text.

I just downloaded Visual Studio Code so maybe I will find the time to try it with some Arduino C++ example code.



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Posted by: @robotbuilder

I just downloaded Visual Studio Code so maybe I will find the time to try it with some Arduino C++ example code.

::boggle::

Good for you. No, seriously, good for you.

Even if it doesn't pan out, I'm impressed.


The one who has the most fun, wins!


   
ReplyQuote