There is a huge learning curve when it comes to building a small hardware project. To start, there are a lot of decisions that have to be made, from what hardware do I use to what library/framework to use to program the device. I do have great news: it is quickly getting better. As the community gets bigger and hardware gets cheaper, it will get easier create things in the physical world.
get your environment setup:
Install the USB driver for the NodeMCU
See the installation instructions
Install the ThingsSDK CLI
$ npm install thingssdk/thingssdk-cli -g
wire it up:
follow the wiring diagram below
Flashing the NodeMCU:
Open up Flasher.js and Plug the USB into the NodeMCU
Once plugged in, you should get a notification confirming that Flasher.js found your NodeMCU. If not, make sure you have the drivers installed.
Now, let's flash our NodeMCU. Before flashing, make sure you select your device along with the latest version of Espruino.
Time to write some code:
Use the CLI tool to scaffold a project
We can create a new project by using the
new command and giving it a project name.
$ thingssdk new night-light
newcommand asks for two things: a port and a baud rate. The port you select will be the same one you selected in Flasher.js. The baud rate will always be 115200 while using the NodeMCU.
We also need to go into that directory and install our npm modules:
$ cd night-light && npm i
In main.js, copy/paste this code:
In the code, using
setInterval, we read the resistance from the photocell every second. We use a threshold to determine if the light is detected. Photocells are best to determine the presence of light, not to measure it. A configurable threshold is needed because these photocells are very inaccurate. As mentioned in the Adafruit docs:
They are very low cost, easy to get in many sizes and specifications, but are very innacurate. Each photocell sensor will act a little differently than the other, even if they are from the same batch. The variations can be really large, 50% or higher! For this reason, they shouldn't be used to try to determine precise light levels in lux or millicandela. Instead, you can expect to only be able to determine basic light changes.
Now that we have the light level, we can turn the light on/off based on the presence of light.
Side Note: You may notice that, in the code, we are writing to pin D5. It's not a mistake. If we look at the schematics for the NodeMCU, D1 is GPIO 5. Here is why we use D5 ( GPIO 5) in the code , but we plug the jumper wire into D1.
Deploy Code to NodeMCU
Now it's time to deploy our code to the Node MCU.
$ npm run push
When you generated code with the
new command, it also came with a bunch of helpful scripts that are nicely packaged in
npm run push. You can take a look at exactly what that script does in the
package.json. If everything worked properly, you would see a REPL start with the light level being logged.
Now, if you turn off the lights, or cover the photocell with your finger, the LED should turn on.
That's a wrap!
To create and learn more, Losant has compiled a list of great you can use for inspiration for more projects.
Any Feedback? Drop a comment below!