WiFi Car with NodeMCU
By chance I saw at http://www.banggood.com an interesting WiFi controlled toy car (order number SKU256467) for about $22. Normally such a toy is closed source, but not in this case. After some digging in the Internet, I found an enormous amount of open source documentation and an active community, starting with the ESP8266 System on Chip (SoC). There is also an Arduino plugin available Links #17, to make programming in C easy . So, in summary a lot of ingredients for a nice low cost maker project (buzzword: Internet of Things, IoT). Lets start the description from the core, the ESP8266.
Since end of 2014 there is a very interesting WiFi module with application processor (32 bit) on the market, called ESP8266, at a price of about 3 EUR. The main application is a Serial to WiFi Bridge, named AT. But it can do a lot more.
Look at http://www.ebay.de for ESP8266 Serial WIFI Wireless Transceive Modul LWIP AP+STA. In early 2015 the EPS8266 module ESP-12E started on the market. It has 18 active pins, see Links #4.
Short thereafter a 30 pin Dual-Inline (2.54 mm spacing) module named NodeMCU (Version 0.9, later 1.0) appeared on the market, which carries an ESP-12 module and a Serial to USB converter with micro USB socket. The pinout and schematic can be found at Links #5. The firmware API description you can find at Links #6. This module is similar to the Arduino Nano 3 module.
For the NodeMCU you can get a motor shield for either 2 DC motors, or 1 stepper motor, see at Links #7.
That motor shield placed on a car chassis with 2 driven DC motors will give a WiFi-car, that can be radio controlled (WiFi) via smart phone, see at Links #1, and the picture on the right.
On the following pages I describe, how to program such a nice System on Chip (SoC), containing a lot of debug assistance and HowTo about the software routines.
I made all programming with the Arduino IDE version 1.6.5.
A big Thank You for all the genius programmers, who created that.
Click on the pictures to enlarge them.
WiFiCar setup
It is not easy to setup the WiFiCar with the provided software. You should have good knowledge of network handling, and Arduino and C programming. Following the steps you need to do:
Install the Arduino IDE (Integrated Development Environment) and the ESP8266 plugin Links #17 for programming the NodeMCU.
With Arduino IDE upload my program wifi-car-AP.ino (Access Point mode) to the NodeMCU of the car.
Power Up the NodeMCU, and try to find the WiFi hotspot (name: wifi-car) on IP address 192.168.4.1.
Upload the provided JavaSript program wifi-carAP.js to the NodeMCU, with the URL http://192.168.4.1/upload (you will be asked for the File name/path).
With an Android Smartphone connect with WLAN to the WiFicar Hotspot (name: wifi-car).
Enter in your Web browser the URL http://192.168.4.1 . A HTML file is loaded (embedded in the C program) and the Java script file is loaded and executed.
As soon, as you tilt the smartphone, the WiFicar will move in that direction.
All the needed details you will find in the following documentation.
Doit WiFi car
It was developed from http://www.doit.am/ (Shenzhen Doctors of Intelligence & Technology). An introduction can be found at Links #9.
The chassis plate is made of transparent plexiglass. The brown color you see on many photos derives from the protection film covering the plexiglass. Once you peel it off, you will see the transparent plexiglass.
The assembly of the Do-It-Yourself kit is quick and easy, but it needs soldering the connecting wires from the power supply (4 x AA batteries, or rechargeables), power switch and the 2 motors.
A simple control program can be installed from the Android Play Store (App: Car(AP Mode)). The Access Point (AP) mode is the default of the delivered controller. Yes, the Android app works, but the control is really difficult, see the picture on the right. The kit accompanied ESP8266 is already programmed with the proper program to make an WiFi Access Point.
Fortunately I found another Android program, which controls the motion of the WiFi-Car via the orientation of a smart phone, keep reading.
For changing the forward/backward direction of the car, it helps to put a few drops of thin oil in the ball bearing of the back wheel.
ESP8266 module
Features:
- SDIO 2.0, SPI, UART
- 32-pin QFN package
- Integrated RF switch, balun, 24dBm PA, DCXO, and PMU
- Integrated RISC processor, on-chip memory and external memory interfaces
- 64 KiB of instruction RAM, 96 KiB of data RAM
- External QSPI flash - 512 KiB to 4 MiB
- Integrated MAC/baseband processors
- Quality of Service management
- I2S interface for high fidelity audio applications
- On-chip low-dropout linear regulators for all internal supplies
- Proprietary spurious-free clock generation architecture
- Integrated WEP, TKIP, AES, and WAPI engines
For more details please see at WiFi2AT-ESP8266.
ESP-12E module
The most versatile module with printed PCB antenna, see also Links #10/11:
- 22 x 2.54 mm pitch header
- built-in PCB-printed antenna
- 18 IOs
- 4 MByte Flash ROM
- LED, blue on GPIO2
- VCC/GND 3.3 V
- Dimensions: 24mm x16mm x 3mm
The WiFi-car signals are also mentioned. Pinout: Antenna Signal Name pin pin Signal Name RST Reset 1 22 TXD0 Serial 0 TXD WARNING: 3.3V adapter ONLY ADC Analog IN 2 21 RXD0 Serial 0 RXD WARNING: 3.3V adapter ONLY EN 3 20 GPIO5 Motor PWMA GPIO16 4 19 GPIO4 Motor PWMB GPIO14 5 18 GPIO0 Button Flash, Motor DirA GPIO12 EncA D6 6 17 GPIO2 LED, blue, Motor DirB GPIO13 EncB D7 7 16 GPIO15 VCC 3.3V ONLY 8 15 GND pin, Signal 9 CS0, 10 MISO, 11 GPIO09*, 12 GPIO10*, 13 MOSI, 14 SCLK Note: The pins 9 to 14 are exclusively used for Quad-SPI communication with flash memory. They can't be used for application!
NodeMCU
NodeMCU is an ESP-12E module mounted on a carrier board, together with an USB to serial converter. Have a look to the user manual, see Links #12. In short some details:
NodeMCU = ESP-12E DevKit
- Support STA/AP/STA+AP 3 working modes
- 0~D8, SD1~SD3: used for GPIO, PWM, IIC, ect; the driven ability can be arrived at 15mA
- LED, blue on GPIO16, for debug purpose
- AD0: one-way ADC, 0 to 1.0 V, 10 bit = 0..1023
- Power input: 4.5V~9V(10VMAX), support USB powered and USB debug
- Working current: ≈70mA(200mA MAX, continue), standby<200uA
- Support update firmware remotely (OTA)
- Size: 25.4 x 48.3 mm
- Firmware supports LUA programming language
Wikipedia has some information, at Links #13.
The official user manual is at Links #12.
USB interface: I have seen that you can have a SiLabs CP2102 or WCH CH340G (Winchiphead) chip on the board. In case of MacOS Sierra 10.12.1 you need for the CH340G a new driver version 1.3, otherwise you get a kernel panic when pluggin in the module. I got the information from here.
Since 2015-09-30 a SPIFFS (Serial Peripheral Interface Flash File System) software was included in the Arduino plug-in. Now you can upload and download files by name, which are store in the SPI Flash ROM. For initialization you need to format the SPIFFS. This can be done in the Arduino (version 1.6.5) software, with the menu Tools/ESP8266 Sketch Data Upload. Beforehand you must place your file(s) in a data folder in your project folder. This will format and overwrite the complete SPIFFD area. Another possibility to format provides the Flash test program Arduino/hardware/esp8266com/esp8266/tests/FSWrapper.ino, you just upload it into the ESP8266 and let it run.
Motor shield
At http://www.banggood.com you can get a motor shield (part number: SKU256454) for about $5. The NodeMCU module fits in the dual inline socket. Some short data to the shield:
- Motor power supply (VM): 4.5V ~ 36V, can be powered separately
- Control power (VIN): 4.5V ~ 9V (10VMAX), can be powered separately
- Logic Operating current Iss: ≤60mA (Vi = L), ≤22mA (Vi = H)
- Driver IC: L293, drive = PWM (Pulse Wide Modulation)
- Drive part of the work current Io: ≤1.2A
- Maximum power dissipation: 4W (T = 90 ℃)
- Control signal input level: High: 2.3V≤VIH≤VIN; Low: -0.3V≤VIL≤1.5V
- ESP12E Dev Kit Control Port:
- GPIO0: Motor DirA, Button FLASH; GPIO5: Motor PWMA
- GPIO2: Motor DirB; GPIO4: Motor PWMB
The screw terminal is labeled with the signal names.
The push button switch connects VIN to the board.
For the user manual see at Links #14.
Note: the motor shield won't fit for a NodeMCU V0.9!
With an on board jumper behind the screw terminal you can connect the two voltage inputs together.
Make sure to plug in the NodeMCU module in the orientation, that the WiFi antenna fits the sign on the board.
Wheel Encoder
Within the kit, two 20 slot encoder wheels are supplied, which are mounted on the axis of the motors. I mounted two sets of fork light barrier modules (Ebay #351291975401) above the encoder wheels, see the picture on the right.
Every slot of the encoder wheel gives 18° turn of the motor wheel.
Lets calculate the distance:
- Motor wheel diameter: about 70 mm
- Motor wheel circumference: 70 mm x 3.1414 = 200 mm
- Encoder step distance: 200 mm / 20 = 10 mm
- Signal to ESP-12E: EncA - GPIO12, EncB - GPIO13
Next step in the development is to drive distances, with the help of the encoders.
Motor data
In order to know how fast the car can run, it is important to know the motor data. By chance I found the motor specification for the DC Motor, listed in the table on the right.
Working Voltage |
DC 3V |
DC 5V |
DC 6V |
Working Current |
100mA |
100mA |
120mA |
Reduction Ratio |
48:1 |
||
No load (with wheel) |
100 RpM |
190 RpM |
240 RpM |
Wheel diamter |
6.6 cm |
||
Speed (no load) |
20 m/min |
39 m/min |
48 m/min |
Weight |
50 g |
||
Size |
70x22x18 mm |
||
Noise |
< 65 db |
Please consider, that the motor needs a minimum voltage to start running.
Community Forum and Wiki
There is a ESP8266 Community Forum and wiki at Links #22.
I will mention important postings.
- how is the code in the spi flash executed? is it memory mapped? or copied to (D)RAM?
- Copied to DRAM on reset
- The SoC is built around the Tensilica Xtensa LX3 processor (see the compiler info in the library binaries)
- The CPU is clocked at 80 MHz (esp_iot_sdk_v0.6/include/eagle_soc.h, line 58)
hackaday.io info, see Links #23
- how is the code in the spi flash executed? is it memory mapped? or copied to (D)RAM?
Official GCC compiler VM from Espressif
Alternative: github esp-open-sdk
Wireless Througput Tests with netio, about 20 KB/s RX.
Youtube, LUA firmware einrichten (German, 31 min, 2015-01-17)
WiFi Car control
In Access Point mode you connect your smart phone direct to WLAN hotspot wifi-car. You need no password. To find out the IP-Address, connect the NodeMCU to your PC via USB, start the Arduino IDE and the Serial Monitor. After pushing the Reset button the Serial Monitor will show you the IP-Address. Enter in the URL field of your web browser that IP-address.
In Station Access mode either have a look in the administration page of your WiFi router, or use the Arduino Serial Monitor as mentioned before.
At Links #15 you will find a description and the source code to control your WiFi-car with the orientation of your smart phone acceleration sensor. Unfortunately this design has a few drawbacks:
The Javascript program needs to be downloaded from a web server, in the run. That means, you must have an online connection to the Internet. For development purpose that is welcome, but for normal operation it is cumbersome.
The WiFi-car control is difficult, because the motors starts turning from above 600 (control range 0..1023) on, and stop at below 300. This was not taken into consideration.
- In the speed calculation I found a bug (Math.min).
- The call of the Javascript program must be at the end of the HTML statements.
With the Arduino plug-in examples and the help of the ESP8266 community I managed it to rewrite and extend the software to an Stand alone Access Point, which can be used without Internet access.
WiFi Mode
In WiFi Access Point mode, if not otherwise specified in the program, the IP Address for the wifi-car (Access Point, hotspot) is set to 192.168.4.1 and the connected client will get via DHCP the IP-Address 192.168.4.2.
In WiFi Station mode, you must define SSID and password of the WiFi Router in the program (wifi-car-AP.ino). When connecting to the WiFi Router the wifi-car will get via DHCP the IP Address. You can find the IP Address in the Smartphone, or via web interface from the WiFi Router.
Program wifi-car-AP.ino
Let me describe the program from top to bottom. The functions for file handling are copied from program example FSBrowser.ino. The ESP8266 Arduino Core Reference API is described in Links #17.
The const char* serverIndex is a HTML form to access the filename for upload to the SPIFFS.
With #define ACCESSPOINT you decide if the ESP8266 will run as WiFi Access Point with the Javascript program in SPIFFS (for car use), or if commented out, as Station Access to a WiFi router, with the Javascript program on a web server, for easier software development. Please adopt SSID and password according to your WiFi router.
The new ESP8266WebServer is used, instead of the Arduino like web server.
Function handleFileCreate() - will create an empty file in SPIFFS for later editing, but is not used here. See for more details Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino.
Function handleFileDelete() - will delete a file in SPIFFS. If a leading "/" is missing, it will be added.
call syntax: http://<URL>/delete?filename=<filename>
Function handleFileList() - list the files in SPIFFS, and send them back to the web browser as JSON array.
call syntax: http://<URL>/list?dir=/
- Example output: [{"type":"file","name":"wifi-car.js"},{"type":"file","name":"data/wifi-carAP.js"},{"type":"file","name":"data/edit.htm.gz"},{"type":"file","name":"data/graphs.js.gz"},{"type":"file","name":"data/index.htm"},{"type":"file","name":"data/favicon.ico"}]
Function handleFileRead(String path) - Read (shown in web browser) the specified file from SPIFFS.
call syntax example: http://<URL>/wifi-carAP.js
Function handleRoot() - This loads (web browser) the file index.html for the car application.
call syntax: http://<URL>/
Function handleMotor() - This function decodes the motor drive data (XML) from the Javascript program (running in web browser), and feeds the data to the IO ports.
Function setup() - Initialization
- The blue LED is flashed for 100 ms, to show the start of the program.
The WiFi Access Point is started, or in Station Access mode the WiFi router is connected.
The function handles for the web access are setup.
- The GPIOs are initialized.
- The SPIFFS content is listed on Arduino Serial Monitor.
- The SPIFSS info is shown on Arduino Serial Monitor.
Next, the upload command is handled:
The handler for the command http://<URL>/upload sends to the web browser a HTML form serverIndex, to select the filename for upload. This is usually done from a PC, not from a smart phone.
ESP8266WebServer function onFileUpload() - When you click in the HTML form serverIndex on the button Update this function is started, and writes the file to SPIFFS, in pieces of 2048 bytes. An already existing file with the same name will be overwritten.
After finishing the file write the handler server.on("/update", HTTP_POST is called to report success or Fail to the client. Then a SPIFFS file list is shown on Arduino Serial Monitor.
- The blue LED is flashed for 300 ms, to show the end of initialization.
Function loop() - That is the main loop of the program, it just takes care of the HTTP traffic. All other functions are event driven.
Show/Download wifi-car-AP.c, after download please rename the extension from .c to .ino for the Arduino IDE. I had to rename it to .c, in order to get the lexer to work (color printing).
Javascript wifi-carAP.js
Let me describe the program from top to bottom.
The orientation of the smart phone is send every 200 ms via AJAX (Asynchronous JavaScript And XML) to the ESP8266 (function move_car(left, right)), see also Links #16. The update rate of the accelerator sensor is about 20 ms, see also Links #19/20/21.
If you tap on the smart phone display on one of the direction buttons, a maximum drive signal is send for 200 ms duration (function move(dir)).
Then it is checked, if an accelerator sensor is on board of the smart phone. If yes, the trap handler deviceMotionHandler is setup.
In function deviceMotionHandler the smart phone orientation (x, y, z; -10.0 .. +10.0) is converted to a motor drive value 0..1023, and shown on the display (left, right). As a debug help, the program version number is also shown on the display.
Show/Download wifi-carAP.js
Feedback
For any questions please use the KontaktEmail.
Links
2WD L293D WiFi RC Smart Car with NodeMCU + Shield for ESP-12E based on ESP8266
Motor Shield Board (L293DD) for NodeMCU from ESP8266 ESP-12E
List of pages in this category:
-- RudolfReuter 2015-11-15 17:50:55
Go back to CategoryAVR or StartSeite ; KontaktEmail (ContactEmail)