Making an Arduino Mega, a slave to the Spark Core

Hey all,

I’ve got a whole heap of legacy Ardino’s lying around, and I want to find a way to easily make them “cloud ready”. What I’m hoping to do is connect to them, and the Spark Core via the I2C channels, then use the Spark to do all the smart stuff.

Getting a bit more specific about my project, I’ve got a Arduino Mega, which has three sensors connected to it via it’s serial interface (a PH sensor, and an Electric Conductivity Sensor, and a Oxygen reduction Sensor - it’s a hydroponics system). As the Spark core only has one of it’s serial ports enabled right now (and it’s second one may be a no go… https://github.com/spark/core-firmware/issues/89), it seems using the Ardino Mega would be a easy, and cost effective solution to give me three Serial Ports…).

In the ideal world I’d be able to say “on the Mega, read the buffer at pin 14” or “on the Mega, write “HIGH” to pin 7” etc.

Any advice / tips on exactly how I would go about this (or if there is a better way)… would be much appreciated.

Tim

I’m looking for a pH sensor - could you post a link to the sensor you use?

I’m using the range from Atlas Scientific (https://www.atlas-scientific.com/product_pages/embedded/ph.html)

1 Like

Hi @timcopeland,
have you seen this thread?

As far as I recall there were two possible solutions outlined for your problem:

  1. Multiplexing the existing USART2
  2. Using SoftSerial

For 1. you’d need a mux circuitry and some digital pins of the Core to select the sensors and set your sensors up for a one-shot read, rather than continous reporting.
For 2. I’m not sure what the porting status of SoftSerial is, but if you only do short reads, even a semi-perfect port might be reliable enough.

timcopeland, I do not recommend softserial for any processor as it disables interrupts while reading serial bits in order to get bit timing right.

However, I recommend using I2C to connect your Spark and Mega together. A similar connection was discussed in this topic:

On the Mega side, one possible solution is to run Firmata which acts like Spark Tinker. There is an I2C version "example" in the Arduino 1.55r2 libraries I looked at. This would allow you to send the types of simple commands you are looking for. What's nice with Firmata is it would make your Arduinos appear "generic" to your Sparks, providing a consistent and simple command structure. Let me know if you need any help. :slight_smile:

1 Like

Hi @peekay123, thanks heaps for your suggestion - that is exactly I was looking for :slight_smile: Any chance you’re able to help point me in right direction with the initial direction, (ie what code I’d put on the Mega, and what code I’d put on the Spark Core, in order to say, read the value on Pin 7, and write to pin 9).

Cheers,
Tim

timcopeland, now I done and spoke too soon but not so soon! The arduino Firmata is designed to listen for commands on its serial port and use I2C for slave devices. On top of that, there are not commands in the Firmata “world” for communicating with a device over serial port.

So, you have a couple of choices:

  1. Rewrite Firmata to handle commands over I2C as a slave to the Spark and add serial “devices” into Firmata, possibly rewriting the I2C “device” to use serial instead

  2. Forget Firmata and write your own arduino code that listens for commands on I2C and supports the serial devices you have. All commands from the Spark and how they are implemented on the arduino need to be defined and written.

I would be game to help with #1 since it could be applied to any arduino. It is obviously forking away from the “true” firmata but could be a cool toolset. Any thoughts?

@peekay123 both your options sounds good to me - though I’m way out of my depth to make any technical recommendation, so I’m just grateful for anything you choose to do in this space.

With that said, I like the idea of using the I2C to connect them, because it’s a common standard, and means the only hardware you need is two wires to connect both controllers.

I also like the idea of using an RF communication chip, (I’ve seen people do cool things with boards like these - http://www.aliexpress.com/snapshot/6063121589.html) which would mean you’d be able to use an Sparkcore to drive a whole army of RF enabled Arduino’s, and in essence make them all internet ready.

Both options would also theory also mean giving the Sparkcore, 100% compatibility with existing Arduino shields, without the need to port any libraries, making it a nice bridge & speeding up adoption of the core.

timecopeland, can you give me more details on the serial communications with the sensors? Perhaps model numbers? I need to understand how they communicate so I can code that into firmata. What I am doing is essentially removing the serial interface to firmata and replacing it with I2C. Then I will replace the I2C slave “device” in firmata with a serial device where you can select the serial “channel” (serial, serial1, serial2) much like you select an I2C address. :smile:

UPDATE - just found your reference to the sensors so never mind that part! :blush:

All the sensors I’m using at the moment are from Atlas Scientific.

My sensor current setup looks

While I’m dreaming - the other thing to add into the wishlist for the sort of solution would be the ability to update the firmware of a standard Arduino via the Core (so you could in turn update all firmware via the cloud), though I suspect that wouldn’t be possible.

Let me know if there is anything I can do to help :smile:

Actually that's very possible... since the Arduino can be reprogrammed through the SPI interface.

The tricky bit would be compiling your Arduino sketch, and getting it into the Sparkulator. However, now with the Spark CLI (Command Line Interface) it could be possible to have a One-Touch button/command that compiles your sketch, parses the code into a spark code application and pushes the code up to the Cloud to be compiled and flashed to your Core. A real dream come true :wink:

Man, I just love the future :slight_smile:

2 Likes

Okydoky, let’s review a bit here. Firmata is cool but not a panacea. The way it works now is all serial-based with no specific handshaking like I2C. Firmata can do continuous reporting of certain data elements like analog or I2C (slave devices) back to a PC but this will affect how the master (Spark) side code is written. I am using the “StandardFirmata” sketch provided in the examples and modifying it.

At present, this code supports a large subset of the full Firmata functionality but serial devices don’t exist in Firmata so I have to extend it. Motor control is not either but could be added. The temperature probe could be connected to an analog pin on the arduino and read on a regular basis via Firmata. I would say anything that does not need to be fast real-time can be done via Firmata.

As BDub pointed out, it is possible to program the Arduino via the Spark but not simple. One thing to remember is that Firmata is not designed to control. It is simply an extension of the Spark I/O and all control is done on the Spark. So once the Firmata code is complete, tested and installed, it is unlikely to need updating. Nonetheless, the possibility remains… :wink:

I plan to usurp the standard firmata I2C (slave) functions and replace them with the serial interface for the probes you are using. I am at the point where I should soon be able to test the Spark to Firmata I2C interface. :smiley:

Sounds great - I’ll be able to do any testing you need from this Friday :smile: have will have a pretty clear run for the proceeding week :slight_smile:

timcopeland, it seems my Win8.1 system took a crapper and I have to repair it so stay tuned!

@peekay123 did you manage to find any time to look @ this?

@timcopeland, I have unfortunately not. Another member, @Blake ported firmata to the Spark:

@timcopeland @peekay123 My port is functional for using the Firmata protocol to send data, but I never needed to port the StandardFirmata library (so it doesn’t support receiving data/commands). @jjrosent has submitted a PR upstream to Firmata, that adds full Spark support - but it’s taking a little while to get things ready. I think the Spark is the first unofficial Arduino board to get support.

You can take a look at the PR at

1 Like

Thanks guys - I’ll be sure to check this out :smile:

Hi @timcopeland - I’m working on a similiar hydroponic project and wanted to see if you had any luck connecting the spark to the arduino? Any help would be greatly appreciated!