You can compile the arduino code using the Arduino CLI
Next, you can work on this application using the Arduino IDE or the Arduino VSCode Extension. Using the VSCode extension you will need to run the Arduino: Initialize command to configure the IDE to be aware of the relevant libraries - For the board I am using I will select NodeMCU 1.0 (ESP-12E Module) thereafter select the Serial Port that your device is connected on via USB as well as selecting the sketch with the Arduino: Select Sketch command
To compile the code that will run on the device you can use the following commands:
The above commands provide a basic outline for running your board and the relevant tasks associated. You will only need to run the installation once, and the compiliation is not needed if using arduino-cli upload as it will upload the files as well
Note you can also do all of the above using the Arduino VSCode Extension
Install The Board
Before you can do anything else, you will need to install the board
First, define a config file for the Arduino CLI called .cli-config.yml with the following in it:
Then, install the board using this config:
Using the Serial Monitor
Note that you will have to set the Baud rate when connecting to a device using the serial monitor in order to view the output
You can connect the Serial Monitor to the device as follows:
Note that the baudrate needs to be the same as what is configured during the device setup function using Serial.begin
Connecting the serial monitor like this should also cause the device to restart so you should be able to get the from the start. Note that you will have to kill this process if you want to upload any code to the device again
To use the Arduino Extension’s Serial Monitor you can simply connect to the device. In the event you want to capture output from the moment the device turns on you can press the RST button on the device while the Serial Monitor is connected which wil allow you to connect as soon as the device starts up. This is necessary since when the device is being flashed the serial monitor must be disconnected
Examples
Note that when switching sketches you will need to use the VSCode Extension’s Arduino: Select Sketch and thereafter the Arduino: Initialize and Arduino: Rebuild Intellisense Configuration command to configure VSCode to register the installed libraries
For the sake of example, I will have all my sketches contained in a sketches directory with a directory for each sketch (as is required by Arduino)
Blink
This is effectively the standard getting started hardware programming task - blinking the light on the board. For this example create the sketches/beep/beep.ino file:
Provided you have the board connected via USB, you can then run it as follows:
This is also a good opportunity to connect the Serial monitor to view some output from the device using the previous command
Beep
For this example we will play a short sound on the device when the setup hook runs using the builtin tone function. Add the following content to a file with the path esp-8266/sketches/beep/beep.ino:
This can then be uploaded and run on the device using:
WebServer
For this example we will create a small application that responds to HTTP requests and can be accessed from other devices on the same network:
First, define the WiFi connection details by adding a config.h in the project directory with the following:
Then, the code for the Web Server will look like so:
You can then upload and run it on the device using:
And thereafter you can connect the serial monitor as normal. This is necessary since we want to view the IP adress of the
WebSocket
For this example we will create a small application that connects to a WebSocket server and prints out the received messages:
Before running the following, you will need to install the ArduinoWebSockets library using the Arduino CLI
Next, define the config by adding a config.h in the project directory with the following:
And the code for the application can be seen below:
Then, you can run the following command to upload and run the code on the device
And thereafter you can connect the serial monitor as normal to view any logs from the websocket response
Screen
This one was a bit more complicated since it required a little more info, the following youtube videos were really handy in explaining the hardware setup process in more detail:
We also need a few libraries which we can install with:
For running the display there are two things we need to do
Get the Screen Address
Firstly, we need to figure out the screen address, this can be done by using the i2cdetect library using their example as-is. This is just needed the first time we use the screen:
You can run this with:
Then, we need to connect the Serial Monitor and view the output. In the output we should see a value that is active, in my case C3:
This is the value we will use for the Screen Address in the next setup
Write Some Stuff
To write stuff to the screen we can use the Adafruit sample from the library as a base, I’ve simplified it to just print a message on the screen to demonstrate the basic usage: