Wednesday, March 18, 2015

Using HotPaw Basic with a LightBlue Bean Bluetooth LE device

HotPaw Basic 1.7.1 for iOS now includes direct support for communication with the LightBlue Bean.

This example HotPaw Basic program below lets you scan for Bluetooth LE devices, select a Bean, connect to it, blink the LED blue, read scratch bank 3, and read the temperature. Note that 1 or 2 second waits between all Bluetooth commands can be helpful or necessary (depending on your device model and how busy it is, etc.)

100 rem
102 print "starting scan for Beans"
120 fn bean.init()
140 fn sleep(1)
160 fn bean.list()
180 b$ = fn bean.selected()
190 print b$
192 if len(b$) < 20 then print "no bean selected" : end
200 fn bean.connect(1,b$)
210 for i = 1 to 20 : rem wait for bean to connect
212   if fn bean.connect(1) > 0 then exit for
214   fn sleep(0.5)
216 next i
220 print fn bean.name(1)
230 fn bean.led(1,0,0,1) :rem blink blue
246 fn bean.temp() :rem start temperature read
260 fn sleep(1)
270 fn bean.led(1,0,0,0) :rem off
280 fn sleep(1) : rem wait
300 fn bean.rdsc(1,3,0) :rem start scratch read
310 fn sleep(1)
320 s$ = fn bean.rdsc(1,3,1) :rem as a string
322 print len(s$),s$
340 s$ = fn bean.rdsc(1,3,16) :rem as a hex string
342 print len(s$),s$
374 print fn bean.temp()
950 fn sleep(1)
960 fn btle(9) :rem disconnect bluetooth devices
970 print "done"
990 end

Combine this with the MQTT sample app (from in an earlier post in this blog), and you can have your Bean send messages over the internet, using an iOS device as the relay to a broker.

The Bean serial protocol is also supported, see HotPaw Basic's built-in documentation for details. But don't expect a data rate much higher than 2400 baud (due to restrictions involving iOS built-in power saving and WiFi/BLE antenna sharing modes, etc.), so make sure your app sleeps a bit between sending small chunks of serial data.

Using HotPaw Basic with Bluetooth 4.0/BLE/BTLE devices

Bluetooth LE is a great way to get data from external sensors into your iPhone, and without needing any wires or cables. Bluetooth LE, also known as Bluetooth 4.0, BTLE or BLE, is a standardized short-range wireless communications protocol, designed so the tiny sensors can run for weeks to years on just a coin cell battery (or less!). It's now used by such devices as newer heart rate monitors and fitness trackers.

Here's a tutorial on BTLE (written by a former colleague of mine) with far more details: http://chapters.comsoc.org/vancouver/BTLER3.pdf

One inexpensive BTLE device, usable for experimentation, is the TI SensorTag (around $25 or $30 from TI), smaller than a matchbook, that contains tons of sensors. Another very popular device is the LightBlue Bean, which contains a programmable Arduino processor and a thermometer, as well as a BTLE radio. Runs on a 2032 coin cell and small enough that it ships in a matchbox.

Here's an example of connecting to the TI SensorTag using HotPaw Basic, and getting notified by any change in the status of button 1.

100 rem
120 fn btle.init()
130 fn sleep(2)
140 status = fn btle.list()
150 print status
160 if status < 0 then print "BTLE is off!" : end
170 uuid$ = fn btle.selected()
180 if len(uuid$ < 2) then print "No tag selected" : end
400 print "SensorTag Found!"
410 servic$ = "FFE0"
420 charac$ = "FFE1"
440 fn btle.connect(1,uuid$,servic$,charac$)
450 fn sleep(1)
460 print fn btle.connect(1) : rem connection status
500 dim r%(256) : rem array for characteristic data
520 while (1)
530   fn sleep(0.5)
540   n = fn btle.responsedata(1,1,r%(0))
550   if (n > 0)
560     print n,r%(0)
570   endif
582   if fn touch(0) > 0 then exit while
590 wend
950 fn btle.connect(-1) :rem disconnect
960 print "done"
990 end
Tap the display with 2 fingers to exit the while loop.

HotPaw Basic now supports MQTT messaging

MQTT support has been added to HotPaw Basic version 1.7.1 for iOS.

What is MQTT? It's one of the hottest new technologies for the Internet of Things. MQTT is a lightweight messaging protocol, where short messages, such as number from a temperature sensor, can get relayed though servers in the cloud, called Brokers. These messages can be relayed to an app somewhere else, such as the iPhone of someone on the road who want to check the latest home temperature reading. IBM is one of the major vendors supporting MQTT.

MQTT is "light-weight", meaning there's a lot less bits and bytes of overhead that need to get passed back and forth to send a message, much less than needed for accessing even a tiny web page. This makes MQTT more suitable for button-sized devices which can run off a tiny battery for over a year.

