Wikiwatch

From XPUB & Lens-Based wiki
Revision as of 16:51, 24 November 2011 by Eleanorg (talk | contribs)

Python script checks the PZI wiki for recent changes and writes to the Arduino's serial port. Arduino script listens for changes as recorded by the python script to the serial port, and beeps if a change has been made.


Upload this to the Arduino:

 /*
 * Make arduino beep if python script detects wiki changes
 */
int incomingByte = -1;
int ledPin = 13; 
int val = 0; 
char code[10]; 
int bytesread = 0;


void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);
  digitalWrite(ledPin, LOW);   // turns the beep off
}

void loop()        {             // run over and over again
  checkSerial();
}

void checkSerial() {
  if(Serial.available() > 0) {          // if data available 
    if((val = Serial.read()) == 1) {   // check for header start 
     
             // if 1 digit read is complete 
      doBeep();
    } 
    
  } 
  delay(50);                       // wait for a second 
}

void doBeep()
{
   digitalWrite(ledPin, HIGH);    // turns the beep on
   delay(2000);
   digitalWrite(ledPin, LOW);     // turns beep off
}

...Then run this python script:

#!/usr/bin/env python
# coding: utf-8
import urlparse, urllib2, datetime, json, time, serial
from sqlite3 import *

#set this port to the port being used by your Arduino - may vary between machines
ser = serial.Serial("/dev/ttyUSB1", 9600)


#Initate DB
connection = connect('eleanor.db')
cursor = connection.cursor()

#create tables – run once then comment out
#cursor.execute('''CREATE TABLE wiki (id integer primary key, guid integer)''')
#connection.commit()

while True:
  rssFeedUrl = "http://pzwart3.wdka.hro.nl/mediawiki/api.php?action=query&list=recentchanges&format=json&rclimit=20"
  f = urllib2.urlopen(rssFeedUrl)
  data = json.load(f)
  
  for r in data["query"]["recentchanges"]:
    rcid = r['rcid']
    type = r['type']
    
    cursor.execute("SELECT id FROM wiki WHERE guid = ?", (rcid,))
    data=cursor.fetchall()
    if len(data)==0:
      #print('There is no RCID %s'%rcid)
      print "SUPER CHANGE!!!"
      ser.write("\x01")
      
      cursor.execute("INSERT INTO wiki VALUES (NULL, ?)", (rcid,))
      connection.commit()

    else:
       print "NO CHANGE"
       ser.write("\x00")
       #print('RCID %s found with rowids %s'%(rcid,','.join(map(str,zip(*data)[0]))))

    
  time.sleep(4)