The oldest interface on computers and Micro controllers is the serial interface, often featured with RX / TX (= Receive / Transmit) or UART (= Universal Asynchronous Receiver Transmitter). In technical realization, standards were published under the name RS-232 or EIA-485. With initially 75 Baud, the first computers have found a modem connection in the world of telex. Technical progress has then brought transfer rates of 300, 1200, 2400, 9600, 19200 baud to 56kbaud. Then the age started with DSL.
Remaining from the time these interfaces are as a connection between the computers and micro controllers. Because of the absence of the old COM interface on the PC today is a USB adapter with a virtual comm. Port (VCP) needs. The essential block, e.g. Atmega 16U2, CH340 / 341 or FT232 can be installed on the MCU or - for the very small micro controllers - as a separate USB serial adapter, e.g. UART-TTLUSB Adapter 3.3V 5V CH340G Interface or FT232-AZ USB to TTL Serial Adapter for 3.3V and 5V. If necessary, the necessary drivers must be installed under Windows.
With our Micro Controller with ATMEGA 328P, the connections 2 and 3 (top left in the picture) are connected to both chip for the USB interface ATMEGA 16U2 as well as with the pins 0 = RX and 1 = TX. During programming and using the serial monitor during operation, GPIO 0 and 1 may not be used differently. It is best to do not dispense with their use.
In this blog, however, we want to connect Micro Controllers to each other and send data (e.g., sensor data) from an MCU to a second micro controller. The sketch for the sending Micro Controller looks quite easy. The code is identical to that for sending data to the serial monitor. The serial interface is opens and sends data with Serial.Print () or Serial.PrintLn ().
Here is the sketch for the display of rel. Humidity and temperature in the serial monitor, at the same time the "sending" Micro Controller (Download):
For the second micro controller we start the Arduino IDE again.
Attention: To open two different virtual COM ports, the Arduino IDE must be started a second time (instantiated), a new window is not enough.
First, the sketch is uploaded to receive the data, then TX and RX are connected via cross.
Attention: As long as the one or the other micro controller sketches are uploaded, the MCUs must not be connected to each other. Wistance to this we create by the use of software serial. This allows us to receive data at the same time and (if necessary) Display data in the serial monitor. More on that later.
Here is first the circuit diagram:
Due to the different voltages on the micro controller with Atmega 328p (5V) and the D1 Mini (3.3V) I have built a voltage divider with 1 kOhm and 2.2 kOhm between TX at the sending micro controller and RX on the D1 Mini. Alternatively one could have one Logic Level Converter use.
Here are the explanations of the Library SoftwareSerial
As mentioned above, the Arduino hardware has integrated support for serial communication to the PINS 0 = RX and 1 = TX (which also goes to the computer via the USB connection). Therefore, the digital pins 0 and 1 should not be used in the program as far as possible.
Native serial support is made via a hardware (integrated in the chip), which is called UART. This hardware allows the Atmega chip to receive serial communication while working on other tasks as long as there is space in the serial 64-byte buffer.
The program library HardwareSerial is integrated in the Arduino IDE and does not need to be included (exception: ESP32 Serial1 and Serial2)
The software serial library has been developed to enable additional serial communication on other Digduino digital pins, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to initialize several serial software ports with speeds of up to 115200 bps, but always only one time to use. An optional parameter allows inverted signals for devices that require this protocol.
To use this library, it must first be included with:
Then an object with a freely selectable name, often myserial or SoftSerial, with the pins used (in Examples 7 and 8) instantiated with:
It must be noted that the RX pin is interrupt capable.
Finally, the object myserial in the function Void Setup () Started with the desired speed (baud rate) (Baudraten of 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 are permitted):
Further explanations and above all restrictions can be found at https://www.arduino.cc/en/Reference/SoftwareSerial
Here is an overview of the methods / functions (with link to the Arduino documentation):
And here comes the sketch for data reception (Download):
Notes on this sketch:
The data is received as a single character in ASCII format. For further processing, we first need the concateation (= juxtaposition) of the characters to a string. As long as the end of the line (\ n) is not reached, the individual characters (Data) will be followed by conversion Char () to the string dataset Composed.
If the received character is equal to the line break (\ n), the jump takes place Else With the output of the string dataset in the serial monitor, storing the string in a second variable dataset (with the big S) for further processing, as well as resetting dataset to the empty string for the renewed reception of data.
Of course you can set the complete string with an ESP micro controller as a web server to the domestic WLAN and display completely in the browser of the PC or smartphone. However, if you want to process the values for temperature and rel. Humidity (engl. Humidity), the string must be disassembled and the characters are converted into the respective numerical values with decimal places (float).
Lastly, the question remains which values we each for from and to Insert to get the two part strings so that we then in the floating point numbers (= floating point!) Can convert.
In the first attempt I have counted the characters. The count of the index starts at 0, from If the index is included, from which the part string begins, to The index for the end excluding, so the first sign after. This works as long as the values for the temperature and the humidity have two digits before the comma. Single-digit temperatures or minus signs cause the counting close. So we need reference points for the indexes. I decided to determine the index for T and H, because the functions for characters (Isalpha (), Isalphanumeric (), Isascii (), IsControl (), ISDIGIT (), isgraph (), IshexadecimalDigit (), IslowerCase (), ISSPRINTABLE (), ISPUNCT (), Isspace (), Isuppercase (), ISWHITESSPACE () ) Unfortunately were unsuitable for this purpose.
So I have programmed a for-loop, which is checked by index = 0 to the last place (function length ()), whether Charat (i) is the same 't' or 'h'. (Attention: Char requires 'simple quotation mark') Now I had a reference for my part strings. Of course, the exact values depend on the transmitted string.
Here is the complete sketch (Download):
I hope these little tips and tricks around data transfer and string manipulation are helpful. I myself had to tinker a whole while. The overall result of our attempts, including the data issue in the WLAN, can soon be in the ebook to our Smart Home Starter Set read.