To send a message using MQTT, one needs to initialize a few details. You need to know at least two things so that the message knows where to start heading: the name of a MQTT host server in cloud and a port number for that broker. Optionally you will need a username and password a private broker, or for one of many MQTT brokers run as commercial services. IBM, cloudmqtt.com and hivemq.com provide commercial MQTT broker services. You can use Google to find "public MQTT brokers", or you can run a broker on your own server (even a Raspberry Pi). There are currently a few public brokers available for testing which need no password. One such is: "iot.eclipse.org", port 1883. Please don't use public brokers for more than a small amount of basic testing. Note that any the data one might send thru these public brokers is, well, completely public.

One needs to know 2 more things to identify the sending device and the message. A unique client ID helps the broker to not get confused between zillions of device trying to talk to it. IBM uses a MAC address as a unique ID. You can look up the MAC address for your iPhone in Settings:General:About:WiFi Address. You will also need a identifying topic so that a subscriber can find your devices messages out of the zillions of other messages sent by zillions of other devices. This is a short hierarchical string separated by slashes, such as "sample.corp/london.office/building.3/room.4/temperature".

And last, there is your short message itself, usually just a few characters, although IBM would like it packed into a JSON formatted string.

Here's an example HotPaw Basic program to send a message to an MQTT server.

100 rem MQTT test send
200 host$ = "iot.eclipse.org" :rem a public MQTT broker
210 port = 1883
220 client$ = "12345678" :rem Please put you MAC ADDR here.
300 fn mqtt.connect(host$,port,client$)
310 fn sleep(1)
320 print fn mqtt.connected()
400 topic$ = "hotpaw/test/randomdata/0"
410 msg$ = "hello 123"
500 fn mqtt.publish(topic$,msg$,1)
900 fn mqtt.disconnect()
910 print "done"
990 end

The JSON formatted version of the message would look more like this:

410 temperature = 22.5
412 msg$ = '{ "d": {"myName":"Arduino","temperature":'
414 msg$ = msg$ + str$(temperature) + " } }"

There are a few MQTT apps in the App store that can be used to check your message.

To see the message, you could run this HotPaw Basic program on another iOS device. Since these messages are relayed though a remote broker, the 2 iOS devices don't need to be on the same local network.

100 rem MQTT test subscribe
200 host$ = "iot.eclipse.org"
210 port = 1883
220 client$ = "12345678" :rem Please put you MAC ADDR here.
300 fn mqtt.connect(host$,port,client$)
310 fn sleep(1)
320 print fn mqtt.connected()
400 topic$ = "hotpaw/test/randomdata/0"
500 fn mqtt.subscribe(topic$)
520 for i = 1 to 10
530   fn sleep(10) : rem waits for message
535   print ".";
540   r$ = fn mqtt.message$()
560   if len(r$) > 0 then print : print "message = ", r$
570   if len(r$) > 0 then exit for
590 next i
900 fn mqtt.disconnect()
910 print "done"
990 end

Tuesday, June 4, 2013

New Music Spectrograph Description for Updated Version 1.3.4


Visually reverse engineer sound and music in real-time.  See music and sound as it might appear on a live piano-roll MIDI music editor.   The Hotpaw Music spectrograph presents a high-resolution scrolling spectrogram as you listen, so that you can see the patterns in sound and music lined up against a piano keyboard.  This piano alignment makes it far easier to spot which frequencies in a sound correspond to which musical notes.  Music Spectrograph is a great visual tool to help a music student see notes, that they might not be able to hear clearly at first, and assist with music transcription.

Features include:
- 12th-octave midi-pitch-centered spectrograph
- usable with either live audio or tunes from your device's music library
- up to a 4 minute spectrograph history you can stop, scroll back and review
- slow down tunes by up to 5X, or speed music up by 2X to skip sections faster
- hear tunes in mono, left or right channel only, or with center-panned vocals reduced
- stop music in time, and still hear the music frequencies present via resynthesis
- color frequencies to see if they are in-tune or out-of-tune
- color frequencies for loudness, or graph in black-and-white.

Use the Song button to select a tune from your library (DRM protected iTunes files are not supported).  Use the Gear button to select among several viewing modes.  There's a volume control to manually set the sensitivity threshold.  Stop playing or recording to scroll back and review or resynthesize sound.  Touch the keyboard during resynthesis to isolate one note.   For many musical sounds, you will see lots of overtones and harmonics, not just the fundamental note pitch frequencies.  That's just the way many musical sounds work.

Version 1.3.4 just release to the iOS App Store.

Thursday, March 7, 2013

Music Spectrograph App Updated

The newest version 1.2.1 is a big update for the HotPaw Music Spectrograph app.  It includes a lot of internal improvements in the quality of musical sound analysis, several bug fixes, plus one added major new feature.

My first problem was in deciding whether to keep the word "Spectrograph" in the name of the app.  When I looked at journal articles and books published by music professors, I saw at least 3 different terms used to describe these colored 2-dimensional time vs. audio frequency charts: Spectrograms, Spectrographs and Sonograms.  These terms also seem to have multiple uses, with Spectrograph also referring to optical devices used to split light into its constituant spectral colors, Spectrogram referring to the picture of a spectrum, and Sonogram referring to the results of ultrasound medical 3D imaging.  The term Sonogram, regarding music, is more rare; but I found this term used in a recently published textbook on sound by a professor of physics at Harvard University, as well as in a few other academic works.

