How to start using NodeMCU for WiFi projects (Goodbye Arduino?)

Ah Arduino. You were my first gateway to making tiny electronic widgets that interact with the world. I’ve played with platforms that range from small and simple to expensive and complex like FPGAs, but nothing beats the cost and ease-of-use of Arduinos for making tiny devices you sprinkle around your home. Until now…

See the thing is, WiFi has become as common as electricity in many countries. So I keep finding that I want all my devices to connect to the internet — something that Arduino doesn’t do very well. Especially if you want to talk at a higher level; like with HTTP or MQTT. Plus, getting WiFi-enabled Arduino chips or going with alternative platforms like Electric Imp can get pricey.

So that led me to a family of devices built around the ESP8266 chip. Not only do these tend to be cheaper than Ardunios, they have integrated WiFi! (E.g.: D1 mini NodeMCU).

But I noticed something popular among users of these devices. They don’t use the normal Arduino platform, but instead a different NodeMCU firmware. I looked it up and what I liked about it was that its firmware libraries help you do many complex things you normally want to, easily. E.g., you can make your NodeMCU device a web server, or talk MQTT with just a few lines of code.

So I got a NodeMCU chip for under $3 to play with one weekend. I figured out how to flash the firmware and upload my code to it. The process is slightly different from using an Arduino. So I compiled all my notes, scripts and whatever else I needed into a bare-bones package. I plan to use this as my personal template for starting new NodeMCU projects. Since it might be useful to others too, I posted it to github here:
https://github.com/secretsciencelab/nodemcu-skeleton

The broad steps for starting a NodeMCU project are:

  1. Download and build the firmware. I recommend using the Docker option. That way, all you do is download the firmware source code from git, customize it (optional), then use docker to build it.
  2. Flash the firmware. The flash command is also in the link in the previous step. Or check out “flash.sh” in my nodemcu-skeleton git.
  3. Upload your code using luatool. See “upload.sh” in my git’s ‘test’ dir for reference.

Although the steps are more ‘manual’ than with Arduino, I actually like it better. I never liked working in the clunky Arduino IDE. I prefer coding in my favorite editor, then uploading to the device via command-line. Working with NodeMCU does come with its own quirks, but they’re not too bad once you get into the groove.

I am still experimenting with these $3 NodeMCU chips, but so far I’m liking it. My first project was a sound monitor to keep an ear on the house while we were away for a few days. This was mainly because my wife was worried about leaks from her new aquarium setup. So first, we placed regular water alarms under the aquariums. The sound monitor measures the sound level continuously and streams the data to the web (via Thingspeak). Then I have a cron job that watches the data stream and emails me if anything looks funny. It’s sensitive enough to detect anything from doorbells, loud conversations and microwave beeps… to water alarms and fire alarms.

I might switch to these NodeMCUs as my go-to device for all my future projects!

Happy hacking 🙂

DIY home automation almost-instant-response WIFI smart switch for < $16 with MQTT

Parts list:

Total: $15.55 ($7.28 if you go with the mini NodeMCU)

WARNING!!! this switch can switch up to 110/240V AC @ 10A. This is more than enough to cook your insides or “stop” your heart and kill you. Do not play with your house mains unless you know what you’re doing and you understand the risks. One mistake and you’re dead. If you’re not sure, close this page. It’s not worth it.

I made this WiFi switch to add an IP camera to the outside of my house, without having to run new power lines to it. The idea was to tap into the 110V AC of an existing outdoor porch light to power my camera. I would then keep the switch to the light ON all the time, to power my camera ON all the time. But I didn’t want my porch light to be on all the time. So, a WiFi smart switch will replace the wall switch to control the light. This was acceptable, since we don’t turn the porch light on/off frequently, and it’s better off scheduled (turn on at night) or automated (turn on when motion detected).

My only constraint was that everything had to fit into an in-wall power conduit or switch box. The largest part was the AC/DC transformer/power supply. I managed to find a 1.25A power supply that is 70 x 39 x 31 mm. As a bonus, you can open its metal case and pack your microcontroller and other electronics inside it:

