Last active
May 10, 2018 17:48
-
-
Save unixweb/41ba79a2a602e27ede4e1069d0ed9332 to your computer and use it in GitHub Desktop.
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 <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <SparkFunBME280.h> | |
#include <Wire.h> | |
#include <Adafruit_BME680.h> | |
String matrixausgabe_text = " "; // Ausgabetext als globale Variable | |
volatile int matrixausgabe_index = 0;// aktuelle Position in Matrix | |
//-------------- definition mqtt-object ueber WiFi | |
WiFiClient espClient; | |
PubSubClient mqttclient(espClient); | |
//--------- list of mqtt callback functions | |
#define MAX_MQTT_SUB 10 // maximal 10 subscriptions erlaubt | |
typedef void (*mqtthandle) (byte*,unsigned int); | |
typedef struct { // Typdeklaration Callback | |
String topic; // mqtt-topic | |
mqtthandle fun; // callback function | |
} | |
subscribe_type; | |
subscribe_type mqtt_sub[MAX_MQTT_SUB]; | |
int mqtt_sub_count=0; | |
String MQTT_Rx_Payload = "" ; | |
//--------- mqtt callback function | |
void mqttcallback(char* to, byte* pay, unsigned int len) { | |
String topic = String(to); | |
String payload = String((char*)pay); | |
MQTT_Rx_Payload=payload.substring(0,len); | |
Serial.println("\ncallback topic:" + topic + ", payload:" + MQTT_Rx_Payload); | |
for (int i=0;i<mqtt_sub_count;i++) { // durchsuche alle subscriptions, bis topic passt | |
if (topic==mqtt_sub[i].topic) | |
mqtt_sub[i].fun(pay,len); // Aufruf der richtigen callback-Funktion | |
} | |
} | |
//------------ reconnect mqtt-client | |
void mqttreconnect() { // Loop until we're reconnected | |
if (!mqttclient.connected()) { | |
while (!mqttclient.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
if (mqttclient.connect("MyProducer" , "IoTWerkstatt", "82135e776bee..." )) { | |
Serial.println("connected"); | |
for (int i=0;i<mqtt_sub_count;i++) { // subscribe topic | |
mqttclient.subscribe(mqtt_sub[i].topic.c_str()); | |
Serial.println("\nsubscribe"); | |
Serial.print(mqtt_sub[i].topic); | |
} | |
} | |
else { | |
Serial.print("failed, rc="); | |
Serial.print(mqttclient.state()); | |
Serial.println(" try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
else { | |
mqttclient.loop(); | |
} | |
} | |
// Marshall Tylor@sparkfun https://github.com/sparkfun/SparkFun_BME280_Arduino_Library | |
BME280 boschBME280; // Objekt Bosch Umweltsensor | |
// BME680 Lib written by Limor Fried & Kevin Townsend for Adafruit Industries, http://www.adafruit.com/products/3660 | |
Adafruit_BME680 boschBME680; // Objekt Bosch Umweltsensor | |
void setup(){ // Einmalige Initialisierung | |
Serial.begin(115200); | |
//----------------------------------MQTT-Client | |
mqttclient.setServer("mqtt.unixweb.de", 1883); | |
mqttclient.setCallback(mqttcallback); | |
Wire.begin(); // ---- Initialisiere den I2C-Bus | |
if (Wire.status() != I2C_OK) Serial.println("Something wrong with I2C"); | |
boschBME280.settings.runMode = 3; // Normal Mode | |
boschBME280.settings.tempOverSample = 4; | |
boschBME280.settings.pressOverSample = 4; | |
boschBME280.settings.humidOverSample = 4; | |
boschBME280.begin(); | |
if (!boschBME680.begin(118)) { | |
Serial.println("Failed to communicate BME680"); | |
while (1) { | |
delay(1); | |
}; | |
} | |
// Set up Bosch BME 680 | |
boschBME680.setTemperatureOversampling(BME680_OS_8X); | |
boschBME680.setHumidityOversampling(BME680_OS_2X); | |
boschBME680.setPressureOversampling(BME680_OS_4X); | |
boschBME680.setIIRFilterSize(BME680_FILTER_SIZE_3); | |
boschBME680.setGasHeater(320, 150); // 320*C for 150 ms | |
//------------ WLAN initialisieren | |
WiFi.persistent(false); | |
WiFi.mode(WIFI_STA); | |
delay(100); | |
Serial.print ("\nWLAN connect to:"); | |
Serial.print("WLAN Connected"); | |
WiFi.begin("XXX","Password"); | |
while (WiFi.status() != WL_CONNECTED) { // Warte bis Verbindung steht | |
delay(500); | |
Serial.print("."); | |
}; | |
Serial.println ("\nconnected, meine IP:"+ WiFi.localIP().toString()); | |
matrixausgabe_text = " Meine IP:" + WiFi.localIP().toString(); | |
matrixausgabe_index=0; | |
} | |
void loop() { // Kontinuierliche Wiederholung | |
delay( 5000 ); | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME280.readTempC())); | |
mqttclient.publish("bme280/feeds/Temperature",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME280.readFloatPressure()/100.)); | |
mqttclient.publish("bme280/feeds/Pressure",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME280.readFloatHumidity())); | |
mqttclient.publish("bme280/feeds/Humidity",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME680.readTemperature())); | |
mqttclient.publish("bme680/feeds/Temperature",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME680.readHumidity())); | |
mqttclient.publish("bme680/feeds/Humidity",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME680.readPressure()/100.)); | |
mqttclient.publish("bme680/feeds/Pressure",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
mqttreconnect(); | |
{ | |
String pay=String(String(boschBME680.readGas()/1000.)); | |
mqttclient.publish("bme680/feed/Gas",pay.c_str()); | |
Serial.print("\nmqtt publish: "); | |
Serial.print(pay); | |
}; | |
delay( 5000 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment