AVR goes ESP mit MicroPython - Teil 2

This post is also available as PDF file for download.

Micropython on the ESP32 and ESP8266

Partytime is the order of the day. The AVRS are made fine to introduce yourself to the ESP clan. Of course, we are very exclusive paparazzi who follow the events from the front row. Welcome to

AVR goes ESP8266/32 (part 2)

Figure 1: LDR, NRF24L01 and Atmega328 microcontroller

Figure 1: LDR, NRF24L01 and Atmega328 microcontroller

In the previous episode we pretted ESP32 and ESP8266 for the reception. Today the AVRS are on. All ATMEGA328 controllers known to me have a SPI bus. This connection can be used to program the chips native. Before an AVR can be programmed from the Arduino IDE via USB/RS232, a boot loader must be given him and that is exactly what happens via the SPI bus. The boards that we want to use today already have this boot loader, so we can all program via the Arduino IDE and the USB connection and use the serial monitor for control and results. For the Arduino Pro Mini, a USB-RS232 TTL adapter is also necessary. We need the SPI bus to dock the NRF24L01 to the microcontroller, as we did in the last episode in the ESP8266.

Figure 2: Atmega328 microcontroller AZ-Delivery

Figure 2: Atmega328 microcontroller AZ-Delivery

Figure 3: Nano V3

Figure 3: Nano V3

Figure 4: per mini with Atmega328

Figure 4: per mini with Atmega328

Figure 5: USB-RS232 adapter ftdi232RL

Figure 5: USB-RS232 adapter ftdi232RL

The hardware

Components from the first episode

1

ESP8266 Node-MCU or

1

ESP32 Node-MCU or ESP32 D1 Mini

1

FDTI-USB-RS232-TTL adapter

2

NRF24L01+

2

NRF24L10+ Breadboard adapter

1

LDR With 10kΩ trimpoti or

1

LDR module

2

Minibreadboards

 

Components for the current episode

1

ATMEGA328 microcontroller or

Nano v3 or

Per mini

1

Breakboard

various

Jumper cable

2

Matching USB cable

1

Possibly battery 4.5V or 5V plug-in power supply

The follow -up assignments for the three AVRs can be seen from the following circuit diagrams. Simply select a circuit as desired. Because the chip (Atmega 328p) is the same for all, there are no differences in the program.

Figure 6: NRF24L10 am ATMEGA328 microcontroller

Figure 6: NRF24L10 am ATMEGA328 microcontroller

Figure 7: circuit with Nano V3 - 5V supply

Figure 7: circuit with Nano V3 - 5V supply

Figure 8: circuit with per mini - 3.3V from the FTDI adapter

Figure 8: circuit with per mini - 3.3V from the FTDI adapter

When it comes to energy supply from the USB connection via the FTDI adapter, we have to note that the jumper is inserted in the 3.3V position before we connect the NRF24L01. That In no case must NRF24L01 board be directly with 5V be supplied. However, the logic lines are 5V festival. This uses the circuit in Figure 7.

The PIN numbers of the SPI bus on the AVR board can also be transferred directly to various other family boards. The connections to the "minis" are on the board, for example, in other positions, but have the same numbers and the same function.

The software

For flashing and the programming of the ESP32:

Thonny or

µpycraft

For the AVR

Arduino id

Library for the NRF24L01+

Sketch for the NRF24L01+

NRF24L01 Sketch

Used firmware for the ESP8266/ESP32:

Micropython firmware

Please choose a stable version

The micropython programs for the project:

NRF24-L01 module For the ESP8266/ESP32

master+slave.py: Demo program for the ESP8266 RX and TX function of the NRF24L01

Micropython - Language - Modules and Programs

To install Thonny you will find one here detailed instructions. There is also a description of how that Micropython firmware (As of 03.02.2022) on the ESP chip burned will.

Micropython is an interpreter language. The main difference to the Arduino IDE, where you always flash entire programs, is that you only have to flash the Micropython firmware once on the ESP32 so that the controller understands micropython instructions. You can use Thonny, µpycraft or ESPTOOL.PY. I have the process for Thonny here described.

