//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
#define PRESISTOR_VIRTUAL_CHANNEL 0
#define DISTANCE_VIRTUAL_CHANNEL 1
// Defines pins numbers for the HC-SR04 connections.
const int trigPin = 9;
const int echoPin = 10;
const int pResistor = A0;
long duration;
int distanceCm;
int value;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
Cayenne.begin(username, password, clientID);
}
void loop() {
// Cayenne.loop();
value = analogRead(pResistor);
Serial.println(value);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
Serial.println(distanceCm);// if distance less than 0.5 meter and more than 0 (0 or less means over range)
delay(100);
if( (distanceCm < 68)&&(distanceCm > 10) && (value < 180))
{
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distanceCm, "prox", "cm");
Cayenne.virtualWrite(PRESISTOR_VIRTUAL_CHANNEL,value);
}
}
CAYENNE_OUT(PRESISTOR_VIRTUAL_CHANNEL)
{
value = analogRead(pResistor);
Serial.println(value);
Cayenne.virtualWrite(PRESISTOR_VIRTUAL_CHANNEL,value);
}
// This function is called at intervals to send HC-SR04 sensor data to Cayenne.
CAYENNE_OUT(DISTANCE_VIRTUAL_CHANNEL)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
Serial.println(distanceCm);// if distance less than 0.5 meter and more than 0 (0 or less means over range)
delay(100);
// Send the distance value to Cayenne on proximity widget in centimeter.
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distanceCm, "prox", "cm");
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}