Painless WiFi on Arduino
Introduction: Painless WiFi on Arduino
Communication with the outside world is a great feature for Arduino Projects and mandatory for many of them.
In this instructable, we will, painlessly, create an Arduino WiFi project that allows you to send it commands to turn on/off LED's connected to the Arduino from a computer over your WiFi.
The development cycle will be as follows:
1. we will use an Arduino Mega+ESP8266 WiFi board for development and debugging.
2. Once it is working properly we will move that project to an Arduino Uno + ESP8266 WiFi Shield.
You could also use an Arduino Uno+ESP8266 WiFi board at step 2 or any other combination that uses the ESP WiFi shield.
This Instructable is also readily adaptable to other hardware that, like the ESP8266 WiFi uses the main "Serial" device on Arduino to interact with it - e.g. many Bluetooth modules.
If your preference is to jump right in and wish to skip the background, simply jump to the next step and start hooking things up, uploading code and entering commands. You can always come back and revisit this later if you wish to.
There are lots of options for Arduino communications, I often use Wired Ethernet or USB because I have that infrastructure available to me, it is generally easy to use and works for me. But there are times when a Wireless approach would be much easier.
There are plenty of wireless connectivity options available such as LoRa, Bluetooth, IR, WiFi and more. Each of them have different attributes and ideal use cases. Arguably the most ubiquitous is WiFi which is what this article is about.
However, all of the WiFi solutions for Arduino that I have (ESP-8266 solutions) and others that I have read about online seem, IMHO, quite tedious (i.e. 382
painful) to work with. The Arduino project development cycle requires hardware hacks and / or constant switching of DIP switches to work with them.
The tediousness that I allude to is due to the fact that interaction between the Arduino and the WiFi hardware is via the Arduino's "Serial" device. The Arduino Serial device is, by default, used for two things:
* The IDE's Serial monitor for interaction with the program running on the Arduino (e.g. debug messages)
and
* the uploading of new programs to the Arduino.
The shared use of Serial by Arduino and the WiFi is to all intents and purposes a conflict - that is, is Serial being used to upload a new program(/sketch) to Arduino, or is it being used to send commands to the WiFi or is it being used to output debug messages? The solution to this conflict implemented on many WiFi board is to use a series of DIP switches or require additional hardware to resolve the dilemma of what the Serial port is being used for.
This means that when you are developing your Arduino program and want to upload it after making a change, you need to set the DIP switches to a "certain configuration" (Serial device is connected to the USB) to allow the updated program to be uploaded.
When you are done uploading and want to run your new code, you need to set the DIP switches to a different "certain configuration" (Serial device is connected to the WiFi) and reset your Arduino so as to restart the program - or have some other mechanism that causes the program to wait before starting to issue WiFi directives (e.g. a delay to allow you time to reset the switches or the press of a button etc).
Of course if you have debug messages in your program that normally are output to the Serial device and thus visible in the Arduino IDE's Serial Monitor, you are out of luck as the these debug messages will be sent to, you guessed it, to the WiFi device which will unlikely be able to process them.
After testing, you identify a new code change, add a new feature or whatever then need to reset the switches for upload then re-reset the switches to run your modified ode. You need to repeat this cycle every time you change your code - no matter how small the change is.
To me, this is extremely tedious and on some boards, the switching of the switches has a risk of damaging it if you are not careful - or put another way, this, IMHO, is extremely painful!
One option is to use a SoftwareSerial or if you have it, another Serial port (e.g. Serial1 on Leonardo) for debug messages or WiFi interaction, but this would require some sort of converter (e.g. another Arduino, a FTDI USB-Serial converter etc) to connect the SoftwareSerial/Serial1 "port" to a PC and so on. And doesn't resolve the main problem of the main Serial device being used for both program upload as well as communication with the WiFi module. 152