Uncategorized

ESP32 + MicroPython + Mac, Wi-Fi scan example

I have been trying to upload MicroPython to a ESP32 on a Mac. It uses the CP210x serial-to-usb driver, so you need to have this installed before you even bother.

img_6988
ESP32, an amazing little board.

Flashing MicroPython from a Mac to ESP32

I got MicroPython from the official site, at http://micropython.org/download/#esp32. Current version is v1.9.3-240-ga275cb0f, from January 21st, 2018 (today).

To upload it, I used the NodeMCU flasher tool, which I got from Marcel Stör’s github. It allows you to use a GUI to Flash ESP8266 and ESP32 boards. You might need to install WxWidgets – the python’s PIP doesn’t work, but the brew one works. Easy peasy:

brew install wxpython

And you are good to go:

git clone https://github.com/marcelstoer/nodemcu-pyflasher.git

And follow the instructions on how to build it.

Once you have it, you can try the GUI, but when I try to flash it, I have an error like this:

rst:0x1 (POWERON_RESET),boot:0x16 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
Falling back to built-in command interpreter.
OK
>ets Jun 8 2016 00:22:57

So, I went to the directory where I cloned it, and ran from the command-line:

python esptool.py --port /dev/cu.SLAB_USBtoUART write_flash 0x1000 ~/Downloads/micropython-esp32-20180121-v1.9.3-240-ga275cb0f.bin

Which gave me a working MicroPython on the ESP32.

Wi-Fi Scan

Making the wifi scan test is a four-liner:

import network
station = network.WLAN(network.STA_IF)
station.active(True)
station.scan()

