Copyright ©

Mongoose OS Forum


PWM - how-to? With ESP8266, need some tips, please.

(ESP8266) Love mongooseOS, got everything running, LED blinking, I2C devices running, MQTT mqtt-ing like crazy and OTA editing of files!

Except ... PWM. I found one mention somewhere seeming to show how to do it (see below).

I can do everything described in that short how-to except the:
"add line "mgos_pwmset" to the list of exported symbols"

Where would that be? I am not very familiar with C++, more a C# guy.

The usage (once activated) should be:
let PWM = { set: ffi('int mgos_pwmset(int, int, int)') };
Is that correct?

Some info I found here:

PWM is only available on esp8266 currently.
If you want to use it, do checkout mongoose-os repo
cd to fw/examples/mjs_base
add line "mgos_pwm_set" to the list of exported symbols <<< Where would I do that???
type "mos build --arch esp8266"
type "mos flash"
Now you can use PWM from JS: let PWM = { set: ffi('int mgos_pwmset(int, int, int)') };


  • bump! this is a very pertinent question... someone please clarify!

  • SergeySergey Dublin, Ireland

    we'll add pwm api soon, eta - week after next

  • Thanks, Sergey! :)
    Then I can get rid of LUA and use MOS for my Wifi HVAC flap project, V 2.0.
    While the current LUA solution works, the LUA limitations bug me quite a bit.
    V 2.0 with MOS and bi-directional MQTT data exchange and easy to change Wifi password thanks to MOS will be significantly better.

  • SergeySergey Dublin, Ireland

    could you elaborate on the lua limitations please?

  • The nodeMCU LUA is nice to get one's feet wet, but limitations compared to MOS are:

    1. Timer, very complicated to have a larger program running or use larger than a few lines web pages served w/o having to deal with the timer issues or have the ESP8266 panic and reboot. Made me put all the HVAC selection HTML onto an external (NanoPi2 Fire) web server, which does other nefarious things too. I know there are some tricks to circumvent that issue, but too much hassle.

    2. No built in OTA FW or file update. (Big one!) It is kind of possible, to a degree, but very cumbersome in the execution, thus I'd say not really feasible with LUA/nodeMCU

    Admittedly, compared to using the Arduino IDE with ESP8266 support approach I used first, the one good thing with nodeMCU LUA is when using the free ESplorer (Java software), one can "relatively" easy and quickly test programs, by just editing them and then uploading them, a little bit like MOS.

    But again, not easily possibly via OTA, a killer feature of MOS.

    MOS will give me the best of both worlds in this respect. Easy to update OTA, in any respect, no more timer woes, MQTT built in, very easy to program, perfect!

  • SergeySergey Dublin, Ireland

    @JohnCz , thanks for the elaborate answer, appreciated

  • JohnCzJohnCz Canada

    No pressure, just a quick check if there are any updates. I can see from your blog you guys are very busy :)
    Any updates on the horizon re: PWM for Servo control?

  • SergeySergey Dublin, Ireland
    edited May 2017

    Not doing PWM yet!
    By the way, do you think bitbang implementation would do?
    We have a pretty good bitbang API - it is good enough even for neopixel, which is quite time-sensitive.

    This is neopixel implementation on top of bitbang api:

  • michlpatzmichlpatz Belgique

    Can you give a simple exemple how to use bitbang as PWM?

  • SergeySergey Dublin, Ireland

    See below.
    Again, this is a poor man thing, not serious. A kludge until we do a real (hardware-backed) pwm.

    let led = 2;     // built-in LED on NodeMCU. Inverted: writing 1 turns it off.
    let freq = 100;  // Timer frequency in milliseconds
    Timer.set(freq, true, function() {
      BitBang.write(led, BitBang.DELAY_MSEC, 0, 1, 1, 0, '\x00', 1);
      GPIO.write(led, 1);
    }, null);
  • michlpatzmichlpatz Belgique

    Thanks fr the fast reply.

    But i don't understand how to specify the dutty cycle?

    In fact, I don't understand what does the last 6 parameter of the fonction do :s

  • atmegaatmega Schweiz

    @Sergey I thought it's implemented already?! I'm using
    let PWM = { set: ffi('int mgos_pwm_set(int, int, int)') };
    PWM.set(4, 1000, 100);
    for a 10% 1kHz PWM on Pin 4, ESP8266, since yesterday, successfully! I don't know how it's implemented, but it works without noticeable problems while communicating over WebSockets.

  • JohnCzJohnCz Canada

    Thanks, will give that a try right away, am finally off being on-call after today, so got time to tinker with this again.
    If that works with a servo, I am a very happy camper indeed.
    Mark II of my Wifi HVAC Flap project will rise and destroy humanity ... Er .. I mean, can finally be updated remotely :) Pics will follow if successful.

  • JohnCzJohnCz Canada

    @atmega, so I tried your example, latest MOS and .. no go. :(
    esp_mgos_init2 mjs_base 1.0 (20170518-165149/???)
    esp_mgos_init2 Mongoose OS Firmware 2017051816 (20170518-165149/???)

    MJS error: bad ffi signature: "int mgos_pwm_set(int, int, int)": dlsym('mgos_pwm_set') failed

    Latest MOS from the website. Running via web browser editor.
    Any tips? Maybe copy&paste changed some characters? ESP8266 (NodeMCU)
    Tried various combinations, example:
    let PWM = { set: ffi('int mgos_pwm_set(int, int, int)') };
    PWM.set(12, 50, 100);

  • atmegaatmega Schweiz
    Ill try and get a barebones together for you later...
  • JohnCzJohnCz Canada

    Much appreciated!
    I just need basically: Tell servo to go to position x. :)

  • SergeySergey Dublin, Ireland

    Folks, we do not have proper PWM at this moment.
    Will have it soon!

  • JohnCzJohnCz Canada

    Thx for the info, Sergey. I can wait a bit longer, no problem.
    But if atmega has an interim solution, I'll take it, just to get the second prototype done and tested.

  • atmegaatmega Schweiz
    edited May 2017

    @Sergey Then how does my version work (It DOES work, even though I think the API is probably SW PWM... but it runs perfectly)
    I can get a 1kHz PWM with 0-100% duty via RPC call over WebSockets.
    BTW: No modifications to mos, cloud build via "mos build && mos flash"

  • atmegaatmega Schweiz

    And one more thing: I had this invalid signature error too, after which I added mgos_pwm_set to mos.yml. Then I had a different error while building, removed the mgos_pwm_set from mos.yml and from then on it just worked. No idea how this is possible, but maybe there is a bug somewhere which leads to the symbols not being generated correctly... or maybe it was just a temporary weird phenomenon.

  • JohnCzJohnCz Canada

    @atmega: Thanks! Tried your FW, and the error message is gone
    Still, cannot make the servo go to a specific degree/setting. Tbh, I just don't know enough to figure out why or how.
    It only ever moves the same small amount, regardless how I change the values.

    But, no matter.
    This is absolutely not urgent, the Mark I of the HVAC Wifi flap works fine, albeit not geek-perfect (= no feedback, no MQTT, no remote editing, etc.)
    I think I'll wait for the Mongoose HW servo implementations. I bet, other people waiting for that too.
    Would be nice to get an approximate ETA, plus minus a month, if possible?

    In the meantime, I got ArduinoNano-NRF24L01-Servo boards to play with. :smile:

  • SergeySergey Dublin, Ireland
    edited May 2017

    @atmega yes that works. the implementation is ESP8266-only, and it is a kludge to be honest. We'll reimplement it in a proper way (with HW support on esp32)

  • atmegaatmega Schweiz

    @JohnCz try mgos_pwm_set(pin, 20000, value from 1000 to 2000) and it should work. 1000-2000 should give you the full range.
    @Sergey ah yes, I see. Looking forward to that, but in the meanwhile it's good enough for my purposes, in fact it works without any problems (in the relatively controlled environment I'm using it in)

  • JohnCzJohnCz Canada

    @atmega: THAT WORKS! Thx atmega!
    I can now easily prototype this, and when Sergey and the gang have time to implement this a better way, just change that one function and done :)
    Many thanks, everyone!

  • FrodeLFrodeL Norway

    Hi, reading through this forum thread I'm a bit confused. Is mgos_pwm_set working in the C-api for ESP32?

    I also looked in the Issues list for Mongoose-OS repository, but there are no open issues there for PWM?

  • SergeySergey Dublin, Ireland

    mgos_pwm_set is currenly an esp8266-only kludge.

    We are working on a PWM API right now, expect it next week.

  • FrodeLFrodeL Norway

    Ok, thanks! PWM is important for my ESP32 project, so looking forward to it.

    My autonomous RC car isn't moving or turning anywhere until it gets PWM :wink:

    Thanked by 1marmarti
  • FrodeLFrodeL Norway
    edited June 2017

    Hi, any news on PWM for ESP32?

    Thanked by 1marmarti
  • SergeySergey Dublin, Ireland

    Nearly there! Last iterations of a long review!

    Thanked by 1cristiand391
  • JohnCzJohnCz Canada

    Sergey: Kudos, I see you delivered as promised, and much more :)
    - Added ESP32.hall() and ESP32.temp() JS bindings,
    - Synced ESP-IDF SDK to 2.0.0-1.5.0-r6
    - Added PWM.set() API,
    I know what I am going to do over the WE, my shiny new ESP32 just arrived. Time for a test drive :)

Sign In or Register to comment.