Master and Slave connection


#1

I have successfully connected ESP8266 NodeMCU with ADXL345 to Cayenne. But i wanted to connect Arduino UNO as slave to ESP8266 (inorder to connect two more digital sensors). I have separate code uploaded in both. But i wanted to know connections involved in transferring and receiving data.

My master code is

#define CAYENNE_PRINT Serial
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "xxxxx";
char wifiPassword[] = "xxxxxxxxx#";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char password[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char clientID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

unsigned long lastMillis = 0;

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup(void) {
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
  Serial.begin(115200);
  Serial.println("Accelerometer Test"); Serial.println("");

  /* Initialise the sensor */
  if (!accel.begin())
  {
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while (1);
  }
}

void loop(void) {
  Cayenne.loop();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 100) {
    lastMillis = millis();
    /* Get a new sensor event */
    sensors_event_t event;
    accel.getEvent(&event);

    /* Display the results (acceleration is measured in m/s^2) */
    Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
    Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
    Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  ");Serial.println("m/s^2 ");
    delay(10);
    Serial.println("");
    Cayenne.virtualWrite(1, event.acceleration.x);
    Cayenne.virtualWrite(2, event.acceleration.y);
    Cayenne.virtualWrite(3, event.acceleration.z);
  }
}

My slave code is

#include <SparkFun_ADXL345.h>         // SparkFun ADXL345 Library

ADXL345 adxl = ADXL345();             // USE FOR I2C COMMUNICATION

void setup(){
  
  Serial.begin(9600);                 // Start the serial terminal
  Serial.println("SparkFun ADXL345 Accelerometer Hook Up Guide Example");
  Serial.println();
  
  adxl.powerOn();                     // Power on the ADXL345
}

void loop(){
   // Accelerometer Readings
  int x,y,z;   
  adxl.readAccel(&x, &y, &z);        

  // Output Results to Serial
  /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */  
  Serial.print(x);
  Serial.print(", ");
  Serial.print(y);
  Serial.print(", ");
  Serial.println(z); 
}

Please help


#2

I can see two ways of doing this:

  1. Send data from the Uno to the ESP via serial and then upload that info to Cayenne with the ESP
  2. Use the ESP as a shield for the Uno

Personally I’d go for option 2 which you can read about here


#3

How did this work out for you @amruthrajsagar?


#4

I just make a similar setup for my neopixel leds. I have a ESP8266-01 running cayenne only and a SAMD dev board driving the neopixels. They talk via I2C and it works very good. When I change a value on the cayenne dashboard, it sends a Wire library interrupt (which is blocking) to reset the board (to break the many, many neopixel loops) and then sends the new data from cayenne to the dev board and runs the specified neopixel pattern.


#5

Sorry for the late reply,
i am trying to use as SPI communication rather than I2C


#6

Why use SPI with all those wires?


#7

Haha, was just talking with a friend about that. I2C is so much easier…