📜 ⬆️ ⬇️

We use Twitter for its intended purpose

Once I was very bored. So much so that I decided to get all the details of my "designer" and collect something.



The following was found in the bins: Arduino compatible board, tp-link mr3020 router, GPS module, gyroscope / accelerometer, and a whole bunch of other stuff.

Looking ahead, so as not to upset some readers, I will say that the GPS module and the gyroscope were not useful
.
And here twitter. My attitude to sabzh balances on the verge of indifference and negativity. Well, what is so interesting to tell using only 140 characters without pictures and video? An incomprehensible toy for those who live in social networks and post about what he ate for breakfast. This is my personal opinion, if it does not coincide with yours, I can understand it. Therefore, I ask dear readers not to holivarit on this topic, at least in the comments to this article.
All these thoughts prompted me to an idea for an indispensable device that would tweet every time I visited the toilet! Well, that all were in the know. And not just about the visit, but in more detail: what exactly did I do there? Let's get started
')
The first thing to do is define the criteria. I think that the signs of finding me in the toilet are the door closed and the lights on. At hand was some kind of sensor from an automatic gate, from which a photosensitive element was removed.



Having played with the multimeter, I found out that when the illumination changes, a potential difference arises in it. At maximum illumination, the voltage meter showed approximately 0.330 volts. We cling it to the "Arduine". I decided to determine the position of the door (opened / closed) simply by closing the contacts. This, by the way, turned out to be the most difficult, because I did not have a suitable button.



Here is the program for "Arduin":

#include <SoftwareSerial.h> //   SoftwareSerial mySerial(2, 3); // RX, TX     rs323 int cnt = 0; //   void setup() { mySerial.begin(115200); //   mySerial.println("Halo world"); } void loop() { int lightValue = analogRead(A0); //      int doorValue = analogRead(A1); //    boolean light = false; boolean door = false; if(lightValue > 60) light = true; //    60 -   .    if(doorValue > 500) door = true; //       0,   1023. //   ,  ...   if(light && door) cnt++; //    -   else { if(cnt>0) { //    mySerial.print("<"); mySerial.print(cnt); //      mySerial.println(">"); } cnt = 0; //   } delay(1000); } 


The second thing we need is a router. I took the tp-link mr3020. You need to install OpenWRT on it, set up a connection to the Internet and install php. How this is done is described here many times, including by me. Therefore, we will not linger on this. Only important points: here we listen to the port and get the data we need

 <?php //     $child_pid = pcntl_fork(); if ($child_pid) { exit(); } posix_setsid(); //      include("post.php"); $filename = "/dev/ttyATH0"; // serial-  $handle = fopen($filename, "r"); //  while (!feof($handle)) { $sym = fread($handle, 1); //       if($sym=='>') { $f=false; echo "<<$cnt>>\n"; flush(); act($cnt); $cnt=''; } elseif($sym=='<') $f=true; elseif($f) $cnt .= $sym; } fclose($handle); function act($time) { if($time<40) {echo "dunno\n";} if(40<=$time && $time<150) { postMyPost("i was pee");} if(150<=$time && $time<300) { postMyPost("i was poo");} if(300<=$time && $time<900) { postMyPost("i take shower");} } ?> 


We post using this library:

The third important point is the ability to post a program tweeter. To do this, you need to log in, go to dev.twitter.com , move the cursor at the top right to your bright face, select My Applications. To do Create New Application, answer all questions. Then in the Settings you must specify Access: Read and Write. Then in Details, click Create my access token and remember the fields Consumer key, Consumer secret, Access token and Access token secret, we still need them. Downloading PHP library: github.com/abraham/twitteroauth / archive / master.zip
In config.php, we substitute the previously stored data:

 define('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxx'); define('CONSUMER_SECRET', 'yyyyyyyyyyyyyyyyyyyyyyyyyy'); define('OAUTH_TOKEN', 'qwertyqwertyqwertyqwertyqwertyqwerty'); define('OAUTH_SECRET', 'sashagreysashagreysashagreysashagrey'); define('OAUTH_CALLBACK', 'http://example.com/twitteroauth/callback.php'); 


Well, the script itself to send (post.php):

 <?PHP require_once('lib/twitteroauth/twitteroauth.php'); require_once('lib/config.php'); function postMyPost($status) { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,OAUTH_TOKEN,OAUTH_SECRET); $connection->format = 'xml'; $connection->post('statuses/update', array('status'=>$status)); } ?> 


Debugging process:



Product Assembly:



Connection:



Using:
connect to the router via ssh, run the script:
 root@OpenWrt:~# php-cgi /www/twitter/run.php X-Powered-By: PHP/5.4.11 Content-type: text/html root@OpenWrt:~# 

This starts the background process, and the parent ends. Thus, the script will work even after disconnecting from the console.

To stop the script, you need to look at the process ID and make it kill:
 root@OpenWrt:~# ps | grep run.php 1665 root 10436 S php-cgi /www/twitter/run.php 1670 root 1492 S grep run.php root@OpenWrt:~# kill 1665 


The result of the work can be found here: twitter.com/kruz_ivan

What are the plans:
1) Tighten the camera so that you can also see a photo of the process itself.
2) Odor transfer technologies are being actively developed. With the advent of the first serial devices, I will definitely use them in my project.
3) Creating similar devices integrated with social networks that will tweet when you, for example, open a refrigerator, or instagram photos every time you sit down to eat.

Source: https://habr.com/ru/post/181852/


All Articles