📜 ⬆️ ⬇️

Management of a curtain on the RS-485 interface

About the benefits of controlling the curtain through rs-485


Some time ago I had an electric carriage for sliding curtains AKKO AM72E. It’s not that I’m too lazy to move the curtains with my hands, but progress is going forward and I try to go after it. The electric motor can be controlled both by dry contacts and from the radio control. But who needs this commonplace, if the motor supports the RS485 interface, which allows not only to give commands, but also to read the condition of the curtains. And in general, the ultimate goal to manage the eaves from your phone, and why not.

The easiest way would be to find a USB-RS485 adapter and start testing. But there was no such adapter nearby. If ordered, it would take some time to wait. Faster to do. I have several USB-UART adapters on all popular microcircuits, but I mostly use a pair of adapters for the CP2103. They look like this:


')
Approximately, because the connectors are installed and additional signals are output. All you need to do adapter UART-RS485. The store bought several MAX485. It might be easier to use something like the MAX13487 with automatic gearshift and reception. But I didn’t find one at a local store (maybe I was just looking bad). Honestly, I was too lazy to go to the store for MAX485. Initially, there was a desire to make reception at the OS, and a transmitter on transistors - it’s so easy to test whether the AM72E electric screen works using this protocol or not.

Back to "Modern Electronics"


The case for the scheme. For the basis, I took the scheme from the magazine "Modern Electronics" №1 for 2007. Everything looked rosy. The article says that the CP2103 microcontroller pins are programmed by default as control outputs and correspond to their use in the USB-RS485 adapter circuit. It turned out that this is not my case. I have adapters for a long time. A rare thing that fell into my hands is not subjected to "improvement." The firmware in the adapters was not an exception, and even if the girlhood could pester the RS485, now these skills have been completely canceled.

The evening stopped being languid. It was necessary that something that will switch the MAX485 from transmission to reception. By the way, the transfer adapter worked fine. In general, this could be enough, since there is nothing special to read with the AM72E.

There were many options. MAX485 on the board is mounted on the panel, and can easily be replaced with a microcircuit, with auto-switching transmit and receive. But this is not an option at all, because you have to go to the store. I also have adapters for FT232, and these chips can switch the MAX485. Too easy. And I already had a plan, how can I have some fun with AM72E and for FT232 there was no place in it. It is necessary to supplement the circuit so that when the start bit appears on the TX UART, MAX485 switches from reception to transmission and is in this state at the time of transmission of the entire byte, and then again switched to reception. Referring to the experience gained through me through google, I found out that this problem is solved with the help of the timer NE555. Indeed, why wise. But I didn’t have anything from the NE555 family timers. Further you know: shop - laziness.

Odometer for lazy ... not needed


Make one-shot in a million ways. I even wanted to quickly remake the board and put the STM8S003 for these purposes. At first glance, this may look like a cannon on sparrows, but if you compare the circuits on the NE555 and STM8S003, then the circuit on the MC will be even simpler, because of the external elements only one capacitor is needed. The program is literally a few lines in assembler. With the price, too, everything is not bad - it is cheaper than the MAX485 in our store. There is one problem with the hardware timer (on the NE555). It will work fine at the same speed. As soon as you need to change the exchange rate, you will have to rebuild the timer. I often come across devices that, when launched, give out debugging information to the UART at one speed, and after loading, they switch to exchange mode for another. And you never know why you may need to change the speed! Each time you do not want to climb into the scheme. This is where the timer on STM8 can help - you can write the program so that the necessary timings are set on-signal and do not require intervention. It is not very difficult. I don’t understand why I need to know something about the UART exchange rate. Many years ago, I came across UART devices that automatically determined the speed at which another device was connected and were tuned to it on the fly.

I know that you would like to see the scheme without STM8S003. Okay, I'll go to meet you. And without this, I can make something interesting out of a simple test. There will be no one-shot in the circuit:



What and how it works here, I will not explain - everything is standard and obvious. I can only say that I did not install the jumpers near the resistors R5 and R7, that is, the circuit can be made easier by removing all the jumpers and these two resistors. The maximum you may need is R5. There are devices that, when answered, simply release the line when transmitting the last bits, if they are ones. Then, without R5, the last byte can be distorted. In our case, this is not important. the last byte of the most significant byte of the response checksum. The scheme will work without R1, but we will need it later.

It looks like this to me:



Do not be discouraged if you do not see all the elements that are on the diagram. At first I made the board according to the scheme from the article, and then experimented with the transfer-pickup scheme. The transistor (DTC143 in SOT23, immediately with the base resistor) and SMD resistors are soldered directly to the tracks on the back side of the board.

If you put the MAX13487, then nothing will be left of the circuit at all. Even better, get a standard USB-RS485 adapter. But then you will be tied with wires to the curtains. Stupid sight. Would I bother with the UART-RS485 adapter if I didn’t have a cunning plan?

Lua to help us


With iron on this finish. Need to write a program. The program is only for the test. Nothing complicated. It is necessary to send commands for AM72E to a serial port. Well, you can still read that he is responsible for us there. For the experiences take a computer with Windows. We must choose the language in which we will write. The first thing that came to my mind was powershell. No, I will not torture you powershell. Then python. Python is good for everyone - the code on it is portable to any OSes, it’s understandable, you can immediately bolt the GUI, and for Windows you can also package it as an executable so that few people will understand that the program is in python. And still not python. There are many examples of working with a serial port on Python and without it - anyone can find it on their own. The program will write on Lua. Yeah, a strange choice. Actually, I had no big choice. Either C or Lua. Why - more on that later. It is possible and C. But no, not this time. Just because in C I write the code in such a way that after a couple of months I can’t understand it myself without accepting substances that expand consciousness. Just kidding So I write in any language.

Lua needs to be installed. We take from here: https://code.google.com/p/luaforwindows/downloads/list . It is installed in almost one click. Includes a sufficient number of modules. There is everything you need. Including for the graphical interface - iup. If you decide that you need it - use on health. But we will manage the command line. We are only for testing. And we need a module to work with the serial port. If we decided to conduct testing under Linux or Mac OS, then it would be possible to work with a serial port without an additional module - just as with a file. To access the UART for Windows, we need the luars232 module. In the assembly it is already there. In addition, nothing to search and install is not necessary.

The file with the program - curtain.lua - only a few dozen lines. If desired, you can still reduce. How to use, I will not explain. I will show the picture:



I will make only one explanation. This is obvious, but if the line starts with a “>” character, then this line is entered from the keyboard. If this character is not at the beginning of the line, the line is received from the program.

I did not expect that everything will be clear to everyone. The command line, like the wires, we do not need. What matters now is that the RS485 on the AM72E works great. No, I had a moment when I gathered and checked everything, and I started sending commands to the AM72E, but he didn’t react at all. The idea that the RS485 still does not work flashed through my mind. But then I looked under the table, where I had a network extension cable, and saw that the AM72E should still be connected to the network. After that, when I sent the “Close” command, I heard a cheerful buzzing of the engine - everything works.

I’ll take a little rest and move on to implementing my “cunning plan” - I’ll teach my electric carnet to accept commands via WiFi. What will report in the next article.

Source: https://habr.com/ru/post/366979/


All Articles