ESP8266 – Serial Communication
Today we will talk about an extremely powerful tool in the use of microcontrollers. The Serial communication, specifically the USART (Universal Synchronous Asynchronous Receiver Transmitter) standard.
The system works using two wires. RX (Receiver) and TX (Transmitter), connecting two devices. The RX of one is connected to the TX of the other. If the choice is for a synchronous connection, it may be necessary to add one or two more pins to operate as a “traffic light”. But most current microcontrollers can operate asynchronously, which saves us the expense of pins. Data is sent, as the name implies, in a series of bits.
ESP8266 provides us with two ports, one of them converted to USB in the NodeMCU module.
Where To Buy? |
---|
No. | Components | Distributor | Link To Buy |
1 | ESP8266 | Amazon | Buy Now |
Applications
The range of uses of serial communication is limited only by creativity. Here I will mention three more general scenarios and detail some applications within them.
1. Communicating with computers
With a USB cable, we were able to connect the NodeMCU with a computer via Serial.
Arduino IDE already gives us the first use. With Serial Monitor, we can send commands or debug the functioning of our code.
But the integration with other software is pretty powerful. Imagine, for example, access control in which a circuit with ESP8266 reads an RFID card and sends it via Serial to a Permission Validation System. Or even an LED panel that receives a software display text.
2. Communicating with microcontrollers
Probably the most common application. Whether to integrate into a preexisting circuit or to distribute functionality between microcontrollers.
This integration can be done between different microcontrollers. As long as they operate with the same voltage (3.3V) or use a level converter.
An application example is to integrate the ESP8266 wifi with the analog ports of an ATMEGA328 to build an IoT toxic gas sensor. We already know that the ESP8266's analog port is quite inefficient. Then it is possible to perform the analog reading on the ATMEGA328 and send the information to ESP8266 by Serial.
3. Communicating with industrial machines
This, without a doubt, is the most interesting way to integrate industrial machinery with WEB systems. The older industrial equipment that allows some automation, provides RS232 or RS485 ports for integration with PLCs.
In these cases, the commands are pretty plastered, but hopefully, well documented. The voltage level is 12V or 24V, but there are converters and logic levels to solve this.
The industry 4.0 paradigm has been operating to make this kind of integration. Some PLCs are already developed with wifi modules. And on that account, circuits with the ESP8266 have the immense advantage of low cost.
The vision behind this is to be able to remotely monitor or control an entire industrial plant. Tracking KPIs for predictive maintenance, doing recalibrations, and managing production.
Main Functions
The Serial library has a good variety of functions for Serial communication, but most are for very specific uses. We will discuss the most common ones and may return to others in the future as needed by the projects.
Serial.begin()
The first function to be used. It initializes Serial communication informing the data transfer speed (in bits per second) and, optionally, a configuration parameter.
By default, Serial is configured to send data in 8-bit packets plus a terminator, not including parity. The best way to imagine this communication is to imagine a train, where each byte (8 bits) sent is a car, and the terminator is the connection between them. This standard is used in most devices, but if you need to integrate a device with another standard, the config parameter allows you to change the number of bits in the packet (from 5 to 8), the number of stop bits (1 or 2 ) and enable or disable parity (a packet integrity check).
The speed uses some preset values. The fastest that remains stable for the ESP8266 is 115200 changes per second.
So we could start with:
Serial.begin(155200)
The function below presents the same result, making the config parameter explicit.
Serial.begin(115200, SERIAL_8N1)
Serial.available()
The function returns an integer with the number of bytes available in the read buffer. The maximum of bytes in the buffer is 64.
This function is very useful for monitoring incoming information.
Value = Serial.available()
Serial.read()
The function returns the most recent byte in the input buffer and removes it from the buffer. It is ideal if your communication is byte-to-byte. For example, if you are receiving input from a keyboard, the function would return the key you typed.
It returns an integer with byte or -1 if no data is available.
Serail.readString()
This function is best suited for reading strings. Like words. It is the equivalent of calling the read() function continuously until it reads all the information from the buffer. The function returns a string with the data.
Serial.print() and Serial.println()
The two functions are very similar. It takes a value and sends it serially in ASCII format. It is also possible to define the numerical base before sending (binary, decimal...) and the number of decimal places.
The function can be used either in the format:
Serial.print(value)
And the format:
Serial.print(value, format)
The table below presents some usage examples.
The println function works pretty much the same, but it adds the return character ‘\r’ and newline ‘n’ at the end of the packet. It's the equivalent of typing in a text editor and pressing the "Enter" key.
Serial Monitor
The Arduino IDE provides a very powerful tool for debugging and testing Serial communication. The Serial Monitor.
The tool is pretty intuitive, but let's take a look at its fields.
- Sending data: This allows us to send commands directly to the microcontroller
- Autoscroll: Very useful when we've already received enough information to fill the screen
and don't want to move it down manually.
- Terminator: Choose whether or not to include the new line and carry return characters at the end of the message before
- BaudRate: Defines the communication It must be the same as the microcontroller, or
packet loss or character misreading problems will occur.
Controlling and monitoring an LED
Let's make a simple code to control and monitor the NodeMCU LED from the Serial monitor.
The code will monitor the Serial, and each time it receives the command “ON”, it will turn on the LED, when it receives “OFF”, it will turn off the LED when it receives “STATUS”, it will return the status of the LED in the Serial.
We will create three functions to perform the actions.
- turnOn() : To turn on the
- turnOff() : To turn off the
- statusLED() : To read the pin status and return information in the serial.
We initialize the Serial in the Setup function.
In the loop() function, we check if there is any data in the input buffer of the serial, if there is, it saves the buffer values in the variable “payload”.
Finally, we check the payload value to decide the action.
Here it is important to note that we use an equality comparison and that “ON” is different from “ON “. For this reason, when sending the information through the Serial Monitor, we choose the “No line ending” option.
And so is our final code. Compiled, written to nodeMCU. Open the Serial Monitor, remember to put the correct baud rate and the "No Line ending" and send one of our 3 commands.
This is simple code, but very powerful.
So, that was all for today. I hope you have enjoyed today's tutorial. If you have any questions, please ask in the comments. Thanks for reading.
Skills & Attributes Needed In Engineering
If you are planning on a career as an engineer, it is important that you are aware of the various skills and attributes that employers look for. This is a terrific field to work in with highly rewarding and valuable work, high earning potential, and great job opportunities, but this is also one of the more challenging industries to crack, and there are many skills and attributes that are required. These are skills and attributes that can be developed, though, so if you lack in any, then you should be able to find ways to develop and improve. Read on to discover the skills and attributes that employers are looking for in engineering.
Hard Skills
Obviously, hard skills are essential in a field that is so technical. The hard skills that you need will depend on the field of engineering that you are entering but could include skills such as:
- Programming
- Computer science
- Statistics
- Structural analysis
- Data modeling
- System design and analysis
These are hard skills that you can learn in school or teach yourself. These are hard skills that need to be strong skills, so you need to really focus on these areas and ensure that you have a high level of knowledge when looking to enter the field.
Problem-Solving
Engineering is, essentially, problem-solving, so it is vital that you are able to assess situations, determine problems and find the best solutions. There are many issues and problems that can arise during a project, so employers will want to see that you are able to solve problems effectively and efficiently. You can develop your critical thinking skills to become a better problem-solver and find the best solutions to issues that you might face.
Teamwork
Engineering is all about teamwork, so you need to be able to work with others and understand the importance of playing your role. Engineers rarely work alone, and you will usually be working with colleagues and other professionals, so it is important that you are a team player and a strong communicator in order to find success and to stand out to employers.
Communication Skills
Leading on from this, engineers need to have strong communication skills, and this is sometimes an overlooked aspect. Having hard skills is vital, but you will not get very far if you are not a good communicator as you will constantly be talking to people throughout the day. You need to be able to listen to the needs of the client, clearly present your ideas, work as part of a team and also be capable of leading on projects.
Additionally, having good communication skills will help you to develop a positive reputation and form strong relationships. As with any industry, this is crucial for getting ahead and finding new opportunities. It is true that some people are better communicators than others, but it is possible to improve your communication skills with research and practice, and this should improve your life in many ways.
Project Management
You also need to be able to manage projects if you want to rise to leadership positions in engineering. This will involve managing teams and integrating external professionals while delivering projects on time and within the pre-agreed budget. Project management is a difficult skill to master, which is why many aspiring engineers find it useful to take project management training. For engineers, Six Sigma training is popular and will help to develop the project management skills that you need to find success in engineering.
Innovation
In order to find success in engineering, you need to be innovative and creative. Engineering is always about improving and making things better, so you need to be able to innovate and use creativity to constantly be improving and coming up with the best solutions. In order to be innovative, you need to think outside of the box, stay current with technology, continue to learn, and constantly push yourself to think bigger.
Attention To Detail
Engineers need to be able to see the big picture and be innovative, but you also need to have excellent attention to detail. It is often the small things that matter in engineering, as a slight miscalculation can cause the whole project to fail, and this could even cost lives. This is why you need to be meticulous in your work and take pride in your attention to detail. In order to be meticulous in your work, you need to have high concentration levels and be able to focus intensely. This also means that you need to know how to disconnect from work, recharge your batteries and unwind so that you can give it your all each day.
Engineering Management
If you have aspirations of climbing the ladder and rising to leadership positions, you need to have specialist engineering and management skills that will help you to lead teams, deliver projects and run a profitable business. You can get a master's in engineering management at established schools like the University of Ottawa, and this will teach you all that you need to know to excel in engineering management. You will learn about key aspects such as budgeting and finance, people management, leadership, product innovation management, AI-driven decision making, and operations management, just as a few examples.
You can also earn a masters in engineering management entirely online. This can make it easier for people to fit into their lifestyle and allows you to start using what you learn straight away to stand out and start to climb the ladders.
Committed
Engineering does not work that you can succeed with if you are not committed. With engineering projects, you may have to spend long periods of time away from home and be on call 24/7, so it will inevitably have a big impact on your lifestyle. This means that you need to be committed to the cause and willing to put your job first. Working as an engineer will have an impact on your personal life, but it is also working that is hugely rewarding and can be lucrative, so this is something that you will need to weigh up. While it can be demanding, you may also benefit from quieter periods where you can spend much more time at home and with loved ones too.
Resilience
Another part of the job that you need to be prepared for is setbacks. When you are working on a major project with lots of moving parts, it is inevitable that there will be setbacks along the way. This can be incredibly frustrating, so you need to be resilient in order to not allow these setbacks to put you off and so that you can simply continue working and find the best solution to keep the project moving forwards at all times.
Pressure-Management
Leading on from this, you also need to be able to handle pressure and be able to work to a high standard in stressful situations. Working on large projects involving lots of money and tight deadlines creates tense working conditions, and you will have people breathing down your neck, so you need to be able to handle this and prove yourself to be reliable under stress. This is something that is developed with experience, but you can also develop your abilities to work under pressure by improving your work ethic, setting yourself targets, and breaking tasks down into smaller tasks.
Commitment To Learning
Even with a master’s in engineering management, there is still a lot to learn, and you need to be committed to learning in order to find success as an engineer. This is because this is a field that is constantly going through change, so you need to be able to stay current and up to date with the latest trends and technological developments. You can do this by attending industry events, networking, reading publications, taking further courses, engineering blogs and podcasts, and research.
Positive Mindset
Finally, you will need a positive mindset if you want to be an engineer. You will be facing a lot of negativity in your work with setbacks, stressful working conditions, and tight deadlines, so you need to be able to maintain a positive mindset and this is something that employers will always be looking for. Those with a positive mindset will be able to stay motivated, work to a high standard each day and help to lift the overall atmosphere. Crucially, to have a positive mindset, you need to be able to lead a healthy lifestyle, switch off after work and find ways to enjoy yourself. Having a positive mindset also requires being in the right job, so if you find that you are not happy in your work, then you may want to consider a change.
These are the main skills and attributes that are needed to find success in engineering. If you do not possess these, they can be developed, and it is worth the effort as this will help you to stand out to employers and find work.