{"id":581,"date":"2019-08-21T10:28:24","date_gmt":"2019-08-21T08:28:24","guid":{"rendered":"http:\/\/miniprojets.net\/?p=581"},"modified":"2022-12-17T17:25:50","modified_gmt":"2022-12-17T16:25:50","slug":"using-a-raspberry-pi-3-to-get-information-of-two-pzem004t-sensors-which-are-connected-to-the-arduino-mega-microcontroller","status":"publish","type":"post","link":"https:\/\/miniprojets.net\/index.php\/2019\/08\/21\/using-a-raspberry-pi-3-to-get-information-of-two-pzem004t-sensors-which-are-connected-to-the-arduino-mega-microcontroller\/","title":{"rendered":"Using a Raspberry Pi 3 to get information of two PZEM004T sensors which are connected to the Arduino Mega microcontroller"},"content":{"rendered":"\n<p>Hi everyone, <\/p>\n\n\n\n<p>Today and for the end of my internship, i propose this tutorial to connect raspberry pi and 2 Pzem004T together.<\/p>\n\n\n\n<p>All the sources are  <strong>CC-BY-NC-SA<\/strong> <a href=\"https:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/fr\/\">https:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/fr\/<\/a> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"141\" src=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/09\/licence.png\" alt=\"\" class=\"wp-image-796\" srcset=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/09\/licence.png 403w, https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/09\/licence-300x105.png 300w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure><\/div>\n\n\n\n<p><strong>Brief description:<\/strong><\/p>\n\n\n\n<p>The aim of this article is to explain how to get the voltage, current and power measured by two PZEM 004T sensors from a Raspberry Pi 3. The sensors are not directly connected to the Raspberry but they are connected to an Arduino microcontroller (Mega 2560) and this is connected to the Raspberry via USB cable. We first get all these values by using the arduino microcontroller and then we send them to the Raspberry.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"332\" src=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image.png\" alt=\"\" class=\"wp-image-582\" srcset=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image.png 602w, https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-300x165.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><strong>Procedure:<\/strong><\/p>\n\n\n\n<p>As stated before, the first step is to collect all the values from the Arduino microcontroller, so in order to do that we should use the library &lt;PZEM004T.h&gt;. This library can be downloaded from the webpage: <a href=\"https:\/\/github.com\/olehs\/PZEM004T\">https:\/\/github.com\/olehs\/PZEM004T<\/a> as a zip package, then it should be added within the Arduino IDE as shown in the picture.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"353\" src=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-1.png\" alt=\"\" class=\"wp-image-583\" srcset=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-1.png 627w, https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-1-300x169.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure>\n\n\n\n<p>The next step is to\nconnect the sensors to the arduino microcontroller. We will use the\nSoftwareSerial library to allow serial communication on other digital pins of\nthe Arduino and be able to read more than one serial device. So, the RX and TX\nterminals of first sensor are connected to the pins 11 and 10 of the\nmicrocontroller and the RX and TX terminals of second sensor are connected to\nthe pins 13 and 12 respectively.<\/p>\n\n\n\n<p>Now, it is time to develop the code in Arduino IDE as follows:<\/p>\n\n\n\n<pre class=\"lang:arduino decode:true\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n \/\/* Code to get the voltage, current and power from two AC PZEM sensors connected to the Arduino Mega microcontroller, then all the values *\/\/\n \/\/  are concatenated in one char variable in order to send it through serial communication                                                 \/\/ \n \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n  \n #include <softwareserial.h>\n #include <pzem004t.h>\n  \n IPAddress ip(192,168,1,11);\n IPAddress ip1(192,168,1,10);\n  \n char ix[10];\n char iy[10];\n char iz[10];\n char conc[360];\n  \n char ix1[10];\n char iy1[10];\n char iz1[10];\n  \n float V, V1;\n float i, i1;\n float p, p1;\n  \n void setup() {\n Serial.begin (9600);\n }\n  \n void loop() {\n   \n PZEM004T pzem(10,11); \/\/(RX TX)\n pzem.setAddress (ip);\n V=pzem.voltage(ip); \/\/voltage obtained from the pzem library\n i = pzem.current(ip); \/\/current obtained from the pzem library\n p = pzem.power(ip);\/\/power obtained from the pzem library\n dtostrf(V,7,3,ix); \/\/function used to stored the current value in the char variable ix, specifying 3 as the number of digits after the point\n dtostrf(i,7,3,iy); \/\/function used to stored the current value in the char variable iy, specifying 3 as the number of digits after the point\n dtostrf(p,7,3,iz); \/\/function used to stored the power value in the char variable iz, specifying 3 as the number of digits after the point\n delay(1000);\n  \n PZEM004T pzem1(12,13); \/\/(RX TX)\n pzem1.setAddress (ip1);\n V1=pzem1.voltage(ip1); \/\/voltage obtained from the pzem library\n i1 = pzem1.current(ip1); \/\/current obtained from the pzem library\n p1 = pzem1.power(ip1);\/\/power obtained from the pzem library\n dtostrf(V1,7,3,ix1);\/\/function used to stored the current value in the char variable ix1, specifying 3 as the number of digits after the point\n dtostrf(i1,7,3,iy1); \/\/function used to stored the current value in the char variable iy1, specifying 3 as the number of digits after the point\n dtostrf(p1,7,3,iz1); \/\/function used to stored the power value in the char variable iz1, specifying 3 as the number of digits after the point\n delay(1000);\n  \n sprintf(conc,\": %s, : %s, : %s, : %s, : %s, : %s,\\n\", ix,iy,iz,ix1,iy1,iz1); \/\/ function used to concatenate all the values in one unique char variable \n Serial.write(conc);\n  \n }  <\/pzem004t.h><\/softwareserial.h><\/pre>\n\n\n\n<p><strong>Remarks:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> The sensors are not read at the same time. <\/li><li> The type of the values given by the sensor is float. <\/li><li> After getting all the values these are converted from float to char  type by using the \u201cdtostrf\u201d function in which the decimal places  are limited to three. <\/li><li> All the 6 values are concatenated, using the function \u201csprintf\u201d, in  one unique char array and then it is sent as serial data. <\/li><li> Besides putting all the values together in the char array, we also  put the \u2018:\u2019 and  \u2018,\u2019 characters. In the end, the char array looks  like: \u201c: val1, : val2, : val3, : val4, : val5, : val6, :\u201d. It is  done in order to analyze and get the values easily in python.&nbsp;&nbsp;&nbsp; <\/li><\/ul>\n\n\n\n<p>After uploading and testing\nthe code on the microcontroller, it is time to deal with the python script. The\nArduino microcontroller should be connected to the Raspberry by using a USB\ncable.<\/p>\n\n\n\n<p>Before starting with the\ncode, it could be good to know how to create a python file from the Linux\nterminal. It is done by typing the next command line:<\/p>\n\n\n\n<p><strong>touch PZEM_Sensors.py <\/strong><\/p>\n\n\n\n<p>Then to open the\nfile already created, we should type the next command line:<\/p>\n\n\n\n<p><strong>nano PZEM_Sensors.py<\/strong><\/p>\n\n\n\n<p>The next screen will appear:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"268\" src=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-2.png\" alt=\"\" class=\"wp-image-586\" srcset=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-2.png 504w, https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-2-300x160.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<p>So now, let\u2019s take a look at the code:<\/p>\n\n\n\n<pre class=\"lang:python decode:true\">import serial\n import time\n import re\n  \n port = \"\/dev\/ttyACM0\"\n s1 = serial.Serial(port,9600)\n  \n while True:\n     if s1.inWaiting()&gt;0:\n         inputValue = s1.readline().decode() \n         m = re.search('.*:(.*),.*:(.*),.*:(.*),.*:(.*),.*:(.*),.*:(.*),',inputValue) # command used to read the information and split it between the charcaters ':' and ','\n         v1 = m.group(1).replace(\" \",\"\") ## command used to saved the information splitted before in a the variable \n         i1 = m.group(2).replace(\" \",\"\")\n         p1 = m.group(3).replace(\" \",\"\")\n         v2 = m.group(4).replace(\" \",\"\")\n         i2 = m.group(5).replace(\" \",\"\")\n         p2 = m.group(6).replace(\" \",\"\")\n         a = float(v1)\n         b = float(i1)\n         c = float(p1)\n         d = float(v2)\n         e = float(i2)\n         f = float(p2)\n         print(\"Voltage1:\",+a)\n         print(\"Current1:\",+b)\n         print(\"Power1:\",+c)\n         print(\"Voltage2:\",+d)\n         print(\"Current2:\",+e)\n         print(\"Power2:\",+f)  <\/pre>\n\n\n\n<p><strong>Remarks:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> We should specify the port of the Raspberry where the sensor is  connected to. To know it, we should just type the next command from  the LX Terminal  window:&nbsp; ls \/dev  &#8211;&gt; The names of the connected USB devices start with: \u201cttyUSBX\u201d. Where X indicates the number of each USB device connected. <\/li><li> The serial port is read and decoded to make sure we have a string  of chars. It is done with the function readline() and decode()  respectively. <\/li><li> All the six  values are getting by using regular expressions. Using  \u2018.*:(.*),\u2019, we take all the characters which are between \u2018:\u2019 and  \u2018,\u2019 and then they are stored in groups. <\/li><li> In the end the values are converted from char to float type.&nbsp;&nbsp; <\/li><\/ul>\n\n\n\n<p>After writing all\nthe code and pressing the keys Ctrl + X you will be asked to save it or not.\nYou should press the key y or n and then Enter.<\/p>\n\n\n\n<p>To run the code, we should type the next command line:<\/p>\n\n\n\n<p><strong>python PZEM_Sensors.py <\/strong><\/p>\n\n\n\n<p>Then all the results\nand messages will appear in the Terminal windows or a message of error will\nappear if something wrong is inside the code.<\/p>\n\n\n\n<p>The figure below shows all the results got from the two PZEM sensors. The current 1 is 0 due to there is not any current sensor connected to and for that the reason the power 1 is also 0. If there is any problem of communication the value, we will get is -1.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"358\" src=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-3.png\" alt=\"\" class=\"wp-image-588\" srcset=\"https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-3.png 552w, https:\/\/miniprojets.net\/wp-content\/uploads\/2019\/08\/image-3-300x195.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p>I hope this tutorial can help you, <\/p>\n\n\n\n<p>Best regards, <\/p>\n\n\n\n<p>Richard Flores Diaz<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, Today and for the end of my internship, i propose this tutorial to connect raspberry pi and 2&hellip;<\/p>\n","protected":false},"author":6,"featured_media":590,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[82],"class_list":["post-581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-capteurs","tag-capteurs"],"_links":{"self":[{"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":0,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/media\/590"}],"wp:attachment":[{"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miniprojets.net\/index.php\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}