Hi @adam ,
I’m working on a project similar to yours and i’m trying to put a widget for my DHT22 sensors.
Can you please teach me how did you add the widget showing the values of the temperature and the humidity please?
With MQTT they will be created automatically as it sends the data to the dashboard. You can manually add them but I wouldn’t recommend it. Add New>Device/Widget>Custom Widgets
there are some typing error in your code. try this code :
// 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>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
dht.begin();
sensor_t sensor;
}
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);
sensors_event_t event;
dht.temperature().getEvent(&event);
Cayenne.celsiusWrite(1, event.temperature);
dht.humidity().getEvent(&event);
Cayenne.virtualWrite(2, event.relative_humidity, "rel_hum", "p");
}
}
//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("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
I’ve got it now but it seems it displays the value wrongly. The value is very big for a temperature.
Would you please help me im very sorry im new to this valuetoobig|690x388
it looks like you are not getting any reading from DDHT11 sensor. please check the connection and add a serial command to check whether you can read any value from the sensor.
the value you are seeing on the dashboard (big value) is the current uptime in millis on the channel 0.
I’ve got it working now. But can you help me combine the code into my code? I cant really figure it out how to combine it. I have a low knowledge of programming, as well as using Cayenne application. I’m working on a project about greenhouse and I want to just display the photoresistor and the dht readings in the dashboard. I’ve tried to combine it but it doesn’t display the value. But I dont want to use the trigger part. Please help, and thank you in advance.
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#include "DHT.h"
#define LIGHTSENSOR_PIN 0 //pin for photoresistor
#define VIRTUAL_CHANNEL 1 //channel in cayenne
#define DHTPIN 7 //pin for dht
#define DHTTYPE DHT11 //dht type used
#define fan 4 //pin for cooling fan
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxx";
char password[] = "xxx";
char clientID[] = "xxx";
int maxHum = 45; //ideal humidity levels
int maxTemp = 27; //ideal temperature level
DHT dht(DHTPIN, DHTTYPE);
int PROBE= 1; //soil moisture sensor pin
int value= 0; //soilmoisture value
int TIP120pin = 3; //fan pin
int sensorPin = A0; // photoresistor pin
int sensorValue = 0; // value for photoresistor
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
pinMode(fan, OUTPUT);
dht.begin();
// set the digital pin as output:
pinMode(5,OUTPUT);
pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin
pinMode(4, OUTPUT);
pinMode(2, OUTPUT); //pin connected to the
}
void loop() {
Cayenne.loop();
// Wait a few seconds between measurements.
delay(2000);
// 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
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(h > maxHum || t > maxTemp) {
digitalWrite(fan, LOW);
} else {
digitalWrite(fan, HIGH);
}
Serial.println("_____________________________________________");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" *C ");
//code for the light
sensorValue = analogRead(sensorPin);
Serial.print("Light Intensity = ");
Serial.println(sensorValue);
if(sensorValue > 500) //threshold for optimum lumen
digitalWrite(2,HIGH); //turn relay ON
else digitalWrite(2,LOW); //turn relay OFF
//code water pump
value= analogRead(PROBE);
value= value/100; //divided by 100
Serial.print("Moisture of soil = ");
Serial.println(value);
Serial.println("_____________________________________________");
if(value<5) //modify here the value
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}
// 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(2, 23.0); //tukar ni for data tipu Temperature
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 58); //tukar ni for data tipu humidty
}
// 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");
}
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(LIGHTSENSOR_PIN));
}
my code is fine, but i want to combine your code into my code. so that in the dashboard, there will be displays of temperature, humidity, and luminosity
they are not neccesary. you can use CAYENNE_IN_DEFAULT() when you have more then 32 actuator widgets and use switch case to get data from channel.
CAYENNE_LOG gives detail about any error.
there is currently a bug with trigger engine and team is working to get a new trigger engine soon.
/*
This example shows how to connect to Cayenne using a Serial USB connection and send/receive sample data.
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.
This requires the use of the Serial USB connection so you cannot use the Serial device for
printing messages. If you need to print you can use SoftwareSerial and connect another device
to read messages via the SoftwareSerial pins.
In order for this to work you must run the connection script on the machine the Arduino is connected to.
The scripts are located under the extras\scripts folder in the main library folder. This redirects the traffic
from the Arduino to the Cayenne server.
Steps:
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload this sketch.
3. Launch the connection script as described below for Windows or Linux/OSX.
Windows:
1. Open the Windows command line (cmd.exe)
2. Navigate to the scripts folder by typing "cd [path]", e.g. "cd C:\Users\[YourUserName]\Documents\Arduino\libraries\CayenneMQTT\extras\scripts"
3. Run the script by typing "cayenne-ser.bat -c COM4" (where COM4 is the Arduino serial port) and hitting Enter
Linux and OSX:
./cayenne-ser.sh (may need to run with sudo)
You can specify port, baud rate, and server endpoint like this:
./cayenne-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>
For instance :
./cayenne-ser.sh -c /dev/ttyACM0 -b 9600 -s mqtt.mydevices.com -p 1883
Run cayenne-ser.sh -h for more information
Be sure to select the right serial port (there may be multiple).
ATTENTION!
Do not use Serial to display any output in this sketch. It will interfere with the Serial
USB connection. When uploading sketches the Arduino IDE may complain with "programmer is
not responding" or "Access is denied." You will need to terminate the connection script
before uploading new sketches since it blocks access to the Serial port. Also make sure
the Serial Monitor is disabled in the IDE since that can prevent the Arduino from
connecting to the Windows/Linux/OSX machine. If you use Visual Micro for Visual Studio make
sure Automatic Debugging is disabled. Otherwise the Serial Monitor can interfere with the
Serial connection.
*/
#include <CayenneMQTTSerial.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT_Unified dht (DHTPIN, DHTTYPE);
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
void setup()
{
//Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
Cayenne.begin(username, password, clientID);
dht.begin();
sensor_t sensor;
}
void loop() {
Cayenne.loop();
}
// 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());
sensors_event_t event;
dht.temperature().getEvent(&event);
Cayenne.celsiusWrite(1, event.temperature);
dht.humidity().getEvent(&event);
Cayenne.virtualWrite(2, event.relative_humidity, "rel_hum", "p");
}
// 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()
{
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}