Added DSP310 Sensor with MQTT and Wifi
This commit is contained in:
parent
9b0e84f001
commit
a4b95097ba
3
mqtt-wifi-dsp310-sensor/ardunio_secrets.h
Normal file
3
mqtt-wifi-dsp310-sensor/ardunio_secrets.h
Normal file
@ -0,0 +1,3 @@
|
||||
//arduino_secrets.h header file
|
||||
#define SECRET_SSID "xxxx"
|
||||
#define SECRET_PASS "xxxx"
|
114
mqtt-wifi-dsp310-sensor/dsp310.ino
Normal file
114
mqtt-wifi-dsp310-sensor/dsp310.ino
Normal file
@ -0,0 +1,114 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <WiFiS3.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Adafruit_DPS310.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
Adafruit_DPS310 dps;
|
||||
Adafruit_Sensor *dps_temp = dps.getTemperatureSensor();
|
||||
Adafruit_Sensor *dps_pressure = dps.getPressureSensor();
|
||||
|
||||
// Please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
char ssid[] = SECRET_SSID; // Your network SSID (name)
|
||||
char pass[] = SECRET_PASS; // Your network password (use for WPA, or use as key for WEP)
|
||||
const char* mqtt_server = "192.168.10.7"; // IP of your MQTT Broker
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
unsigned long lastMsg = 0;
|
||||
#define MSG_BUFFER_SIZE (50)
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
int value = 0;
|
||||
|
||||
void setup_wifi() {
|
||||
delay(100);
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, pass);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// Handle incoming MQTT messages if needed
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
String clientId = "ESP8266Client-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
if (client.connect(clientId.c_str())) {
|
||||
Serial.println("connected");
|
||||
client.publish("outTopic", "Sensor Online!");
|
||||
client.subscribe("inTopic");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10);
|
||||
|
||||
Serial.println("DPS310");
|
||||
if (! dps.begin_I2C()) {
|
||||
Serial.println("Failed to find DPS");
|
||||
while (1) yield();
|
||||
}
|
||||
Serial.println("DPS OK!");
|
||||
|
||||
// Setup highest precision
|
||||
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
|
||||
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
|
||||
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
StaticJsonDocument<32> doc;
|
||||
char output[55];
|
||||
|
||||
sensors_event_t temp_event, pressure_event;
|
||||
|
||||
if (dps.temperatureAvailable() && dps.pressureAvailable()) {
|
||||
dps_temp->getEvent(&temp_event);
|
||||
dps_pressure->getEvent(&pressure_event);
|
||||
|
||||
Serial.print(F("Temperature = "));
|
||||
Serial.print(temp_event.temperature);
|
||||
Serial.println(" *C");
|
||||
Serial.print(F("Pressure = "));
|
||||
Serial.print(pressure_event.pressure);
|
||||
Serial.println(" hPa");
|
||||
Serial.println();
|
||||
|
||||
doc["temperature"] = temp_event.temperature;
|
||||
doc["pressure"] = pressure_event.pressure;
|
||||
|
||||
serializeJson(doc, output);
|
||||
client.publish("/home/sensors", output);
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user