Open the metal case, pack it in. AC comes in the top. DC goes out the bottom to the Relay, microcontroller and my IP camera.

Open the metal case, pack it in. AC comes in the top. DC goes out the bottom to the Relay, microcontroller and my IP camera.

The input of the power supply is 110/240VAC and the output is 12V DC. My IP Camera uses 12V DC, so it took the raw 12V DC. But the Cactus Micro and the Relay need lower voltage. So I used the DC-DC converter to step down 12V DC to 5V DC.

Closer look at the DC bits

Closer look at the DC bits. The blue board I’m lifting up is the microcontroller that talks over WiFi and switches the relay. USB for scale.

Closer look at my camera

Closer look at my camera

Closed up. Compact enough to fit into a wall conduit/switch box.

Closed up. Compact enough to fit into a wall conduit/switch box.

And that’s the WiFi switch. Wire the switch up like this:

Your WiFi switch controls your light or whatever AC load

Your WiFi switch can now control your light or whatever AC load you want up to 240V 10A

The rest of the magic happens in software. There are 2 parts to the software. 1 part is the code you upload to the Arduino microcontroller. That connects to WiFi, listens for on/off commands and switches the relay. The 2nd part is what sends on/off commands to your smart switch.

Software Part 1:
Let’s set up your MQTT broker first. MQTT is the protocol that we’ll use to send commands to your Smart Switch and switch it on/off almost instantly. You can think of MQTT as an “IoT Twitter”. It lets your switch “subscribe” to a topic. Then something else can “publish” to that topic to turn it on/off. You need a MQTT broker because that’s what passes your messages between publisher and sender.

The easiest way to get a MQTT broker is to sign up for CloudMQTT’s free “Cute Cat” plan: https://www.cloudmqtt.com/plans.html. The 4 pieces of info you need after you set it up are:
– Your broker’s hostname or IP address
– Your broker’s port
– Your broker’s user
– Your broker’s password

Software Part 2:
Arduino code. I used a Cactus Micro, which has 2 chips in one package: an Arduino chip and an ESP8266 for WiFi. 1st step is to program the ESP8266 with Espduino. Espduino is an Arduino library that makes it easy to connect to WiFi and talk MQTT.

  1. Set up your Cactus Micro arduino to flash the ESP8266’s firmware — upload this Arduino progrmamer sketch: http://wiki.aprbrother.com/wiki/How_to_made_Cactus_Micro_R2_as_ESP8266_programmer.
  2. Flash the ESP8266 with the “espduino firmware”: https://github.com/AprilBrother/espduino/tree/master/esp8266/release. Use the NodeMCU Flasher if you’re on Windows and the esptool if you’re on Linux.
  3. Upload the Arduino smartswitch sketch below to your Cactus Micro…

IMPORTANT: Use Arduino IDE version 1.0.6. If you use version 1.0.5 your Cactus Micro will be unstable and randomly crash.

Disclaimer: The code below has a little more than the MQTT on/off stuff. I wanted to also show you how to sync time and also upload data to thingspeak.com. If you don’t need those, just delete them. I included them because I’ve found them useful in all my projects and they show you how you can upload/download regular HTTP via Espduino. I also included some bulletproofing code that reboots the ESP8266 if it malfunctions.

Replace all the “TODO_…” strings with your own info

Once you have everything set up, you can power your switch and test it using the CloudMQTT “Websocket UI.” The code above subscribes your switch to the “/homebot/switches/MY_SMARTSWITCH/cmd” topic. Send a “1” over MQTT to turn it on. Send a “0” or whatever else to turn it off.

Here is mine in action:

Happy switching!

P.S. If you get stuck or have any questions, email me at aaronn@secretsciencelab.com

P.P.S Please don’t die being stupid with electricity.

“Sprinkler Brain” – Cactus Micro Arduino Wi-Fi multi-zone Smart Sprinklers (DIY for under $20)

In some parts of the world, water usage is serious business. For example, there is a drought in California. If you live here, you know that they want you to water your lawn only twice a week. And if they catch you using too much water, they slap you with a higher rate for being a water pig. So water is serious business. But you love your garden and vegetables and trees and such. And they need water. So what can you do?

To be honest, I’m struggling to figure that out myself. My neighbor is getting hip with lush succulents that barely need any water. I might go that route too, but even if you do, you still need to be smart about your water. In fact, as you sprinkle more diverse plants around your landscape, watering might get more complicated since different plants need different amounts of water. And the first step to optimizing water usage is being able to CONTROL water flow from a computer program. On, off. Once we can do that, you can smart it up any which way to optimize when to switch sprinklers on or off. So that’s the main goal of this write-up — to show you how to set up a cheap and bulletproof base rig to programmatically switch your sprinklers.

But since we’re building, how about we make a wishlist of all the things we can do better than grandpa’s garage wall sprinkler controller:

  • Program sprinkler schedule from anywhere, without going to the wall unit
  • Never lose your programs when you lose power or forget to replace the backup battery
  • Automatically skip sprinkling when it rains
  • Measure how much water each sprinkler zone uses
  • Never have to physically be at the wall unit to turn it on or off
  • Must be able to manually turn individual sprinklers on and off from your phone, when you want to test and fix sprinklers around the yard
  • Must be able to spray your kids/neighbors/dog/squirrels with the tap of your finger, from the comfort of your armchair
  • Etc.

Parts list:

Total: $17.14

Now here’s what we’ll be building…

The secret plans

My secret schematic. Disclaimer: use at your own risk, I’m not responsible for death or damages, don’t play with electricity, etc etc

S1 and S2 at the bottom are your sprinklers. 24 AC on the right is the standard power supply for your sprinkler valves.
Block (A) on the left is the 8-channel relay.
Block (B) in the middle-ish is the Cactus Micro microcontroller.
Block (C) is the AC/DC converter to tap 24V AC from the standard sprinkler circuit and convert it to 5V DC for our control circuit.

At this point you might be wondering why is there a transistor in the middle of the schematic, if we already have a relay. Well, you don’t need it. But I put it in there as a safety feature. Remember how water is serious business? I figured that I’m okay if my chip malfunctions and it doesn’t turn on the water. But what’s worse is if it malfunctions and the water gets stuck OPEN and floods the yard and the sidewalk and the street and neighbors while I’m away. That would be an expensive disaster.

The possibility crossed my mind because I fried one of my Cactus Micro pins while soldering the headers, and it got stuck and pulled one relay open. Had that relay been connected to a sprinkler, and had that sprinkler locked open while I wasn’t home, I would have returned to a $1000 water bill before I could shut it off.

So that’s why I added that extra transistor switch. (I would have added 5 more safety mechanisms if I had the patience.) It functions like a Two-man rule control. Like when they needed two keys to arm the nuclear missile in the Hunt for Red October. You can see that the transistor guards the power supply of the relay. What this means is that TWO things have to work to open a sprinkler relay:
1) an “open” signal from the Cactus Micro microcontroller D1-D8 pins to the relay’s IN1-IN8 pins
2) an “enable” signal from the Cactus Micro’s D15 pin to the transistor

This way, two pins have to fail for the relay to be stuck open — which is still possible — but less likely than one pin failing. Can’t be too sure.

OK. Now we build. First, prepare the Cactus Micro. The original firmware is garbage. We want to replace it with espduino. Espduino gives you rock-solid WiFi and lets you work like a civilized person via a REST API, not a caveman via serial commands.

Follow instructions in the 2 links below. First upload the Arduino sketch to configure the Cactus as a serial programmer. Then flash the ESP8266 firmware through the Cactus host board.
http://wiki.aprbrother.com/wiki/How_to_made_Cactus_Micro_R2_as_ESP8266_programmer
https://github.com/tuanpmt/espduino

After the espduino firmware is on the ESP8266, replace the “serial programmer” sketch above with our real “Sprinkler Brain” sketch below:

