Monday, September 10, 2018

DF Robot micro:Maqueen Robot For BBC Micro:Bit (programmed with the Arduino IDE)

DF Robot micro:Maqueen Robot
For BBC Micro:Bit (programmed with the Arduino IDE)
Sep 10, 2018



Prerequisites:
  1. Arduino IDE 1.8.x (recommend 1.8.5) installed and setup for your machine. https://www.arduino.cc/en/Main/Software
  2. Install the Arduino-nRF5 board core. Follow the instructions here: https://github.com/sandeepmistry/arduino-nRF5
  3. Install the Adafruit Micro:Bit Library: https://github.com/adafruit/Adafruit_Microbit and learn more about it here:  https://learn.adafruit.com/use-micro-bit-with-arduino
  4. Finally install the arduino-BLEPeripheral library (either from the library manager or by following instructions here: https://github.com/sandeepmistry/arduino-BLEPeripheral  ) * Note be sure it’s the one from sandeepmistry if you are using the library manager *
  5. NewPing Library (Which I believe is this library) https://github.com/eliteio/Arduino_New_Ping and is only needed if you are going to use an ultrasonic.

For this project you will need at least one Micro:Bit Controller:

And the Maqueen Robot chassis: https://www.dfrobot.com/product-1783.html

The Micro:Bit is setup to use Microsoft:MakeCode or Scratch and Python.
The Sandee P Mistry Board core lets us use this board with the Arduino (C style) language as well.

General Information:
I used the general information that is found either in the Maqueen MakeCode library on the board itself, or from trial and error to come up with something that works (mostly) with the Arduino IDE.

Working (or mostly working):
  1. Motors (I2C interface, easier to port than I thought it would be)
  2. Line Sensors (P13, P14)
  3. Ultrasonic (using newping library) P1, P2 * Attempted to get this working without the library, but it failed (????) *
  4. 2 LEDs P8, P12
  5. Buzzer P0 - *  tone is not yet implimented with the board, analogWrite works but doesn’t sound great, bit bang also works, but doesn’t sound great - this is in the mostly working columb)
What’s not working: (These things work if you use MakeCode, but so far I’m unable to get them to work with Arduino).
  1. NeoPixels (P15 4 pixels) - The Adafruit Neopixel library appears to be the only library that supports the Micro:bit, this isn’t surprising. But I’ve not been able to get it work on P15, it does work on P2 which is what the example provided is using. https://learn.adafruit.com/micro-bit-lesson-3-neopixels-with-micro-bit/software
  2. IR receiver (P16) - so far I am unable to get this to work at all - *I need something that doesn’t mess with interrupts or timings, but I think that is a big ask for this device*
  3. Communication between two Micro:bit controllers - It’s a bit upsetting to me that such a basic thing was neglected in the Arduino IDE (If this does work, and you have supporting code please share - I have not been able to get this to work, or find any code that claims it works) ** This is a basic thing in the MakeCode, I’ve not been able to find the library they use or information about how they make it work **  https://learn.adafruit.com/micro-bit-radio-controlled-puppet/code-the-two-micro-bits    


Basic Sketches:  (To be duplicated)
  1. Drive and turn - (done) I2CMotortest, I2CMotortest2
  2. *RGB Breathing Ambient Light - unable to complete at this time.
  3. LED Light Flash - (DONE) - blinkdemo
  4. Read Ultrasonic Distance  (*DONE*) - They output to the LED Matrix, I output to the Serial console of the Arduino IDE (Easy to change as needed) newping
  5. *Read Infrared Key Assignments - unable to complete at this time. *
  6. *IR Remote Control - unable to complete at this time*
  7. Line Tracking - Currently Working on it.
  8. Ultrasonic Obstacle Avoiding - (DONE) - avoid
  9. Light Operated Sprite (unsure about this one)
  10. Wireless Remote Control  - * They are using a Micro:bit Gamepad (see below), I believe I can make this work with the Tablet and Adafruits Bluefruit LE Connect software * This is a work in progress.

The compass and accelerometer on the Micro:bit also work, as well as the LED Matrix and the two buttons on it.  *The Compass is in the wrong orientation to be used as a true compass, but it maybe able to be used to do relative positions, I will have to test that more.*

I am in the process of making a library and attempting to duplicate the applications in the DF  Robot wiki. It is not even close to being ready. I’ll post a link when it is ready.

