Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <Homie.h>
- /*
- * README:
- * Diese Software wurde für einen Arduino-kompatiblen ESP8266
- * geschrieben, auf anderen Board wird sie vorraussichtlich nicht laufen.
- *
- * Vor dem ersten starten bitte die konfigurationssektino ausfüllen, dann anschalten.
- * Nach dem start öffnet der ESP einen Hotspot, an dem man sich mit dem laptop oder smartphone anmelden muss.
- * mit der HOMIE SETUP APP oder der Homie Setup Website (die wohl defekt ist),
- * lässt sich der ESP nun konfigurieren.
- *
- * auf dem MQTT Server kann der Client nun angemeldet und in die datenverarbeitung integriert werden.
- * es ist wichtig in der konfiguration die selben werte zu verwenden die auf dem server bereits registriert sind,
- * da der server sie sonst nicht loggen kann.
- *
- * Lukas Berghegger
- */
- //Configuration section
- #define REFRESH_INTERVAL 5 // x*1000milis
- #define CALIBRATION_VALUE 46 // calibrate against sensor drift
- #define VENDOR "E-Motion_WerkstattIoT" // generic vendor name
- #define FIRMWARE_NAME "PressureDataProvider" // generic firmware name
- #define FIRMWARE_VERSION "1.1.0" // current firmware version
- #define ITEM_NAME_INTERNAL "vacuumpump" // mqtt/Homie internal item Name, must be lowercase
- #define ITEM_NAME_EXTERNAL "VacuumPump" // Itemname to be displayed, can be uppercase
- #define PROPERTY_NAME_INTERNAL "pressure" // mqtt/Homie internal property Name, must be lowercase
- #define PROPERTY_NAME_EXTERNAL "Pressure" // property name to be displayed, can be uppercase
- #define PROPERTY_DATATYPE_INTERNAL "float" // type of the data to be send
- #define PROPERTY_DATATYPE_EXTERNAL "mBar" // will be appended to the data
- #define RS D5
- #define EN D7
- #define DS4 D3
- #define DS5 D2
- #define DS6 D1
- #define DS7 D0
- #define READTIME 2500
- unsigned long lastPressureSent = 0;
- HomieNode pressureNode(ITEM_NAME_INTERNAL, ITEM_NAME_EXTERNAL, ITEM_NAME_INTERNAL); //create mqtt datastruct
- LiquidCrystal lcd(RS, EN, DS4, DS5, DS6, DS7);
- struct openHabItem { //custom struct to make using the homie driver easier
- char NAME_INTERNAL[50];
- char NAME_EXTERNAL[50];
- openHabItem(char *NAME_INTERNAL, char *NAME_EXTERNAL) {
- strncpy(this->NAME_INTERNAL, NAME_INTERNAL, 50);
- strncpy(this->NAME_EXTERNAL, NAME_EXTERNAL, 50);
- }
- };
- struct openHabProperty { //custom struct to make using the homie driver easier
- char NAME_INTERNAL[50];
- char NAME_EXTERNAL[50];
- char DATATYPE_INTERNAL[50];
- char DATATYPE_EXTERNAL[50];
- openHabProperty(char *NAME_INTERNAL, char *NAME_EXTERNAL, char *DATATYPE_INTERNAL, char *DATATYPE_EXTERNAL) {
- strncpy(this->NAME_INTERNAL, NAME_INTERNAL, 50);
- strncpy(this->NAME_EXTERNAL, NAME_EXTERNAL, 50);
- strncpy(this->DATATYPE_INTERNAL, DATATYPE_INTERNAL, 50);
- strncpy(this->DATATYPE_EXTERNAL, DATATYPE_EXTERNAL, 50);
- }
- void Print(char *message) {
- Homie.getLogger() << message;
- }
- void Printnl(char *message) {
- Homie.getLogger() << message << endl;
- }
- };
- void startupScreen(){ //Its Pretty
- lcd.print("Hello World!");
- delay(READTIME);
- lcd.clear();
- lcd.print(VENDOR);
- for(int i = 0; i <= sizeof(VENDOR) - 18; i++){
- delay(READTIME/4);
- lcd.scrollDisplayLeft();
- }
- delay(READTIME);
- lcd.clear();
- lcd.print(FIRMWARE_NAME);
- lcd.setCursor(0, 1);
- lcd.print(FIRMWARE_VERSION);
- for(int i = 0; i <= sizeof(FIRMWARE_NAME) - 18; i++){
- delay(READTIME/4);
- lcd.scrollDisplayLeft();
- lcd.setCursor(i + 1, 1);
- lcd.print(FIRMWARE_VERSION);
- }
- delay(READTIME);
- lcd.clear();
- lcd.print("Startup");
- for(int i = 0; i < 3; i++){
- delay(READTIME/4);
- lcd.print(".");
- }
- delay(READTIME);
- lcd.clear();
- }
- void loopHandler() { // this method is called by the homie library every cycle
- if (millis() - lastPressureSent >= REFRESH_INTERVAL * 1000UL || lastPressureSent == 0) { //should we send an update?
- //collect data
- int inputBuffer = analogRead(A0);
- float pressureBuffer = (inputBuffer - CALIBRATION_VALUE);// * (100 / 1023); todo -> scale ADC value to bar
- // show data on screen-------------------------
- lcd.clear();
- lcd.print("Current Pressure:");
- lcd.setCursor(0, 1);
- //lcd.print((int) pressureBuffer);
- // send data to server
- Homie.getLogger() << ITEM_NAME_EXTERNAL << ": " << pressureBuffer << " " << PROPERTY_DATATYPE_EXTERNAL << endl;
- pressureNode.setProperty(PROPERTY_NAME_INTERNAL).send(String(pressureBuffer));
- lastPressureSent = millis();
- }
- }
- void setup() {
- Serial.begin(115200);
- Serial << endl << endl;
- Serial.println("Startup...");
- pinMode(A0, INPUT);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- startupScreen();
- Homie_setBrand(VENDOR);
- Homie_setFirmware(FIRMWARE_NAME, FIRMWARE_VERSION);
- Homie.setLoopFunction(loopHandler);
- pressureNode.advertise(PROPERTY_NAME_INTERNAL).setName(PROPERTY_NAME_EXTERNAL).setDatatype(PROPERTY_DATATYPE_INTERNAL).setUnit(PROPERTY_DATATYPE_EXTERNAL);
- Homie.setup();
- }
- void loop() {
- Homie.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement