Hi all,
I’ve got my project sensors all working and parsing to Cayenne nicely. Through the serial monitor I can see the standard connection process; MAC, IP and “Connecting” until you get the “Ready” and ping response time. I can’t figure out however how to translate this library function across for use with an LCD so when deployed outside where I have no serial monitor I can get confirmation on the lcd before proceeding.
I have had an attempt but it always indicates failure even though the connection runs through serial without difficulty.
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#include <DHT_U.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x26, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <CayenneEthernet.h>
// Mac address should be different for each device in your LAN
byte arduino_mac[] = { yourinfo};
IPAddress arduino_ip(yourinfo);
IPAddress gateway_ip(yourinfo);
IPAddress subnet_mask(255, 255, 255, 0);
EthernetClient client;
// Virtual Pin & Config of the DS18B20
#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V5
#define VIRTUAL_PIN4 V6
const int tmpPin = 2;
float cap1;
float cap2;
float cap3;
float cap4;
OneWire oneWire1(tmpPin);
DallasTemperature sensors(&oneWire1);
//Virtual Pin & Config of the BMP085
#define TEMPERATURE_PIN V3
#define BAROMETER_PIN V4
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
bool bmpSensorDetected = true;
//Virtual Pin & Config for DHT11
#define DHT_HUMIDITY_PIN V7
#define DHT_TEMPERATURE_PIN V8
#define DHTPIN 4 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT11 // DHT 11
float dht11T;
float dht11H;
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS; //Delay between sensor reading
//Setup the Moister sensor variables (Analogue or digital read - set pin accordingly
int moisterPin = A1;
int moisterValue = 0;
#define MOISTER_PIN V9
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "yourinfo";
int runOnce = 0; //State variable for tracking the first time device runs
void setup()
{
Serial.begin(115200); //Ensure you set the correct Baud rate if you intend to add/use serial writes for debugging
Cayenne.begin(token);
sensors.begin();
dht.begin();
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
if (!bmp.begin())
{
CAYENNE_LOG("No BMP sensor detected");
bmpSensorDetected = false;
}
}
void loop()
{
Cayenne.run();
if (runOnce == 0) {
delay(5000);
lcdInitialDisplay ();
runOnce = 1;
}
}
void DHTSensorT () {
// Get temperature event from the DHT11 humidy/Temp Sensor
sensors_event_t event;
dht.temperature().getEvent(&event);
dht11T = (event.temperature);
Serial.print("Temperature:: ");
Serial.print(dht11T);
Serial.println("'C");
}
void DHTSensorH () {
// Get humidity event from the DHT11 humidy/Temp Sensor
sensors_event_t event;
dht.humidity().getEvent(&event);
dht11H = (event.relative_humidity);
Serial.print("Humidity: ");
Serial.print(dht11H);
Serial.println("%");
// Delay between measurements.
//delay(delayMS);
}
void moisterSensor() {
moisterValue = analogRead(moisterPin);
//sensorValue = map(sensorValue,160,1023,0,1000);
Serial.print("Moister: ");
Serial.print(moisterValue);
Serial.println("%");
}
// This function is called when the Cayenne widget requests data for DHT11 Humidity
CAYENNE_OUT(DHT_HUMIDITY_PIN)
{
Cayenne.virtualWrite(DHT_HUMIDITY_PIN, (dht11H), HUMIDITY, PERCENT);
DHTSensorH();
}
// This function is called when the Cayenne widget requests data for DHT11 Temperature
CAYENNE_OUT(DHT_TEMPERATURE_PIN)
{
Cayenne.virtualWrite(DHT_TEMPERATURE_PIN, (dht11T), TEMPERATURE, CELSIUS);
DHTSensorT();
}
// START - These functions are called when the Cayenne widget requests data for the DS18B20 Temp Sensors (x4)
CAYENNE_OUT(VIRTUAL_PIN1)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap1 = sensors.getTempCByIndex(0);
Cayenne.celsiusWrite(VIRTUAL_PIN1, cap1);
}
CAYENNE_OUT(VIRTUAL_PIN2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap2 = sensors.getTempCByIndex(1);
Cayenne.celsiusWrite(VIRTUAL_PIN2, cap2);
}
CAYENNE_OUT(VIRTUAL_PIN3)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap3 = sensors.getTempCByIndex(2);
Cayenne.celsiusWrite(VIRTUAL_PIN3, cap3);
}
CAYENNE_OUT(VIRTUAL_PIN4)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap4 = sensors.getTempCByIndex(3);
Cayenne.celsiusWrite(VIRTUAL_PIN4, cap4);
}
// START - This function is called when the Cayenne widget requests data for the BMP085 Barometer/Temp Sensor Virtual Pin.
CAYENNE_OUT(BAROMETER_PIN)
{
if (bmpSensorDetected)
{
// Send the command to get data.
sensors_event_t event;
bmp.getEvent(&event);
if (event.pressure)
{
// Send the value to Cayenne in hectopascals.
Cayenne.hectoPascalWrite(BAROMETER_PIN, event.pressure);
}
}
else
{
CAYENNE_LOG("No BMP sensor detected");
}
}
// This function is called when the Cayenne widget requests data for the temperature's Virtual Pin on BMP085 Sensor
CAYENNE_OUT(TEMPERATURE_PIN)
{
if (bmpSensorDetected)
{
float temperature;
bmp.getTemperature(&temperature);
// Send the value to Cayenne in Celsius.
Cayenne.celsiusWrite(TEMPERATURE_PIN, temperature);
}
else
{
CAYENNE_LOG("No BMP sensor detected");
}
}
void lcdInitialDisplay () {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Pond M8 Version 1");
lcd.setCursor(0, 2);
lcd.print("IP: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
lcd.print(Ethernet.localIP()[thisByte], DEC);
lcd.print(".");
}
lcd.setCursor(0, 3);
lcd.print("Status: ");
if (client.available()) {
lcd.setCursor(8, 3);
lcd.print("Connected");
} else {
lcd.setCursor(8, 3);
lcd.print("Failed");
}
}