I need help and am new to Arduino


#1

I am trying to get the relays to work on a smart thermostat I am trying to design. The code is below and I hope someone can help me.

/////////////////////////////////
// Generated with a lot of love//
// with TUNIOT FOR ESP8266     //
// Website: Easycoding.tn      //
/////////////////////////////////
#include <ESP8266WiFi.h>

#include "DHT.h"

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"


#define VIRTUAL_CHANNEL 2
#define ACTUATOR_PIN 6 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.


#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>//IMPPORT LIBRARY FROM http://easycoding.tn/index.php/resources/

char Cayenneusername[] = "XXX";
char Cayennepassword[] = "YYY";
char CayenneclientID[] = "ZZZ";
DHT dht5(5,DHT22);


//For Serial Display
#include <Wire.h>
#include <SPI.h>
#include "SH1106.h"
#include "SH1106Ui.h"

// For DHT Tester
#include "DHT.h"
#define DHTPIN 5     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);


//For Serial Display
#define OLED_RESET  D1   // RESET
#define OLED_DC     D2   // Data/Command
#define OLED_CS     D8   // Chip select

// Uncomment one of the following based on OLED type
SH1106 display(true, OLED_RESET, OLED_DC, OLED_CS); // FOR SPI
//SH1106   display(OLED_ADDR, OLED_SDA, OLED_SDC);    // For I2C
SH1106Ui ui     ( &display );


void setup()
{
  Serial.begin(9600);
  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
   WiFi.begin("XXX","1111111122");
  while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
Serial.print("..");

  }
  Serial.println("Connected");
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP().toString()));
 
  
  Cayenne.begin(Cayenneusername, Cayennepassword, CayenneclientID);

 

}


void loop()
{

Cayenne.virtualWrite(0,(dht5.readTemperature(true)));

Cayenne.virtualWrite(1,(dht5.readHumidity(true)));
 
  delay(1000);
  





// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

   
  
  
display.init();

  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 0, "C-ra Thermostat 1 Results");
  display.display();
  
   
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 32, "Temp:  " + String(f) + " *F");
  display.display();
  
  
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 16, "Humidity:  " + String(h) + " %");
  display.display();

  
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 48, "Heat index:  " + String(hif) + " *F");
  display.display();

delay(15000);


}




// This function is called when data is sent from Cayenne.
CAYENNE_IN(V0)
{
// initialize digital pin 2 as an output.
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D9, OUTPUT);
  
  
  // This function will be called every time a Dashboard widget writes a value to Virtual Pin 2.

{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
digitalWrite(D0, HIGH);
  }
  else
  {
digitalWrite(D0, LOW);
  }  

}
{

  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
digitalWrite(D1, HIGH);
  }
  else
  {
digitalWrite(D1, LOW);
  }  

}
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
digitalWrite(D2, HIGH);
  }
  else
  {
digitalWrite(D2, LOW);
  }  
}

}

#2

By the way, I am using an ESP 8266


#3

#4

what is the issue you are facing? you can have a look at this Adding a New Device using MQTT


#5

I had a look at the link Add in a New Device Using MQTT and it could not help me. I revised the sketch and it compiles without error but it still will not get the values and turn on the relay. He is the code I am using.

/////////////////////////////////
// Generated with a lot of love//
// with TUNIOT FOR ESP8266     //
// Website: Easycoding.tn      //
/////////////////////////////////
#include <ESP8266WiFi.h>

#include "DHT.h"


#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>//IMPPORT LIBRARY FROM http://easycoding.tn/index.php/resources/

char Cayenneusername[] = "XXX";
char Cayennepassword[] = "YYY";
char CayenneclientID[] = "ZZZ";
DHT dht5(5,DHT22);


//For Serial Display
#include <Wire.h>
#include <SPI.h>
#include "SH1106.h"
#include "SH1106Ui.h"

// For DHT Tester
#include "DHT.h"
#define DHTPIN 5     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);


//For Serial Display
#define OLED_RESET  D1   // RESET
#define OLED_DC     D2   // Data/Command
#define OLED_CS     D8   // Chip select

// Uncomment one of the following based on OLED type
SH1106 display(true, OLED_RESET, OLED_DC, OLED_CS); // FOR SPI
//SH1106   display(OLED_ADDR, OLED_SDA, OLED_SDC);    // For I2C
SH1106Ui ui     ( &display );


void setup()
{
  Serial.begin(9600);
  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
   WiFi.begin("XXX","111112222");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP().toString()));
 
  
  Cayenne.begin(Cayenneusername, Cayennepassword, CayenneclientID);


}


void loop()
{

    Cayenne.virtualWrite(0,(dht5.readTemperature(true)));
    
    Cayenne.virtualWrite(1,(dht5.readHumidity(true)));
    /*Cayenne.virtualWrite(1,dht5.readHumidity(true),"temp","f")*/
    delay(1000);






// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

   
  
  
display.init();

  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 0, "C-ra Thermostat 1 Results");
  display.display();
  
   
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 32, "Temp:  " + String(f) + " *F");
  display.display();
  
  
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 16, "Humidity:  " + String(h) + " %");
  display.display();

  
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 48, "Heat index:  " + String(hif) + " *F");
  display.display();

delay(15000);
}
 // This function will be called every time a Dashboard widget writes a value to Virtual Channel 0.
CAYENNE_IN(0)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  // You can also use:
   int i = getValue.asInt();
  // double d = getValue.asDouble()

  if (i >= 0)
  {
    Serial.println(i);
    digitalWrite(D4, HIGH);
    
  }
  else
  {
    Serial.println(i);
    digitalWrite(D4, LOW);
    
  }

}

/*CAYENNE_IN(0){
  // read the input on analog pin 0:
  int sensorValue = analogRead(D5);
  // print out the value you read:
  Serial.println(sensorValue);
  
}*/

#6

if you have followed the steps from the link there should not be an issue. Are you getting any issue in the serial monitor. Use a plain code without the sensor and first connect your device to cayenne. then add the remaining code.


#7

The serial monitor is working fine and the device is communicating with the Cayenne platform. I just cannot get it to work with the three relays, one is for a fan, the other is for air-conditioning and the last one is for the heat. The last part of the code is what I am trying to get to work.


#8

you are using channel 0 for sending data and also for receiving data. You need to change either one to another number and have a button on cayenne dashboard with same channel number. And also you are sending data continuously in the void loop(). this will cause to hit rate limit. Have a look at this and make changes accordingly Sending MQTT messages within rate limits


#9

Thanks, I am working on it and will be in touch.