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