Commit c056e57d authored by Jason Williams's avatar Jason Williams

Adding code for distance sensors

parent 307a009e
......@@ -11,6 +11,10 @@ const int pin_doorclosed = D1;
const int pin_sidedoor = D2;
const int pin_doortoggle = D6;
const int pin_temperature = D7;
const int pin_trigger1 = D3;
const int pin_echo1 = D4;
const int pin_trigger2 = D5;
const int pin_echo2 = D8;
DeviceAddress ambient = { 0x28, 0x6F, 0x08, 0x0A, 0x31, 0x14, 0x01, 0x20 };
DeviceAddress freezer = { 0x28, 0x6C, 0x96, 0x00, 0x31, 0x14, 0x01, 0x77 };
......@@ -30,12 +34,16 @@ const char* topic_doorstate = "garage/door/state";
const char* topic_sidedoor = "garage/sidedoor";
const char* topic_ambienttemp = "garage/temperature";
const char* topic_freezertemp = "garage/freezer";
const char* topic_spot1 = "garage/spot1";
const char* topic_spot2 = "garage/spot2";
// Currently reported states and/or times
bool state_sidedoor = 1;
int state_door = 0;
unsigned long sent_temperature = millis();
unsigned long periodic_update = millis();
long duration;
int distance;
WiFiClient espClient;
PubSubClient client(espClient);
......@@ -139,7 +147,7 @@ void loop() {
delay(500);
}
// Check the temperatures every 30 seconds and send an update
// Check the temperatures and parkgin spots every 30 seconds and send an update
unsigned long elapsed_temperature = millis() - sent_temperature;
if(elapsed_temperature >= 30000) {
DallasTemp.requestTemperatures();
......@@ -157,11 +165,43 @@ void loop() {
Serial.print(frztempstr);
Serial.println(" degrees");
// Clear the triggers
digitalWrite(pin_trigger1, LOW);
digitalWrite(pin_trigger2, LOW);
delayMicroseconds(2);
char dist1str[4], dist2str[4];
// Take a reading on sensor 1
digitalWrite(pin_trigger1, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger1, LOW);
duration = pulseIn(pin_echo1, HIGH);
distance = duration*0.017;
// Publish sensor 1 reading
sprintf(dist1str, "%d", distance);
client.publish(topic_spot1, dist1str);
Serial.print("[Distance] Sensor 1 at ");
Serial.print(distance);
Serial.println("cm");
// Take a reading on sensor 2
digitalWrite(pin_trigger2, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger2, LOW);
duration = pulseIn(pin_echo2, HIGH);
distance = duration*0.017;
// Publish sensor 2 reading
sprintf(dist2str, "%d", distance);
client.publish(topic_spot2, dist2str);
Serial.print("[Distance] Sensor 2 at ");
Serial.print(distance);
Serial.println("cm");
sent_temperature = millis();
}
// TODO: ULTRASONIC DISTANCE SENSORS
// Check how long it's been since the last periodic update, and send another one if required
unsigned long elapsed_since_update = millis() - periodic_update;
if (elapsed_since_update >= 120000) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment