logo elektroda
logo elektroda
X
logo elektroda

ESP8266 UDP packet selection and I / O and PWM output control

adambehnke 5211 14
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16375560
    adambehnke
    Level 24  
    Hello

    It is possible that I am not writing in the section where I should, but I have not found a better place.
    Well, I am struggling with ESP8266 modules.
    Previously, I had connected this module via Uart to Atmega.
    And I communicated without any problem. But since ESP has its own fast controller and a lot of memory, I decided to get rid of atmega and write the program directly on ESP.
    I browse the internet for a few days and look for a specific clue in what and how to program. I will emphasize right away that I am not going to use the Arduino IDE.
    I don't like it and I don't use it. I program atmega in Eclipse (GCC) on a daily basis.
    But unfortunately I didn't know examples and courses on how to program.
    Instead, I found rudimentary information about LUA.

    Let me start with the details:
    I have firmware in ESP: NodeMCU 0.9.5 build 20150214 powered by Lua 5.1.4
    so it displays to me in the terminal after startup.

    Please help me with programming. Some specific course etc.
    In atmega microcontrollers I am used to the Main loop etc ..

    And in LUA, programming reminds me more of some "bush" ...

    My assumptions are simple and I would like to achieve this goal:

    0 I would like to use UDP packets

    1.After connecting the power supply, ESP connects to the wifi network under a fixed (in some variable) IP address - say 192.168.1.200 and is to listen on port 5000

    2. After receiving the text, e.g. "kanal1-ON", it turns on the low state on the pin, e.g. GPIO_1

    3. After receiving the text, e.g. "channel1-OFF", it turns on the high state on the pin, e.g. GPIO_1

    4. After receiving, for example, "kanal2-120" sets my PWM on GPIO_2 to 120.

    e.t.c.

    I'm just going to control the led strip. Now I steer correctly, but I have an atmega and ESP harnessed for this purpose. And I would like to put such a simple function directly on ESP.

    In addition, I would like to be able to return some data to the IP and port of the sender so that, for example, from a smartphone, sending the "REPORT" command, the ESP module sends back a previously prepared report, eg "RAPORT_KANAL1-ON ^ KANAL2-120 ^ ......"

    My assumptions are simple but I can't get down to writing LUA code.
    I just don't get it ...

    Any help will be worth its weight in gold.
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • Helpful post
    #2 16375613
    malcompl
    Level 13  
    In Lua, like in another one with dynamic typing, closures and contexts (JS etc), the writing is quite nice, but you need to introduce yourself a bit. You can always crash structurally and functionally, it will be easier. Though there's nothing like strong static typing in compilers.

    As for your questions, you should refer to the documentation of the firmware you are using, i.e. NodeMCU Documentation . It is very rich with numerous examples and you will find exactly what you need there.

    Once upon a time I was doing some tests with Lua, JS and Python under ESP, although ultimately I see C ++ there, but for simple things, or prototyping, Lua does the job ;)
  • #3 16375634
    adambehnke
    Level 24  
    Thanks for the info. Well, normally it's incredibly difficult to change from Eclipse and C.

    At the moment I am testing the reception like this:
    Code: Python
    Log in, to see the code


    and it works fine.
    But what is this ... :P Where some main loop, some function, loop etc.
    Little do I understand, Admittedly, I understand this code roughly.
    ESP is listening on port 5000 and if it receives "on" it sets pin8 to high state, and if it receives something else, it sets pin8 to low state.
    For the test, I added that it displays in the terminal that it received the command and also displays what it received.

    But how to establish a connection on a given IP right after ESP starts. And what next with my assumptions ...

    Without help, it will be a mecca ...
  • ADVERTISEMENT
  • Helpful post
    #4 16375661
    malcompl
    Level 13  
    Since it's scripted, it runs like a regular script, executing statements and expressions one by one. The loop is probably in the `Listen` method from the server, (` srv :: listen () `), which listens over and over again and fires a specific connected function or callback to that action on some action. It's almost like a typical event-driven, often used.

    Custom the `wifi` module exactly, it's probably supposed to act as a typical client / workstation, so you're interested wifi.sta.config and wifi.sta.setip if you don't want dhcp addresses.
  • #5 16375790
    adambehnke
    Level 24  
    I just can't use the addresses DHCP will give me. Because in the smartphone application I have to rigidly set the IP to send commands to. There will be several LED strips with ESP and each will have a different IP. This will simplify the controls. I guess ...? Am I getting to it badly? Maybe it could be easier?
  • Helpful post
    #6 16375839
    malcompl
    Level 13  
    Well, there is such a problem with dhcp that later it is not known what address ESP got, unless there are some rules in setting IP based on MAC. This is usually not a problem, because there are probably more projects where ESP sends data somewhere than accepts it directly or works as an AP.

    Your idea makes sense how you want independent devices. You might even think about some self-reporting of your devices. For example, after connecting to the network, the device could send information about its name and IP address to the brodcast, or on request after sending some request to the brodcast.

    Ehh ... I have to finish my prototype of a wifi controlled socket one day ;)
  • ADVERTISEMENT
  • #7 16376029
    adambehnke
    Level 24  
    All in all, the longer I browse the NodeMCU documentation, the more I like this module. Virtually most of the things I did on atmegs can be done easily, faster and easier on ESP. At least the functions that require wireless connectivity.
    I have yet to delve into exactly how the program execution in ESP works.
    From what I can see it is executing line by line but there are places where it loops because I put the Print command "something there" so that I would know where the program is executing.
    In addition, I have to find a way to get the IP and port from which the data came to ESP and to send the answer exactly to this port and IP.
    I still ask anyone with some knowledge to comment on the topic.
    I will describe the progress of the work in detail so that it will be useful to others.
    Of course, I do not want a ready-made tray, but this is my beginning with this module, so any help is invaluable.
  • ADVERTISEMENT
  • Helpful post
    #8 16377453
    krzbor
    Level 27  
    Why are you so critical of the Arduino IDE? After all, it's the C language that you know when writing in GCC. For me, the most important were the examples that were installed in the Arduino. I also appreciated the fact that I can program different CPUs in the same environment.
  • #9 16377635
    adambehnke
    Level 24  
    Well, it just so happened that at the moment I am sitting in the Arduino IDE :)
    All in all, it looks closer to the C from Eclipse. There are loops etc. :)

    and now I have to assign the IP to the ESP module one by one so that DHCP will not assign me and set the port on which the module will listen.
    Then I have to recognize the command that will come, e.g. from a smartphone and set two PWM outputs to control the leds (through transistors).
    But I also want to be able to submit a "report" to the IP from which the command came. So I want ESP to send some UDP packet to the smartphone.

    How was it in the People's Republic of Poland .... "Will you help?" :P
  • #10 16377715
    malcompl
    Level 13  
    Arduino is a C ++ with non-standard additions ... I have never used it because it writes in standard C ++ ...

    In one word RTFM! Documentation, documentation, documentation ... according to her the full prototype of the callback for the UDP `receive` event looks like this:
    Code: Lua
    Log in, to see the code


    Even when sending it is example how to make simple echo :
    Code: Lua
    Log in, to see the code
  • #11 16377757
    adambehnke
    Level 24  
    wooow that's called answer. I cleanly reinstall ArduinoIDE to keep things in order and start testing :)
  • #12 16380167
    adambehnke
    Level 24  
    Currently I work on the following code:

    Code: Lua
    Log in, to see the code


    and basically works fine.

    But I can't configure this example with Echo. It gives me an error with createUDPSocket ()

    Code: Lua
    Log in, to see the code


    In fact, when I enter the code and press shift + space, it displays hints and I do not have createUDPSocket () there ...
    I do not know why. Or maybe I have old firmware in ESP? But it doesn't really affect that. I have the newest Esplorer ... What could be?
    Screenshot below ESP8266 UDP packet selection and I / O and PWM output control

    Added after 54 [minutes]:

    It seems to me that, however, Arduino IDE is probably the only right solution when it comes to ESP8266.
  • #15 21357385
    xury
    Automation specialist
    ESP32 has its own IDF in addition to the Arduino platform Quite similar to write on them. Not on the native IDE of course but I prefer Platformio with MSCode.

Topic summary

The discussion focuses on programming the ESP8266 module for UDP packet reception and controlling I/O pins and PWM outputs without using the Arduino IDE. The user initially interfaced ESP8266 with an Atmega via UART but now aims to program directly on the ESP using NodeMCU firmware with Lua 5.1.4. Key challenges include understanding event-driven programming in Lua, setting static IP addresses to avoid DHCP for multiple ESP devices controlling LED strips, and implementing UDP communication with command parsing and response echoing. Example Lua code demonstrates UDP server creation, GPIO pin control based on received commands, and static IP configuration. Issues with outdated firmware lacking the `createUDPSocket()` function are noted, with recommendations to build updated firmware via nodemcu-build.com. The discussion also touches on the benefits of Arduino IDE for C++ examples despite initial reluctance, and mentions ESP32 development environments like PlatformIO with Visual Studio Code as alternatives.
Summary generated by the language model.
ADVERTISEMENT