Serial flush in sleep method also means one less code in examples. Removed set(bool) in message. Not needed. getBool just calls getInt __attribute__((packed)) for RPI compability Print more info in send/read debug prints. Get internalTemp returns full degrees and uses longer Vref delay Fix debug print gateway prepend (wrong command/type) Fixed problem in RelayWithButton example. Now working with ack in both directions. Serial protocol changed (incoming serial message have an new ack-parameter)
45 lines
1.3 KiB
Arduino
45 lines
1.3 KiB
Arduino
#include <SPI.h>
|
|
#include <MySensor.h>
|
|
#include <NewPing.h>
|
|
|
|
#define CHILD_ID 1
|
|
#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
|
|
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
|
|
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
|
|
unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
|
|
|
|
MySensor gw;
|
|
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
|
|
MyMessage msg(CHILD_ID, V_DISTANCE);
|
|
int lastDist;
|
|
boolean metric = true;
|
|
|
|
void setup()
|
|
{
|
|
gw.begin();
|
|
|
|
// Send the sketch version information to the gateway and Controller
|
|
gw.sendSketchInfo("Distance Sensor", "1.0");
|
|
|
|
// Register all sensors to gw (they will be created as child devices)
|
|
gw.present(CHILD_ID, S_DISTANCE);
|
|
boolean metric = gw.getConfig().isMetric;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
int dist = metric?sonar.ping_cm():sonar.ping_in();
|
|
Serial.print("Ping: ");
|
|
Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
|
|
Serial.println(metric?" cm":" in");
|
|
|
|
if (dist != lastDist) {
|
|
gw.send(msg.set(dist));
|
|
lastDist = dist;
|
|
}
|
|
|
|
gw.sleep(SLEEP_TIME);
|
|
}
|
|
|
|
|