Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MS5837.h"
  3. #include <PubSubClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <WiFiClient.h>
  6. #include <WiFiUdp.h>
  7. #include "cactus_io_BME280_I2C.h"
  8.  
  9. WiFiClient wifiClient; // WiFi für MQTT
  10. PubSubClient mqttclient(wifiClient); // MQTT client
  11.  
  12. const char* ssid1 = "xxxxxxxx";
  13. const char* password1 = "xxxxxxxx";
  14. const char* ssid2 = "xxxxxxxx";
  15. const char* password2 = "xxxxxxxx";
  16. int messintervall = 50000;
  17. static float pressure;
  18.  
  19. BME280_I2C bme(0x76);
  20. static boolean bme_active = false;
  21. static float temp = bme.getTemperature_C();
  22. static float humidity = bme.getHumidity();
  23.  
  24. MS5837 tiefensensor;
  25. static boolean tiefensensor_active = false;
  26. static float w_temp;
  27. static float w_depth;
  28. static float w_realdepth;
  29. static float w_altitude;
  30. static float w_pressure;
  31.  
  32. // ----------- MQTT SERVER -------------------------
  33.  
  34. #define BASE "WasserTeststationFdb" // <-----!!Entsprechend für jeden Sensor anpassen!!
  35. #define mqtt_client_id BASE
  36.  
  37. const char* mqtt_server = "xxxxxxxx"; // Friedberg
  38. const char* userName = "xxxxxxxx";
  39. const char* passWord = "xxxxxxxx";
  40. const char* thisDevice = BASE;
  41.  
  42. void setup()
  43. {
  44. Serial.begin(115200);
  45. Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  46. Serial.println("----------------------------------- Version 0.9 ----------------------------------");
  47.  
  48. // ------------ MQTT - Server --------------
  49. mqttclient.setServer(mqtt_server, 1887);
  50. //mqttclient.setCallback(callback);
  51. mqttclient.setClient(wifiClient);
  52. setup_wifi();
  53.  
  54. Wire.begin(4,5); //(SDA = D2, SCL D1)
  55. delay(100);
  56. if (!tiefensensor.init()) {
  57. Serial.println("Es konnte kein Tiefensensor gefunden werden!");
  58. Serial.println("Bitte überprüfen Sie die Verkabelung!");
  59. mqttclient.publish(BASE "/Tiefensensoraktiv", "false");
  60. } else {
  61. tiefensensor_active = true;
  62. mqttclient.publish(BASE "/Tiefensensoraktiv", "true");
  63. tiefensensor.setModel(MS5837::MS5837_30BA);
  64. tiefensensor.setFluidDensity(997); // kg/m^3 (997 freshwater, 1029 for seawater)
  65. }
  66.  
  67. delay(250);
  68. Serial.println(F("Sensor Bus 0"));
  69.  
  70. Wire.begin(2,0); //(SDA = D4, SCL D3)
  71. delay(100);
  72. //Setup für den BME280 Sensor
  73. if (!bme.begin()) {
  74. Serial.println("Es konnte kein BME280 Sensor gefunden werden!");
  75. Serial.println("Bitte überprüfen Sie die Verkabelung!");
  76. mqttclient.publish(BASE "/BMEaktiv", "false");
  77. } else {
  78. bme_active = true;
  79. mqttclient.publish(BASE "/BMEaktiv", "true");
  80. bme.setTempCal(-1);// Temp was reading high so subtract 1 degree
  81. }
  82.  
  83. delay(250);
  84. Serial.println(F("Sensor Bus 1"));
  85.  
  86.  
  87. Serial.println("----------------------------------- L O O P ----------------------------------");
  88.  
  89. }
  90.  
  91. // ---------------------------------------------------------------------------------------
  92. // -----------------------------------LOOP -----------------------------------------------
  93. // ---------------------------------------------------------------------------------------
  94.  
  95. void loop() {
  96. if (!mqttclient.connected()) {
  97. reconnect();
  98. }
  99.  
  100. // ------------- BME Sensor auslesen ------------------
  101. Wire.begin(2,0); //(SDA = D4, SCL D3)
  102. delay(100);
  103. if (!bme_active){
  104. if (!bme.begin()) {
  105. Serial.println("Es konnte kein BME280 Sensor gefunden werden!");
  106. Serial.println("Bitte überprüfen Sie die Verkabelung!");
  107. mqttclient.publish(BASE "/BMEaktiv", "false");
  108. } else {
  109. bme_active = true;
  110. mqttclient.publish(BASE "/BMEaktiv", "true");
  111. bme.setTempCal(-1);// Temp was reading high so subtract 1 degree
  112. }
  113. } else {
  114. bme.readSensor();
  115. temp = bme.getTemperature_C();
  116. humidity = bme.getHumidity();
  117. pressure = bme.getPressure_MB();
  118. printBME2Serial(temp, humidity, pressure);
  119. //publishMQTTbme(humidity, temp, pressure);
  120. publishMQTTfloat(BASE "/Luftfeuchte", humidity);
  121. publishMQTTfloat(BASE "/Temperatur", temp);
  122. publishMQTTfloat(BASE "/Luftdruck", pressure);
  123. delay(250);
  124. }
  125.  
  126.  
  127. // ------------- Tiefensensor auslesen ----------------
  128. Wire.begin(4,5); //(SDA = D2, SCL D1)
  129. delay(100);
  130. if (!tiefensensor_active){
  131. if (!tiefensensor.init()) {
  132. Serial.println("Es konnte kein Tiefensensor gefunden werden!");
  133. Serial.println("Bitte überprüfen Sie die Verkabelung!");
  134. mqttclient.publish(BASE "/Tiefensensoraktiv", "false");
  135. } else {
  136. tiefensensor_active = true;
  137. mqttclient.publish(BASE "/Tiefensensoraktiv", "true");
  138. tiefensensor.setModel(MS5837::MS5837_30BA);
  139. tiefensensor.setFluidDensity(997); // kg/m^3 (997 freshwater, 1029 for seawater)
  140. }
  141. } else {
  142. tiefensensor.read();
  143. delay(500);
  144. w_temp = tiefensensor.temperature();
  145. w_depth = 100*tiefensensor.depth();
  146. w_realdepth = tiefensensor.pressure()-pressure;
  147. w_altitude = tiefensensor.altitude();
  148. w_pressure = tiefensensor.pressure();
  149. printMS5837Serial(w_temp, w_depth, w_realdepth);
  150. //publishMQTTdepth(w_temp, w_depth, w_realdepth, w_altitude, w_pressure);
  151. publishMQTTfloat(BASE "/Wassertemperatur", w_temp);
  152. publishMQTTfloat(BASE "/Wassertiefe", w_depth);
  153. publishMQTTfloat(BASE "/bereinigteTiefe", w_realdepth);
  154. publishMQTTfloat(BASE "/Wasserhoehe", w_altitude);
  155. publishMQTTfloat(BASE "/Wasserdruck", w_pressure);
  156.  
  157. publishMQTTfloat(BASE "/Memory", ESP.getFreeHeap());
  158. delay(250);
  159. }
  160.  
  161.  
  162. mqttclient.loop();
  163. }
  164.  
  165. // ---------------------------------------------------------------------------------------
  166. // ----------------------- Setup Routines ------------------------------------------------
  167. // ---------------------------------------------------------------------------------------
  168. void setup_wifi(){ // ------- Connect to WiFi -----------
  169. Serial.println();
  170. Serial.println("Booting Sketch...");
  171. WiFi.mode(WIFI_STA);
  172. WiFi.begin(ssid1, password1);
  173.  
  174. while(WiFi.waitForConnectResult() != WL_CONNECTED){
  175. mqttclient.disconnect();
  176. WiFi.begin(ssid1, password1);
  177. Serial.print("WiFi(1) "); Serial.print(ssid1); Serial.println(" failed, retrying.");
  178. delay(80);
  179. WiFi.begin(ssid2, password2);
  180. Serial.print("WiFi(2) "); Serial.print(ssid2); Serial.println(" failed, retrying.");
  181. delay(80);
  182. }
  183. Serial.println("");
  184. Serial.println("WiFi connected");
  185. Serial.println("IP address: ");
  186. Serial.println(WiFi.localIP());
  187. delay(200);
  188. if (!mqttclient.connected()) {
  189. reconnect();
  190. }
  191. }
  192. void reconnect(){
  193. // Loop until we're reconnected
  194. while (!mqttclient.connected())
  195. {
  196. Serial.print("Attempting MQTT connection...");
  197. delay(100);
  198. if (mqttclient.connect(thisDevice,userName,passWord))
  199. {
  200. Serial.println("connected");
  201. static String ipaddress = WiFi.localIP().toString(); // char String Array für IP Adresse
  202. char ipchar[ipaddress.length()+1]; // IP Adresse in String umwandeln
  203. ipaddress.toCharArray(ipchar,ipaddress.length()+1); // noch iwas damit machen
  204. mqttclient.publish(BASE "/IP", ipchar); // IP Adresse als MQTT senden
  205. } else {
  206. Serial.print("failed, rc=");
  207. Serial.print(mqttclient.state());
  208. Serial.println(" try again in 5 seconds");
  209. // Wait 6 seconds before retrying
  210. delay(500);
  211. }
  212. }
  213. }
  214.  
  215. // --------------------------------------------------------------------------
  216. // --------------- Print to MQTT --------------------------------------------
  217. // --------------------------------------------------------------------------
  218.  
  219. int publishMQTTfloat(const char* topic, float value){
  220. char value_as_string[10];
  221. sprintf(value_as_string, "%f", value);
  222. //dtostrf( value, 7, 2, value_as_string );
  223. return mqttclient.publish(topic, value_as_string);
  224. }
  225.  
  226.  
  227. // --------------------------------------------------------------------------
  228. // --------------- Print to Serial ------------------------------------------
  229. // --------------------------------------------------------------------------
  230. void printBME2Serial(float temp, float humidity, float pressure){
  231. Serial.print(" \t"); Serial.print("Temperature");
  232. Serial.print(" \t");Serial.print(" \t"); Serial.print("Luftfeuchtigkeit: ");
  233. Serial.print(" \t");Serial.print(" \t"); Serial.println("Luftdruck: ");
  234. Serial.print(" \t");
  235. Serial.print(temp);Serial.print(" C");Serial.print(" \t");Serial.print(" \t");Serial.print(" \t");
  236. Serial.print(humidity);Serial.print(" %");Serial.print(" \t");Serial.print(" \t");
  237. Serial.print(pressure);Serial.println(" mb");
  238. }
  239. void printMS5837Serial(float watertemp, float waterdepth, float waterrealdepth){
  240. Serial.print(" \t"); Serial.print("Temperature");
  241. Serial.print(" \t");Serial.print(" \t"); Serial.print("Tiefe: ");
  242. Serial.print(" \t");Serial.print(" \t"); Serial.println("Wasserdruck: ");
  243. Serial.print(" \t");
  244. Serial.print(watertemp); Serial.print(" deg C"); Serial.print(" \t");Serial.print(" \t");
  245. Serial.print(waterdepth); Serial.print(" cm"); Serial.print(" \t");Serial.print(" \t");
  246. Serial.print(waterrealdepth); Serial.println(" m");
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement