shramik_salgaonkar
Follow my code below
I’m using arduino one and w5100
//#define CAYENNE_DEBUG
//#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11
#define DHTPIN A0
DHT_Unified dht(DHTPIN, DHTTYPE);
#include <EEPROM.h>
#include “MQ7.h”
char username = “f6070370-ffbf-11e6-86d2-ad2f93147d99”;
char password = “a7b2f014cd572d647bd96f019e81bc8fca2226b7”;
char clientID = “86d36650-eb37-11e8-a254-e163efaadfe8”;
byte arduino_mac = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress arduino_ip(192, 168, 1, 100);
IPAddress dns_ip(8, 8, 8, 8);
IPAddress gateway_ip(192, 168, 1, 1);
IPAddress subnet_mask(255, 255, 255, 0);
#define VIRTUAL_CHANNEL 1 // RELE1
#define VIRTUAL_CHANNEL 2 // RELE2
#define VIRTUAL_CHANNEL 3 // RELE3
#define VIRTUAL_CHANNEL 4 // RELE4
#define VIRTUAL_CHANNEL 5 // RELE5
#define VIRTUAL_CHANNEL 6 // RELE6
#define VIRTUAL_CHANNEL 7 // TEMPERATURA
#define VIRTUAL_CHANNEL 8 // UMIDADE
#define VIRTUAL_CHANNEL 9 // LUX_1
#define VIRTUAL_CHANNEL 10 // LUX_2
#define VIRTUAL_CHANNEL 11 // CO2
#define VIRTUAL_CHANNEL 12 // SENSOR UMIDADE
#define VIRTUAL_CHANNEL 13 // DIMER
#define VIRTUAL_CHANNEL 14 // CONTATO SECO PARA PORTA
#define VIRTUAL_CHANNEL 15 // SENSOR LUX
#define RELE1_PIN 2
#define RELE2_PIN 3
#define RELE3_PIN 4
#define RELE4_PIN 5
#define RELE5_PIN 6
#define RELE6_PIN 7
#define RELE7_PIN 9
#define RELE8_PIN 8
#define LDR A1
MQ7 mq7(A2, 5.0);
#define SENSOR_SOLO A3
#define SENSOR_LUX A4
#define BUTTON1_PIN A5
unsigned long lastMillis = 0;
int buttonValue;
int state;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
dht.begin();
sensor_t sensor;
pinMode(RELE1_PIN, OUTPUT);
pinMode(RELE2_PIN, OUTPUT);
pinMode(RELE3_PIN, OUTPUT);
pinMode(RELE4_PIN, OUTPUT);
pinMode(RELE5_PIN, OUTPUT);
pinMode(RELE6_PIN, OUTPUT);
pinMode(RELE7_PIN, OUTPUT);
pinMode(RELE8_PIN, OUTPUT);
pinMode(LDR, INPUT_PULLUP);
pinMode (DHTPIN, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(SENSOR_SOLO, INPUT_PULLUP);
pinMode(SENSOR_LUX, INPUT_PULLUP);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
digitalWrite(2, EEPROM.read(10));
digitalWrite(3, EEPROM.read(20));
digitalWrite(4, EEPROM.read(30));
digitalWrite(5, EEPROM.read(40));
digitalWrite(6, EEPROM.read(50));
digitalWrite(7, EEPROM.read(60));
analogWrite(9, EEPROM.read(70));
}
void loop() {
buttonValue = analogRead(BUTTON1_PIN);
if (buttonValue >= 160 && buttonValue <= 180) {
RELE1();
}
if (buttonValue >= 210 && buttonValue <= 220) {
RELE2();
}
if (buttonValue >= 240 && buttonValue <= 260) {
RELE3();
}
if (buttonValue >= 10 && buttonValue <= 30) {
RELE4();
}
if (buttonValue >= 60 && buttonValue <= 80) {
RELE5();
}
if (buttonValue >= 110 && buttonValue <= 130) {
RELE6();
}
if (millis() - lastMillis > 2000){
lastMillis = millis();
Cayenne.loop();
sensors_event_t event;
dht.temperature().getEvent(&event);
Cayenne.celsiusWrite(7, event.temperature);
delay(250);
dht.humidity().getEvent(&event);
Cayenne.virtualWrite(8, event.relative_humidity, “rel_hum”, “p”);
delay(250);
int vout1 = analogRead(LDR);
float vout = vout1 / 204.6;
float R = (11000 - vout * 2200) / vout;
//float R = pow( X, -1);
float lux = (pow( R, (1 / -0.8616))) / (pow( 10, (5.118 / -0.8616)));
float lux2 = 65.9 * (pow( vout1, 0.352));
Cayenne.luxWrite(9, lux );
delay(250);
Cayenne.luxWrite(10, lux2 );
delay(100);
Cayenne.virtualWrite(11,mq7.getPPM(),“co2”,“ppm”);
delay(250);
int sensorsolo = analogRead(SENSOR_SOLO)/1023*100;
delay(250);
Cayenne.virtualWrite(12,sensorsolo,“rel_hum”, “p”);
delay(250);
int sensorlux = analogRead(SENSOR_LUX);
delay(250);
Cayenne.luxWrite(15,sensorlux);
Cayenne.virtualWrite(1,digitalRead(2));
delay(250);
Cayenne.virtualWrite(2,digitalRead(3));
delay(250);
Cayenne.virtualWrite(3,digitalRead(4));
delay(250);
Cayenne.virtualWrite(4,digitalRead(5));
delay(250);
Cayenne.virtualWrite(5,digitalRead(6));
delay(250);
Cayenne.virtualWrite(6,digitalRead(7));
delay(250);
Cayenne.virtualWrite(13,state);
delay(250);
Cayenne.virtualWrite(14,digitalRead(8));
delay(250);
}
}
CAYENNE_IN(1)
{
int value = getValue.asInt();
digitalWrite(RELE1_PIN, value);
EEPROM.update(10,digitalRead(2));
}
CAYENNE_IN(2)
{
int value = getValue.asInt();
digitalWrite(RELE2_PIN, value);
EEPROM.update(20,digitalRead(3));
}
CAYENNE_IN(3)
{
int value = getValue.asInt();
digitalWrite(RELE3_PIN, value);
EEPROM.update(30,digitalRead(4));
}
CAYENNE_IN(4)
{
int value = getValue.asInt();
digitalWrite(RELE4_PIN, value);
EEPROM.update(40,digitalRead(5));
}
CAYENNE_IN(5)
{
int value = getValue.asInt();
digitalWrite(RELE5_PIN, value);
EEPROM.update(50,digitalRead(6));
}
CAYENNE_IN(6)
{
int value = getValue.asInt();
digitalWrite(RELE6_PIN, value);
EEPROM.update(60,digitalRead(7));
}
CAYENNE_IN(13)
{
int state = getValue.asInt();
analogWrite(RELE7_PIN,state);
EEPROM.update(70,state);
}
CAYENNE_IN(14)
{
int value = getValue.asInt();
digitalWrite(RELE8_PIN, value);
delay(500);
if ((RELE8_PIN)> 0) {
digitalWrite(RELE8_PIN,LOW);
}
}
void RELE1() {
int state = digitalRead(RELE1_PIN);
digitalWrite(RELE1_PIN,!state);
EEPROM.update(10,digitalRead(2));
}
void RELE2() {
int state = digitalRead(RELE2_PIN);
digitalWrite(RELE2_PIN,!state);
EEPROM.update(20,digitalRead(3));
}
void RELE3() {
int state = digitalRead(RELE3_PIN);
digitalWrite(RELE3_PIN,!state);
EEPROM.update(30,digitalRead(4));
}
void RELE4() {
int state = digitalRead(RELE4_PIN);
digitalWrite(RELE4_PIN,!state);
EEPROM.update(40,digitalRead(5));
}
void RELE5() {
int state = digitalRead(RELE5_PIN);
digitalWrite(RELE5_PIN,!state);
EEPROM.update(50,digitalRead(6));
}
void RELE6() {
int state = digitalRead(RELE6_PIN);
digitalWrite(RELE6_PIN,!state);
EEPROM.update(60,digitalRead(7));
}
Thank you for your attention.