3

Coming from JS, I find it very difficult to program Arduino in C++ (not even knowing which version or what features are supported), being bound to the loop and delay, not being able to (easily) use anonymous functions in parameters or async code.

I know JS isn't natively supported by Arduino, but I know languages can be transpiled.

I have seen many solutions, so just to pick few:
JohnnyFive J5, which runs via cable on a host PC,
NectarJS with their arduino-uno target,
or andrei-markeev/ts2c which converts ES3 to C89.

Each of them come with their own limitations and it's very difficult to find the right without prior experience with IoT. Did anyone walk this path already and found a viable solution?

4
  • 2
    Forgive me if this question is inappropriate for this site. This is my first Q here and I am totally new to IoT. However, outright downwoting, without providing a feedback isn't really constructive and welcoming to new members.
    – Qwerty
    Commented Feb 14, 2020 at 22:18
  • 1
    I suggest you look at the boards and components that are supported by these frameworks before you commit to any one. Maybe start here, johnny-five.io/api and see if the supported components will cover what you are trying to achieve. Commented Feb 14, 2020 at 22:35
  • 2
    my recommendation is, learn C++
    – Juraj
    Commented Feb 15, 2020 at 5:37
  • 1
    Just dive in and learn C++ while you go. You could also try learning Python so you can program your Teensy in Circuitpython or Micropython (although I now see it might not be easy to get it going on the Teensy).
    – StarCat
    Commented Feb 15, 2020 at 8:56

3 Answers 3

2

Even if such a transpiler exists (which I don‘t know) you will still have to deal with the loop structure and the fact that you should avoid using delays. On a microcontroller you have to forget about things like multithreading and even dynamic memory allocation should be used carefully. Most of those things that make a programmer‘s life easy on bigger systems are usually paid with ram or cpu power, but on a microcontroller with some kB RAM there are only few resources to pay with.

So my recommendation is to learn C++ instead of looking for ways to convert code from another language, which will probably introduce its own problems. You‘ll soon find yourself one layer below and there are exciting things that higher languages hide from you.

2
  • Is it possible that some of the C++ constructs are valid, but not working specifically on IoT devices? (In my case the Teensy LC or 3.2.)
    – Qwerty
    Commented Feb 17, 2020 at 13:26
  • With JavaScript, you don't use delay(), nor multithreading: you do non blocking programming, in a single thread running an event loop. That's exactly the approach you would use in C++ on a microcontroller. Commented May 21, 2021 at 7:20
0

Why don't you just start with a microcontroller that supports Javascript?

I find the Adafruit Circuit Playground Express quite fun to work with. You can still connect sensors and other hardware, the same as you can do with Arduino. It also has a bunch of sensors built-in.

You can try it out right now without owning a hardware board at adafruit.makecode.com. Click the Javascript tab on the top of the page.

Using a JS>Arduino transpiler will probably cause a lot of headache, since it often won't do what you expect it to do.

3
  • I bought the Teensy specifically because it's best for HID. I want to repurpose an old keyboard, but if I were to do something larger, I would probably choose a board that supports node.js and JavaScript.
    – Qwerty
    Commented Feb 17, 2020 at 13:22
  • 1
    There aren't any boards that support node.js sadly. You can run Johnny Five with almost every board, but that runs on your PC. It allows you to read the board through the USB cable.
    – Kokodoko
    Commented Feb 18, 2020 at 12:19
  • I had to put the project away for a while, but now that I think of that... The keyboard must be connected to the PC anyway, so I can as well run nodejs to control it. Johny Five may be the best solution for hacking after all. After the PoC works, I may rewrite it in C++.
    – Qwerty
    Commented May 21, 2021 at 15:50
0

andrei-markeev/ts2c JavaScript/TypeScript to C transpiler looks promising.

I'll probably give it a go tonight.

According to the github page:

Produces readable C89 code from JS/TS code.

For example, this JavaScript:

console.log("Hello world!");

transpiles to the following C code:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}

src: https://github.com/andrei-markeev/ts2c

Not the answer you're looking for? Browse other questions tagged or ask your own question.