Last active
February 26, 2018 05:52
-
-
Save Gabyyh/8069f5de87226d11f7ff10899d527b50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data. | |
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. | |
//#define CAYENNE_DEBUG | |
#define CAYENNE_PRINT Serial | |
#include <CayenneMQTTESP8266.h> | |
#include "SoftwareSerial.h" | |
#include "DHT.h" | |
// WiFi network info. | |
char ssid[] = "DIGI-01093812"; | |
char wifiPassword[] = "25ff3h53"; | |
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard. | |
char username[] = "5b9ad610-fb57-11e7-8e1f-631f7dc7614e"; | |
char password[] = "c32522cbbd03367d6365d2a25d3934f14543ee4c"; | |
char clientID[] = "de7bc7d0-171b-11e8-b59c-db84183bf26b"; | |
#define DHT1PIN D5 | |
#define DHT1TYPE DHT22 // DHT 22 elszívott | |
#define DHT2PIN D1 | |
#define DHT2TYPE DHT22 // DHT 22 befújt | |
#define DHT3PIN D2 | |
#define DHT3TYPE DHT22 // DHT 22 friss | |
#define DHT4PIN D3 | |
#define DHT4TYPE DHT22 // DHT 22 kifújt | |
// Virtual Pin of the widget. | |
const int VIRTUAL_PIN1 = 1; //temp elszívott | |
const int VIRTUAL_PIN2 = 2; //hum elszívott | |
const int VIRTUAL_PIN3 = 3; //temp befújt | |
const int VIRTUAL_PIN4 = 4; //hum befújt | |
const int VIRTUAL_PIN5 = 5; //temp friss | |
const int VIRTUAL_PIN6 = 6; //hum friss | |
const int VIRTUAL_PIN7 = 7; //temp kifújt | |
const int VIRTUAL_PIN8 = 8; //hum kifújt | |
const int VIRTUAL_PIN9 = 9; //co2 elszívott | |
#define pwmPin D4 //co2pwm narancs | |
#define PWM_DIGITAL_PIN1 D6 //elszívó vent | |
#define PWM_DIGITAL_PIN2 D7 //befújó vent | |
const int VIRTUAL_PIN10 = 10; //elszívó vent speed | |
const int VIRTUAL_PIN11 = 11; //befújó vent speed | |
const int VIRTUAL_PIN12 = 12; //hatásfok % 1 | |
const int VIRTUAL_PIN13 = 13; //hatásfok % 2 | |
SoftwareSerial mySerial(RX, TX); // RX, TX co2 zöld fehér | |
//#define pwmPin D4 //co2pwm narancs | |
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; | |
unsigned char response[9]; | |
unsigned long th, tl, ppm, ppm2, ppm3, eff, eff2 = 0; | |
DHT dht1(DHT1PIN, DHT1TYPE); | |
DHT dht2(DHT2PIN, DHT2TYPE); | |
DHT dht3(DHT3PIN, DHT3TYPE); | |
DHT dht4(DHT4PIN, DHT4TYPE); | |
unsigned long lastMillis = 0; | |
void setup() { | |
Serial.begin(9600); | |
Cayenne.begin(username, password, clientID, ssid, wifiPassword); | |
dht1.begin(); | |
dht2.begin(); | |
dht3.begin(); | |
dht4.begin(); | |
mySerial.begin(9600); | |
pinMode(pwmPin, INPUT); | |
pinMode(PWM_DIGITAL_PIN1, OUTPUT); | |
pinMode(PWM_DIGITAL_PIN2, OUTPUT); | |
} | |
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); | |
//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.virtualWrite(PWM_DIGITAL_PIN1, value); | |
float t1 = dht1.readTemperature(); //temp elszívott | |
float t2 = dht2.readTemperature(); //temp befújt | |
float t3 = dht3.readTemperature(); //temp friss | |
float t4 = dht4.readTemperature(); //temp kifújt | |
float h1 = dht1.readHumidity(); //temp elszívott | |
float h2 = dht2.readHumidity(); //temp befújt | |
float h3 = dht3.readHumidity(); //temp friss | |
float h4 = dht4.readHumidity(); //temp kifújt | |
float eff = (t2-t3)/(t1-t3)*100; | |
float eff2 = (t1-t4)/(t1-t3)*100; | |
mySerial.write(cmd,9); | |
mySerial.readBytes(response, 9); | |
unsigned int responseHigh = (unsigned int) response[2]; | |
unsigned int responseLow = (unsigned int) response[3]; | |
ppm = (256*responseHigh)+responseLow; | |
//CO2 via pwm | |
do { | |
th = pulseIn(pwmPin, HIGH, 1004000) / 1000; | |
tl = 1004 - th; | |
ppm2 = 2000 * (th-2)/(th+tl-4); | |
ppm3 = 5000 * (th-2)/(th+tl-4); | |
} while (th == 0); | |
Serial.println(ppm); | |
Serial.println(th); | |
Serial.println(ppm2); | |
Serial.println(ppm3); | |
Serial.println("-----------"); | |
Serial.println(t2); | |
delay(10000); | |
temp_el(VIRTUAL_PIN1); | |
hum_el(VIRTUAL_PIN2); | |
temp_be(VIRTUAL_PIN3); | |
hum_be(VIRTUAL_PIN4); | |
temp_friss(VIRTUAL_PIN5); | |
hum_friss(VIRTUAL_PIN6); | |
temp_ki(VIRTUAL_PIN7); | |
hum_ki(VIRTUAL_PIN8); | |
co2_el(VIRTUAL_PIN9); | |
effa(VIRTUAL_PIN12); | |
effb(VIRTUAL_PIN13); | |
} | |
} | |
void temp_el(int VIRTUAL_PIN1) //temp elszívott | |
{ | |
// Read data from the sensor and send it to the virtual channel here. | |
// You can write data using virtualWrite or other Cayenne write functions. | |
// For example, to send a temperature in Celsius you can use the following: | |
float t1 = dht1.readTemperature(); | |
Cayenne.virtualWrite(VIRTUAL_PIN1, t1); | |
} | |
void hum_el(int VIRTUAL_PIN2)//hum elszívott | |
{ | |
float h1 = dht1.readHumidity(); | |
Cayenne.virtualWrite(VIRTUAL_PIN2, h1); | |
} | |
void temp_be(int VIRTUAL_PIN3)//temp befújt | |
{ | |
float t2 = dht2.readTemperature(); | |
Cayenne.virtualWrite(VIRTUAL_PIN3, t2); | |
} | |
void hum_be(int VIRTUAL_PIN4)//hum befújt | |
{ | |
float h2 = dht2.readHumidity(); | |
Cayenne.virtualWrite(VIRTUAL_PIN4, h2); | |
} | |
void temp_friss(int VIRTUAL_PIN5)//temp friss | |
{ | |
float t3 = dht3.readTemperature(); | |
Cayenne.virtualWrite(VIRTUAL_PIN5, t3); | |
} | |
void hum_friss(int VIRTUAL_PIN6)//hum friss | |
{ | |
float h3 = dht3.readHumidity(); | |
Cayenne.virtualWrite(VIRTUAL_PIN6, h3); | |
} | |
void temp_ki(int VIRTUAL_PIN7)//temp kifújt | |
{ | |
float t4 = dht4.readTemperature(); | |
Cayenne.virtualWrite(VIRTUAL_PIN7, t4); | |
} | |
void hum_ki(int VIRTUAL_PIN8)//hum kifújt | |
{ | |
float h4 = dht4.readHumidity(); | |
Cayenne.virtualWrite(VIRTUAL_PIN8, h4); | |
} | |
void co2_el(int VIRTUAL_PIN9) //co2 elszívott | |
{ | |
//int ppm = readCO2(); | |
Cayenne.virtualWrite(VIRTUAL_PIN9, ppm3); | |
} | |
void effa(int VIRTUAL_PIN12)//hatásfok | |
{ | |
//float t4 = dht4.readTemperature(); | |
float t1 = dht1.readTemperature(); | |
float t2 = dht2.readTemperature(); | |
float t3 = dht3.readTemperature(); | |
float eff = (t2-t3)/(t1-t3)*100; | |
Cayenne.virtualWrite(VIRTUAL_PIN12, eff); | |
} | |
void effb(int VIRTUAL_PIN13)//hatásfok2 | |
{ | |
float t1 = dht1.readTemperature(); | |
float t4 = dht4.readTemperature(); | |
float t3 = dht3.readTemperature(); | |
float eff2 = (t1-t4)/(t1-t3)*100; | |
Cayenne.virtualWrite(VIRTUAL_PIN13, eff2); | |
} | |
CAYENNE_IN(10) | |
{ | |
int currentValue = getValue.asInt(); // 0 to 255 | |
//CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_PIN10, PWM_DIGITAL_PIN1, value); | |
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255. | |
// analogWrite(PWM_DIGITAL_PIN1, value); | |
Serial.println(currentValue); | |
int value1= map(currentValue, 0, 100, 0, 255); | |
analogWrite(PWM_DIGITAL_PIN1, value1); | |
} | |
CAYENNE_IN(11) | |
{ | |
int currentValue = getValue.asInt(); // 0 to 255 | |
//CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_PIN11, PWM_DIGITAL_PIN2, currentValue); | |
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255. | |
//analogWrite(PWM_DIGITAL_PIN1, currentValue); | |
//Serial.println(currentValue); | |
// Serial.println(value2); | |
int value2= map(currentValue , 0, 100, 0, 255); | |
analogWrite(PWM_DIGITAL_PIN2, value2); | |
} | |
// Default function for sending sensor data at intervals to Cayenne. | |
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data. | |
CAYENNE_OUT_DEFAULT() | |
{ | |
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0. | |
Cayenne.virtualWrite(0, millis()); | |
// 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); | |
} | |
// 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("Channel %u, value %s", request.channel, getValue.asString()); | |
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment