Full text
Fernando García Llorente Implementation of a reading meter-bus data program and system, using a Raspberry Pi Bachelor Thesis Faculty of Engineering and Computer Science Department of Information and Electrical Engineering Fakultät Technik und Informatik Department Informations- und Elektrotechnik
Fernando García Llorente Implementation of a reading meter-bus data program and system, using a Raspberry Pi Bachelor Thesis based on the examination and study regulations for the Bachelor of Engineering degree programme Information Engineering at the Department of Information and Electrical Engineering of the Faculty of Engineering and Computer Science of the University of Applied Sciences Hamburg Supervising examiner: Prof. Dr.-Ing. Franz Schubert Second examiner: M. Eng. Peter Lorenzen Day of delivery March 20th 2017
Fernando García Llorente Title of the Bachelor Thesis Implementation of a reading meter-bus data program and system, using a Raspberry Pi Keywords Meter Bus, MBus, Raspberry Pi, Python, Wrapper, HTML, CSS, LibMBus, Library, PyCharm, C, Programming Language, RS232, Serial, Converter, GPIO, jQuery, JavaScript Abstract This thesis describes the first steps of the improvement of a heat transfer station in the CC4E building in Hamburg, in order to make this station a smarter station than it is. To achieve that improvement in this thesis, the station is made accessible from any part of the building using low cost components, while this is done using the same programming language that is already used by the engineers of the center, as well as a program that allows the maximum flexibility and manageability possible to achieve such an outcome. To achieve this flexibility and manageability using the same programming language, it’s created an own whole Wrapper that englobes up to 5 different programming languages. Also, the hardware components are expensive, so a deep research in the functionality of the system is made to start developing an own hardware system that will allow to reduce the cost considerably. This thesis is also made as the first step of a further development, so another’s future researchers of the C4DSI can continue improving the smart heat transfer station.
Fernando García Llorente Thema der Bachelorarbeit Implementierung eines Programmes zum Auslesen eines Meter-Bus mit einem Raspberry Pi. Stichworte Meter Bus, MBus, Raspberry Pi, Python, Wrapper, HTML, CSS, LibMBus, Library, PyCharm, C, Programming Language, RS232, Serial, Converter, GPIO, jQuery, JavaScript Kurzzusammenfassung Diese Arbeit beschreibt die ersten Schritte der Verbesserung einer Wärmeübertragungsstation im CC4E-Gebäude in Hamburg, um diese Station zu einer intelligenteren Station zu machen. Um diese Verbesserung in dieser Arbeit zu erreichen, wird die Station von jedem Teil des Gebäudes mit kostengünstigen Komponenten zugänglich gemacht, während dies mit der gleichen Programmiersprache erfolgt, die bereits von den Ingenieuren des Zentrums verwendet wird. Um ein solches Ergebnis zu erreichen wird außerdem wird ein Programm verwendet, das eine maximale Flexibilität und Verwaltbarkeit ermöglicht. Um diese Flexibilität und Verwaltbarkeit mit der gleichen Programmiersprache zu erreichen, entsteht ein eigener Wrapper, der 5 verschiedene Programmiersprachen umfasst. Da die Hardwarekomponenten sehr teuer sind, wird die minimale Funktionalität ermittelt, um ein eigenes Hardwaresystem zu entwickeln, das es erlaubt, die Kosten erheblich zu reduzieren Diese Thesis wird auch als erster Schritt Einer weiteren Entwicklung gemacht, so dass weitere zukünftige Forscher des C4DSI die intelligente Wärmeübertragungsstation weiter verbessern können.
Acknowledgment First, and most important, I would like to thank the big support given by the M. Eng. Peter Lorenzen, who has followed all the steps made during the development of this thesis, as well as for his availability and patience to try to solve the doubts that I was arising along the research. Also, I would like to thank the Prof. Dr.-Ing. Franz Schubert for giving me the chance to research in such an important research center as is the CC4E, with all the new and sophisticate equipment that it has. I don’t want to forget to express my gratitude to the Informatic Engineer David González, who gave me a lot of useful information about the different programming languages and helped me learning a lot of these ones. Finally, I would like to thank to the entire C4DSI team for the longterm willingness to ask questions and for the warm welcome they have given to me, despite not knowing German. Specially to Ina, Sebastian and Matthias.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 6 Content Introduction ................................................................................................................... 10 MOTIVATIONS ............................................................................................................ 10 APPROACH ................................................................................................................ 11 TECHNICAL BASICS ................................................................................................... 12 Meter Bus Protocol ................................................................................................ 12 Rs-232 ...................................................................................................................... 13 Raspberry Pi ........................................................................................................... 14 Goal Diagrams ...................................................................................................... 16 Preparatory work .......................................................................................................... 18 OPERATING SYSTEM .................................................................................................. 18 CONFIGURING INTERNET .......................................................................................... 19 INSTALLING LIBRARIES AND UPDATING ................................................................... 20 libmbus ................................................................................................................... 21 SERIAL PORT CONNECTION ..................................................................................... 27 RECEIVING FIRST MBUS DATA................................................................................... 30 CONNECTING RPI TO PYCHARM ............................................................................ 34 Software concepts ....................................................................................................... 37 LIBRARIES .................................................................................................................... 37 CTypes .................................................................................................................... 38 Wrapper ................................................................................................................. 38 SOFTWARE IMPLEMENTATION .................................................................................. 39 readingData.py .................................................................................................... 39 webServer.py ......................................................................................................... 45 webPage.html ....................................................................................................... 47 SOFTWARE VALIDATION ........................................................................................... 50 Hardware concepts ..................................................................................................... 52 HARDWARE IMPLEMENTATION AND VALIDATION................................................. 53 First Checking ........................................................................................................ 55 Second Checking ................................................................................................. 58 Third Checking ...................................................................................................... 59 SOFTWARE VALIDATION WITH THE HARDWARE ..................................................... 61 ADDITIONAL HARDWARE IMPLEMENTATION.......................................................... 64 Evaluation ...................................................................................................................... 66 OUTLOOK ................................................................................................................... 66
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 7 Conclusion .................................................................................................................... 67 References .................................................................................................................... 68 Appendix ....................................................................................................................... 71 Declaration ................................................................................................................... 72
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 8 Figures Figure 1 – Main diagram of Goal 1. [1] [2] [3] [4] [5] ............................................... 12 Figure 2 - A male DB-9 connector viewed from the front. [9] ................................ 14 Figure 3 – Raspberry Pi 1 Model B revision 1.2. [11].................................................. 15 Figure 4 – GPIO RPi Pins. [13] ....................................................................................... 15 Figure 5 – Detailed diagram of the previous heat meter station. ......................... 16 Figure 6 – Main detailed diagram of Goal 1. ........................................................... 16 Figure 7 – Main detailed diagram of Goal 2. ........................................................... 17 Figure 8 – Main detailed diagram of Goal 3. ........................................................... 17 Figure 9 – Main Components to run a RPi [14] ......................................................... 18 Figure 10 – Installing Raspbian .................................................................................... 18 Figure 11 - etc/network/interfaces ............................................................................ 19 Figure 12 – etc/resolv.conf .......................................................................................... 19 Figure 13 – etc/init.d/networking restart ................................................................... 19 Figure 14 – sudo apt-get update ............................................................................... 20 Figure 15 – sudo apt-get upgrade ............................................................................. 21 Figure 16 – wget ........................................................................................................... 22 Figure 17 – wget file ..................................................................................................... 22 Figure 18 – unzip ........................................................................................................... 23 Figure 19 – unzip file ..................................................................................................... 23 Figure 20 – sudo apt-get install libtool automake ................................................... 23 Figure 21 – autoheader && aclocal && libtoolize ................................................... 24 Figure 22 – configure ................................................................................................... 24 Figure 23 – configure (2) .............................................................................................. 24 Figure 24 – configure files ............................................................................................ 25 Figure 25 – sudo make ................................................................................................. 25 Figure 26 – sudo make install ...................................................................................... 26 Figure 27 – Location of the installed library .............................................................. 26 Figure 28 – sudo ln -s .................................................................................................... 26 Figure 29 – USB-RS232 converter diagram. ............................................................... 27 Figure 30 – Digitus USB 1.1 serial converter. [3] ......................................................... 27 Figure 31 - ls -l /dev/ttyUSB0. ...................................................................................... 27 Figure 32 – New connection in Tera Term. ................................................................ 28 Figure 33 – Configuring connection in Tera Term. ................................................... 28 Figure 34 – Configuring the serial port connection in Tera Term. .......................... 29 Figure 35 – Message received in RPi terminal from Tera Term. .............................. 29 Figure 36 - Message received in Tera Term from RPi Terminal. .............................. 29 Figure 37 – First assembled system. ............................................................................ 30 Figure 38 – mbus-serial-scan -b 2400 /dev/ttyUSB0. ................................................ 31 Figure 39 – mbus-serial-request-data -b 2400 /dev/ttyUSB0 0. .............................. 32 Figure 40 – PuTTY interface. ......................................................................................... 33 Figure 41 – RPi login through PuTTY. ........................................................................... 33 Figure 42 - Connecting RPi to PyCharm. ................................................................... 34
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 9 Figure 43 - Connecting RPi to PyCharm (2). ............................................................. 35 Figure 44 - Connecting RPi to PyCharm (3). ............................................................. 35 Figure 45 – mkdir pythonProjects. .............................................................................. 36 Figure 46 - Connecting RPi to PyCharm (4). ............................................................. 36 Figure 47 - Connecting RPi to PyCharm (5). ............................................................. 36 Figure 48 – Main detailed diagram of the code. .................................................... 39 Figure 49 – Main detailed diagram of the readingData.py file. ........................... 40 Figure 50 – Main detailed diagram of the webServer.py file. ................................ 45 Figure 51 – Main detailed diagram of the webServer.py file. ................................ 48 Figure 52 – Main detailed diagram of Goal 2 (2). ................................................... 50 Figure 53 – readingData.py PyCharm running ........................................................ 50 Figure 54 – Web Page Running. ................................................................................. 51 Figure 55 – Converter 1 diagram circuit. [32] ........................................................... 52 Figure 56 – Converter 2 diagram circuit.................................................................... 53 Figure 57 – MAX3232 diagram. [35] ........................................................................... 54 Figure 58 – Circuit 1 built. ............................................................................................. 54 Figure 59 – Circuit 1 ready to be checked. .............................................................. 55 Figure 60 – Oscilloscope screenshot 1. ...................................................................... 56 Figure 61 – Voltage levels RS232. [8] .......................................................................... 57 Figure 62 – Voltage levels TTL/CMOS. [37] ................................................................ 57 Figure 63 – Circuit 1 ready to be checked 2. ........................................................... 58 Figure 64 – Oscilloscope screenshot 2. ...................................................................... 59 Figure 65 – Oscilloscope screenshot 3. ...................................................................... 60 Figure 66 – Command line ttyAMA0 UART enabled. .............................................. 61 Figure 67 – Main detailed diagram of Goal 3 (2). ................................................... 62 Figure 68 – Implementation of the Goal 3. ............................................................... 62 Figure 69 – Final receiving data. ................................................................................ 63 Figure 70 – Final receiving data (2)............................................................................ 63 Figure 71 – Circuit 2 built. ............................................................................................. 64 Figure 72 – Converter circuit 2 built. .......................................................................... 65
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 16 Goal Diagrams Some diagrams are made in order to understand better the three main goals of this thesis, and in order to create a more detailed explanation. First of all, in the figure 5 is represented the diagram of how the heat transfer station was working before the start of this thesis. The meter and the MBus are the same models used in this project, whose datasheets can be found in the references 1 and 2. As explained before this meter sends the physical data measured using MBus protocol, using voltage levels between 24 and 36 volts. It also uses current levels between 1 and 1.5 milliamps. This data is received by the converter in order to send the data using RS232 that uses voltage levels between 3 and 15 volts. In this first diagram is the PLC the one who reads the data and send it to the computer through TCP. Mbus 10 TECHBASE Integral-V UltraLite ALLMESS 24-36 V 1-1.5 mA Tx Rx GND 3-15 V MBus METER MBUS CONVERTER COMPUTER DATA PLC RS232 TCP Figure 5 – Detailed diagram of the previous heat meter station. In the figure 6 is represented the diagram of the goal 1. In this goal the PLC is replaced for the Raspberry Pi and a RS232 converter needed to connect the RPi to the converter. This connection is made through a serial RS232 cable, and using one of the 2 USB ports of the RPi. The RPi needs a monitor in order to see the shell command line of the Linux software installed on it, and this connection is made through and HDMI cable. As it can be seen at the top of the RPi of this diagram, this Linux software uses a Meter Bus library called libmbus, that makes the reading of the MBus data possible. RS232 - USB Mbus 10 TECHBASE Integral-V UltraLite ALLMESS 24-36 V 1-1.5 mA Tx Rx GND 3-15 V 0-3.3 V MBus USB HDMI METER MBUS CONVERTER RASPBERRY PI MONITOR DATA RS232 CONVERTER Library libmbus Figure 6 – Main detailed diagram of Goal 1.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 17 In the figure 7 the diagram of the goal 2 is represented. The only main difference between this diagram and the diagram of the figure 6 is the implementation of a Python Wrapper, that will allow not only read the MBus data but also make possible to do more things with this data apart from reading it. RS232 - USB Mbus 10 TECHBASE Integral-V UltraLite ALLMESS 24-36 V 1-1.5 mA Tx Rx GND 3-15 V 0-3.3 V MBus USB HDMI METER MBUS CONVERTER RASPBERRY PI MONITOR DATA RS232 CONVERTER Python Wrapper Figure 7 – Main detailed diagram of Goal 2. And finally, the goal 3 diagram is represented in the figure 8. In this diagram the connection between the converter and the Raspberry Pi is completely changed. Instead of using the USB port of the RPi, the GPIO pins are used, and a little circuit between the converter and the GPIO pins is developed. This circuit allow to convert the high-level voltages that comes out from the converter to voltages levels that the GPIO pins of the Raspberry Pi allows. RS232 - GPIO Mbus 10 TECHBASE Integral-V UltraLite ALLMESS 24-36 V 1-1.5 mA Tx Rx GND 3-15 V 0-3.3 V MBus GPIO HDMI METER MBUS CONVERTER RASPBERRY PI MONITOR DATA RS232 CIRCUIT CONVERTER Python Wrapper Figure 8 – Main detailed diagram of Goal 3.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 18 Preparatory work To configure the Raspberry Pi, you need to do some changes in to the software files but first of all you need the necessary components to do that. These components are shown in the figure 9. Figure 9 – Main Components to run a RPi [14] OPERATING SYSTEM The recommended operating system to use with the Raspberry Pi is called Raspbian, which is a version of GNU/Linux, designed specifically to work well with the Raspberry Pi. In the figure 10 there are 4 screenshots made during the installation process of the Raspberry Pi used for this project. After waiting some minutes, the Raspberry Pi is ready to start. Figure 10 – Installing Raspbian
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 19 CONFIGURING INTERNET Using an Ethernet cable, it is possible to connect the Raspberry Pi to the network of the workplace: the CC4E. But to do that, some steps are needed first. Using the shell command line, or terminal, 3 files of the system must be modified. First of all, the file “interfaces”, located in: “/etc/network/”, using the command: “sudo nano /etc/network/interfaces”. See figure 11. Figure 11 - etc/network/interfaces With this file, the IP address, the netmask, the gateway, and the DNS are set in order connect to the network of the building. So, this RPi is the only device in the whole center with that IP address. Then the file “resolv.conf” located in the folder “/etc” must be modified using the same command: “sudo nano /etc/resolv.conf” so the file looks like the file in the figure 12. Figure 12 – etc/resolv.conf Where the nameserver 141.22.192.100 is the preferred DNS server, and the nameserver 141.22.192.101 is the alternate DNS server. Then, a restart of the network must be done to apply correctly all the changes, using the command: “sudo /etc/init.d/networking restart”. See figure 13. Figure 13 – etc/init.d/networking restart
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 20 INSTALLING LIBRARIES AND UPDATING The function of the Raspberry Pi is to read the information using the Meter Bus protocol. So, a library for this protocol is needed to read the information coming from the meter. This library has a lot of functions and files that are needed in order to extract the data correctly from the meter. After setting up the internet connection, updating the packages must be done using the command “sudo apt-get update”. This command downloads the package lists from the repositories and updates them to get information on the newest versions of packages and their dependencies. The screenshot of the process done during this process is shown in the figure 14. Figure 14 – sudo apt-get update Also, an upgrade of the packages is highly recommended, using the command “sudo apt-get upgrade”. Which this one lasts for almost one hour in the Raspberry Pi 1 Model B. See figure 15.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 21 Figure 15 – sudo apt-get upgrade Then, some steps are needed to install correctly the library. The library that is used in this project is the one called libmbus, developed by rSCADA. The description of this library and how it is installed in the RPi of this project, is explained below. libmbus This is an open source library. This kind of software is free distributed and developed. It is focused more on the practical benefit, the access to the source code. It’s possible to modify the source of the software without license restrictions. The main function of the libmbus library is to perform the communication with the Meter Bus slaves and to encode and decode Meter Bus data. The last version of the library developed by rSCADA dates from 2012 and allows the connection through Meter Bus gateways with TCP and Serial interfaces. In this project, only the Serial interface is used. One of the main reasons why this library is the chosen one is the fact that it also presents the data in an easy XML format. This characteristic allows to simplify a lot the delivery of the MBus data in the Raspberry Pi, and it’s easier to work with that kind of format. [15]
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 22 The commands used to install correctly the libmbus library in to the Raspberry Pi of this project are the next ones: “wget https://github.com/rscada/libmbus/archive/master.zip”, this command downloads the zip file containing the library in the actual folder. See figures 16 and 17. Figure 16 – wget Figure 17 – wget file The command “unzip master.zip” creates a zip file in the actual folder. See figures 18 and 19.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 23 Figure 18 – unzip Figure 19 – unzip file “sudo apt-get install libtool automake”, see figure 20. Figure 20 – sudo apt-get install libtool automake Then, inside the folder libmbus-master, using “cd libmbus-master/”, the command “autoheader && aclocal && libtoolize –ltdl –copy –force && auto make –-add- missing –copy && autoconf” is executed. This commands includes some
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 24 commands in only one. The screenshot of the process done during this project is shown in the figure 21. Figure 21 – autoheader && aclocal && libtoolize After this, the library has to be configured. To do that, and inside the folder libmbus-master, it is run the command “/configure”, and some files are created inside the folder. This command use to require like half an hour, the process done during this process is shown in the figures 22, 23 and 24. Figure 22 – configure Figure 23 – configure (2)
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 25 Figure 24 – configure files Finally, is time to install the library. The commands “sudo make” and “sudo make install” must be executed in this order, inside the folder libmbus-master. These commands also require a “large” amount of time. The execution process done during this project is shown in the figure 25, 26 and 27. Figure 25 – sudo make
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 32 Figure 39 – mbus-serial-request-data -b 2400 /dev/ttyUSB0 0. The PuTTY program that is used in the project allow access to the RPi from another computer through the network. Through SSH protocol it’s possible to access its shell. See figure 40.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 33 Figure 40 – PuTTY interface. And after introducing the user and the password of the Raspberry Pi, it’s possible to control everything of the device. For example, it can be asked for the information of the internet configuration using the command “ifconfig”. See figure 41. Figure 41 – RPi login through PuTTY.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 34 CONNECTING RPI TO PYCHARM PyCharm software offers a lot of possibilities. One of them is creating Python files for the RPi remotely. But to do that some steps are required. First of all, inside the PyCharm software, going to “Tools -> Deployment -> Configuration”, and adding the name of the new server is needed to control and deploy the project files through SFTP. Then on “File -> Settings” adding a remote project interpreter must be done: The correct interpreter is selected and the next parameters are configured in this project: Host: 141.22.122.233 Port: 22 User: pi Password: raspberry Python interpreter: “/usr/bin/python/python3.4” The screenshot made during the configuration of the PyCharm software in this project is shown in the figure 42. Figure 42 - Connecting RPi to PyCharm. Then, if it is wanted to test that everything works fine, a little Python code can be created in the PyCharm program. See figure 43.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 35 Figure 43 - Connecting RPi to PyCharm (2). Going to “Tools -> Deployment -> Configuration” and entering the next parameters is needed, in order to create a server where the code can be run. See figure 44. Host: 141.22.122.233 Port: 22 User: pi Password: raspberry Figure 44 - Connecting RPi to PyCharm (3).
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 36 But first, a new folder in the Raspberry Pi must be created to be able to do the next step. In this project, it’s found in the directory “/home/pi/” called “pythonProjects”. See figure 45. Figure 45 – mkdir pythonProjects. With that, the Mappings configuration can be done, as is shown in the figure 46. Figure 46 - Connecting RPi to PyCharm (4). Then, uploading the file to the RPi is the last step, in “Tools->Deployment->Upload to RPi_Server”. And finally, the Python file can be found in the RPi just in the folder was created before. See figure 47. Figure 47 - Connecting RPi to PyCharm (5). After all these steps, all the work place is configured, working and ready to start creating a program for the C4DSI.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 37 Software concepts In order to make the Meter Bus data obtained from the meter accessible from all the computers of the CC4E it is needed to create program that is able to not only read but extract, modify and send the data obtained by the master. As explained before, the master receives the data from the slave, where in this project the master is the Raspberry Pi and the slave is the meter. But this data is represented as a text in the shell command of the RPi and it shows the data in one exactly moment, not through the time. Creating an own Wrapper is one of the most difficult solutions, but it allows to learn and control a lot. With this solution, up to 5 different programming languages are used and it is required to understand the performance of the libmbus library that was created by rSCADA, tot all the library but most of his files and folders. LIBRARIES Despite having previously commented on the library used in this project, it’s necessary to understand a little better what a library is for the development of this project. A library is a collection of resources used by programs, usually to develop software. These resources can include configuration data, code, classes, values, or type specifications, among others. If it is wanted to write a top-level program, a library can be used to make system calls instead of implementing those system calls repeatedly. The program calls the library through a language mechanism. What distinguishes the call to a library instead of being to another function in the same program, is the way the code is organized in the system. The library code is distributed in such a way that it can be used by several programs, while the code that is part of a program is written to be used only within that program. Most compiled languages have a standard library, although programmers can also create their own custom libraries. [21]
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 38 CTypes It’s also important to explain what CTypes, because it is used several times in the programming code. CTypes is a module capable of calling in a language to routines or make use of services written in another type of language. It allows loading dynamic libraries and calling C functions. In the case of this project it’s used to interact with external C code, which is the code in which the libmbus library of rSCADA is written This library is loaded using the "ctypes.CDLL" function. After loading the library, functions inside the library can already be used as regular Python calls. [22] [23] Wrapper This concept has been mentioned some few times in this document. In programming, Wrapper is a program or script that makes possible the running of another program. “Wrapper libraries consist of a thin layer of code which translates a library's existing interface into a compatible interface. Library Wrappers translates the interface of the library into a compatible interface.” [24] [25]
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 39 SOFTWARE IMPLEMENTATION In this project 3 different main are built to implement the server in the Raspberry Pi. Three files that interacts between them using up to 5 different programming languages. In the next diagram, shown in the Figure 48, it’s possible to see how they are structured and named. Raspberry PiRaspberry Pi 3 files and 1 main library3 files and 1 main library Python, C, JavaScript, HTML, CssPython, C, JavaScript, HTML, Css webServer.py libmbus.clibmbus.c webPage.html readingData.py HTTP CTypes C Python Python JavaScript HTML Css Graph Figure 48 – Main detailed diagram of the code. All the complete code can be found on the attached data of the thesis. readingData.py Is important to start explaining this file first because it’s the file used to extract the data from the meter using the libmbus library. This file contains 363 lines, 5 imports, 15 class definitions and 5 functions. All the imports and class definitions are needed to use the functions of the libmbus library. This file is the Wrapper created for this project, that interacts with the most important and needed functions of the libmbus library, using Python code and interacting with the C
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 40 code used in the library using CTypes. In the figure 49 it’s possible to see an overview of the 5 functions. readingData.pyreadingData.py Python CodePython Code class mBusWrapper:class mBusWrapper: def connect(self): def read(self): def disconnect(self): def _init_(self) def getValue: Loads the library Defines the baud rate, address and serial path Handshakes with the Meter Sets baud rate and asks for the frame Calls ‘read’ function and return the string obtained Creates an empty Meter Bus frame Stores on the empty frame the received data Returns the data as a string in XML format Ends the connection with the meter Extracts one specific value from the XML Meter Bus data Figure 49 – Main detailed diagram of the readingData.py file. The functions used in the Wrapper, as well as in the other two files, are needed to make possible the goal 2 exposed at the start of this document. The explanation of the code is made trying to comment only the most important code lines. Def _init_(self): Inside the mBusWrapper class, this one is the first function that is executed when the class is loaded. It defines the variables that makes possible the communication with the meter of this project and loads the C library libmbus using CTypes. The serial path needs to be encoded to be used correctly by the C library.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 41 class mBusWrapper: def __init__(self): self.myLib = ctypes.cdll.LoadLibrary("/usr/local/lib/libmbus.so") self.baudRate = 2400 self.address = 0 self.serialPath = "/dev/ttyUSB0" self.utfSerialPath = self.serialPath.encode('utf-8') Def connect(self): Also inside the mBusWrapper class, the handshake between the Raspberry Pi and the meter is done by this function. This function is executed only when is called by the webServer.py file. First, the type of data that the C function “mbus_connect_serial” needs and returns, must be set. This is very important to interact correctly with the meter. With the next lines, it is said that the given data to the function is a pointer of type char and the result is a pointer of type ‘mbus_handle’. def connect(self): self.myLib.mbus_connect_serial.argtype = ctypes.POINTER(ctypes.c_char) self.myLib.mbus_connect_serial.restype = ctypes.POINTER(mbus_handle) It’s also important to know what a pointer is, because there are a lot of pointers used in the Wrapper. “In computer science, a pointer is a programming language object, whose value refers to, or “points to”, another value stored elsewhere in the computer memory using its memory address.” [26] The type of data ‘mbus_handle’ and all the other types of data used by the libmbus library, are defined at the start of the file as a class. “A class is an extensible programcode-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).” [27] The result pointer of type ‘mbus_handle’ that the handshake between the RPi and the meter has given by the C function “mbus_connect_serial” is stored in the variable ‘handleValue’. self.handleValue = self.myLib.mbus_connect_serial(self.utfSerialPath) From this data, it is needed to extract only the Serial value, because the library is made for 2 ways of connection, Serial and TCP. In the case of this project only the Serial value is needed. This is done using the pointer of type ‘mbus_handle’ from before. handleSerialValue = self.handleValue.contents.m_serial_handle
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 48 webPage.htmlwebPage.html </html></html> <html><html> Defines the structure of the web page using HTML Defines the style of the web page using CSS Defines the structure of the web page using HTML Defines the style of the web page using CSS Creation and definition of the 3 chartsCreation and definition of the 3 charts Function requestData(): Sends a HTTP request to the server to get XML Meter Bus data, extracts and print the data in the 3 graphs created before Figure 51 – Main detailed diagram of the webServer.py file. With the JavaScript code, 3 objects are defined that contains 3 charts that are created thanks to the HighChart import made before in the HTML code. These ones contain things like the title, axis, or series. Also, contains a call to a function that will control later that all the 3 graphs are loaded before adding values to the graphs. This function is only called when the constructor (the library) finishes building the object. Before the definition of the charts, a jQuery function is used in order to make sure that all the HTML code is ready in the navigator. This jQuery function is represented by the $ symbol and is loaded before in the HTML code, as well as the HighChart import. function requestData() { This is the main function of this file, is written in JavaScript and is the one that prints the graphs with the data taken from the server.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 49 This function prints extracts and print the data into the 3 graphs defined before in the code. function requestData() { $.ajax({ url: '/all', success: function(xml) { var volumeFlow = parseFloat(xml.querySelectorAll("DataRecord Value")[4].firstChild.textContent); var flowTemperature = parseFloat(xml.querySelectorAll("DataRecord Value")[5].firstChild.textContent)/10.0; var returnTemperature = parseFloat(xml.querySelectorAll("DataRecord Value")[6].firstChild.textContent)/10.0; var now = new Date().getTime(); volFlowChart.series[0].addPoint([now, volumeFlow], true, volFlowChart.series[0].data.length > 20); flowTempChart.series[0].addPoint([now, flowTemperature], true, flowTempChart.series[0].data.length > 20); retTempChart.series[0].addPoint([now, returnTemperature], true, retTempChart.series[0].data.length > 20); // call it again after one second setTimeout(requestData, 1000); } }); } Request data function uses the jQuery library and Ajax, the technique that allows to make possible that webpages actualize themselves without having to download all the page again, automatically. Inside this function, an “/all” HTTP request is made to the server. And when the data has been received correctly, the next function “function(xml)” is called. Them, the needed values are extracted from the XML data and sets into the Y axis of each corresponding graph. Also, a time is set for the X axis, the same for the 3 graphs, and the printing of the values inside the graphs is made. This function is also important because it calls itself every one second, actualizing the graphs. The last line makes all the process explained start again. It makes the whole process start since the start. All the whole process. Endless until it the user closes the navigator. [28] [29] [30]
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 50 SOFTWARE VALIDATION Some screenshots were taken during the several tests of the developing of this thesis. The next screenshots shown in this part of the project, shows the final result when all the code is working correctly. These screenshots are taken on windows, in a computer of the CC4E building and using the PyCharm software. The software is run while in the heat meter station the Raspberry Pi the whole system explained in the goal 2 is connected and working. See figure 52. RS232 - USB Mbus 10 TECHBASE Integral-V UltraLite ALLMESS 24-36 V 1-1.5 mA Tx Rx GND 3-15 V 0-3.3 V MBus USB HDMI METER MBUS CONVERTER RASPBERRY PI MONITOR DATA RS232 CONVERTER Python Wrapper Figure 52 – Main detailed diagram of Goal 2 (2). Also, some videos were taken, in order to see more detailed and in first person how the whole programming server works. This videos are attached in the CD of this project. In the next figure, it can be seen the screenshot validations of the working Wrapper, defined in the readingData.py file. The screenshot is taken when only the readingData.py has been written. See figure 53. Figure 53 – readingData.py PyCharm running
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 51 In the next screenshot, it can be seen how the final web page looks like after all the 3 files are written. This is how the web page looks like running. See figure 54. Figure 54 – Web Page Running.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 52 Hardware concepts The last goal of this project is to replace successfully the previously explained RS232-USB converter made by Digitus, in order to make the system as cheaper as possible. To replace this converter the GPIO pins from the Raspberry Pi are used, so it’s not need to use the USB port anymore. The exactly pins used are the UART pins 14 and 15 for transmitting and receiving data, respectively. “A universal asynchronous receiver/transmitter, UART, is a computer hardware device for asynchronous serial communication in which the data format and transmission speeds are configurable. The electric signaling levels and methods (such as differential signaling, etc.) are handled by a driver circuit external to the UART.” [31] In the case of this project the external circuit that the definition of UART is talking about is the circuit built for this project and it is explained below. In the next figure 55 it can be seen a diagram of this circuit. Figure 55 – Converter 1 diagram circuit. [32] Also, as an extra goal, in this project it’s built another circuit trying to replace the most expensive part of the system, the Meter Bus converter made by TechBase. The circuit design is extracted from the official documents, in this case the UNE-EN 1434-3 Spanish rule, edited and printed by AENOR, the Spanish association of normalization and
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 53 certification. In the next figure 56 it can be seen a diagram of the circuit, extracted from the official PDF named before. Figure 56 – Converter 2 diagram circuit. The implementation and validation of the hardware concepts are made at the same time. It must be made before connecting everything in order to not damage the devices or the components of the circuit. The software validation with the hardware implemented is also done, but after all the implementation and hardware validations. HARDWARE IMPLEMENTATION AND VALIDATION To do the goal 3, a level converter is built in a protoboard using some components, as it is shown in the previous figure 55. The info of how to build a level converter from serial to TTL/CMOS can be extracted from the references [33] and [34], or from the official datasheet [35]. This is the circuit that the UART definition is mentioning, as said before. The circuit is composed by 5 capacitors of 0.1μF each one and one MAX3232 CPE transceiver. In the figure 57 it’s possible to see the name of every pin of the transceiver
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 54 and its internal circuit, figure extracted from the official datasheet that can be also found in the reference [35]. Figure 57 – MAX3232 diagram. [35] With all this information, the circuit is built in a protoboard as it can be seen in the figure 58 and after double checking everything it is prepared to be tested by an oscilloscope to check its behavior. From now on this circuit will be called circuit 1. Figure 58 – Circuit 1 built.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 55 First Checking The power supply it’s providing +3.3 volts to the pins 16 and 11 of the MAX3232, Vcc and T1IN respectively. The transceiver needs to be fed with that voltage level. The ground is connected to the same ground of the circuit, pin 15. See figure 59. Figure 59 – Circuit 1 ready to be checked. The channel 1 of the oscilloscope, in the case of this project with a strong blue color, is connected to the same ground of the power supply and the circuit, and at the same time to the pin 11, T1IN. The channel 2, soft blue color, is connected to the pin 14 of the MAX3232, which is the T1OUT. With this connection, it is tried to see how the circuit is acting when the data is coming from the Raspberry Pi to the RS232 serial cable that goes to the Meter Bus converter. The result provide by the oscilloscope is shown in the figure 60.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 56 Figure 60 – Oscilloscope screenshot 1. In this previous figure 60 it can be seen how the input is 3.3v, the strong blue line seen in the figure. The same that our power supply is giving us, a constant value along the time. That’s exactly what is going inside T1IN, as explained before, and this represents the data sent from the Raspberry Pi. In the line marked with a soft blue color, it can be seen the result that represents the signal after going through the transceiver. The level voltage has been increased a little bit more than 5 volts. This voltage is negative because in the internal circuit of the transceiver it goes through an inverter, as it can be seen in the previous MAX3232 diagram. “In digital logic, an inverter or NOT gate is a logic gate which implements logical negation.” [36] When the input bit is a ‘0’, the inverter returns a ‘1’; and when the input bit is a ‘1’ it returns a ‘0’. This circuit is in the middle of a digital communication between the Raspberry Pi and the meter. The bit ‘0’ and the bit ‘1’ used sent by the RPi are interpreted different by the RS232 protocol. This transceiver MAX3232 is used because in RS232 it’s represented as a bit ‘0’ the signals with voltage levels between +3V and +15V; and as a bit ‘1’ for the voltage levels between -3V and -15V. See figure 61.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 57 Figure 61 – Voltage levels RS232. [8] It’s also necessary explain the TTL/CMOS voltage levels. Every input voltage signal between 2V and 5V (or 3.3V in the case of the RPi) is considered as a bit ‘1’; while every input voltage signal between 0V and 0.8V is considered as a bit ‘0’. See figure 62. Figure 62 – Voltage levels TTL/CMOS. [37]
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 64 ADDITIONAL HARDWARE IMPLEMENTATION It this project, it is also attempt to replace the Meter Bus converter made by TechBase, as explained before following the UNE-EN 1434-3 Spanish rule, an official document. The diagram circuit can be checked in the previous figure 56. The circuit is double checked, making sure that everything is correctly connected. After connecting it into the main circuit of our project, the Raspberry Pi is detecting something connected in the ‘/ttyAMA0’ port, but the data is not received correctly. This circuit is called circuit 2 in this project. In the next figures 71 and 72 it is shown how the circuit is built and the test in the stand of the CC4E building. Figure 71 – Circuit 2 built.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 65 Figure 72 – Converter circuit 2 built. The circuit is fed with +15v and -15v as it can be seen in the bottom of the previous figure. The outputs of the Meter Bus are connected to the inputs of the circuit, and the output of the circuit is connected to the output of our circuit 1 built before. With the RPi correctly working and the circuit 1 correctly working as well, the RPi detects something connected but is not able to detect any meter connected.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 66 Evaluation The main goals proposed at the beginning of this project have been achieved, not without some difficulty. It has been possible to create a program in Python code that allows reading the data, and that data can be obtained from any part of the building where the project is made: the CC4E. This data is also readable in a very clear and dynamic way, showing not only an instant data, but also all the data over time, uninterruptedly. In addition, the previous PLC has been successfully replaced by a Raspberry Pi, which offers many more possibilities for the future and it is also way cheaper, and the small circuit between the same and the meter has tested successfully. What has not been done despite trying, is replacing the expensive Meter Bus converter, with a much more complex circuit than the previous one, but that would have saved a lot of money at the station. The circuit seems to work properly and both the meter and the Raspberry Pi are not affected by it, but more investigation is needed in order to see if it is just a Software problem. OUTLOOK The project can continue in several ways. The RPi offers a world of possibilities with which to investigate more about how to make the station a much more dynamic and intelligent. In addition, the circuit that has not been successfully replaced can be tried again, starting from the bases and the initial investigation carried out in this project. With all the research and verification made in this document it is easy for a next person to continue from this work. Another possible would be to replace the small circuit implemented in the protoboard by a printed circuit, more robust, with which it can be fixed in a place of the building. Also, the meter in the CC4E is unconfigured, so the program can be modified and used with a configured meter, that provides real data. With this change the code can improve a lot. Another possible way to improve the code is adding some features to the web page, or asking to the client for the parameters of the meter that it is request, so is the user the ones who asks for it.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 67 Conclusion This thesis improves a previous heat transfer station allocated in the CC4E building, in the city of Hamburg, in order to make this station a smarter station. To achieve that, the station has been made accessible from any part of the building using low cost components, and it uses the same programming language that used by the engineers of the center. The components used in order to save as much money possible has been a Raspberry Pi and one own designed circuit. Also, the code inside the RPi is a program that allows the flexibility and manageability, and allows the possibility to improve the program a lot. This thesis is also made as the first step of a further development, so another’s future researchers of the C4DSI can continue improving the smart heat transfer station. It is a great beginning of a project that can become very beautiful and exciting. With the bases that are established on this thesis, great progress can be made.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 68 References [1] "DataSheet Meter," 16 03 2017. [Online]. Available: http://www.allmess.de/fileadmin/multimedia/alle_Dateien/DB_P0014_Int egral-V_UltraLite_TS1015.pdf. [2] "DataSheet MBus Converter," 16 03 2017. [Online]. Available: http://energycare.dk/wp-content/uploads/2014/05/ENERGYCARECONVERTER-TECHBASE-MBus10_v2_eng.pdf. [3] "Digitus Converter," 16 03 2017. [Online]. Available: http://www.digitusprofessional.com/it/products/computer-accessories-and- components/computer-accessories/serial-and-parallel-adapter/da- 70155-1/. [4] "Raspberry Pi WebPage," 16 03 2017. [Online]. Available: https://www.raspberrypi.org/blog/raspberry-pi-2-on-sale/. [5] "Monitor Photo," 16 03 2017. [Online]. Available: https://pixabay.com/en/photos/monitor/. [6] "Meter Bus Info," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Meter-Bus. [7] "MBus User Guide," 16 03 2017. [Online]. Available: https://www.openmuc.org/m-bus/user-guide/. [8] "RS232 Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/RS-232. [9] "RS232 Info," 16 03 2017. [Online]. Available: https://www.commfront.com/pages/3-easy-steps-to-understand-and- control-your-rs232-devices. [10] "Raspberry Pi modelB Web Page," 16 03 2017. [Online]. Available: https://www.raspberrypi.org/products/model-b/. [11] "Raspberry Pi Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Raspberry_Pi. [12] "GPIO Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/General-purpose_input/output. [13] "GPIO UART Pins," 16 03 2017. [Online]. Available: http://www.sustainablenetworks.org/CIS508/?page_id=1585. [14] "Raspberry Pi Guide," 16 03 2017. [Online]. Available: https://www.raspberrypi.org/learning/software-guide/.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 69 [15] "libmbus rSCADA Web Page," 16 03 2017. [Online]. Available: http://www.rscada.se/libmbus/. [16] "Library Install RPi," 16 03 2017. [Online]. Available: http://www.raspberrypispy.co.uk/2012/05/install-rpi-gpio-python-library/. [17] "Install libmbus Library Web Page," 16 03 2017. [Online]. Available: http://bends.se/?page=anteckningar/automation/m-bus/libmbus. [18] "MBus Domotiga Project," 16 03 2017. [Online]. Available: https://www.domotiga.nl/projects/domotiga/wiki/M-Bus. [19] "RPI Serial info," 16 03 2017. [Online]. Available: http://elinux.org/RPi_Serial_Connection. [20] "RPI Serial Connection," 16 03 2017. [Online]. Available: http://elinux.org/RPi_Serial_Connection#Console_serial_parameters. [21] "Library Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Library_(computing). [22] "Ctype WikiBooks," 16 03 2017. [Online]. Available: https://en.wikibooks.org/wiki/Python_Programming/Extending_with_ctype s. [23] "Ctypes Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Dynamic-link_library. [24] "Wrapper Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Wrapper_library. [25] "Wrapper Definition," 16 03 2017. [Online]. Available: http://searchmicroservices.techtarget.com/definition/wrapper. [26] "Pointer Definition," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Pointer_(computer_programming). [27] "Class Definition," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Class_(computer_programming). [28] "Ajax Definition," 16 03 2017. [Online]. Available: https://es.wikipedia.org/wiki/AJAX. [29] "HighCharts Web Page," 16 03 2017. [Online]. Available: http://www.highcharts.com/. [30] "JQuery Web Page," 16 03 2017. [Online]. Available: https://jquery.com/. [31] "UART Wikipedia," 16 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter .
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 70 [32] "MAX3232 Diagram," 16 03 2017. [Online]. Available: http://s288.photobucket.com/user/ninexunix/media/Max3232.png.html. [33] "GPIO Circuit Info," 16 03 2017. [Online]. Available: http://elinux.org/RPi_Serial_Connection#Console_serial_parameters. [34] "Circuit 1 Info," 16 03 2017. [Online]. Available: http://codeandlife.com/2012/07/01/raspberry-pi-serial-console-with- max3232cpe/. [35] "MAX3232 DataSheet," 16 03 2017. [Online]. Available: http://www.alldatasheet.com/datasheetpdf/pdf/73152/MAXIM/MAX3232CPE.html. [36] "Inverter Gate Info," 19 03 2017. [Online]. Available: https://en.wikipedia.org/wiki/Inverter_(logic_gate). [37] "TTL Logic Levels," 16 03 2017. [Online]. Available: https://learn.sparkfun.com/tutorials/logic-levels/ttl-logic-levels. [38] "Slew Rate Definition," 16 03 2017. [Online]. Available: https://es.wikipedia.org/wiki/Slew_rate.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 71 Appendix This Bachelor Thesis contains an appendix of program listings, hardware descriptions etc. on a CD (disk or supplementary booklet). This Appendix is deposited with Prof. Dr. Eng. Franz Schubert.
Fernando García Llorente | DAS CENTER FOR DEMAND SIDE INTEGRATION (C4DSI) 72 Declaration I declare within the meaning declare within the meaning of part 16(5) of the General Examination and Study Regulations for Bachelor and Master Study Degree Programmes at the Faculty of Engineering and Computer Science and the Examination and Study Regulations of the International Degree Course Information Engineering that: this Bachelor Thesis has been completed by myself/ourselves independently without outside help and only the defined sources and study aids were used. Sections that reflect the thoughts or works of others are made known through the definition of sources Hamburg, 20. March 2017 Signature _______________________