DF Robot also provided a Micro:Bit Gamepad for use with this project, the gamepad has 8 buttons total (various P#s), a LED on P16, buzzer on P0, and a vibration motor on P12
Button assignments:
DOWN  - P13
LEFT - P14
UP - P8
RIGHT - P15
X - P1
Y - P2
A - P5
B - P11

This was very easy to port over to the Arduino IDE, the buttons are all HIGH until they are pushed then they go LOW.
I may not be able to use this as intended because I am having problems getting two Micro:bit to communicate directly with each other.

A game or something may be coming thou...we will see

Friday, June 29, 2018

Using the Mediatek LinkIt One board with Linux and Arduino IDE (1.8.5)

Using the Mediatek LinkIt One board with linux and Arduino IDE
June 29, 2018

By piecing together a couple of instructables, and a “answer” from stackoverflow. And a couple of things of my own, I was able to get “Blink” to work on the LinkIt One board using Arduino IDE 1.8.5 - most of the thanks goes to user v-i-s-h from instructables Who provided most of what is needed.
Both of these instructables where written by v-i-s-h.

They mostly work as written - if you take the 1st one, STEP 1 and STEP 2 then start on the 2nd instructable, when that is done move back to the 1st for STEP 4 and 5. It mostly works.

On Arduino IDE version 1.8.5 you will get compile errors, which can be fixed using the information found here:
User Alan Wootton has the correct answer - you will still get compiler warning, but it works.
I got it to work by editing platform.txt
and adding core/ so that it reads:
{build.path}/core/syscalls_mtk.c.o
instead of
{build.path}/syscalls_mtk.c.o
platform.txt was located in:
/Users/awootton/Library/Arduino15/packages/LinkIt/hardware/arm/1.1.17
answered Dec 16 '15 at 0:04
111


STEP 1 - Install the LinkIt One core into the Arduino IDE,

v-i-s-h has provided a core url that will work on linux machines.
https://raw.githubusercontent.com/v-i-s-h/LinkIt-One-Linux-Arduino-Support-/master/package_vish_linkItOne_linux_index.json

You will have to make a few changes, so you’ll need to go into the hidden “.arduino15” directory that should be in your home folder. You are looking for the packages directory under .arduino15
So it will look something like this:
/home/{user}/.arduino15/packages
You’ll want to find the LinkItOneLinuxArduino directory.

STEP 2 - in the tools/linkit_tools/1.1.17 directory

“/home/{user}/.arduino15/packages/LinkItOneLinuxArduino/tools/linkit_tools/1.1.17”
We need to make the scripts executable by issuing the command
chmod a+x *.sh
  ***DON’T REPLACE THE platform.txt file yet, there is a new version that uses  the updated “packtag.py” tool.

--So now we jump over to the 2nd Instructable.

STEP 3 (STEP 1) Get the updated packtag.py package.

And you’ll copy or move this file to the linkit_tools/1.1.17 directory from the previous step.
And you’ll need to make the python script executable.
chmod +x packtag.py

STEP 4 (STEP 2) Download the new platform.txt

You’ll need to right click on the link and then save as - if it doesn’t have the name platform.txt - rename it.
Replace the platform.txt found in
“/home/{user}/.arduino15/packages/LinkItOneLinuxArduino/hardware/arm/1.1.17” with the file you just downloaded - remember it needs to be called “platform.txt”

Important - Restart the IDE!

Now we can jump back to STEP 4 and STEP 5 from the original instructable.

STEP 5 - Blink The Built in LED.

Make sure to select the Linkit One from the board manager, look for the blink example (standard Arduino example)
*** NOTE The LinkIt One doesn’t define  LED_BUILTIN (The board core was written long before this became a standard thing to do) ***

The easiest way to fix this is at the top of the sketch add the line:
#define LED_BUILTIN 13

Or can also replace all the LED_BUILTIN with the number 13.

At this point, this is where the instructable kind of gets a little lost and confusing, So here is what I learned.
You need to save the modified version of your sketch (To compile sketches for this board you need to have a saved copy somewhere that you can get to easy).

On my system, the LinkIt One enumerates as two serial ports, /dev/ttyUSB0 and /dev/ttyUSB1 (of course those could change depending on what else I have plugged in.)
And apparently the device could show up as /dev/ttyACM1 and /dev/ttyACM0
What I found is compile the sketch, you need to have the board plugged in, it will fail to upload, but that is ok because we are not going to compile the sketch the way we would with a “normal” Arduino board.
To compile - click on the “Sketch -> Export” tab in the IDE,
In your sketch directory (This is why you need to save it somewhere), you will find a binary called “app.vxp”

Now we need to switch the LinkIt One from UART mode to MS mode - what I’ve found about this is the battery for the LinkIt One needs to also be installed and on….
(this could just be me).
Besure to make the mode change with the power removed.

Once you restart the LinkIt One, a small 10Mb removable drive should show up.
You are going to copy the “app.vxp” file to the MRE folder on the removable drive.

*** A ONE TIME STEP IS NEEDED ***
In the root of the LInkIt One drive you’ll see an ‘autostart.txt’ file, and you’ll need to edit it and point it to the app.vxp file, not the test.vxp file it is pointed at.
This should be on the 2nd line of the file, and it should look something like:
App=C:\MRE\app.vxp

You only need to do that once.

Disconnect your LinkIt One, (make sure battery is off as well, no power) - put it back in UART mode, and you should see the LED blink (or your sketch run on it).

That’s about it.

Saturday, September 2, 2017

Create a unique SSID with a ESP32 board.

I recently started to work on a ESP32 project.  And I needed a unique SSID for each access point I was creating.  I didn't want to hard code a different SSID in each device, since that would be time consuming.

I did what most people do and googled what I wanted to do.  And I really didn't find what I was looking for.

The ESP32 doesn't really have a unique ID itself, but it does contain a MAC address that should be different on each device.

I did find a sketch that does give the MAC address:
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ChipID/GetChipID/GetChipID.ino

But I still need a unique SSID - well here's how to do it....as simple as I can make it.


#include <WiFi.h>
         char ssid[15]; //Create a Unique AP from MAC address
void createSSID() {
  uint64_t chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
  uint16_t chip = (uint16_t)(chipid>>32);
  snprintf(ssid,15,"LoRaHam-%04X",chip);
  
}
void setup() {
  createSSID();
  Serial.begin(9600);
   WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    Serial.print("SSID: ");
    Serial.println(ssid);
}

That is it, simple.... add the rest of your code and have fun.

Monday, June 12, 2017

Raspberry PI Zero W - Speech Synthesizer using MQTT protocol

Raspberry PI Zero W - Speech Synthesizer using MQTT protocol
Jun 12, 2017

The goal of this project is to make a low cost speech synthesizer using text to speech (TTS) for the Arduino or other Microcontroller to use. *And one day voice Recognition*

For the project you need just three things and some time.
  1. Raspberry PI Zero W currently $10.00
  2. 16 or larger Gigabyte SD card with Raspberian installed and configured $7 to $10
  3. Fe-PI Audio sound card (I2S sound) for the Raspberry PI $14
Total Cost: about $34.  

You will spent quite a bit of time setting this up and that maybe worth more than money.

I used the Zero W because in the end I needed it connected to the internet, you could use the Zero but you will most likely want to get it on the net.
A Raspberry PI (model A or B) 1st generation, B+, 2, and 3 should work as well.

I’m not going into how to setup the PI other than the software that needs to be installed.


One of the first things you’ll also want to do is setup the Fe-PI. See my notes here:

  • The Fe-PI is currently supported in the kernel and setup isn’t as bad as it seems.

After you have your SD card setup and the PI booting. And are able to connect to your network. You will most likely want to rename your Raspberry PI from the default name.
Again, I’m not going to go into this as there are hundreds of tutorials on the subject.
I renamed mine to rpi-speak  
Now to connect to the PI with SSH or Web, or MQTT I can use “rpi-speak.local”  
So:
ssh pi@rpi-speak.local will let me ssh into my system with out knowing the IP address.


Next you’ll want to make sure you are up to date.

sudo apt-get update
sudo apt-get upgrade

  • This might take a while, you’ll want to reboot when it done.

You’ll want to install flite - (festival-lite) speech synthesizer. http://www.festvox.org/flite/

sudo apt-get install flite

Once installed give it a test and make sure you hear the audio.
flite -t “Hello World”


*** OPTIONAL STEP ***
Install Apache2 and PHP, Why? At first I thought I would beable to just send a web form and have the PI speak.  It seems that while I was able to execute the commands, everything was being piped to the webpage. And it just didn’t work.
I didn’t uninstall because there are some useful things that can still be done with a web site running on the PI.  But at this point, it’s a optional step, and not needed.
Install instructions:


It was at this point that I still needed away for the Arduino to “talk” to the PI. There are many ways to do this, and here are just a few (I didn’t use).


What is MQTT?

Almost every tutorial on the subject starts off asking this question. Some answers go into great detail, and others are very simple.  I’ll stick with the simple.
MQTT is a machine to machine simple protocol, that uses a publish/subscribe. MQTT uses a BROKER which is really just a server that handles all the messages.

MQTT Brokers can either be public or private, and there are good uses for both.
A Public Broker generally is reachable from any where, and by any one on the internet. Most do provide some type of security.  The draw back however is that anyone/everyone can subscribe to your information. It is unwise to publish sensitive data to a public Broker. Most of the FREE public brokers will not guarantee 100% update.

Here are some public Brokers: (There are many more)
http://www.mqtt-dashboard.com/ (a HiveMQ Dashboard)

I used a Private MQTT Broker, and it’s running on the Raspberry PI.
Mosquitto is a open source, free broker that is easy to install and easy to use.

Here are some tutorials I used to install this, but it comes down to a simple apt-get command on the PI.
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
It’s really that simple, the mosquitto server starts as soon as it’s installed, the mosquitto-clients has the command line commands to publish and subscribe. And python-mosquitto is the library to use MQTT with python.

If you are using linux, you can also install the mosquitto-clients package, and test your install out.

MQTT uses “Topics” - this is how messages are delivered. For my project I use two “topics” - Say and Said (I will probably end up changing at least one of these).
Topics are created by a subscribe or publish to the MQTT Broker.

Here is where it gets interesting thou.
On the Raspberry PI I have a python script running that subscribes to “Say”, and publishes to “Said”. Using the desktop MQTT client I can run one CLI to publish to “Say” and one CLI to subscribe to “Said”. The effect, the two computers can talk to each other.

So with the MQTT (mosquitto broker) running on the PI,
ssh into your pi, and issue the command:
mosquitto_sub -h localhost -t “Say” -v

And issue this command on your desktop.
mosquitto_pub -h rpi-speak.local -t “Say” -m ‘-voice slt -t “Hello I am Sally”’

You should see:
-voice slt -t “Hello I am Sally”
In your SSH window.
So the -h is the host flag, it is the server running the MQTT broker, and can be public or private (such as in this case)
-t is the topic we want to subscribe or publish to “Say”.

It’s really that simple to send a message to a machine running a MQTT subscription.

Once you verify that is working, you’ll need to install a couple of more things, and a python script to get it all working.

Installing paho-mqtt for python.

Type: pip install paho-mqtt on the Raspberry Pi.

I published my python script to github here:

Most of what I learned, I learned from here:
And while he is turning GPIO on and off, it contained enough information to make my “Speech” script.

You will want to copy and paste the script to your Raspberry PI. I just put it in my home directory. “/home/pi/say.py”

You can test the script at this point by typing:
python say.py

And on your desktop, publish a message to the “Say” topic.
mosquitto_pub -h rpi-speak.local -t “Say” -m ‘-t “Hello World!”’

You should hear, the PI say “Hello World”.

We are almost done with the Raspberry PI, we will want to set the script to start when the PI is booted or rebooted.
The easiest way is to setup a cron job. You probably don’t have any cron jobs, yet - so the PI will create a new cron file.
Type: crontab -e to edit the file, and add @reboot python /home/pi/say.py & to the end of the file, save and exit nano, and reboot.  If all goes well when the system is ready - it will say “Ready”
  • Don’t forget to the & at the end of the reboot command.

That is everything you need for the Raspberry PI, at the start of this project I wanted a cheap
speech synthesizer that an Arduino, or other microcontroller could use. So we need to add a MQTT library to the Arduino IDE. There are a number of MQTT libraries, but this tutorial
https://www.baldengineer.com/mqtt-tutorial.html recommends the PubSubClient library.
The easiest way to install is with the “library manager” on 1.6.x and above IDE.
The github for the library is here: https://github.com/knolleary/pubsubclient and the full documentation for it is here:  http://pubsubclient.knolleary.net/

This is a pretty easy to use library, I modified the mqtt_esp8266 example to publish to the “Say” topic, and subscribe to the “Said” topic.  
The only problem I had was the library was unable to connect with the host name “rpi-speak”
Or “rpi-speak.local” and needed an IP address.
This was the only hickup - But being able to SSH into the PI, or using the Webserver and CGI script for the environment (env.sh) it was pretty easy to get the IP address.

My modified sketch can be found here: