Advertisement
Guest User

Untitled

a guest
Nov 10th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <Homie.h>
  3. /*
  4.  *  README:
  5.  *  Diese Software wurde für einen Arduino-kompatiblen ESP8266
  6.  *  geschrieben, auf anderen Board wird sie vorraussichtlich nicht laufen.
  7.  *  
  8.  *  Vor dem ersten starten bitte die konfigurationssektino ausfüllen, dann anschalten.
  9.  *  Nach dem start öffnet der ESP einen Hotspot, an dem man sich mit dem laptop oder smartphone anmelden muss.
  10.  *  mit der HOMIE SETUP APP oder der Homie Setup Website (die wohl defekt ist),
  11.  *  lässt sich der ESP nun konfigurieren.
  12.  *  
  13.  *  auf dem MQTT Server kann der Client nun angemeldet und in die datenverarbeitung integriert werden.
  14.  *  es ist wichtig in der konfiguration die selben werte zu verwenden die auf dem server bereits registriert sind,
  15.  *  da der server sie sonst nicht loggen kann.
  16.  *  
  17.  *  Lukas Berghegger
  18.  */
  19.  
  20. //Configuration section
  21. #define REFRESH_INTERVAL            5                       // x*1000milis
  22. #define CALIBRATION_VALUE           46                      // calibrate against sensor drift
  23.  
  24. #define VENDOR                      "E-Motion_WerkstattIoT" // generic vendor name
  25. #define FIRMWARE_NAME               "PressureDataProvider"  // generic firmware name
  26. #define FIRMWARE_VERSION            "1.1.0"                 // current firmware version
  27. #define ITEM_NAME_INTERNAL          "vacuumpump"            // mqtt/Homie internal item Name, must be lowercase
  28. #define ITEM_NAME_EXTERNAL          "VacuumPump"            // Itemname to be displayed, can be uppercase
  29. #define PROPERTY_NAME_INTERNAL      "pressure"              // mqtt/Homie internal property Name, must be lowercase
  30. #define PROPERTY_NAME_EXTERNAL      "Pressure"              // property name to be displayed, can be uppercase
  31. #define PROPERTY_DATATYPE_INTERNAL  "float"                 // type of the data to be send
  32. #define PROPERTY_DATATYPE_EXTERNAL  "mBar"                  // will be appended to the data
  33.  
  34. #define RS        D5
  35. #define EN        D7
  36. #define DS4       D3
  37. #define DS5       D2
  38. #define DS6       D1
  39. #define DS7       D0
  40. #define READTIME  2500
  41.  
  42. unsigned long lastPressureSent = 0;
  43.  
  44. HomieNode pressureNode(ITEM_NAME_INTERNAL, ITEM_NAME_EXTERNAL, ITEM_NAME_INTERNAL); //create mqtt datastruct
  45. LiquidCrystal lcd(RS, EN, DS4, DS5, DS6, DS7);
  46.  
  47. struct openHabItem {  //custom struct to make using the homie driver easier
  48.   char NAME_INTERNAL[50];
  49.   char NAME_EXTERNAL[50];
  50.   openHabItem(char *NAME_INTERNAL, char *NAME_EXTERNAL) {
  51.     strncpy(this->NAME_INTERNAL, NAME_INTERNAL, 50);
  52.     strncpy(this->NAME_EXTERNAL, NAME_EXTERNAL, 50);
  53.   }
  54. };
  55.  
  56. struct openHabProperty {  //custom struct to make using the homie driver easier
  57.   char NAME_INTERNAL[50];
  58.   char NAME_EXTERNAL[50];
  59.   char DATATYPE_INTERNAL[50];
  60.   char DATATYPE_EXTERNAL[50];
  61.   openHabProperty(char *NAME_INTERNAL, char *NAME_EXTERNAL, char *DATATYPE_INTERNAL, char *DATATYPE_EXTERNAL) {
  62.     strncpy(this->NAME_INTERNAL, NAME_INTERNAL, 50);
  63.     strncpy(this->NAME_EXTERNAL, NAME_EXTERNAL, 50);
  64.     strncpy(this->DATATYPE_INTERNAL, DATATYPE_INTERNAL, 50);
  65.     strncpy(this->DATATYPE_EXTERNAL, DATATYPE_EXTERNAL, 50);
  66.   }
  67.   void Print(char *message) {
  68.     Homie.getLogger() << message;
  69.   }
  70.   void Printnl(char *message) {
  71.     Homie.getLogger() << message << endl;
  72.   }
  73. };
  74.  
  75. void startupScreen(){ //Its Pretty
  76.   lcd.print("Hello World!");
  77.   delay(READTIME);
  78.   lcd.clear();
  79.  
  80.   lcd.print(VENDOR);
  81.   for(int i = 0; i <= sizeof(VENDOR) - 18; i++){
  82.   delay(READTIME/4);
  83.   lcd.scrollDisplayLeft();
  84.   }
  85.   delay(READTIME);
  86.   lcd.clear();
  87.  
  88.   lcd.print(FIRMWARE_NAME);
  89.   lcd.setCursor(0, 1);
  90.   lcd.print(FIRMWARE_VERSION);
  91.   for(int i = 0; i <= sizeof(FIRMWARE_NAME) - 18; i++){
  92.   delay(READTIME/4);
  93.   lcd.scrollDisplayLeft();
  94.   lcd.setCursor(i + 1, 1);
  95.   lcd.print(FIRMWARE_VERSION);
  96.   }
  97.   delay(READTIME);
  98.   lcd.clear();
  99.  
  100.   lcd.print("Startup");
  101.   for(int i = 0; i < 3; i++){
  102.     delay(READTIME/4);
  103.     lcd.print(".");
  104.   }
  105.   delay(READTIME);
  106.   lcd.clear();
  107. }
  108.  
  109. void loopHandler() {  // this method is called by the homie library every cycle
  110.   if (millis() - lastPressureSent >= REFRESH_INTERVAL * 1000UL || lastPressureSent == 0) { //should we send an update?
  111.     //collect data
  112.     int inputBuffer = analogRead(A0);
  113.     float pressureBuffer = (inputBuffer - CALIBRATION_VALUE);// * (100 / 1023); todo -> scale ADC value to bar
  114.  
  115.     // show data on screen-------------------------
  116.     lcd.clear();
  117.     lcd.print("Current Pressure:");
  118.     lcd.setCursor(0, 1);
  119.     //lcd.print((int) pressureBuffer);
  120.  
  121.     // send data to server
  122.     Homie.getLogger() << ITEM_NAME_EXTERNAL << ": " << pressureBuffer << " " << PROPERTY_DATATYPE_EXTERNAL << endl;
  123.     pressureNode.setProperty(PROPERTY_NAME_INTERNAL).send(String(pressureBuffer));
  124.     lastPressureSent = millis();
  125.   }
  126. }
  127.  
  128. void setup() {
  129.   Serial.begin(115200);
  130.   Serial << endl << endl;
  131.   Serial.println("Startup...");
  132.  
  133.   pinMode(A0, INPUT);
  134.  
  135.   // set up the LCD's number of columns and rows:
  136.   lcd.begin(16, 2);
  137.   // Print a message to the LCD.
  138.   startupScreen();
  139.  
  140.   Homie_setBrand(VENDOR);
  141.   Homie_setFirmware(FIRMWARE_NAME, FIRMWARE_VERSION);
  142.   Homie.setLoopFunction(loopHandler);
  143.  
  144.   pressureNode.advertise(PROPERTY_NAME_INTERNAL).setName(PROPERTY_NAME_EXTERNAL).setDatatype(PROPERTY_DATATYPE_INTERNAL).setUnit(PROPERTY_DATATYPE_EXTERNAL);
  145.  
  146.   Homie.setup();
  147. }
  148.  
  149. void loop() {
  150.   Homie.loop();
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement