{"id":2345,"date":"2013-07-22T11:53:07","date_gmt":"2013-07-22T16:53:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2345"},"modified":"2017-01-04T15:42:44","modified_gmt":"2017-01-04T20:42:44","slug":"a-tweeting-s-function-for-the-raspberry-pi","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/07\/22\/a-tweeting-s-function-for-the-raspberry-pi\/","title":{"rendered":"A Tweeting S-Function for the Raspberry Pi"},"content":{"rendered":"<p>I recently bought a <a href=\"http:\/\/www.raspberrypi.org\/\">Raspberry Pi<\/a>. After trying a few of the demos included with the <a href=\"https:\/\/www.mathworks.com\/hardware-support\/raspberry-pi-simulink.html\">Simulink support package for Raspberry Pi<\/a>, I began thinking about what I could do next.<\/p>\r\n\r\n<p>Just for fun, I thought... <em>could I get a Simulink model deployed on the Raspberry Pi to send Tweets?<\/em><\/p>\r\n\r\n<p><strong>Sending Tweets From Linux<\/strong><\/p>\r\n\r\n<p>In case you were not aware, the Raspberry Pi is running a Linux-based operating system. Knowing that,  I thought that if I could find a way to Tweet from the Linux shell, I could Tweet from an S-function. (as you know... if you can program it, you can put it in an S-function)<\/p>\r\n\r\n<p>I did some search and figured out that the simplest way to send Tweets from the Linux shell was using the <a href=\"http:\/\/www.supertweet.net\/\">SuperTweet.net Twitter API<\/a>. <\/p>\r\n\r\n<p>You first need to install <a href=\"http:\/\/en.wikipedia.org\/wiki\/CURL\">cURL<\/a>. In a Linux shell, execute:<\/p>\r\n\r\n<code>sudo apt-get install curl<\/code>\r\n\r\n<p>Once this is done, you can send Tweets using cURL and a line like:<\/p>\r\n\r\n<code>curl -u USERNAME:PASSWORD -d status=\"My First Tweet Test\" http:\/\/api.supertweet.net\/1.1\/statuses\/update.json<\/code>\r\n\r\n<p>Now that I know I can send Tweets programmatically, I know I can send Tweets from Simulink generated code!<\/p>\r\n\r\n<p><strong>An S-Function that sends Tweets<\/strong><\/p>\r\n\r\n<p>If you know how to accomplish something in C\/C++, there are always many ways to incorporate that in the code generated from Simulink.<\/p>\r\n\r\n<p>The <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sfunctionbuilder.html\">S-Function Builder<\/a> and the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/sfg\/integrating-existing-c-functions-into-simulink-models-with-the-legacy-code-tool.html\">Legacy Code Tool<\/a> are two ways to help this process. Those two tools will help you to create C and TLC wrappers to inline your C code within the C code generated by Simulink.<\/p>\r\n\r\n<p>Personally, for simple cases like this one,  I prefer to write the S-function and TLC. I feel like this gives me a better understanding of the process.<\/p>\r\n\r\n<p>For my first test, I wrote a very simple S-function, no input and no output that does nothing in simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/s_function_for_sim.png\" alt=\"Empty S-Function\" \/><\/p>\r\n\r\n<p>I placed it inside a triggered subsystem because I do not want to send a Tweet at every time step.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/tweeting_model.png\" alt=\"Simulink Model that can Tweet\" \/><\/p>\r\n\r\n<p>Finally, I wrote a TLC file to inline the S-function. For this simple test, all I had to do it use <tt>system<\/tt> command to execute the same line as I tested previously in a shell.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/tweeting_tlc.png\" alt=\"TLC that can Tweet\" \/><\/p>\r\n\r\n<p>I hit Ctrl+B to build the model and download it on the Raspberry Pi automatically. Then I went back to my web browser, refreshed the page, and got pretty impressed when I saw: <\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/final_account.png\" alt=\"I received a Tweet from my Raspberry Pi!\" \/><\/p>\r\n\r\n<p><strong>What's next?<\/strong><\/p>\r\n\r\n<p>Now that I know I can make simulink models for my Raspberry Pi that send Tweets, I could try a few more things. I could input a few signals into my S-function to add data to my Tweets. I could define a few different strings to be Tweeted depending on some signal value. I could put the C code sending the Tweet in a seperate C file to make the inlining simpler, etc... <\/p>\r\n\r\n<p>Let me know if you have suggestions on what I could do with my Raspberry Pi and Simulink!<\/p>\r\n\r\n<p><strong>Now it's your turn?<\/strong><\/p>\r\n\r\n<p>Do you have low-cost target hardware supported by Simulink? What kind of funky application did you implement? Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2345&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/final_account.png\" onError=\"this.style.display ='none';\" \/><\/div><p>I recently bought a Raspberry Pi. After trying a few of the demos included with the Simulink support package for Raspberry Pi, I began thinking about what I could do next.\r\n\r\nJust for fun, I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/07\/22\/a-tweeting-s-function-for-the-raspberry-pi\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,21,71,87],"tags":[333,334],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2345"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=2345"}],"version-history":[{"count":31,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2345\/revisions"}],"predecessor-version":[{"id":6255,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2345\/revisions\/6255"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}