{"id":1036,"date":"2018-10-25T21:27:13","date_gmt":"2018-10-25T19:27:13","guid":{"rendered":"http:\/\/eiseler.de\/wordpress\/?p=1036"},"modified":"2018-11-12T00:09:06","modified_gmt":"2018-11-11T23:09:06","slug":"connect-raspberry-pi-arduino-usb-bidirectional","status":"publish","type":"post","link":"http:\/\/eiseler.de\/wordpress\/?p=1036","title":{"rendered":"Connect Raspberry Pi &#038; Arduino USB bidirectional"},"content":{"rendered":"<p>How to connect an Arduino to a raspberry pi via USB bidirectional.<\/p>\n<p>The raspberry reads all from the serial and prints it. Next it reads a textile from a webpage and sends the text (one word) to serial, next a random car to the Arduino. Arduino got a LCD attached and shows the data it got via serial. It sends back the seconds it run and a string. The example is at this time without real use, it is just for testing the bidirectional serial connection.<\/p>\n<p>Using Python on the raspberry side. Change the port to what you find out:<\/p>\n<pre>\r\nimport serial\r\nimport time\r\nimport random\r\nimport urllib2\r\nimport feedparser\r\n \r\ndef read_all(port, chunk_size=200):\r\n    \"\"\"Read all characters on the serial port and return them.\"\"\"\r\n    if not port.timeout:\r\n        raise TypeError('Port needs to have a timeout set!')\r\n\r\n    read_buffer = b''\r\n\r\n    while True:\r\n        # Read in chunks. Each chunk will wait as long as specified by\r\n        # timeout. Increase chunk_size to fail quicker\r\n        byte_chunk = port.read(size=chunk_size)\r\n        read_buffer += byte_chunk\r\n        if not len(byte_chunk) == chunk_size:\r\n            break\r\n\r\n    return read_buffer\r\n#end def\r\n\r\ncars = [\"Ford\", \"Volvo\", \"BMW\", \"VW\"]\r\n\r\nser = serial.Serial(\r\n    port = '\/dev\/ttyACM0',\r\n    baudrate = 9600,\r\n    parity = serial.PARITY_NONE,\r\n    stopbits = serial.STOPBITS_ONE,\r\n    bytesize = serial.EIGHTBITS,\r\n    timeout=0.5, # IMPORTANT, can be lower or higher\r\n    )\r\n\r\n#s = serial.Serial('\/dev\/ttyACM0', 9600) # Namen ggf. anpassen\r\n#s.flushInput()\r\n#s.open()\r\n\r\ntime.sleep(2) # der Arduino resettet nach einer Seriellen Verbindung, daher \r\n\r\nser.write(\"xyx\")\r\n\r\n\r\nd = feedparser.parse('http:\/\/www.reddit.com\/r\/news\/.rss')\r\ndlen = len(d['entries'])\r\ndnr = 0\r\ntry:\r\n    while True:\r\n        nb = \"01234567890123456789012345678901\"\r\n        text = \"nn.mm\"\r\n        data_str = []\r\n        data_str = read_all(ser)\r\n        print(data_str) \r\n        #nb = raw_input('Choose it: ')\r\n        #nb = cars[random.randint(0,3)] + \"x\"\r\n        try:\r\n            #nb = d['entries'][dnr]['title'].encode('utf-8').strip() + \"x\"\r\n            nb = d['entries'][dnr]['title'].encode('utf-8').strip() \r\n            dnr += 1 \r\n            if (dnr > dlen -1):\r\n                d = feedparser.parse('http:\/\/www.reddit.com\/r\/news\/.rss')\r\n                dlen = len(d['entries'])\r\n                dnr = 0\r\n            #ser.write(nb)\r\n        except:\r\n            print(\"fehler1\")\r\n        try: \r\n            response = urllib2.urlopen('http:\/\/4johannes.de\/weather\/lowest.txt')\r\n            text = response.read().strip()\r\n            print(text)\r\n        except:\r\n            print(\"fehler2\")\r\n        ll = len(text)\r\n        text2 = text + \" \" + nb[0:33-ll] + \"\\n\"\r\n        #print(text2)\r\n        #ser.write(text2)\r\n        #time.sleep(5)\r\n        #text2 = nb[16-ll:33-ll] + \"y\"\r\n        print(text2)\r\n        print(nb)\r\n        ser.write(text2)\r\n        time.sleep(5)\r\nexcept KeyboardInterrupt:\r\n    ser.close()\r\n\r\n<\/pre>\n<p>And this is the Arduino part:<\/p>\n<pre>\/*********************\r\n\r\nExample code for the Adafruit RGB Character LCD Shield and Library\r\n\r\nThis code displays text on the shield, and also reads the buttons on the keypad.\r\nWhen a button is pressed, the backlight changes color.\r\n\r\n**********************\/\r\n\r\n\/\/ include the library code:\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_RGBLCDShield.h&gt;\r\n#include &lt;utility\/Adafruit_MCP23017.h&gt;\r\n\r\n\r\n\/\/ The shield uses the I2C SCL and SDA pins. On classic Arduinos\r\n\/\/ this is Analog 4 and 5 so you can't use those for analogRead() anymore\r\n\/\/ However, you can connect other I2C sensors to the I2C bus and share\r\n\/\/ the I2C bus.\r\nAdafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();\r\n\r\n\/\/ These #defines make it easy to set the backlight color\r\n#define RED 0x1\r\n#define YELLOW 0x3\r\n#define GREEN 0x2\r\n#define TEAL 0x6\r\n#define BLUE 0x4\r\n#define VIOLET 0x5\r\n#define WHITE 0x7\r\n\r\nbyte nr;\r\nchar myString[] = \"Hello MArius\";\r\nString readString;\r\nlong previousMillis = 0;\r\nlong interval = 1000;\r\n\r\nvoid setup() {\r\n\/\/ Debugging output\r\nSerial.begin(9600);\r\n\/\/ set up the LCD's number of columns and rows: \r\nlcd.begin(16, 2);\r\n\r\n\/\/ Print a message to the LCD. We track how long it takes since\r\n\/\/ this library has been optimized a bit and we're proud of it :)\r\nint time = millis();\r\nlcd.print(\"Hello, Marius!\");\r\ntime = millis() - time;\r\n\/\/Serial.print(\"Took \"); Serial.print(time); Serial.println(\" ms\");\r\nlcd.setBacklight(WHITE);\r\nlcd.setCursor(0, 1);\r\n\r\n}\r\n\r\nuint8_t i=0;\r\nvoid loop() {\r\n\/\/ set the cursor to column 0, line 1\r\n\/\/ (note: line 1 is the second row, since counting begins with 0):\r\n\/\/lcd.setCursor(0, 1);\r\n\/\/ print the number of seconds since reset:\r\n\/\/lcd.print(millis()\/1000);\r\n\r\n\r\nunsigned long currentMillis = millis();\r\n\r\nif(currentMillis - previousMillis &gt; interval) {\r\n\/\/ save the last time you blinked the LED \r\npreviousMillis = currentMillis;\r\n\/\/do nothing at the moment \r\n\/\/Serial.println(millis()\/1000);\r\n\r\n}\r\n\r\nnr = 1;\r\n\r\nif (Serial.available() &gt; 0) {\r\n\/\/nr = Serial.read();\r\n\/\/myString = Serial.readln();\r\n\/\/Serial.print(\"Folgender char wurde empfangen: \");\r\n\/\/Serial.println(nr, DEC);\r\n\r\nchar c = Serial.read(); \/\/gets one byte from serial buffer\r\nreadString += c; \/\/makes the string readString\r\n\/\/lcd.setCursor(5,1);\r\n\/\/lcd.print(readString);\r\n\/\/lcd.print(c);\r\n\/\/readString = \"\";\r\n\r\nif (c == 'x') {\r\nlcd.setCursor(0,1);\r\nlcd.print(\" &lt;\");\r\nlcd.setCursor(0,1);\r\nlcd.print(readString);\r\nSerial.println(millis()\/1000);\r\nSerial.println(\"x\/n\");\r\nreadString = \"\";\r\n\r\n}\r\n\r\n}\r\n\r\nuint8_t buttons = lcd.readButtons();\r\n\r\n\r\nif (buttons) {\r\nlcd.clear();\r\nlcd.setCursor(0,0);\r\nif (buttons &amp; BUTTON_UP) {\r\nlcd.print(\"UP \");\r\nlcd.setBacklight(RED);\r\n}\r\nif (buttons &amp; BUTTON_DOWN) {\r\nlcd.print(\"DOWN \");\r\nlcd.setBacklight(YELLOW);\r\n}\r\nif (buttons &amp; BUTTON_LEFT) {\r\nlcd.print(\"LEFT Marius\");\r\nlcd.print(nr);\r\n\r\nlcd.setBacklight(GREEN);\r\n}\r\nif (buttons &amp; BUTTON_RIGHT) {\r\nlcd.print(\"RIGHT Paulina\");\r\nlcd.setBacklight(TEAL);\r\n}\r\nif (buttons &amp; BUTTON_SELECT) {\r\nlcd.print(\"SELECT \");\r\nlcd.setBacklight(VIOLET);\r\n}\r\n}\r\n}<\/pre>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1036\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1036\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-1036\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>How to connect an Arduino to a raspberry pi via USB bidirectional. The raspberry reads all from the serial and prints it. Next it reads a textile from a webpage and sends the text (one word) to serial, next a &hellip; <a href=\"http:\/\/eiseler.de\/wordpress\/?p=1036\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-1036\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-1036\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-1036\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"http:\/\/eiseler.de\/wordpress\/?p=1036&amp;share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-1036","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8zAuQ-gI","jetpack-related-posts":[{"id":1038,"url":"http:\/\/eiseler.de\/wordpress\/?p=1038","url_meta":{"origin":1036,"position":0},"title":"Raspberry pi & waveshare 2.7 e-ink Display & python","date":"11\/11\/2018","format":false,"excerpt":"Start with a headless raspberry, no need to connect monitor, keyboard:\u00a0https:\/\/raspberrypi.stackexchange.com\/questions\/10251\/prepare-sd-card-for-wifi-on-headless-pi Find the raspberry in your router and connect with ssh. Useullay steps are: sudo raspi-config sudo reboot (neu einloggen) sudo apt-get update -y sudo apt-get upgrade -y Here's some example python code for drawing text horizontal on the waveshare\u2026","rel":"","context":"In &quot;Hardware&quot;","img":{"alt_text":"","src":"https:\/\/i2.wp.com\/eiseler.de\/wordpress\/wp-content\/uploads\/2018\/11\/img_8466-e1541953811554.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":503,"url":"http:\/\/eiseler.de\/wordpress\/?p=503","url_meta":{"origin":1036,"position":1},"title":"G\u00fcnstig Temperatur, Luftfeuchte und Energie messen mit Raspberry Pi oder PC","date":"19\/03\/2014","format":false,"excerpt":"Drahtloses Anbinden von Temperatursensoren und Energiemesser. Und das noch g\u00fcnstig. Sehr hilfreich war dieser Beitrag. Er beschreibt die Anbindung von drahtlosen Sensoren an Fhem. Fhem wollte ich jetzt (noch) nicht aufsetzen, deshalb realisierte ich meine eigene Anbindung in der die Temperaturkurven auf dem Webserver des Raspi angezeigt werden. Zutaten: Ein\u2026","rel":"","context":"In &quot;Arduino&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/eiseler.de\/wordpress\/wp-content\/uploads\/2020\/08\/Bildschirmfoto-2020-08-18-um-03.09.47.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":473,"url":"http:\/\/eiseler.de\/wordpress\/?p=473","url_meta":{"origin":1036,"position":2},"title":"X-MAS Special - Was ein Geek alles braucht","date":"04\/12\/2013","format":false,"excerpt":"X-MAS Special - Was ein Geek alles braucht Weihnachten steht vor der T\u00fcr, deshalb heute ein Special was ein geek unbedingt braucht: PC, Smartphone und Pad: Laufen als Grundausstattung, deshalb keine Deatillierung hier. NAS (Network Attached Server):\u00a0 Mit minimalen Stromverbrauch und einfacher Bedienung kann das mittlerweile (fast) jeder bedienen. Geliefert\u2026","rel":"","context":"In &quot;Arduino&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1435,"url":"http:\/\/eiseler.de\/wordpress\/?p=1435","url_meta":{"origin":1036,"position":3},"title":"Raspberry Pi 4 & Camera","date":"19\/08\/2022","format":false,"excerpt":"Problem was that I didn't get a live picture from the camera. Solution: Use the HDMI 1 not 2 port.","rel":"","context":"In &quot;Raspberry Pi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":954,"url":"http:\/\/eiseler.de\/wordpress\/?p=954","url_meta":{"origin":1036,"position":4},"title":"Raspberry Pi as NAS with OpenMediaVault and Wifi","date":"28\/03\/2018","format":false,"excerpt":"Be warned, the Raspberry Pi with wlan is a slow nas. But I wanted to build one and there are no full instructions for it. So here are the steps to follow. I used this instruction, but some things are missing and so I put everything down in short form:\u2026","rel":"","context":"In &quot;Gadgets&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/eiseler.de\/wordpress\/wp-content\/uploads\/2018\/04\/img_5026.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":479,"url":"http:\/\/eiseler.de\/wordpress\/?p=479","url_meta":{"origin":1036,"position":5},"title":"Raspberry Pi mit Internetradio und Airplay","date":"30\/11\/2013","format":false,"excerpt":"Hier der einige Tipps zum Aufbau eines Raspberry Pis ohns Monitor, Tastatur und Maus mit den F\u00e4higkeiten remote eine Oberfl\u00e4che bereit zu stellen, Internetradio wieder zu geben und als Airplay Station zu dienen. Achso und nebneher noch einen Webserver (LAMP) laufen lassen. Was braucht man? Raspberry Pi, Stromversorgung dazu, WLAN-Stick,\u2026","rel":"","context":"In &quot;iOS App&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1036"}],"version-history":[{"count":3,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1036\/revisions"}],"predecessor-version":[{"id":1048,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1036\/revisions\/1048"}],"wp:attachment":[{"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/eiseler.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}