So, I managed to get my application working. I need to use an ultrasonic sensor to measure water level on a tank, in order to turn the pump on or off depending on level of water, and avoid burning the pump if it ran out of liquid to pump.
I also need to keep track of the water level historically, but I’m still working on it as I want to record it to the Arduino SD slot.
I’m using MQTT and “bring your own device option”. Everything works just fine, but I can’t see my device on my mobile app (android)…I can see the regular arduino boards, the raspberry, but not the custom device…
Any idea why?
For the community, here is the code:
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#define trigPin 2
#define echoPin 3
char username = “”;
char password = “”;
char clientID = “”;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode (4,OUTPUT);
pinMode (5,OUTPUT);
}
void loop() {
Cayenne.loop();
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(2000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print (distance);
Serial.println (“cm”);
//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if(millis() - lastMillis > 5000) {
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(0, distance, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
}
//CAYENNE_CONNECTED()
//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());
if (request.channel = 4)
{
String value;
value = (String)getValue.asString();
if (value == “1”)
{
digitalWrite(4,HIGH);
Serial.println(“Pump ON”);
}
else
{
digitalWrite(4,LOW);
Serial.println(“Pump OFF”);
}
}
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}