Some noteworthy bits in the sketch above:
– syncClock() syncs the time on your Cactus Micro with timeapi.org every 15 minutes
– readHygro() is an example of how you might add a sensor to your system
– fetchCommands() fetches commands from my “cloud” mothership (in the form of a string like

where 1 is force on, 0 is force off, – is follow program).
– uploadData() pushes Cactus data/state to thingspeak.com servers. I use this thingspeak data stream to render my smartphone “app” UI
– restGet() is the wrapper function I use to make HTTP REST calls. It counts errors so that if I see too many consecutive ESP8266 errors, I can power-cycle the ESP8266.
MinuteMap is the compact data structure I use to store my sprinkler schedule
– The Time.h Arduino library is from here

I set up my “server” on Google App Engine. It’s awesome and Google gives you a very generous free daily quota. Unlike Amazon’s EC2 which rails you with no lube and annoying bills even if you do something innocent like leave one terminal connected to your instance. With Google App Engine, I have never needed more than the free daily quota, even with my many projects bashing one app.

(Email me at aaron@secretsciencelab.com if you want my AppEngine code. I didn’t have time to carve it out and clean it up to post here)

Then I made a simple web app UI which serves 2 purposes:
1) manually turn sprinklers on/off to impress friends
2) monitor the sprinklers in action and to make sure it’s working

Here we can see that Sprinkler Channel 1 is running and it has 7 mins left on its schedule

Here we can see that Sprinkler Channel 1 is running and it has 7 mins left on its schedule. I can tap the on/off buttons on the left to manually override the schedule.

(Email me at aaron@secretsciencelab.com if you want my HTML/Javascript web app code. I’m happy to share, just too lazy right now to package it nicely to post here)

After you have the software loaded, hook it up following the schematic. Hopefully you will end up with something that looks better (and is less of a fire hazard) than this:

AC/DC converter at top left. Mini breadboard to connect wires at top right. Transistor is on breadboard. 8-channel relay at bottom of box. Cactus micro with gazillion wires going to the breadboard.

AC/DC converter at top left. Mini breadboard to connect wires at top right. Transistor is on breadboard. 8-channel relay at bottom of box. Cactus micro with gazillion wires going to the relay and breadboard. I used the Rev1 Cactus in this pic. You should get the Rev2.

Action shot:

Happy Sprinkling!

How to flash espduino firmware on Cactus Micro ESP8266 rev1

Modify Cactus Micro:
http://blog.aprbrother.com/p/283
1) convert default serial port to hardware serial (cut/solder)
2) connect Digital pin 5 to ESP8266’s GPIO0 (so arduino sketch at the bottom of this page can pull ESP8266’s GPIO0 to GND – this puts the ESP8266 in program mode to receive firmware update)

Load Arduino programmer sketch – sends computer COM serial straight to ESP8266 hardware serial
https://github.com/volca/arduino_esp8266_programmer/blob/master/arduino_esp8266_programmer.ino

Flash firmware. Do NOT use esptool.py. I kept getting this error:

Instead, use the GUI for Node MCU
http://randomnerdtutorials.com/flashing-nodemcu-firmware-on-the-esp8266-using-windows/
https://github.com/nodemcu/nodemcu-flasher

Click Config tab
Uncheck the internal Node MCU firmware (selected by default)
Browse for espduino 0x00000 bin
Browse for espduino 0x40000 bin

Enjoy espduino!
https://github.com/tuanpmt/espduino

Important: when using the serial passthrough Arduino sketch, don’t change the default baud rate of espduino – I tried to be smart and set it at 9600 — didn’t work.

Below is the “passthrough” sketch I used to stream the firmware from USB through Arduino serial through ESP8266 hardware serial. It’s ugly and there are many other similar sketches, but I’m saving the one I used here, just in case.

P.S. You’ll probably want this Arduino library next:
ArduinoJSON
https://github.com/bblanchon/ArduinoJson/wiki/Using%20the%20library%20with%20Arduino

DIY Electric Imp WiFi garage opener + sensor for under $50 (weekend project)

Background story / motivation
One night, I opened my garage door to take out the trash. Then I came back in and went to bed. The next morning, as I was strapping my son into his car seat, he asked me, “Daddy, why is that exercise ball over there?” “What? What exercise ball?” “That one. Who put that ball there?” I replied without looking, “I don’t know, I didn’t put it there.” He said, “Maybe mama put it there.” “Maybe she did.” “Maybe it fell.” I looked back and saw it in the middle of the floor. Strange. Didn’t seem like her to leave it like that. Then I looked around. My toolbox was gone. I looked left. My bicycle was gone. I peeked into my wife’s car. Every compartment was open and trashed.

I had left the garage door open and we had been robbed.

Since then, whenever I left the house, I found myself checking and double-checking the garage door. And sometimes even after triple-checking… I would drive back to check ONE MORE TIME. Because you can’t be too sure. Even when I was home, I found myself constantly peeking into the garage to make sure I didn’t leave the door open again.

It was driving me nuts. So I decided to do something about it. If you’re like me, this weekend project will finally give you some peace of mind. If you’re handy with electronics and code, you could even get it done in one evening. When you’re done, you’ll get to open your garage door from your phone. And best of all, you’ll always know if your door is open or closed.

(By the way, before going for this solution to the garage door problem, I considered leaving my garage door open again. I really wanted to set a trap to destroy whoever dared to rob me again. But my wife said no. So here’s just some instructions for a garage door opener… instead of an automated crossbow turret.)

Parts list

Total: $44.75

Tools list

Finished pictures

Mini breadboard ties everything together. The Imp is sitting in the April Breakout board. The April board's soldered header pins plug into the breadboard. The blue thing is the relay with 3 pins in the breadboard. You see a switch in the picture but I replaced that with an Ultrasonic sensor. The problem with the switch was my garage door doesn't always stop at the same place, so no physical switch was going to reliably trigger. On the other hand, the Ultrasonic sensor measures accurate distances up to 3m, so you can make a software switch that you can tune to perfection. Dinosaur for scale.

Mini breadboard ties everything together. The Imp is sitting in the April Breakout board. The April board’s soldered header pins plug into the breadboard. The blue thing is the relay with 3 pins in the breadboard. You see a switch in the picture but I replaced that with an Ultrasonic sensor. The problem with the switch was my garage door doesn’t always stop at the same place, so no switch was going to reliably trigger. On the other hand, the Ultrasonic sensor measures accurate distances up to 3m, so you can make a software switch that you can tune to perfection. Dinosaur for scale.

I SEE YOU, garage door

I SEE YOU, garage door

Goodies tucked into small cardboard box on the side. USB power from ceiling.

Goodies tucked into small cardboard box on the side. USB power from ceiling.

Black wires go from relay to the same terminals used by the wall switch.

The tiny black wires connect the relay to the same terminals used by the wall switch.

Smartphone UI screenshot. Just a big button. The three checkboxes below it are to arm the button. This is so I can't accidentally press the big button.

Smartphone UI screenshot. Just a big button. The three checkboxes below it are to arm the button. This is so I can’t accidentally press the big button.

Circuit diagram

I drew this with Fritzing!

I drew this with Fritzing!

Electric Imp setup/prep

Solder male headers onto Imp

Electric Imp Agent code

Electric Imp Device code

(Optional) rolling authentication code
I made a Google AppEngine app to be a gatekeeper and for the smartphone UI to open/close the door. The idea is:

  • Take advantage of AppEngine’s built-in authentication to control who can access your app
  • When an authorized person taps “Open”, save a random “rolling code” into your web app’s DB
  • Then, tell your Electric Imp to “Open” and pass it the rolling code you just generated
  • When your Electric Imp Agent receives the “Open” command, it checks the received rolling code with your AppEngine webapp
  • If the rolling code matches the latest code in the DB, the webapp destroys the code (so it cannot be reused) and the Imp Agent tells the Imp Device to open the door

If you want to go this route and need more info, email me at aaron@secretsciencelab.com. If enough of you want it I’ll put it here. I’m just too lazy right now!

Installation
The hardest part is soldering header pins to the Imp, and soldering wires to the Ultrasonic sensor. But other than that, everything else is pretty much plug and play. That’s the beauty of using the Imp, the Keyes Relay and an Ultrasonic sensor. All the electronics are nicely packaged in each of them, so all you need to do is connect them with wires.

