Merge sketch to cayenne


#1

Hi everyone, I was successful read with two sensor, they are voltage sensor and current sensor. And I feel dificult to merge with cayenne. Could you help me to merge these 3 sketch please?

// This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>

#define led_1_pin 2
#define led_2_pin 3
#define led_3_pin 4
#define led_4_pin 5
#define led_5_pin 6
#define led_6_pin 7
#define led_7_pin 8

// 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);
pinMode(led_1_pin, OUTPUT);
pinMode(led_2_pin, OUTPUT);
pinMode(led_3_pin, OUTPUT);
pinMode(led_4_pin, OUTPUT);
pinMode(led_5_pin, OUTPUT);
pinMode(led_6_pin, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop() {
Cayenne.loop();

//Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);
//Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
}

CAYENNE_IN(1) // Get current value of led 1 button
{
int x = getValue.asInt();
digitalWrite(led_1_pin, x);
}
CAYENNE_IN(2) // Get current value of led 2 button
{
int x1 = getValue.asInt();
digitalWrite(led_2_pin, x1);

}
CAYENNE_IN(3) // Get current value of led 3 button
{
int x2 = getValue.asInt();
digitalWrite(led_3_pin, x2);
}
CAYENNE_IN(4) // Get current value of led 4 button
{
int x3 = getValue.asInt();
digitalWrite(led_4_pin, x3);

}
CAYENNE_IN(5) // Get current value of led 5 button
{
int x4 = getValue.asInt();
digitalWrite(led_5_pin, x4);

}
CAYENNE_IN(6) // Get current value of led 6 button
{
int x5 = getValue.asInt();
digitalWrite(led_6_pin, x5);

}

void setup() {
pinMode (A0, INPUT);
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned int c = 0.0;
float nilaiACS =0.0, sampling=0.0, rate=0.0, fnilaiACS=0.0;

for (int i =0; i<150; i++){
nilaiACS = analogRead(A0);
sampling = sampling + nilaiACS;
delay(10);
}
rate = sampling/150.0;
fnilaiACS = (2.71 - (rate*(4.7 / 1024.0)) / 0.185);
if (fnilaiACS < 0.01)
{
fnilaiACS = 0.0;
}

Serial.print(fnilaiACS);
Serial.println (" Ampere");
delay(100);
}

/*
DC Voltmeter Using a Voltage Divider
Based on Code Created By
T.K.Hareendran
*/

int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;
void setup() {
pinMode(analogInput, INPUT);
Serial.begin(9600);
Serial.print(“DC VOLTMETER”);
}
void loop() {
// read the value at analog input
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2 / (R1 + R2));
if (vin < 0.1)
{
vin = 0.0;
}

Serial.print("INPUT V= ");
Serial.println(vin, 2);
delay(500);
}


#2

i am guessing you want to show current and voltage value on cayenne dashboard.
use Cayenne.virtualWrite(1, fnilaiACS, "current", "a"); for current value widget.
and Cayenne.virtualWrite(1, vin , "voltage", "v"); for voltage value widget.


#3

Thanks a lot, its work.


#4

glad to hear that. can you share the code if possible.


#5

Here we go,

// This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

// #define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>

#define led_1_pin 2
#define led_2_pin 3
#define led_3_pin 4
#define led_4_pin 5
#define led_5_pin 6
#define led_6_pin 7
#define led_7_pin 8
#define analogInput0 0
#define analogInput1 1

// Volmeter
int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;

// 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);
  Serial.print("DC VOLTMETER");
  pinMode(led_1_pin, OUTPUT);
  pinMode(led_2_pin, OUTPUT);
  pinMode(led_3_pin, OUTPUT);
  pinMode(led_4_pin, OUTPUT);
  pinMode(led_5_pin, OUTPUT);
  pinMode(led_6_pin, OUTPUT);
  pinMode(analogInput0, INPUT);
  pinMode(analogInput1, INPUT);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();

  // ACS712-30A
  unsigned int c = 0.0;
float nilaiACS =0.0, sampling=0.0, rate=0.0, fnilaiACS=0.0;

for (int i =0; i<150; i++){
  nilaiACS = analogRead(A0);
  sampling = sampling + nilaiACS;
  delay(10);
}
rate = sampling/150.0;
fnilaiACS = (2.71 - (rate*(5.0 / 1024.0)) / 0.185);
  if (fnilaiACS < 0.05)
  {
    fnilaiACS = 0.0;
  }

  //Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, fnilaiACS, "current", "a");
    Cayenne.virtualWrite(1, vin , "voltage", "v");
    //Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    //Cayenne.luxWrite(2, 700);
    //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
      // read the value at analog input

      //Voltmeter
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0; // see text
  vin = vout / (R2 / (R1 + R2));
  if (vin < 0.50)
  {
    vin = 0.0;
  }

unsigned int c = 0.0;
float nilaiACS =0.0, sampling=0.0, rate=0.0, fnilaiACS=0.0;

for (int i =0; i<150; i++){
  nilaiACS = analogRead(analogInput0);
  sampling = sampling + nilaiACS;
  delay(10);
}
rate = sampling/150.0;
fnilaiACS = (5.0 - (rate*(5.0 / 1024.0)) / 0.9);
  if (fnilaiACS < 0.05)
  {
    fnilaiACS = 0.0;
  }
  
  }
}

CAYENNE_IN(1) // Get current value of led 1 button
{
  int x = getValue.asInt();
  digitalWrite(led_1_pin, x);
}
CAYENNE_IN(2) // Get current value of led 2 button
{
  int x1 = getValue.asInt();
  digitalWrite(led_2_pin, x1);
  
  }
  CAYENNE_IN(3) // Get current value of led 3 button
{
  int x2 = getValue.asInt();
  digitalWrite(led_3_pin, x2);
}
CAYENNE_IN(4) // Get current value of led 4 button
{
  int x3 = getValue.asInt();
  digitalWrite(led_4_pin, x3);
  
  }
  CAYENNE_IN(5) // Get current value of led 5 button
{
  int x4 = getValue.asInt();
  digitalWrite(led_5_pin, x4);
  
  }
  CAYENNE_IN(6) // Get current value of led 6 button
{
  int x5 = getValue.asInt();
  digitalWrite(led_6_pin, x5);





}

#6

thank you @asmararahardian . it will great if you can submit your project for the community to have a look. http://community.mydevices.com/c/project-with-cayenne