i2c connection hangs (two arduinos)

i am using i2c to connect two arduinos, 1.counting rain and 2.posting to cloud (nano reading+softwaredebounce and sending rain gauge tip`s via i2c to uno+w5100 and uno posting to cloud)

it works some time but then connection hangs … why so ?

is it possible to reset connection if its hanging ?

Nano raingauge reader code :

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin
// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 10;    // the debounce time; increase if the output flickers
int state;

#include <Wire.h>
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
       // int x = 0;
void loop() {
  // read the state of the switch into a local variable:
 int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:
    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
   digitalWrite(ledPin, ledState);
Wire.beginTransmission(8); // transmit to device #8
  Wire.write(1);              // sends one byte
  Wire.endTransmission();    // stop transmitting
  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;

Uno posting to exosite cloud:

#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Exosite.h>
byte macData[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // <-- Fill in your MAC here! (e.g. {0x90, 0xA2, 0xDA, 0x00, 0x22, 0x33}) 
String cikData = "xxx";      // <-- Fill in your CIK here! (https://portals.exosite.com -> Add Device)
Exosite exosite(&Ethernet, macData, cikData);
void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output

void loop() {


// function that executes whenever data is received from master
// this function is registered as an event, see setup()

void receiveEvent(int howMany) {
  while (1 < Wire.available()) {
           // loop through all but the last
   // char c = Wire.read(); // receive byte as a character
   // Serial.println(Wire.read());         // print the integer
  int x = Wire.read();    // receive byte as an integer
  Serial.println(1);         // print the integer



Remove delay() and print() functions from receiveEvent() interrupt context of UNO sketch.

Remove also the exosite.sendToCloud() from the interrupt function.
Remove also the while-loop from the interrupt function.

Please slow down the Master, it is sending data via the I2C bus with the maximum possible speed.
How often do you want to update the data to the cloud ? Who controls how often that is updated ? the Master or the Slave ?

In the Slave, I prefer to check the 'howMany' parameter.
How many bytes do you want to transfer ?

volatile byte data;
volatile bool newData;

void setup()

void loop()
  if( newData)
    Serial.println( data);
    exosite.sendToCloud("rain", data);
    Serial.println( "sendToCloud");
    newData = false;            // reset the flag

void receiveEvent( int howMany) 
  if( howMany == 1) 
    data = (byte) Wire.read();
    newData = true;       // set flag

is it possible to use also RTC same time as recieving raingauge transmissions ?

Yes, as long as there is only one Master on the I2C the bus.

The rain-counting-Arduino board is the Master for the I2C bus. You can add more sensors to the I2C bus, as long as the rain-counting-Arduino controls them.