Temperature measurement


#1

Hi. I have a problem with showing figures of the temperature. When I just start an arduino it shows the temperature in web browser but after a few seconds it shows 0.0. But in app on my phone it works good. How could I solve this problem? Thank you.


#2

can you share the code you are using?


#3
#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#define DHTTYPE DHT11
#define DHTPIN  5
#define VIRTUAL_CHANNEL 3
#define ACTUATOR_PIN 4

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

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

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
float h;
float t;
bool Umidade = false;
bool Temperatura = false;

DHT dht(DHTPIN, DHTTYPE);



ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600);
  delay(10);
  dht.begin();
pinMode(ACTUATOR_PIN, OUTPUT);
  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
  Cayenne.loop();
}
CAYENNE_OUT(V1){
  //Serial.println("Umidade");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    h = dht.readHumidity();

    delay(1000);
  } while  (isnan(h));
  Cayenne.virtualWrite(V1, h);
}
CAYENNE_OUT(V2){
  //Serial.println("Temperatura");
  
  //Check if read failed and try until success
  do {
    //Read temperature as Celsius
   
    t = dht.readTemperature();
    

    delay(1000);
  } while  (isnan(t));
Cayenne.virtualWrite(V2, t);
}
CAYENNE_IN(VIRTUAL_CHANNEL)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN, value);
}

// 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");
}

#4

And I have some problems more:

  1. A have a relay on schedule, it’s working, but not properly, so it’s turning “off”, but doesn’t turn “on” the next day.
  2. If relay “on” and electricity go down, and then electricity come back, relay stays “off”, however by the schedule it should be “on”.
  3. It’s always go offline after some time, so you need to restart an arduino.

#5

first delete all the widget from your dashboard. upload this code on your device https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/CommunitySubmitted/DHT/DHT.ino and add the widgets populated by it.

you mean you have two separate schedules to turn OFF and turn ON?

you can store the last of the relay in the EEPROM of the arduino and then read and write the last state to the relay on restart if the arduino.

check your serial monitor for what is causing the issue of disconnect.


#6

Thanks for the sketch. About two separate schedules, I don’t know, it’s look like that:


I will try EEPROM and check serial monitor, but I could do it on weekends. Thank you ones again.


#7

the image you shared does not provide much information on what you are trying to do.


#8

Sorry))) if I understand right, it’s two different schedules.


#9

it should work if they are two separate schedules/events.


#10

So it’s better to make 2 separate schedules or just one?


#11

you have to add two separate schedules for each action at different time.


#12

Yep, that’s what I’ve done, but it’s turning OFF but doesn’t turn ON. Maybe because it’s goes offline every time?


#13

can you PM me your email_id.


#14

i can see that you have added event to an widget which is not present on your dashboard.


#15

It is in my project:


#16

but those widgets are not visible on arduino nano device dashboard.


#17

Hi again.
I’ve try the sketch that you gave me, it doesn’t work.

/*
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected via a hardware serial to an Arduino.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
   folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries) to compile this example.
2. Connect the ESP8266 as a shield to your Arduino. This example uses the Serial1 hardware serial pins available on the Mega. You can also try 
   using a software serial, though it may be less stable.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

NOTE: This code requires ESP8266 firmware version 1.0.0 (AT v0.22) or later.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include <DHT.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

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

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
#define DHTPIN 2     // Digital pin connected to the DHT sensor

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

#define TEMPERATURE_VIRTUAL_CHANNEL 1
#define HUMIDITY_VIRTUAL_CHANNEL 2

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600);
  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
  Cayenne.loop();
}
// This function is called at intervals to send temperature sensor data to Cayenne in Celsius.
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c");
}

// This function is called at intervals to send humidity sensor data to Cayenne.
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p");
}


// 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");
}


#18

Sorry, all good, was my mistake, I forgot to put dht.begin(); sorry


#19

Glad to hear it is working now.


#20

Hi again. Could you tell me how could I control a WiFi relay?

thanks.