Created
February 2, 2023 20:54
-
-
Save SchizoDuckie/8d33c6f4f568e13f462e3ddf22f1b3f3 to your computer and use it in GitHub Desktop.
Stairled sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Adafruit_VL53L1X.h" | |
#include "EspMQTTClient.h" | |
EspMQTTClient client( | |
"YOUR_WIFI_NAME", | |
"YOUR_WIFI_PASSWORD", | |
"atticstairleds.local", // MQTT Broker server ip or hostname | |
NULL, // Can be omitted if not needed | |
NULL, // Can be omitted if not needed | |
"sensor-upstairs", // Client name that uniquely identify your device | |
1883 // The MQTT port, default to 1883. this line can be omitted | |
); | |
String CLIENT_NAME = String("Upstairs"); | |
String DELIMITER = String("|"); | |
#define IRQ_PIN 2 | |
#define XSHUT_PIN 3 | |
Adafruit_VL53L1X sensor = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN); | |
void setup() { | |
Serial.begin(115200); | |
// wait until serial port opens for native USB devices | |
while (! Serial) { | |
delay(1); | |
} | |
Serial.println(); | |
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output | |
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password"). | |
client.enableLastWillMessage("sensor-upstairs/lastwill", "HALP I'M DYING"); // You can activate the retain flag by setting the third parameter to true | |
Serial.println("Booting stairled sensor with VL53L1X"); | |
Wire.begin(); | |
if (! sensor.begin(0x29, &Wire)) { | |
Serial.print(F("Error on init of VL sensor: ")); | |
Serial.println(sensor.vl_status); | |
while (1) delay(10); | |
} | |
Serial.println(F("VL53L1X sensor OK!")); | |
Serial.print(F("Sensor ID: 0x")); | |
Serial.println(sensor.sensorID(), HEX); | |
if (! sensor.startRanging()) { | |
Serial.print(F("Couldn't start ranging: ")); | |
Serial.println(sensor.vl_status); | |
while (1) delay(10); | |
} | |
Serial.println(F("Ranging started")); | |
} | |
// This function is called once everything is connected (Wifi and MQTT) | |
void onConnectionEstablished() | |
{ | |
client.publish("sensor-upstairs/connected", "Hello! I'm gonna start spewing you data!"); | |
} | |
void loop() { | |
int16_t distance; | |
if (sensor.dataReady()) { | |
// new measurement for the taking! | |
distance = sensor.distance(); | |
if (distance == -1) { | |
// something went wrong! | |
Serial.print(F("Couldn't get distance: ")); | |
Serial.println(sensor.vl_status); | |
return; | |
} | |
Serial.print(distance); | |
// data is read out, time for another reading! | |
sensor.clearInterrupt(); | |
client.publish("sensor", CLIENT_NAME + DELIMITER + String(distance)+DELIMITER+String(sensor.vl_status)); // You can activate the retain flag by setting the third parameter to true | |
} | |
client.loop(); | |
delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment