Datenübertragung von Mikro Controller zu Mikro Controller

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):

 #include "dht.h"
 
 #define dhtpin 2// GPIO PIN for Data
 #define DHTTYPE DHT22 // DHT22 (AM2302)
 Dht dht(DHTPIN, DHTTYPE);
 
 void set up(void) {
   Serial.Begin(9600);
   Serial.Println(F("DHT22 Test!"));
   dht.Begin();
 }
 
 void loop(void) {
   float H = dht.Readhumidity();
   float t = dht.Read Temperature();
   Serial.print(F("T ="));
   Serial.print(t);
   Serial.print(F("° C H ="));
   Serial.print(H);
   Serial.Println(F(" % "));
   delay(1000);
 }

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:

 #include 

Then an object with a freely selectable name, often myserial or SoftSerial, with the pins used (in Examples 7 and 8) instantiated with:

 SoftwareSerial myserial(7,8,);     // RX, TX

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):

 myserial.Begin(9600);

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):

SoftwareSerial(), Available(), Begin(), Islistening(), overflow(), peek(), reading(), print(), Println(), list(), writing()

And here comes the sketch for data reception (Download):

 #include 
 SoftwareSerial SoftSerial;
 
 // Global Variables to Store The Incoming Data
 int dateness = 0;
 String dataset="";
 String dataset="";
 
 void set up() {
   // Begin Serial Comms with 9600 Baud
   Serial.Begin(9600);
   SoftSerial.Begin(9600, Swserial_8n1, 4, 5, false);   // rx = 4, tx = 5
 }
 void loop() {
   // When Data Are Available ...
   IF (SoftSerial.Available() > 0) {
       // Read Incoming Data
       dateness = SoftSerial.reading();
     // Data format is ascii (0..255),
     // therefore conversion to char ()
       IF (Char(dateness) != '\ n') {
       dataset += Char(dateness);
    }  
     Else {
       Serial.Println("DataSet");    
       Serial.Println(dataset);
       Serial.Println();
       dataset = dataset;      
       dataset="";
    }
  }
 }

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).

For this we need the functions Substring () and TOFLOAT (). The corresponding lines in the code are:

 String Tstring = dataset.substring(from, to);
 String HString = dataset.substring(from, to);
 T = Tstring.tofloat();
 H = HString.tofloat();

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.

 for (int I=0; I<=dataset.length(); I++)  
    {
       IF (dataset.Charat(I) == "T ') index = I;
       IF (dataset.Charat(I) == 'H') indexh = I;          
    }

Here is the complete sketch (Download):

 for (int I=0;I<=dataset.length();I++)  
 #include
 SoftwareSerial SoftSerial;
 
 // Global Variables to Store The Incoming Data
 int dateness = 0;
 String dataset="";
 String dataset="";
 int index;
 int indexh;
 float T;
 float H;
 
 void set up() {
   // Begin Serial Comms with 9600 Baud
   Serial.Begin(9600);
   SoftSerial.Begin(9600, Swserial_8n1, 4, 5, false);
 }
 void loop() {
   // When Data Are Available ...
   IF (SoftSerial.Available() > 0) {
       // Read Incoming Data
       dateness = SoftSerial.reading();
     // Data format is ascii (0..255),
     // therefore conversion to char ()
       IF (Char(dateness) != '\ n') {
       dataset += Char(dateness);
    }  
     Else {
       Serial.Println("DataSet");    
       Serial.Println(dataset);
       Serial.Println();
       dataset = dataset;      
       dataset="";
       DataConversion();
    }
  }
 }
 void DataConversion() {  
   for (int I=0; I<=dataset.length(); I++)  
    {
       IF (dataset.Charat(I) == "T ') index = I;
       IF (dataset.Charat(I) == 'H') indexh = I;          
    }
     String Tstring = dataset.substring(index+4, indexh-5);
     String HString = dataset.substring(indexh+4, indexh+9);
     T = Tstring.tofloat();
     H = HString.tofloat();
     Serial.print("T =");
     Serial.Println(T);
     Serial.print("H =");
     Serial.Println(H);    
     Serial.Println();
 }

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.

Esp-8266For arduinoProjects for beginners

2 comments

Bernd Albrecht

Bernd Albrecht

Hallo, Herr Schulz,
vielen Dank für Ihr Interesse an unseren Produkten und Blog-Beiträgen. Gerne nehmen wir auch Anregungen auf oder schreiben über „Ihr Projekt“. Bitte haben Sie Verständnis, dass wir keine maßgeschneiderten Auftragsarbeiten annehmen können.
Zu Ihrer Anfrage teile ich Ihnen folgendes mit:
zu 1. Kennen Sie eigentlich die Suchfunktion auf der Seite von AZ-Delivery.de? Ich habe das Wort Ethernet eingegeben und erhalte eine Registerkarte „Products“ (12) und auf Mausklick gelange ich zur zweiten Registerkarte „Pages“ (18). Hier finden Sie die Beiträge zu den Ethernet-Shields und – Modulen.
zu 2. Für das Senden von Befehlen (z.B. Datenabfrage) an den Micro Controller empfehle ich auch die Arduino IDE. In der obersten Zeile können Sie dann z.B. T für Temperatur und P für Luftdruck (pressure) eingeben und auf Senden drücken. Mit der Befehlszeile Serial.read() im Sketch lesen Sie den gesendeten Buchstaben und können dann mit einer if-Abfrage den gewünschten Wert senden. Für die Aufzeichnung größerer Datenmengen benötigen Sie ein Speichermedium. Produkte und passende Blog-Beiträge habe ich über das Wort „Daten“ im Suchfenster erhalten.
zu 3. Zum Thema Anzeige von Messdaten im heimischen WLAN haben wir ebenfalls in der Vergangenheit bereits im Blog berichtet. Ein weiterer Beitrag befindet sich im Vorlauf und sollte zeitnah erscheinen.
zu 4. Um einfache Emails zu versenden, benötigen Sie ein Modul für eine SIM-Karte. Ich selbst habe ein GPS-Modul verwendet, um Positionsdaten zu senden. Aber es gibt auch ein einfaches GSM-Modul im Sortiment.

Thomas Schultz

Thomas Schultz

Ich finde die Idee des Blogs wirklich toll, abr leider ist bis jetzt nicht ein Projekt aufgetaucht,
das mich interessieren würde. Diese Beitrag mc zu mc ist zwar etwas in dieser Richtung, aber immer noch nicht, was ich bräuchte. Was ich mir wünschen würde wären folgende drei Beispiele:
1. Der Einsatz eines LAN-Shields
2. Die Steuerung eines Arduino via USB vom PC aus. Daten- und Befehlsaustausch.
Der arduino soll Daten sammeln und auf Anforderung an den PC schicken oder auf der Platte ablegen.
Und, es sollten vom PC aus Befehle (z.B. via DOS-Zeile) an den Arduino geschickt werden.
3. Die Steuerung z.B. eines Relais via Webseite ist ja bereits behandelt worden. Was mich aber interessieren würde ist, wie ich permanent auf der Webseite einen Meßwert anzeigen lassen kann.
Aktuelles Anwendungsprojekt: Ich möchte gerne den ELV-Gewitterwarner mit einer Logik versehen und aus der Ferne ablesbar machen.

Und zum Schluss noch eine Frage: Kennt ihr eine Möglichkeit um einen Schaltzustand per Emails oder noch besser als Pushnachricht an mehrere Freunde schicken kann.

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN