mypifi
Posts: 131
Joined: Sun Oct 21, 2012 1:28 pm
Location: oxford
Contact: Website

having issues with my script.

Sat Jul 28, 2018 7:10 pm

Hi all, I have spent the past week trying to get this script to work, its for an amazon dash button which once activates tweets a colour with the cheerlights hashtag and changes the cheerlights.

So far it works, till you try a second time and it wont pick a different random colour, now running out of ideas, adding time to the tweet could solve the tweet duplication issue but it wont solve why i cant get a new colour everytime it detects the dashbutton.

here is what I have so far:

Code: Select all

#!/usr/bin/env python

import socket
import struct
import binascii
import datetime
from time import sleep
from random import choice
import tweepy

consumer_key        = 'enter here'
consumer_secret     = 'enter here'
access_token        = 'enter here'
access_token_secret = 'enter here'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

message = ['UB40 Red Red wine #cheerlights',
           'Orange origina #cheerlights',
           'Lets follow the yellow brick road #cheerlights',
           'Going rather green #cheerlights',
           'Look at the blue sky #cheerlights',
           'some wonderful violet flowers #cheerlights',
           'white like a ghost #cheerlights',
           'Cyan cat is coming #cheerlights',
           'my face is going purple #cheerlights',
           'magenta, what kinda colour is that #cheerlights',
           'my nan had oldlace #cheerlights',
           'tickle me pink #cheerlights',
           'warmwhite is that such a colour? #cheerlights',]

my_message = choice(message)

rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
                          socket.htons(0x0003))
MAC = 'dashmac here'

while True:
    packet = rawSocket.recvfrom(2048)

 ethernet_header = packet[0][0:14]
    ethernet_detailed = struct.unpack('!6s6s2s', ethernet_header)

    arp_header = packet[0][14:42]
    arp_detailed = struct.unpack('2s2s1s1s2s6s4s6s4s', arp_header)

    # skip non-ARP packets
    ethertype = ethernet_detailed[2]
    if ethertype != '\x08\x06':
        continue

    source_mac = binascii.hexlify(arp_detailed[5])
    dest_ip = socket.inet_ntoa(arp_detailed[8])

    if source_mac == MAC:
        print "Dash button pressed!, IP = " + dest_ip
        api.update_status(my_message)
        print (my_message)


many thanks in advance I guess ive missed the obvious but i just not going no where now. my code is also at https://github.com/smstext/dashbuttoncheerlights

DirkS
Posts: 10447
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: having issues with my script.

Sat Jul 28, 2018 7:41 pm

The selection is done *before* the loop, so it only selects once.

Put

Code: Select all

my_message = choice(message)
inside the while loop, e.g.

Code: Select all

while True:
    packet = rawSocket.recvfrom(2048)

    ethernet_header = packet[0][0:14]
    ethernet_detailed = struct.unpack('!6s6s2s', ethernet_header)

    arp_header = packet[0][14:42]
    arp_detailed = struct.unpack('2s2s1s1s2s6s4s6s4s', arp_header)

    # skip non-ARP packets
    ethertype = ethernet_detailed[2]
    if ethertype != '\x08\x06':
        continue

    source_mac = binascii.hexlify(arp_detailed[5])
    dest_ip = socket.inet_ntoa(arp_detailed[8])

    if source_mac == MAC:
        my_message = choice(message)
        print "Dash button pressed!, IP = " + dest_ip
        api.update_status(my_message)
        print (my_message)

mypifi
Posts: 131
Joined: Sun Oct 21, 2012 1:28 pm
Location: oxford
Contact: Website

Re: having issues with my script.

Sat Jul 28, 2018 7:55 pm

thanks for the fast reply, it was something i did look at earlier but i think i placed it a line or two further down and it didnt like it.

moving on from there im now getting after a few button presses the following:

Code: Select all

    arp_detailed = struct.unpack('2s2s1s1s2s6s4s6s4s', arp_header)
struct.error: unpack requires a string argument of length 28

thanks again in advance.

DirkS
Posts: 10447
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: having issues with my script.

Sat Jul 28, 2018 8:03 pm

mypifi wrote:
Sat Jul 28, 2018 7:55 pm
moving on from there im now getting after a few button presses the following:

Code: Select all

    arp_detailed = struct.unpack('2s2s1s1s2s6s4s6s4s', arp_header)
struct.error: unpack requires a string argument of length 28
Looks like there's something unexpected coming back. You could try to add a print statement to see what it is.
Maybe add a try / except around that statement to deal with (e.g. ignore the button press)

Return to “Python”