, , , ,

As I get older, I evolve choices to which make me happier.  Perhaps selfish but life is so incredibly short…why not surround each other and ourselves with happiness?  Using my iPhone to interact with the Ladybug Shield makes me far happier than using an LCD.  Besides, programming in Swift is..well…fun….at least fun relative to other programming languages I have bumbled about.

I want to start using the Ladybug Shield around my house.  This “forces” me to get my nose out of the scope and into the UI.

The Goal

The goal of this post is to go through the process of building a minimal iPhone app that display pH and EC readings from a Ladybug Shield.

Open Source

The Arduino and XCode files are located at this GitHub location.

Adding BLE

The Ladybug Shield does not include BLE.  After many starts and stops, I ended up adding the RedBearLab BLE Shield.  

IMG 3383



I spent too much time trying to get the Bean to work (which I had used in a previous prototype).  Ultimately, the Bean could work, but the challenges that got me to decide on using the RedBearLab BLE Shield over the Bean for this prototype included:

      • getting a stable/solid wire connection between the Bean and Arduino over the I2C pins.  At one point I soldered header pins onto the Bean’s holes which screwed up the board….I ordered another….yah, D’OH mistake :-).
      • The Ladybug Shield uses the ADS1015 – another I2C device.  I use Adafruit’s ADS1015 library which assumes the ADS1015 is the master.  This means both the Arduino and the Bean must act as slaves.  This was ok, but complicated my coding.
      • The Bean is a 3.3V device. The Ladybug Shield and Arduino are running at 5V.  I was running into communication over I2C freezing when the ADS1015 and Bean were both involved.  Yes, I could add a level shifter as I have done in the past…but it is one more thing to deal with. 
      • The Bean needs an external power supply.  The easiest is to use a C-Cell battery.  My bad was to keep leaving the Bean on with the battery in.  I’d come back in the morning to yet another dead battery.
      • The Bean must use the older (1.0.5) version of the Arduino SDK.  In general these type of 3rd party dependencies become necessary evils.  While the Bean’s abstractions make getting a BLE app between the Arduino and an iPhone easier, the cost of not being able to update the SDK is high for folks that have at least an intermediate level of BLE programming.
      • The RedBearLab shield uses the nrf8001 BLE chip (data sheet).  I gave up a more friendly library of APIs, being “forced” to gain familiarity with a library for the nrf8001.  My BLE requirements are very minimal so it didn’t take me much time to get the Arduino sketch pumping out pH and EC readings over BLE.

The ReadBearLab shield uses SPI.  Instead of using a CS pin, it has two pins – REQN and RYDN.  As noted in the nrf8001 data sheet:

However, nRF8001 does not behave as a pure SPI slave device; nRF8001 can receive new data over-the air at any time or be busy processing a connection event or new data. Consequently, the traditional CSN signal used to initiate an SPI transaction is replaced by two active low hand-shake signals; RDYN andREQN  

Nordic Semi’s library (for Arduino)  defaults REQN and RYDN to pins 8 and 9. These can be set to any of the pins 2 to 10.  For this prototype the Ladybug Shield will work with the default pins.

Sending pH and EC Over BLE

I did not need real time readings so I decided it was good enough to package the pH and EC value in the RedBearLab’s advertisement packet.  I should be packing the readings into the data portion of the advertisement packet.



This shouldn’t be too hard once some amount of familiarity with Nordic’s interface – Application Controller Interface (ACI) is accomplished. if I follow the steps outlined in this support post:

  • Once the nRF8001 sends the ACI Device Started Standby event, you can use the ACI OpenAdvPipe command (lib_aci_open_adv_pipes) on the broadcast pipe.  When you are opening a pipe using the ACI Command OpenAdvPipe, you need to wait for the Command Response Event [that says the pipe is open…not just that the request was queued]
  • update the data in the Pipe by using the ACI Set Local Data command ( lib_aci_set_local_data)
  • Then start advertising using the ACI broadcast command.
These steps point out what is going on within a figure in the nrf8001 data sheet that discussing setting broadcast data:

Since this effort is “quick and dirty” I decided to set the RedBearLab’s peripheral name to the pH and EC values.  The details are covered in the Arduino sketch LadybugShield_BLESlave_V1.ino:

void loop()
{//don't do anything until BLE is ready
  if (ble_isAdvertising())
    float pH_value = takepHReading();
    DEBUG_PRINTF("--> pH: ");
    uint16_t EC_value = takeECReading();
    DEBUG_PRINTF("--> EC: ");
    DEBUG_PRINTLNF("Wait before taking another reading...");
    delay(10000); //take a break

I did modify the nrf8001 library to:

  • include ble_isAdvertising() to let the Arduino sketch known that the “ACI advertisement has begun event” has occurred.  
  • set the peripheral name:

        lib_aci_set_local_data(&aci_state, PIPE_GAP_DEVICE_NAME_SET , (uint8_t *)&device_name , strlen(device_name));

 iPhone App

 The (very ugly) iPhone app picks up the advertisement packets and displays the pH and EC value.  

IMG 0044

Ugly iPhone app Running on my iPad


I used an updated BTDiscovery class library I had used in a previous prototype to receive the advertisement packets.  The swift file is BTDiscovery02072015.swift.   The BTDiscovery class includes delegates that are implemented by the View Controller (ViewController.swift) to update the pH and EC labels on the UI.  Easy Peasy.  Did I mention how much I like the Swift language?





Thanks for reading this far.  Please find many things to smile about.