I want to tweet when I want to
Made with IFTTT.
Tweet with the current time when the button is pressed. Very convenient to use on smartphones and Apple Watch
The posted text becomes something like Nyan August 24, 2017 at 03:45 AM
, and if it is within 1 minute, you cannot post due to duplicate posts. Sometimes you want to continuously throw Nyan.
I have a Raspberry Pi at home so I want him to work As an exercise, try Webhooking with IFTTT I made an applet to skip webhooks and an applet to receive with IFTTT.
When I open the Webhook Setting with IFTTT, a URL that says POST should be displayed like this, so I should have used it as it is. Webhook is very convenient An example of curl input is also displayed
Specify the event name decided by the person to skip.
No. 2 also works nicely. All you have to do is ask Raspberry Pi to fly it.
Speaking of Raspberry Pi, it seems to be Python, so I decided to use Python
import wiringpi as pi ,time,requests,datetime
SW_PIN = 15 #Pin number to which the switch is connected
url='https://maker.ifttt.com/trigger/python1/with/key/******'
swi=0
pi.wiringPiSetupGpio()
pi.pinMode(SW_PIN,pi.INPUT)
pi.pullUpDnControl(SW_PIN,pi.PUD_DOWN)
while True:
if(pi.digitalRead(SW_PIN)==pi.HIGH):
if(swi==0):
print("Switch ON")
str = datetime.datetime.now().strftime('%H:%M:%S')
payload = {'value1':str}
requests.post(url,data=payload)
swi=1
else:
print("Switch OFF")
swi=0
time.sleep(0.01)
Holds the switch on and off in swi
, fetches the time when it changes from 0 to 1, and skips it to the webhook destination.
payload = {'value1':str}
requests.post(url,data=payload)
You can skip the time data by doing. Post by pressing a button
It takes a long time to send POST, and even if I hit it repeatedly during sending, it doesn't work. I want to hit more
I want to thread only the transmitter and make it asynchronous. When I looked it up, Python had a function to create subthreads, but I'm not sure, so I decided to move only the transmitter in the background and it was solved.
The problem was solved by dividing the transmitter into nyaan.py
and rewriting it as ʻos.system ('sudo python3 nyaan.py &')`.
With this, even if you hit the Nyan button repeatedly, you can Nyan at the shortest interval of 1 second. Ideal.
It's a bit disappointing because you'll be running sudo python3 nyaan.py &
every time.
I want to compile it, but I'm sleepy so I'll do it again.
Since the momentum of repeated hits has increased, it corresponds to the millisecond display. In the specifications so far, one tweet per second was the limit (duplicate tweets)
now = datetime.datetime.now()
str = now.strftime('%H:%M:%S.') + "%03d" % (now.microsecond // 1000)
Difficult Progura Mingu I don't really understand http://shohu.hatenablog.com/entry/20081117/1226936130 I just moved here.
Theoretically, you can tweet 1000 times a second without any problem.
Since then, there have been various things, and it has become multi-threaded and sound comes out from the speakers.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading,tweepy,datetime,kjfsm4,wiringpi as pi,time
import pygame.mixer
SW_PIN = 14
swi=0
pi.wiringPiSetupGpio()
pi.pinMode(SW_PIN,pi.INPUT)
pi.pullUpDnControl(SW_PIN,pi.PUD_DOWN)
auth = tweepy.OAuthHandler(kjfsm4.consumer_key,kjfsm4.consumer_secret)
auth.set_access_token(kjfsm4.access_token,kjfsm4.access_secret)
api = tweepy.API(auth)
pygame.mixer.init()
pygame.mixer.music.load('nyan.mp3')
def tweet():
pygame.mixer.music.play(1)
print("tweet")
now = datetime.datetime.now()
str = now.strftime('%H:%M:%S.') + "%03d" % (now.microsecond // 1000)
print (str)
str = 'Nyan'+str
api.update_status(str)
while True:
if(pi.digitalRead(SW_PIN)==pi.HIGH):
if(swi==0):
th_me = threading.Thread(target=tweet,name="th_me")
th_me.start()
swi=1
else:
swi=0
time.sleep(0.01)
Recommended Posts