Hello World!

This is the first in a three-part beginner series on using Arduino in home automation projects.

  • The first part is the classic Hello World walk through
  • The second post in the series will show how to publish “Hello World!” to an MQTT Broker
  • The Third will be for the Arduino to read the post from the MQTT broker

If you are not familiar with the overall architecture we are going to build to, or the MQTT Broker please see our previous posts on the subjects.

“Hello World!” is the traditional first program to write on a new platform.  The point of this blog is not to “teach” programming I do believe we would do well to start with a few basics to build upon.  So “Hello World!” it is.

The Arduino hardware we are going to use for this project is the AdaFruit Feather Huzza with 8266 WiFi.  I like this board because it is an all in one board with power management, USB port and the ESP8266 all built into one package.  For more information about how to set up this board for our projects see the Post Titled [TK]

  • Hardware: AdaFruit Feather Huzza 8266 WiFi
  • Environment: Arduino 1.6.8
Before we get started I want to go over a couple of key concepts:

First, there are a few ways to see direct output from an Arduino, one of the easiest is serial output. This is old-school, serial output has been in use for a long time, much longer than the use USB protocol has been around. So, we are going to connect to the Arduino with a USB cable, but you should keep in mind that there is an FTDI (serial standard) to USB adapter built into this particular board. This allows for two-way communication (including programming the board itself), in different Arduino board configurations you may have to supply an adapter. We will go over this in greater detail an upcoming post.

The second thing to understand is why you might want to see this output. This is harder to explain because the reasons will be unique to the project one is working on at the time. For our purposes we will mostly use it for troubleshooting and visualizing what is happening in any code we write. If you do not understand this, that is okay, it will become obvious as we move through the projects.

All the code we are going to write in this demo will be in the Void Setup() function. This function runs once every time the Arduino is turned on.

The first we have to do is tell the Arduino that we want to “begin” a serial monitor and at what speed we want to open it. We do that with the statement
Serial.begin(115200);

Once we do that we can print any text we want to the Serial Port simply by using the command Serial.println(“”); any string can be passed to Serial.println and it will be sent to the serial monitor (we are not limited to strings one could send a variety of datatypes). println sends that data and a linebreak. If a line break is not desired simple use Serial.print(“”);

The completed code will look something like this:

void setup() {

//serial communication at 115200 bits per second is started. this must happen for serial communication to happen.

Serial.begin(115200);

//The following statement will print “Hello World!” to the serial port

//fist line outputted to the serial monitor will be gibberish outputted but the firmware. First we are going to go to the second line.

//Serial.println() adds a line break

Serial.println(” “);

//Serial.print does not add a line break (we will put in the line break.

Serial.println(“Hello World!”);

}

So now that we know how to set up and use a serial monitor in the code, we need a way to make it useful. If you have a favorite serial monitor you can just use that. Just make sure you are listening to the right COM port. If you do not have a favorite serial monitor (and I assume most of us don’t) the easiest monitor to use is the monitor that is built right into the Arduino IDE. To use it either type CTRL+Shift+M, or choose it under the tools menu of the IDE

To recap, the steps are relatively straight forward:

  • Write code in the Void Setup() function of an Arduino sketch
  • Setup the serial monitor
  • Write something to the serial monitor (“Hello World” in this case)
  • Upload the sketch to the micro-controller
  • Wait for the upload to complete and then open the serial monitor (the serial monitor and the micro-controller cannot both have access to the same COM port at the same time, so you have to wait).
  • View the output of the program running on the micro-Controller
Advertisements