Programming the Photon in JavaScript

I really like web development but I know very little C or C++. Is it possible to program the Photon is JavaScript using a wrapper? I don’t really know what a wrapper is but I have heard it will let me in program the Photon in languages other than C or C++.

As far as I know it’s not possible to program the Photon with anything besides C/C++.

There are implementations for JS and Python on other microcontroller (even C#) but not for the Photon.

That’s not entirely true… There is Viper, which let’s you program the Photon in python, although you currently lose the Particle Cloud functionalities, if I’m not mistaken.
I don’t think there currently is a way to program it in Javascript, and I’m not sure there will ever be one. C/C++ is rather effective on micro-controllers, and it works pretty well. I’m not sure if JavaScript would be a better choice. That said, if someone feels the need to design something to program it in Javascript, I have no objections.

Theres some javascript support for talking to photons/cores : https://docs.particle.io/reference/javascript/

If you prefer that could be used to keep the majority of code in javascript, and just the lowend i/o in C at the cost of internet being required and latency.

1 Like

What about language wrappers like Particle’s own SparkJS? Does SparkJS work on the Photon?

Hi @rpiswag and @MORA

I believe both of you are mixing up the javascript API that Particle provides to JavaScript programs running in web servers or using node.js on desktops or laptops, with code that actually runs on the Photon (or Core). Those javascript implementations are designed to run on full fledged “big” machines. The Photon and Core devices are very low level, they do not have an OS, and have a very limited amount of physical memory, and no virtual memory management. As a result, it is most efficient to program in a language very close to the bare metal, C, C++, or assembly language. Javascript is a high level interpreted language that requires an interpreter to be running and makes heavy use of dynamic memory management to build and tear down data objects. That said, as @Moors7 points out, there’s an interpreter for python specifically for microcontrollers, and giving Viper a try would be a fun project, I don’t want to discourage you. However you will probably be taking this journey with few followers. For the foreseeable future microcontrollers like this are going to be programmed primarily in a C variant or similar language.

The Espuino is probably what you are looking for. Cortex M4, javascript, extensive module library

That isn't a wrapper, but rather a library for another programming language which provides you which the behind-the-scenes code nessecary to talk to the Cloud API more easily. The JavaScript library doesn't actually do anything on your device, it just calls the cloud API to triggers things already programmed on your devices.

I'd suggest learning C/C++ since that will definitely benefit you in the future. With the amount of examples available for Arduino that shouldn't be too hard.

but C is so hard!

So there is no language wrapper for JavaScript?

The problem is fundamental differences in the languages themselves with the core sticking point being memory management.

Even if you could compile JavaScript down into assembly that was compatible with the Photon’s microprocessor the semantics of creating variables and data structures in JavaScript would result in quickly using up the available memory on the Photon with no ability to free it since the JavaScript language doesn’t have any way to explicitly free memory.

Your best bet is write very minimal C code that simply exposes the data you want via Spark.variable and functions to run via Spark.function then have code running on a computer somewhere that uses the REST APIs to communicate with and control the Photon.

A quick search turned up projects like https://github.com/rwaldron/johnny-five which appear to take this approach to the extreme and they apparently might have some Spark/Photon support: https://github.com/firmata/spark

2 Likes

I might just get a Tessel 2 or a Espruino Pico. What do you think is better?

I am well aware that javascript wont run in the actual hardware, therefore my comment “at the cost of internet being required and latency”, the internet being required part due to cloud API…

But if javascript is preferred and the task is something that is not timing sensitive the javascript code could control a photon and offload the more complex code to javascript, while photon handles stuff like “ReadADC, WriteDigital, etc”.

Oh, and Java is not JavaScript :wink:

Oops, fixed that typo.

@MORA Now I understand your suggested architecture. I dunno, I guess I won't judge @rpiswag, but if I were to judge, :grimacing: at this point this solution has crossed a line. The "program" for your microcontroller is really just spread across two machines and the cloud. I don't know the project you have in mind, but perhaps just forget about a microcontroller and just use a more powerful computer, like an old laptop or something for whatever it is you are doing. Or maybe a raspberry pi.

The reason I want to use a microcontroller is because power is a big concern for my project.

What are you trying to do? If it isn’t overly complicated, we might be able to help you out.

I am trying to use a infrared receiver so that when the infrared receiver receives a signal it will send a wake on lan command to wake up my computer. I have a old computer running as a media center box.

I don’t want to ask for help all the time and if I can program the Photon is javascript I can program the Photon in the programming language I am best at.

Being good at something should not hold you back from learning something new :wink:

And if you start in small steps C is not hard at all - at least standard applications will be very doable.
The internet, the docs and this forum are full of information that can get you started, without needing to ask for everything.

Talking from own experience the main thing about programming is to learn to think like a programmer - the language or dialect is only secondary and usually not too much of an obstacle, if you really want to know.

That is a good point but it is nice to make your project in a language you are comfortable in. It is like trying to write a book in a foreign language. You can write that book in that foreign language but it will take you a lot longer than your native language and you will probably need a lot more help and you will probably make a lot more mistakes.