I stuffed everything into a small cardboard box and tied it to the frame of the garage door control box. I plugged the Imp’s USB adapter into the same outlet that powered the garage door control box. Lastly, I secured the Ultrasonic sensor to a bolt under the garage door rail using a twist-tie. Dirty, but simple!

Bonus (advanced)

  1. Push your sensor data to data.sparkfun.com for free:
    E.g., door open, door close

    E.g., door open, door close

  2. Convert your SparkFun CSV stream into an Atom RSS using my URL: http://secretsciencelab.com/wp-content/scripts/phant/feed.php?csv=http://data.sparkfun.com/output/YOUR_PUBLIC_KEY.csv&key=state
    (replace the bolded parts).

  3. Plug your Atom RSS URL above into IFTTT as a Feed recipe.
  4. Now you can tell IFTTT to: “If new Feed item, then do something.” E.g., “If my garage door opens or closes, text me.”

Happy hacking 🙂

aaron@secretsciencelab.com

Arduino Vader “Force Grip” remote car (a.k.a. smartphone virtual wheel)

The 3-wheel car is built with the Makeblock Robot Starter Kit. (Plus some Legos for style.) It has a Bluetooth module. It is controlled by the phone’s gyros via Bluetooth. You drive it like a steering wheel with two hands, or like Darth Vader with one hand.

1. Code for the smartphone below. I saved this as gyroCar.py and transferred it to my phone (LG Optimus Dynamic). Then I execute/run it using SL4A from the phone itself:

If you use the code above, the part that you’ll need to tweak for your own build is the DroidGyroJoystick class. Particularly, the pitch/roll numbers my phone gives lying on one edge may be different from yours. The other things you might want to change are the tuning parameters. For example, I configured a “deadzone” with accelZero and turnZero so that small motions near “centered” position won’t count as joystick inputs.

(If you’re new to SL4A, I show how to get set up, as well as where the android.py in “import android” above comes from, here.)

2. Arduino sketch for the robot car below. Sorry it’s unnecessarily complicated. It’s because it’s code for a 2-in-1 IR or Bluetooth + obstacle-avoidance car. If you connect only an IR sensor to Port 6, it will be IR-controlled. If you connect only a Bluetooth module to Port 7, it will be Bluetooth-controlled. Why did I do such a thing? Because I wanted to be able to switch remotes for my kids just by swapping modules, without uploading sketches.

Anyway, what’s important here is that in Bluetooth mode, it recognizes my silly custom command set (fwd, left, right, bwd, etc). These command strings are how the SL4A Python script on the phone instructs the car what to do, over Bluetooth. The other useful thing is that the Bluetooth commands let me specify the speed, in addition to direction. So the more you push your joystick forward, the faster it’ll go.

P.S. I just installed the WordPress “Crayon Syntax Highlighter” plugin. It prettifies blocks of code within ‹pre› tags, like above. I like it so far.

Teaser: Makeblock Robot Starter Kit Cellbot + Sensors + shxtty Path Tracking

Well this was fun. Ingredients:

  • Makeblock Bluetooth module
    To execute instructions from smartphone.

  • LG Optimus Dynamic
    Strapped on robot for sensors. WiFi to talk with Mission Control. Bluetooth to control robot.

  • Tornado server
    Runs on PC. Connects to phone via SL4A. Talks with web UI using websockets.

  • Websockets
    To stream sensor data from phone to web UI. Also relays commands from web UI to robot.

  • Web UI
    Basic webpage with Javascript to handle keyboard events and draw things

  • D3.js
    For rendering the accelerometer G-meter (not shown in video)

  • HTML Canvas
    I just used a 2-D grid and rectangles (fillRect) to draw the robot’s snail trail.

  • Physics
    v = d/t; d = v*t
    Estimate distance traveled by robot using motor “velocity” and time.

  • Ye Olde Trigonometry
    To convert bearing + distance traveled to x-y position in room.

You know, this could make a good school project. Lots of applied basic science, math and programming.

I promise to put up details and code in a later post 🙂

