{"id":2016,"date":"2016-11-01T12:19:57","date_gmt":"2016-11-01T11:19:57","guid":{"rendered":"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/?p=2016"},"modified":"2021-02-03T09:25:38","modified_gmt":"2021-02-03T08:25:38","slug":"raspberry-als-datenlogger-teil-3-arduino-nano-als-i%c2%b2c-converter","status":"publish","type":"post","link":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/2016\/11\/01\/raspberry-als-datenlogger-teil-3-arduino-nano-als-i%c2%b2c-converter\/","title":{"rendered":"Raspberry als Datenlogger &#8211; Teil.3 Arduino nano als I\u00b2C Converter"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2016\" class=\"pvc_stats all  \" data-element-id=\"2016\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<div id=\"attachment_1029\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2015\/11\/DSC_4790.jpg\"><img fetchpriority=\"high\" decoding=\"async\" aria-describedby=\"caption-attachment-1029\" class=\"wp-image-1029 size-medium\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2015\/11\/DSC_4790-300x169.jpg\" alt=\"DSC_4790\" width=\"300\" height=\"169\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2015\/11\/DSC_4790-300x169.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2015\/11\/DSC_4790-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1029\" class=\"wp-caption-text\">Raspberry PI als Datenlogger<\/p><\/div>\n<p style=\"text-align: justify\">Im letzten Teil des Projektes &#8222;<a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/2015\/11\/16\/raspberry-als-datenlogger-teil2-die-sensoren-und-python\/\">Raspberry Pi als Datenlogger<\/a>&#8220; habe ich einen Raspberry Pi als Datenlogger f\u00fcr unterschiedliche Sensoren konfiguriert. Als Schnittstelle dient der I\u00b2C Bus und die RS232 Leitungen, an denen die Sensoren angeschlossen sind. Die Sensoren waren ein HYT939 (Luftfeuchtigkeit und Temperatur), ein BME280 (Luftdruck, Luftfeuchte und Temperatur) sowie ein Ultimate GPS Board an der seriellen Schnittstelle. Der Raspberry Pi ist dabei in eine Box eingebaut, wird mit einer Powerbank, die sich ebenfalls in der Box befindet, versorgt und stellt als Schnittstelle eine 8 polige RJ45 Buchse mit I\u00b2C und RS232 nach au\u00dfen zur Verf\u00fcgung. An diese Buchse k\u00f6nnen dann die Sensoren angeschlossen werden. &nbsp; Will man die Sensorik nun \u00e4ndern, bzw. erweitern, so k\u00f6nnen einfach weitere I\u00b2C Bus &#8211; Sensoren angeschlossen werden. Es muss lediglich noch die Software (in diesem Fall Python Skripten) angepasst werden und schon k\u00f6nnen die Daten des neuen Sensors empfangen und aufgezeichnet werden. Wie aber geht das System um, wenn der Sensor nicht \u00fcber I\u00b2C spricht? Wenn zum Beispiel ein analoger Wert eingelesen werden soll? Ganz einfach: Soll die Hardware des Datenloggers nun nicht mehr modifiziert werden, so muss man einfach daf\u00fcr sorgen, dass der neue Sensor, der beispielsweise analoge Spannungen ausgibt, diese an eine, nennen wir sie Black Box weitergibt, die wiederum an ihrem Ausgang ein I\u00b2C Bussignal bereit stellt. Das kann dann wieder in das bestehende System integriert werden.<\/p>\n<div id=\"attachment_2024\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2872.jpg\"><img decoding=\"async\" aria-describedby=\"caption-attachment-2024\" class=\"wp-image-2024 size-medium\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2872-300x169.jpg\" alt=\"dsc_2872\" width=\"300\" height=\"169\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2872-300x169.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2872-768x432.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2872-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-2024\" class=\"wp-caption-text\">Arduino Nano als I\u00b2C Converter<\/p><\/div>\n<p style=\"text-align: justify\">Und genau diese Aufgabe erf\u00fcllt ein Microcontroller. Praktischer Weise habe ich diesmal einen Arduino Nano dazu verwendet. Der Arduino Nano mit dem Atmega 328 besitzt ja mehrere analoge und Digitale Eing\u00e4nge, sowie zwei Interrupts. \u00dcber entsprechende Libraries ist einfach das I\u00b2C Bus Protokoll zu realisieren. Somit ist das geeignete Black-Box Ger\u00e4t gefunden.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Der folgende Arduino Code stellt einen einfachen I\u00b2C Bus Slave dar. Die Busadresse soll beispielsweise 0x08 sein, von einer Messung haben wir den Messwert 1.3321 erhalten. Dieser Wert soll bei Anfrage an diese Busadresse gesendet werden. Das Beispiel sieht wie folgt aus:<\/p>\n<pre><code>\r\n\/\/ I2C Slave\r\n#include\r\nint address = 8;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/i2c busaddresse\r\nfloat wert = 1.3321;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ testwert .... kommt zb aus sensorberechnung\r\n\r\nchar tmp[6];\r\nvoid setup() {\r\n\r\nWire.begin(address);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ i2c adresse 0x08\r\nWire.onRequest(requestEvent);&nbsp; \/\/ register event\r\n}\r\n\r\nvoid loop() {}\r\n\r\nvoid requestEvent() {\r\ndtostrf(wert,1,3,tmp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ float \"wert\" in char array umwandeln\r\nWire.write(tmp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ char array senden\r\n}<\/code><\/pre>\n<p style=\"text-align: justify\">Im Skript ist eine Funktion &#8222;dtostrf&#8220; zu sehen. Sie wandelt den Inhalt einer Variablen des Datentyps &#8222;floating point&#8220; in ein Charakter Array um. Infos sind <a href=\"http:\/\/www.atmel.com\/webdoc\/AVRLibcReferenceManual\/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html\">hier<\/a> zu sehen. Im Python Interpreter des Raspberry k\u00f6nnen wir nun \u00fcberpr\u00fcfen, ob die Daten ankommen. Doch zuvor soll der gesamte Aufbau einmal schematisch dargestellt werden.<\/p>\n<div id=\"attachment_2025\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2025\" class=\"wp-image-2025\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild-300x169.jpg\" alt=\"blockschaltbild\" width=\"500\" height=\"281\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild-300x169.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild-768x432.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild-1024x576.jpg 1024w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/blockschaltbild.jpg 1280w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-2025\" class=\"wp-caption-text\">Schematische Darstellung des Systems<\/p><\/div>\n<p>Sind alle Sensoren inklusive des Arduino angeschlossen, so kann das folgende Python-Skript aus der Linux Konsole des Raspberry aufgerufen werden:<\/p>\n<pre><code># Reading data from arduino as sensor slave\r\n# V0.1 by bihlo 09\/2016\r\n\r\nimport smbus, time\r\n\r\nbus = smbus.SMBus(1)&nbsp;&nbsp;&nbsp; # Raspberry Rev. 2\r\naddress = 0x08&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Fixed default address of Arduino\r\n\r\n# Initialize sensor to acquire measurements\r\n#def MeasurementRequest():\r\n# void = bus.write_quick(address)\r\n#time.sleep(0.1)\r\n\r\n# Read from bus\r\ndef ReadMeasurement():\r\nvoid= bus.write_quick(address)\r\n\r\ntime.sleep(0.1)\r\n# Acquire 6 byte from sensor\r\nReadout = bus.read_i2c_block_data(address,0,6)\r\n\r\nout=''.join(chr(i) for i in Readout) #byte array von ascii in string wandeln\r\n\r\nprint \"Antwort vom Arduino: \", Readout, \" .\"\r\nprint \"Antwort als String:&nbsp; \", out\r\n\r\nReadMeasurement()<\/code><\/pre>\n<p style=\"text-align: justify\">Wenn der Python Interpreter jetzt keinen Fehler meldet und der Inhalt der Variable &#8222;wert&#8220; des Arduino in ASCII und im Klartext dargestellt wird, dann kann jetzt mit einem &#8222;richtigen&#8220; Messwert eines Sensors weiter gearbeitet werden.<\/p>\n<div id=\"attachment_2023\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2873.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2023\" class=\"wp-image-2023 size-medium\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2873-300x169.jpg\" alt=\"dsc_2873\" width=\"300\" height=\"169\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2873-300x169.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2873-768x432.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/DSC_2873-1024x576.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-2023\" class=\"wp-caption-text\">Partikeldetektor<\/p><\/div>\n<p style=\"text-align: justify\">Als Sensor soll an an den Arduino ein DSM501 Dust Sensor Module angeschlossen werden. Der <a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2016\/10\/3-1-Specification-DSM501.pdf\">DSM501A<\/a> ist ein Staubsensor, der mit 5V bei einer Stromaufnahme von 90mA betrieben wird. Er erkennt Partikel ab einer durchschnittlichen Gr\u00f6\u00dfe von 1\u00b5m bei einer Konzentration von 0 bis ca. 1.4mg pro Kubikmeter Raumluft. Laut Datenblatt und Herstellerinformationen wird die Luft im Bereich der Messkammer erhitzt, sodass eine Luftzirkulation entsteht. Die sich st\u00e4ndig bewegende Luft wird mit einem getakteten Lichtstrahl beleuchtet. Ein in einem Winkel zur Lichtquelle angebrachter Fotodetektor empf\u00e4ngt nun, im Falle dass sich Partikel im Licht befinden, dessen Streulicht. Die Intensit\u00e4t des Streulichtes ist von der Anzahl und Gr\u00f6\u00dfe der Partikel abh\u00e4ngig und kann so erfasst werden. Als Ausgangssignal erzeugt der Sensor ein Puls\/Pausensignal mit einer Periode von 100ms. Das Verh\u00e4ltnis von High- zu Lowtime stellt nun die Grundlage f\u00fcr die Berechnung der Partikelanzahl bzw. -dichte dar. \u00dcber einen Countereingang des Arduino werden die Impulse gemessen und der korrespondierende Partikelwert errechnet. Dieser Wert wird nun wie oben beschrieben als Busdatum f\u00fcr den I2C Bus herangezogen und bei Ausl\u00f6sen des Bus Events \u00fcbertragen. Somit hat der Arduino die Aufgabe eines einfachen Protokollkonverters \u00fcbernommen. Mit dieser Methode lassen sich alle m\u00f6glichen Messwerte so aufbereiten, dass der Raspberry mit einer Schnittstelle damit umgehen kann.<\/p>\n<p>Wie das im Detail funktioniert und wie der Code dazu aussieht, werde ich vielleicht sp\u00e4ter einmal posten.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2016\" class=\"pvc_stats all  \" data-element-id=\"2016\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Im letzten Teil des Projektes &#8222;Raspberry Pi als Datenlogger&#8220; habe ich einen Raspberry Pi als Datenlogger f\u00fcr unterschiedliche Sensoren konfiguriert. Als Schnittstelle dient der I\u00b2C Bus und die RS232 Leitungen, an denen die Sensoren angeschlossen sind. Die Sensoren waren ein HYT939 (Luftfeuchtigkeit und Temperatur), ein BME280 (Luftdruck, Luftfeuchte und Temperatur) sowie ein Ultimate GPS Board&hellip; <br \/> <a class=\"read-more\" href=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/2016\/11\/01\/raspberry-als-datenlogger-teil-3-arduino-nano-als-i%c2%b2c-converter\/\">Weiterlesen<\/a><\/p>\n","protected":false},"author":86,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[1187,57,1186],"tags":[605,604,608,607,603,341,606],"class_list":["post-2016","post","type-post","status-publish","format-standard","hentry","category-arduino","category-elektronikbastler","category-raspberrypi","tag-arduino-als-i2c-converter","tag-arduino-nano","tag-datalogger","tag-dsm501a","tag-raspberry-i2c","tag-raspberry-pi","tag-staubsensor"],"a3_pvc":{"activated":true,"total_views":359,"today_views":0},"_links":{"self":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts\/2016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/comments?post=2016"}],"version-history":[{"count":0,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}