As soon as the firmware has flashed, you can easily talk to your controller in a dialogue, test individual commands and see the answer immediately without having to compile and transfer an entire program beforehand. That is exactly what bothers me on the Arduino IDE. You simply save an enormous time if you can check simple tests of the syntax and hardware up to trying out and refining functions and entire program parts via the command line before knitting a program from it. For this purpose, I always like to create small test programs. As a kind of macro, they summarize recurring commands. Whole applications then develop from such program fragments.

Autostart

If the program is to start autonomously by switching on the controller, copy the program text into a newly created blanks file. Save this file under boot.py in WorkSpace and upload it to the ESP chip. The program starts automatically the next time the reset or switching on.

Test programs

Programs from the current editor window in the Thonny-IDE are started manually via the F5 button. This can be done faster than the mouse click on the start button, or via the menu run. Only the modules used in the program must be in the flash of the ESP32.

In between, Arduino id again?

Should you later use the controller together with the Arduino IDE, just flash the program in the usual way. However, the ESP32/ESP8266 then forgot that it has ever spoken Micropython. Conversely, any espressif chip that contains a compiled program from the Arduino IDE or AT-Firmware or Lua or ... can be easily provided with the micropython firmware. The process is always like here described.

Setting up the Arduino IDE

If you have not yet worked with the Arduino IDE and this tool is not yet on your computer, please follow this short guide.

Start with that Download the installation file via this link.

Figure 9: Download Arduino IDE

Figure 9: Download Arduino IDE

Click the version that corresponds to your operating system and save the file in a directory of your choice.

Figure 10: Save installation file

Figure 10: Save installation file

Start the installation file and follow the user guide.

In the next step, please charge the Library file RF24-Master.zip directly via this link or from the Github-Down and save them in a directory of your choice.

Figure 11: NRF24-library download

Figure 11: NRF24-library download

Figure 12: Save rf24-master.zip

Figure 12: Save rf24-master.zip

Figure 13: Select directory

Figure 13: Select directory

Start the Arduino IDE to integrate the new library. in the menu sketch To do this, select the point Incorporate the library and then .Add zip library.

Figure 14: integrate library

Figure 14: integrate library

Navigate to the downloaded ZIP file, mark it and then click open.

Figure 15: Select Library and open

Figure 15: Select Library and open

The sketch for the AVR slave

The AVR variants presented here have the same controller on board, an ATMEGA328P, but each of the three has to be operated with their own attitude. The correct selection of the board, the setting of the processor and of course the right COM port are important.

Figure 16: Per Mini - Board selection

Figure 16: Per Mini - Board selection

Figure 17: per mini with FTDI adapter

Figure 17: per mini with FTDI adapter

The interfacepins of the Pro Mini match those of the FTDI adapter, so that the two boards can be put directly on each other or, like face to face, on a Breadboard. The cables RXD and TXD are already crossed.

That was the preparations for the Arduino IDE. We are now connecting the AVR to a free USB interface and loading the sketch in the editor.

Let us take a closer look at the program before we start it. It is divided into three essential parts.

Includes and declarations
 #include 
 #include "rf24.h"
 #include "Printf.H"
 RF24 radio(9,10); // ce = 9; CSN = 10
 uint8_t Address0[]={0x54, 0x5a, 0x5a, 0x5a, 0x5a};
 uint8_t Address1[]={0x52, 0x5a, 0x5a, 0x5a, 0x5a};
 uint8_t channel=50;
 const intimately Pllength = 8;
 char Payoad[Pllength];
 Bool cycling=1; //
 uint16_t LDR=0; // integrated from the LDR
 intimately LDRPIN = A0;

We first import three libraries for the operation of the SPI bus, the control of the NRF24L01+ and for the output of its configuration data.

The constructor call of class RF24 creates a NRF24L01 instance that we radio to name. So that different units can be addressed, each unit needs two clear addresses, one for sending and one for reception. It is enough if the addresses differ in a byte. You can find more information on addressing addresses in the previous episode Study. Addresses may contain between three and five bytes. We had used an address length of 5 bytes at the ESP8266 and do the same. However, in the Micropython program master+slave.py The addresses defined as 10-digit hexadecimal values, while in the Arduino Idish Sketch used bye arrays of length 5.

In the array there is the lowest quality byte in position 0, which is also the first to go over the SPI bus when transferring to the NRF24L01. The channel 50 becomes our news channel. Values ​​between 0 and 125 are possible. If transmission problems occur in the company, it may be because the selected channel is also used by other stations, for example from WLAN devices that use the same frequency band of 2.4 GHz. Then it is advisable to avoid higher channels. However, the stations involved must use the same channel.

As a payload or Payoad, we can transmit up to 32 characters. We define the number here on 8 and create a char array of this length as a buffer memory. The AVR works as a slave. He receives orders from the ESP8266 and sends measured values ​​back as an answer. We have the LDR on A0 connected. The 16-bit variable takes its converter values LDR on.

Configuration of the AVR
 void set up() {
 
   Serial.Begin(115200);
   while (!Serial) {
  }
   IF(!radio.Begin()){
     Serial.print(F("NRF24 not found"));
     while(1){}  // stop on error
  }
   Serial.print(F("NRF24 Started in Slave Mode on Channel:"));
   radio.setcannel(channel);
   Serial.print(channel);
   radio.setpalenvel(Rf24_pa_low);
   radio.Setaddresswidth(5);
   radio.setpayloadSize(Pllength);
   radio.openwriting pipe(Address1);
   radio.OpenReading pipe(1,Address0);
   radio.start listing();
   printf_begin();
   radio.setdatarates(RF24_250KBPS);
   radio.Printprettydetails();
 }

And we have already reached the setup department. The serial interface for the monitor is set up. We try to start the NRF24L01+. If this worked, then we get the start report in the serial monitor. Otherwise, the program is stuck in the While loop.

Then the working parameters are sent to the NRF24L01+, the channel number, the number of addressbyes and the number of bytes for the payload, we always send and receive 8 characters. A master can maintain up to six connections to Slaves on a channel. These connections are called pipes. We put our transmission line on address 1, the reception management pipe 1 on address 0. This way we crossed the addresses for the master on the ESP8266. The ESP8266 sends on address 0, so the AVR has to listen to one of its reception pipes, here the pipe 1, on this address. The AVR sends on address 1 because the ESP8266 also listens to address 1 on the entrance management 1. We reduce the transmission performance at the lowest level because our two stations are very close to each other during the tests and too much transmission power can overlook the reception section, which leads to disruptions.

After listening to the NRF24L01+ to listen to the radio, we set up the output unit, set the lowest transmission rate (for the best recipient sensitivity) to 250kbit/s and let us issue the entire configuration on the serial monitor.

The main loop does the job
 void loop() {
   uint8_t pipe;
   IF (radio.Available(&pipe)){
     radio.read(&Payoad,Pllength);
     Serial.print(F("Receive:"));
     Serial.print(Pllength);
     Serial.print(F("Bytes on pipe:"));
     Serial.print(pipe);
     Serial.print(F(": "));
     Serial.print(Payoad);
     for (intimately I=0; I<8;I++){
       Serial.print(Payoad[I],Dec);
    }
     String recv=(String((char *)Payoad));
     recv.trim();
     intimately colonate=recv.indexof(":");
     String command=recv.substring(0,colonate);
     String rest=recv.substring(colonate+1);
     // int Val = rest.toint ();
     IF (command=="Send"){
       radio.stop listing();
       LDR=analogead(A0);
       Serial.print(LDR);
       String worth = rest+":"+String(LDR);
       char reply[Pllength]={0,0,0,0,0,0,0,0};
       worth.tokhar marray(reply,Pllength);
       Bool report = radio.write(&reply, Pllength);
       Serial.print(report);
       radio.start listing();
    }
  }
 }

The main loop checks whether a package has arrived with data. If so, the payload becomes the char array Payoad Readed. We receive some reports with the receiving data. The following print commands and the FOR loop only serve to evaluate during development and can be left out in the later production system.

We are now converting the char array into a string, removing non-printable characters and checking the text of the arrived command. We are looking for the position of the ":", because it separates the command from the following serial number. Everything in front of it is command, everything afterwards serial number. If necessary, we can also convert the serial number into an integer in order to create a test digit or encryption to secure the transfer.

Was the command "send", Then we end listening, read the analog value of A0 and convert it into a string. In the deleted char array reply[] Let us transmit the serial number and, separately by a ":", the content of the LDR value string in order to send the data to the master in the following command. Is the content of report Right 1, then the shipment worked and we go back to Lausch post. I will be happy to leave you to you how or whether the answer is processed at the Master (ESP8266). Ideas would be, for example, sending to the network or writing to a log file.

Is everything built and connected to the AVR? Is the correct USB port to the AVR and is the right board selected? Then we can compile and upload the sketch. The AVR runs as a slave and should be able to receive and answer inquiries from the master.

The ESP8266 as a master

So that the AVR can do this, we need the execution of the master program on the ESP8266. There are two options for this. Either an ESP8266 is already prepared and ready to be prepared from the previous episode of the blog, or a station must be created in accordance with the explanations of this episode. A short guide follows for the latter case.

The circuit for the master shows Figure 16. In the event that the master should do other things in addition to the operation of the NRF24L01, an ESP32 may be better suited instead of the ESP8266. program and Driver module are identical to the big brother.

Figure 18: NRF24L10 am ESP8266

Figure 18: NRF24L10 am ESP8266

By means of Thonny the ESP8266/ESP32 with the Micropython firmware Mistake. You can find instructions on this in the chapter Micropython - Language - Modules and Programs Further up in this post or you just follow the same this link.

Once that is done, we load the file NRF24SIMPLE.py to the ESP8266 and open the program Master+slave.py In an editor window of Thonny. After it started with F5, five inquiries are sent to the Arduino. Then there is control of the Micropython Prompt ">>>" in the Terminal area of ​​Thonny. As long as the AVR is not yet running, we will receive timeout messages that we can confidently ignore at the moment.

Figure 19: First start of the ESP8266 as a master

Figure 19: First start of the ESP8266 as a master

We are now starting the serial monitor in the Arduino id. He receives the expenses of our program to 115200 Baud.

Figure 20: Start the serial monitor in the Arduino IDE

Figure 20: Start the serial monitor in the Arduino IDE

After a few seconds we receive the starting reports from our sketch.

Figure 21: Starting reports of the AVR-Slaves

Figure 21: Starting reports of the AVR-Slaves

The slave is now ready to receive. So let's start the master program in Thonny with F5.

In the serial monitor, the inquiries of the ESP8266 with the LDR value in the penultimate line appear in 2-second distances. The 1 tells us that the feedback to the master has successfully left the slave.

Figure 22: Output in the serial monitor of the slave

Figure 22: Output in the serial monitor of the slave

The values ​​also appear in the Terminal of Thonny without a noticeable delay.

Figure 23: Shell-output in Thonny

Figure 23: Shell-output in Thonny

In individual cases, the requirement is sent by the master, but does not arrive or mutilated with the slave, so that no feedback is subsequently sent. This is expressed by a timeout on the master. The program can then take care of the error accordingly, for example by sending another request immediately.

So we arrived at the end of the two -part post for NRF24L01, we summarize.

  • The NRF24L01 provides a radio connection between AVR and ESP8266/32, which is off the usual WLAN frequencies.
  • The SPI interface uses the NRF24L01 for both controller families.
  • The SPI interface on the ESP32/ESP8266 can also be easily programmable in micropython.
  • Existing ARDUINO IDE projects can be easily expanded with WLAN functionality via ESP32 and ESP8266.
  • An ESP32 or ESP8266 can manage up to 6 logical connections to AVR projects or NRF24L01 units in connection with other controllers.

For us, there are further foundations for exciting new projects. Maybe you can already find something in the overview The contributions on Micropyton and ESP32/ESP8266 already appeared on AZ-Delivery.

Esp-32Esp-8266For arduinoProjects for beginnersSensors

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