Cayenne + am2301 (temp. sensor) + sd card

Hi,

How can i monitor am2301 (temp. and hum. sensor) data with Cayenne and to save data on sd card ?

Are you using arduino or raspberry pi?

I am using esp8266

here is an example to connect it to arduino Cayenne-MQTT-Arduino/DHT.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub
for esp8266 you need to combine the above code into this Cayenne-MQTT-Arduino/ESP8266.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub

Dear,

Will sensor data be saved on sd card?

Can you help me with the code please ?
I cant understand how to combine the code.

Waiting for your reply.

Thanka a lot,

But using this code i can only save data on sd card.

I have another problem, i want to save data on sd card and on Cayenne.

Can you me with code please ?

here is how to do it. Did you atleast try it out?

No, because i dont know how to combine these two codes. This is the problem.

I am using this code to show temperature and humidity on dashboard. And i want to save same data on sd card.

Can you help me ?

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include “DHT.h”
#define DHTPIN 12 // modify to the pin we connected
#define DHTTYPE AM2301 // AM2301

DHT dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid = “*******************”;
char wifiPassword = “**************”;

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

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
dht.begin();
}

void loop() {
Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);

float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Cayenne.celsiusWrite(1, temperature);
Cayenne.virtualWrite(2, humidity, "rel_hum", "p");

}
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

then you might need to learn some basic first https://www.arduino.cc/en/Tutorial/HomePage?from=Main.Tutorials

I am using this code to show temperature and humidity on dashboard. And i want to save same data on sd card.

Can you help me ?

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include “DHT.h”
#define DHTPIN 12 // modify to the pin we connected
#define DHTTYPE AM2301 // AM2301

DHT dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid = “*******************”;
char wifiPassword = “**************”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “ ";
char password = “
”;
char clientID = "
*****************************”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
dht.begin();
}

void loop() {
Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);

float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Cayenne.celsiusWrite(1, temperature);
Cayenne.virtualWrite(2, humidity, "rel_hum", "p");

}
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

Do you have a SD card module?

yes

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include “DHT.h”

#include <SD.h>
#define CS_PIN  D8;


#define DHTPIN 12 // modify to the pin we connected
#define DHTTYPE AM2301 // AM2301

DHT dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid = “*******************”;
char wifiPassword = “**************”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xx xx";
char password = "xxxx";
char clientID = "xxxx”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  Serial.print("Initializing SD card...");

  if (!SD.begin(CS_PIN)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
    while (1);
  }

  Serial.println("initialization done.");

dht.begin();
}

void loop() {
Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);

float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Cayenne.celsiusWrite(1, temperature);
Cayenne.virtualWrite(2, humidity, "rel_hum", "p");
 
 // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print(temperature);
      dataFile.print(" | ");
    myFile.println(humidity);

// close the file:
myFile.close();
Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
    }
    }

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

I have used this code, but this message is shown in my dispay:

macro “CAYENNE_IN_DEFAULT” passed 1 arguments, but takes just 0.

Why is happening this ?

you can remove the entire function.

Sir,

this message is shown:

myfile is not declared in this scope.
dataFile is not declared in this scope

How to declare it ?
Do i need to create .txt file ?

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include “DHT.h”

#include <SD.h>
#define CS_PIN  D8;
File myFile;

#define DHTPIN 12 // modify to the pin we connected
#define DHTTYPE AM2301 // AM2301

DHT dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid = “*******************”;
char wifiPassword = “**************”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xx xx";
char password = "xxxx";
char clientID = "xxxx”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  Serial.print("Initializing SD card...");

  if (!SD.begin(CS_PIN)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
    while (1);
  }

  Serial.println("initialization done.");

dht.begin();
}

void loop() {
Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);

float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Cayenne.celsiusWrite(1, temperature);
Cayenne.virtualWrite(2, humidity, "rel_hum", "p");

   myFile = SD.open("test.txt", FILE_WRITE);
 // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.print(temperature);
    myFile.print(" | ");
    myFile.println(humidity);

// close the file:
myFile.close();
Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
    }
    }

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

Sir,

Its the same code. Its not working.

i dont think so it is the same code. do you know coding then please do it.