(If I forget, please email aaron@secretsciencelab.com and I’ll share everything with you.)

How to fix SL4A webCamFacade exception

On some phones, if you try starting the webcam using SL4A, you will get the following exception:

java.lang.RuntimeException: setParameters failed

Yuck!

Thankfully, the fix is simple. I found Paola Garcia‘s solution everywhere I looked on the internet. She was cheerfully answering everyone’s questions all over the place. She said she fixed the problem by changing the preview format from JPEG to NV21:

NV21

Thanks Paola!

But while the fix is simple, the hard part is you have to build a new SL4A app (APK) with the fix. Good news! It’s not really that hard because someone was kind enough to leave instructions. It was a good learning experience for me.

First, follow the first 2/3rds of this post:
http://jokar-johnk.blogspot.com.es/2011/02/how-to-make-android-app-with-sl4a.html
Do only the “compile” steps. The last “how to package your app” part doesn’t really apply to you, since you just want to build SL4A.

I’m writing this post because I ran into some problems using the instructions above. I’m sharing my solutions in case anyone else runs into them too. I think most of the problems are because the post was written in 2011, and the latest libraries and SDKs in 2014 don’t play as nicely together.

The step you’ll want to substitute is when you Import your project source code. Instead of the “official” SL4A, Use hg to clone the SL4A repository from Paola Garcia‘s branch:

hg clone https://olapaola@code.google.com/r/olapaola-android-scripting/

(FYI: I used Tortoise Hg on Windows.)

Now here are the problems I ran into and how I fixed them. (Just in case anyone else runs into them too.)

  1. Missing Android SDK files when compiling
    SL4A needs APIs from the older Android SDK platforms. Check C:\Users\yourname\AppData\Local\Android\sdk\platforms. If you’re missing the directories android-3, android-4, android-7, android-8, you need to download/install them.

    In Eclipse: Window -> Android SDK Manager. Select all the Android SDKs til API Level 8 (Android 2.2).

    Download & install

    Download & install

  2. conversion to dalvik format failed with error 1
    I got this when trying to export the APK after compiling. I couldn’t figure out what the problem was for a while because stupid Eclipse just gave me the error message with no console logs. I finally found the problem when I did “Project -> Run As…” It gave me a helpful error message I could debug. Something about the JDK libraries was conflicting. I made two changes:

    Download JDK 1.6.0_26 and select it here

    Download JDK 1.6.0_26 and select it here

    Drop the Java compiler to 1.6

    Drop the Java compiler to 1.6

  3. Export APK
    I’m an Android noob so this step tripped me up too.
    Export signed package

    Export a signed package. Create a new keystore. I filled in blanks with B.S. and set passwords to “android”.

Finally, out pops your fresh APK with WebCamFacade fixed. Transfer the APK to your phone, uninstall the old SL4A app and install this new one.

Happy hacking! If these instructions were helpful, leave a comment below. Let us know what silly things you used your camera for. I know I have my eye on OpenCV 🙂

How to build a Makeblock Bluetooth/WiFi Cellbot car

Ingredients:
– LG Optimus Dynamic
– PC game controller (joystick)
– pygame for joystick input
– python script runs on PC to translate joystick input to movement commands
– SL4A to send movement commands from PC to smartphone over WiFi
– smartphone sends movement commands to Arduino via Bluetooth (also SL4A)
– Arduino translates commands to movement

First, we taught Bao to listen with Bluetooth

That was using pyserial, connecting directly from PC to Arduino via COM17.

I used that to build a basic instruction set to control movement. E.g.,:
!fwd 10
!bwd 5
!left 3
!right 10
!fwdleft 10 5
!bwdright 6 6
!stop

Those instructions are parsed and executed on the Arduino. The numbers tell how much the joystick was deflected.

But it wasn’t very interesting to connect directly from PC to robot via Bluetooth. I wanted to strap my smartphone on to add that extra layer of sensors and computing power. So my scheme was for my PC to talk to my smartphone and for the smartphone to be like the Jewel Wasp, controlling the robot via Bluetooth.

This was the main Python code running on my PC:

And this was what I had on the Arduino: