Passing serial data over I2C between two Arduino’s

Below you will find some arduino code for passing serial data over I2C between two Arduino’s. Parts of the following code were salvaged from various places around the internet.

8536568652_0500c802fd

This can also be used for:

1) General communication between two Arduino’s over I2C

2) Using a second Arduino to expand the peripherals, for example: add a a second serial port device, add a second arduino for dedicated monitoring of certain interfaces

I used this to communicate between a Diavolino@ 5V with a Arduino Pro @ 3.3V using the Sparkfun level converter TX lines. My attempts at trying to run this without the level converter using pullup registers tied to 3.3V did not work. I will reattempt that once I get a scope for debug.

I2C Master code:

#include <Wire.h>
// SCL - analog pin 5
// SDA - analog pin 4

int led = 13;
int debug = 0;

void setup() {
  pinMode(led, OUTPUT);     
  Wire.begin(); //join I2C as master
  Serial.begin(9600); //setup serial for input and output
  digitalWrite(led, HIGH);
  Serial.println("I2C MASTER 3.3V");
  delay(5000); // the master should be ready after the slave
  Serial.println("Ready");
}

void loop() {
  if (Serial.available()) {
    char inChar = (char)Serial.read(); 
    if (debug) {
      Serial.print("serial:");
      Serial.println(inChar);
    }
    Wire.beginTransmission(4);
    Wire.write(inChar);
    Wire.endTransmission();
  } 
  else {
    if (debug) Serial.println("!serial");
  }

  Wire.requestFrom(4, 1); // request 1 byte from slave device #4
  if (debug) Serial.println("waitOnReq");
  while(!Wire.available());
  char d = Wire.read();
  if (debug) {
    Serial.print(" reqData:");
    Serial.println(d);
    delay(50);
  } 
  else { // ignore special character ?
    if (d != '?') Serial.print(d);
  }

  if (debug) digitalWrite(led, HIGH);
  if (debug) delay(200);
}

I2C Slave code:

#include <Wire.h>

// SCL - analog pin 5
// SDA - analog pin 4
int led = 13;
int debug = 0;
void setup() {
    pinMode(led, OUTPUT);     
    Wire.begin(4);                // join i2c bus with address #4
    Wire.onReceive(receiveEvent); // register event
    Wire.onRequest(requestEvent); // register event
    Serial.begin(9600);           // start serial for input and output
    digitalWrite(led, HIGH);
    Serial.println("I2C SLAVE 5V");
    delay(2000); // the slave should become ready first
    Serial.println("Ready");
}

void loop() {
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
    if (debug) {
        Serial.print("rxEvent:");
        Serial.println(howMany);
    }
    while(Wire.available())
    {
        char c = Wire.read();
        if (debug) {
            Serial.print("i2c_rx:");
            Serial.println(c);
        } 
        else {
            Serial.print(c);
        }
    }
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
    if (debug) Serial.println("reqEvent");
    if (debug) digitalWrite(led, LOW);
    if (Serial.available()) {
        char inChar = '0'; 
        inChar = (char)Serial.read(); 
        Wire.write(inChar);
        if (debug) {
            Serial.print("sent:");
            Serial.println(inChar);
        }
    } else { //send some dummy data if no data is available from serial
        Wire.write("?");
        if (debug) Serial.println("!sent:");
    }
}

Giving Arduino a second UART over I2C by stacking another Arduino on top

Now that I have the Cellular modem working with the PC over serial I am trying to get the same thing implemented on an Arduino. Since the modem uses the serial interface I will not be able to use that to debug my program via a serial terminal running on my PC.

I tried using the SoftSerial (or the NewSoftSerial) library but ran into data corruptions even at the low speeds, so I decided to look for other ways to get another hardware UART on the Arduino. For a while I was contemplating getting a Mega Pro from Sparkfun but its price was a turnoff. In the end I realized that I could just stack another Arduino on my Diavolino (both interfaced to each other over I2C as master slave) and after trimming the UART rx/tx shield pins be able to separate out the two UARTs.

The other issue is that the UART pins of the Cellular modem are not 5V tolerant. This had not affected me till now since I was using the Adafruit FTDI friend at 3.3V logic levels when connecting to the PC serial port. So I could not get another 5V Diavolino. In the end I decied to get a 3.3V Arduino Pro and stack that on top of my 5V Diavolino.

Overall the connections will look something like the following diagram:

arduino extra serial port plan

Note that when I drew this I was planning on using another Diavolino at 3.3V but decided against it since it seemed like more work because I would need to get the 3.3V regulator on my own and also somehow get an 8MHz AVR chip for it. In hind sight this may not have been a good idea since the Arduino Pro seems to be connecting both the Vcc pins in the header to 3.3V. The Diavolino instead has them separated which seems more sensible to me.

Another thing I forgot to show in the diagram above are the pull up registers for the I2C lines (SDA & SCL) between the two Arduino boards. I am hoping that connecting those to 3.3v would work for the 5v arduino, but I am not sure.

PySerial not available for ActiveState Python 64bit free edition on Windows

If you install the free 64bit edition of ActiveState Python you will not be able to use pypm to download the pyserial package, as also listed here:

http://code.activestate.com/pypm/pyserial/

It works fine with the 32bit edition though. It seems like the 64bit editions of some packages are only available to customers with business licenses.

pyserial

Sparkfun GSM/GPRS SM5100b shield on a Diavolino

I have decided to return the SM5100B evaluation board since I am instead using the SM5100B cellular shield. This is because I was not really going to use any of the interface pins of the SM5100B module broken out on the eval board. One useful thing about the shield (that I wasn’t expecting) is that it is able to reset the cellular module using the on board reset button similar to the eval board.

8008528875_923b20b2d9_z

The Diavolino board here is running without any AVR 328 mega chip plugged into it, and has the 2.5mm x 5.5mm power barrel socket for the 5V 3A regulated power supply from EvilMadScience.

Diavolino hosting a GSM/GPRS shield

Meanwhile although I can receive TCP data on the SM5100B modem over GPRS I still haven’t been able to successfully confirm transmission of data.

TCP connection over GPRS with SM5100B module

After having no success sending GET requests to HTTP servers I finally decided to set up my own tcp server by directing a port on my router to my PC and setting up a simple tcpserver listener in cygwin like this:

~ > tcpserver -t1000 -v -Bare_we_receiving 0 9876 bash -c "sleep 1000"
tcpserver: status: 0/40

Then I sent the following commands to the modem

AT+CGATT? 
AT+CGDCONT=1,"IP","epc.tmobile.com" 
AT+CGPCO=0,"None","",1 
AT+CGACT=1,1
AT+SDATACONF=1,"TCP","MY_ROUTER_ADDRESS",9876 
AT+SDATARXMD=1,1,0
AT+SDATASTART=1,1

For some reason the MY_ROUTER_ADDRESS had to be the text host name instead of the IP address, which was something like c-blah-blah.comcast.net for me as per my router.

Immediately I saw a connection appear in the tcpserver log:

tcpserver: status: 1/40
tcpserver: pid 5232 from 208.54.32.166
tcpserver: ok 5232 :::ffff:192.168.1.111:9876 :::ffff:208.54.32.166::50107
tcpserver: end 5232 status 256
tcpserver: status: 0/40

And then some data was received on the GPRS modem side:

> +STCPD:1
>

Which I read back promptly:

AT+SDATASTATUS=1
> +SOCKSTATUS: 1,1,0102,54,0,16
> 
> OK
>
AT+SDATAREAD=1
> +SSTR:1,are_we_receiving
> 
> OK
>

And it exactly matched the banner text. So atleast I can establish TCP connections. The AT command interface though is a real pain.

Basic testing the SM5100B

SM5100B GSM GPRS modem setup

The parts for the first phase have arrived and basic functionality of the GSM modem was tested and found to be operational. Here is a log of the modem receiving a phone call:

+SIND: 1
+SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1
+SIND: 11
+SIND: 3
+SIND: 4
RING
RING

Here is a list of what I’m using:

  1. From SparkFun
    1. SM5100B Evaluation Board
    2. GSM/GPRS Module – SM5100B
    3. Quad-band Cellular Antenna SMA
  2. From EvilMadScience
    1. 5V, 3A Regulated Power Supply
  3. From BG Micro
    1. 2.5mm Jack To 2.1mm Plug

I also tested receiving SMS text messages but it seems that, they aren’t pushed over the UART. Instead I had to poll to check if I had received any new SMS messages.

+CMGL: 1,0,"REC READ","1511","12/09/09,00:29:11+00"
Welcome to T-Mobile! Dial #BAL# to check your balances. Your T-Mobile number is 1***412****
+CMGL: 2,0,"REC READ","+1***721****","12/09/09,01:14:02+00"
Hello, world
+CMGL: 3,0,"REC READ","+1***721****","12/09/09,01:16:33+00"
Hello, world 2

I am currently using this with a T-Mobile pay-by-the-day plan which gives me unlimited voice, text, and 2G data for $2 per day.

Next I am going to spend some time thinking about how I want to proceed.

Cheap Basic Home Security with Arduino

Where by “Cheap” I mean low recurring/monthly subscription costs.

I decided to see if I could quickly build something simple myself since I could not find any off the shelf products that matched my needs or budget.

I was looking for a way to implement some kind of notification on my iPhone driven by a PIR sensor based occupancy detector, remote alarm triggering, resilient to power and internet outages (UPS battery backup), without paying huge monthly bills.

My current draft solution is:

PIR sensor -> Arduino -> GSM/GPRS modem Cellular shield -> Prowl iOS App etc.

The GSM shield would also be able to send/receive phone calls and text messages, but I am not sure if I need to use that. For now I will be using the T-Mobile prepaid pay by the day plan with 2G data which costs $2 for unlimited call/text/2G-data only on the days that it is used on, and can be kept alive for 90 days with $10.

The reason for using GSM here is that it will hopefully still keep on working even if the house power is turned off and the internet/phone lines are disconnected as long as the UPS it is powered by doesn’t die. Another useful thing would be to get main power disconnection notification messages using this current sensor.

I will be adding more information here as my plan crystallizes.