In optics, the term "Spectrograph" is used for devices that produce spectrums or spectrograms.  Since the HotPaw Music Spectrograph app on an iOS device can be used on real-time sound input and works like a continuous chart recorder, I think a word ending with "-graph" more implies this continuous graphing activity; whereas the "-gram" suffix seems to just imply more a static image.  So I'll stick with calling it a spectrograph for now.

Some of the changes in the Music Spectrograph update include 12th-octave frequency band filters which are now better calibrated for the evenness of their response.  A few bugs in selecting, converting and playing Songs from your iTunes library to see their spectrograph have been fixed.  You can hit the Song button to use, and to turn-off this feature.

Now, there are several apps in the App store that can be used to help transcribe or learn music by slowing down songs without changing the pitch.

Music Spectrograph won't slow music down.  The new "ReSynth" feature in Music Spectrograph can actually stop music in time.

The major new feature added to the Music Spectrograph app is sound re-synthesis.  Hit the button to "Stop" recording or music playing.  Then touch the spectrograph and scroll back to some sound earlier in time.  A new button will appear near the top left of the chart.  The "ReSynth" button.  Scroll to an short sound, such as an eighth-note, and just stop and listen to just that one note or chord in time (but with most overtones and harmony included as well).  Scroll back-and-forth to listen to the notes immediately after or before.  See how what you hear corresponds to the piano keys nearest the highlighted frequencies.  The MIDI note numbers will also be displayed at the top.

What you will hear when using this re-synthesis will just be the strongest portions of the audio frequency spectrum, and those which directly correspond to the pitch of MIDI or piano key notes.  This re-systhesis isn't completely accurate, as any transients (such as consonants or percussion) and smaller and non-musical frequencies will have been removed, leaving a sound more like that produced by a vocoder (which is what is happening behind the scenes).  If you scroll back-and-forth in time, you might hear some tweeting/chirping noises as various high frequency overtones become large enough to show up on the graph for just a short time.

To use the Music Spectrograph app for music transcription, you will still need to know how to understand and ignore all the harmonics and sequences of overtones in the graph yourself.  This is not always easy, as many music instruments and types of human voice produce far more overtones then any sound at the actual musical pitch frequency.  This app may be helpful in learning about this important feature of many interesting musical sounds.

Thursday, July 19, 2012

HotPaw Guitar Tuner - Now available for the Mac

The HotPaw iChromatic Strobe Tuner has been a popular app in the Music section of the Mac App store.  Now available is a lower priced HotPaw Strobe Guitar Tuner, with the same accuracy and ease of use, but designed specifically for tuning 6 string guitars.

Wikipedia calls strobe tuners "the most accurate type of tuner", and our tuners for the iPhone, iPad and now the Mac will show you why.

Friday, April 13, 2012

New app: Morse Code Ringtone Maker


HotPaw Productions newest app is Morse Code Ringtone Maker , now available in the iOS App Store.

Create ringtones with your own customized Morse Code messages.  Don't bore people with yet another pop tune blaring from your phone.  Be different... retro-tech.  Communicate.  Or just hide really funny messages in your ringtones for "special" people (be they your best friends or your worse enemies).  Only other radio geeks will understand.

With Morse Code Ringtone Maker, you can customize the text and the Morse Code/CW speed (combined up to 30 seconds in length), as well as the the tone frequency.  The Words-Per-Minute (WPM) speed is variable from 5 to 40 WPM.  The tone frequency is customizable from 300 to 1200 Hz.  On the customization page, you can also configure Farnsworth timing where the dots and dashes for each letter come even faster than the WPM of the letters.

Morse Code Ringtone Maker requires that you can Sync your iPhone using iTunes on your Mac or PC, and have done so previously.   You can either email your newest ringtone file directly from the app, or use iTunes File Sharing to get your ringtone files from this app into iTunes on your Mac or PC, where they can then be Sync'd to your iPhone.

To get your ringtones from Morse Code Ringtone Maker into iTunes, connect your iPhone to iTunes running on your Mac or PC.  Start iTunes on your Mac or PC.  Connect your iPhone.  Select your iPhone under Device on the left side of iTunes.  Select Apps on the top of iTunes.  Scroll down to File Sharing.  Select Morse Ring from the list of Apps.  Select your chosen ringtone from the list of documents, and save it to your disk.  Then drag the .m4r ringtone file icon back from your disk onto iTunes.  To install the ringtone onto your iPhone, select Tones on the top of iTunes.  Select Sync Tones.  Then select which ringtones your want to Sync, then Sync.

To customize your iPhone's ringtone for a particular contact, select a contact from the Contacts app or within the Phone app on your iPhone.  Hit Edit and you will be able to select a ringtone for that contact.  Or you can use Sounds in the Settings app to set a global default ringtone.

Enjoy.