This is the output I get on Arduino Serial monitor, from the reboot to the moment I type those commands (shown in red):

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4332
load:0x40078000,len:0
load:0x40078000,len:10992
entry 0x4007a6c4
[0;32mI (205) cpu_start: Pro cpu up.[0m
[0;32mI (205) cpu_start: Single core mode[0m
[0;32mI (205) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (208) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM[0m
[0;32mI (215) heap_init: At 3FFDCD68 len 00003298 (12 KiB): DRAM[0m
[0;32mI (221) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM[0m
[0;32mI (227) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (233) heap_init: At 4008FC7C len 00010384 (64 KiB): IRAM[0m
[0;32mI (240) cpu_start: Pro cpu start user code[0m
[0;32mI (33) cpu_start: Starting scheduler on PRO CPU.[0m
OSError: [Errno 2] ENOENT
MicroPython v1.9.3-240-ga275cb0f on 2018-01-21; ESP32 module with ESP32
Type "help()" for more information.
>>> import network
>>> station = network.WLAN(network.STA_IF) 
I (42465) wifi: wifi firmware version: 111e74d
I (42465) wifi: config NVS flash: enabled
I (42465) wifi: config nano formating: disabled
[0;32mI (42465) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (42475) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
I (42495) wifi: Init dynamic tx buffer num: 32
I (42495) wifi: Init data frame dynamic rx buffer num: 64
I (42495) wifi: Init management frame dynamic rx buffer num: 64
I (42505) wifi: wifi driver task: 3ffe2d38, prio:23, stack:4096
I (42505) wifi: Init static rx buffer num: 10
I (42515) wifi: Init dynamic rx buffer num: 0
I (42515) wifi: Init rx ampdu len mblock:7
I (42525) wifi: Init lldesc rx ampdu entry mblock:4
I (42525) wifi: wifi power manager task: 0x3ffe852c prio: 21 stack: 2560
[0;32mI (42555) phy: phy_version: 362.0, 61e8d92, Sep 8 2017, 18:48:11, 0, 0[0m
I (42555) wifi: mode : null
>>> station.active(True) 
I (68635) wifi: mode : sta (24:0a:c4:03:a8:90)
[0;32mI (68635) wifi: STA_START[0m
True
>>> station.scan()
[0;32mI (79565) network: event 1[0m
[(b'FRITZ 6360C', b'\x9c\xc7\xa6\x0cc\xbe', 1, -84, 4, False), (b'WLAN-565914', b'\xd4!"\xe9\xb7K', 11, -92, 3, False)]
>>>

I still have to figure out how to write python scripts to the filesystem, haven’t checked on it yet. But I am happy to make it work 🙂

 

Advertisements
Standard
Uncategorized

Esp8266 NodeMCU V3 Lolin: Don’t bother!

I made a robot a while ago – it’s pretty much documented here – with a Esp8266 board. It worked beautifully from the beginning. You set up the board on Arduino IDE and you are good to go.

IMG_8682.JPGA wonderful little board

Recently, inspired by a tweet from my genius friend Ugo Sangiorgi about using the ESP8266 as a wi-fi repeaters, I decided to buy a couple more of those boards, in order to cover my house with cheap wi-fi.

Being the cheapskate I am, I went to AliExpress, and bought the cheapest NodeMCU I found: Something called “NodeMCU V3” (the officials are V1 and V2). It costs around 2 euros. Check it here: NodeMcu on Aliexpress

It damn looked like my first NodeMCU. And a Wi-Fi router for 2 euros, albeit a slow one, is unbeatable.

Until they arrived.

This is the little monster:

IMG_8681.JPGEsp8266 NodeMCU V3 Lolin – looks similar to the one above, eh?

As everyone does, the first thing I did was to power it on and try the Blink example. (It comes with a blink one, but anyway). Nothing.

Turns out, the LED_BUILTIN is set to some other pin. If you and to make it work, replace “LED_BUILTIN” on this board with “2”. It works.

Fine. The board looked a bit large, though. I took an ESP32 that I had around and checked. This thing is bigger than the ESP32 that I was whining about a while ago here!

Lo and behold…

IMG_8684.JPGThe damn board VS a ESP32. Notice how the wifi antenna is somehow covered.

It uses the CH340 serial, which seems to be a piece of crap, but hey, 2 euros. I couldn’t care less.

Making the board run properly on Arduino IDE

The serial output is wrong: One has to set

Serial.begin(74880);

on arduino code in order to get 115200 output on the serial monitor. This is because it uses a 40mhz crystal oscillator instead of the standard 26mhz one. You can fix this with changing the crystal settings at

Arduino15/packages/esp8266/hardware/esp8266/2.4.0/cores/esp8266/core_esp8266_phy.c

Just force setting it to 40mhz, around line 87:

    // crystal_26m_en
    // 0: 40MHz
    // 1: 26MHz
    // 2: 24MHz
      [48] = 0,
.....

This SHOULD fix the fact that this board basically has no wifi. Some people have reported partial success on github, but I can’t say the same. I can see no wifi here.

Did I say that this board is huge? Well, good luck using it in a breadboard. It doesn’t leave any room for anything:

IMG_8685.JPGLove playing with your boards in a breadboard? Well FUCK YOU

All in all, I spend quite some days to find the solution for the LED pin and the clock I mentioned above, and I still can’t have any wi-fi whatsoever. The vendor asked if I have googled it (!).

To sum it up, my recommendation is: don’t buy this crap. There are other cheap boards around.

Oh, and when I mentioned the other Esp8266? Here it is one next to the other:

IMG_8679.JPGThe good, the bad and the ugly?

IMG_8680.JPGThe underbelly of the original ESP8266 vs this Lolin whatever

So, again, the links:

https://github.com/esp8266/Arduino/issues/4005 – the talk about the serial output and wifi

core_esp8266_phy.c The file you should change to make it work

A wifi repeater using ESP8266

 

Standard
Uncategorized

Running a 5V 5mw 650NM laser from Arduino.

Simple as shit, but as I haven’t found it….

It’s almost the same as the “blink” example from Arduino IDE. One just needs to change the pin (I use ~11), and use analog write instead of digital.

Follows the code:

int laser = 11;

void setup() {
 pinMode(laser, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
 analogWrite(laser, 20); // 20:enough. 100: hurt your eyes
 delay(1000);
 analogWrite(laser, LOW);
 delay(500); // cool it down
}

IMG_6156.JPG

Standard
Uncategorized

Robot car controlled by iPhone using Blynk and NodeMCU.

Materials:

I created a simple app on Blynk with 4 buttons connected to virtual Pins.

See how to install and run Blynk here.Then, just connect those buttons to virtual pins, like this:

IMG_4762.PNG

Forward: V0, Backwards: V1, Right: V2 and Left: V3. It look like this:

img_4756

Got a token for the project and used on the following file on your Arduino IDE.

/**************************************************************
 * This example runs directly on NodeMCU chip
 * using the Blynk platform and mobile application.
 * Change WiFi ssid, password, and Blynk auth token to run :)
 **************************************************************/

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define RightMotorSpeed 5
#define RightMotorDir 0
#define LeftMotorSpeed 4
#define LeftMotorDir 2

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "MyWifi";
char pass[] = "MyWifiPassword";

void setup()
{
 Serial.begin(9600);
 Blynk.begin(auth, ssid, pass);

 pinMode(RightMotorSpeed, OUTPUT);
 pinMode(RightMotorDir, OUTPUT);
 pinMode(LeftMotorSpeed, OUTPUT);
 pinMode(LeftMotorDir, OUTPUT);
}

void loop()
{
 Blynk.run();
}

void halt()
{
 digitalWrite(RightMotorSpeed, LOW);
 digitalWrite(LeftMotorSpeed, LOW);
}

void forward()
{
 digitalWrite(RightMotorDir, HIGH);
 digitalWrite(LeftMotorDir, HIGH);
 digitalWrite(RightMotorSpeed, HIGH);
 digitalWrite(LeftMotorSpeed, HIGH);
}

void reverse()
{
 digitalWrite(RightMotorDir, LOW);
 digitalWrite(LeftMotorDir, LOW);
 digitalWrite(RightMotorSpeed, HIGH);
 digitalWrite(LeftMotorSpeed, HIGH);
}

void right()
{
 digitalWrite(RightMotorDir, LOW);
 digitalWrite(LeftMotorDir, HIGH);
 digitalWrite(RightMotorSpeed, HIGH);
 digitalWrite(LeftMotorSpeed, HIGH);
}

void left()
{
 digitalWrite(RightMotorDir, HIGH);
 digitalWrite(LeftMotorDir, LOW);
 digitalWrite(RightMotorSpeed, HIGH);
 digitalWrite(LeftMotorSpeed, HIGH);
}

BLYNK_WRITE(V0)
{
 if (param[0])
 forward();
 else
 halt();
}

BLYNK_WRITE(V1)
{
 if (param[0])
 reverse();
 else
 halt();
}

BLYNK_WRITE(V2)
{
 if (param[0])
 right();
 else
 halt();
}

BLYNK_WRITE(V3)
{
 if (param[0])
 left();
 else
 halt();
}

Wiring up the shield to the engines and battery is straightforward. Left wheel is “Motor B”, right wheel is “Motor A”, and power goes to “ESP Power”. If you insert a jumper on the “shortcut” on the VIN and VM pins, it will power the NodeMCU from the same power source.

original-doit-esp8266-nodemcu-motor-shield-with-l293d-extension-board-for-rc-kit-diy-smart-car

IMG_4761.JPG

Assembling everything together, the thing comes out like this (click to zoom):

And it runs well:

Right now, this is a dumb machine. I will install some intelligence on it pretty soon 🙂 Besides, I will play with different wheel options and maybe a servo on the front.

Standard