TicTacToe

This is a C++ tictactoe game that I’ve been working on, it should compile on Windows, Linux, or OS X. You can grab the code from the svn by running

svn co svn://4ndr3w.me/projects/tictactoe

or by going to http://code.4ndr3w.me/trac/projects/browser/tictactoe

After you have it downloaded just run ./build.sh and it will compile! For Windows I tested it using Dev-C++, but it should also work in Visual Studio.

Enjoy!

Update: The SVN is down because of the server moving to a virtual machine in ESXi. This message will be removed once I get around to getting the general projects repository up again.

Checking for School Delays with Python, PHP, and the Arduino

School Delay Checker

Most people watch the news or go to their school’s site to check for school closings, but that requires getting out of bed, turning on the TV, etc. After looking at my school’s site I noticed when they post a closing/delay, it’s in a div with the id “breakingnews”. One Google search later, and I found the simple_html_dom parser for HTML parsing in PHP, then just whipped up this bit of code to detect a closing/delay.

  1. <?php
  2. include(‘simple_html_dom.php’);
  3.  
  4. function checkschool()
  5. {
  6.         $html = file_get_html(‘http://www.example.com/’);
  7.  
  8.         foreach($html->find(‘div’) as $element)
  9.         {
  10.                         if ( $element->id == "breakingnews")
  11.                         {
  12.                                 if ( strstr(strtolower($element->plaintext), "one hour") )
  13.                                 {
  14.                                         return 1; // One hour delay
  15.                                 }
  16.                                 else if ( strstr(strtolower($element->plaintext), "two hour") )
  17.                                 {
  18.                                         return 2; // Two Hour Delay
  19.                                 }
  20.                                 else if ( strstr(strtolower($element->plaintext), "closed") )
  21.                                 {
  22.                                         return 3; // Closed Delay
  23.                                 }
  24.                                 else
  25.                                 {
  26.                                         return 4; // Nothing
  27.                                 }
  28.                         }
  29.         }
  30.         return 4;
  31. }
  32.  
  33.  
  34. echo checkschool();
  35.  
  36. ?>

So, that code will make the text a number. Then we need a way to visually look at that data, I used a Arduino attached to a servo to point to Full Day, One hour, two hour, or closed. ( I’ll post a picture of my setup soon ). To get that data to the Arduino, I used a Python script with the PySerial module. The code looks like this:

  1. import serial
  2. import os
  3. import time
  4.  
  5. # Windows users: change this to the COM port of the Arduino, plus download Python, PHP, and PySerial
  6. # Mac users: will need to change this to /dev/tty.usbserial-<long string of characters> You can find the Arduino’s serial device by doing "ls /dev | grep tty.usbserial" in Terminal
  7. # Mac users (continued): PHP and Python should be installed as part of OS X. However you will need to install PySerial. Just download and run setup.py.
  8. # Linux users: This should work as-is, but you may need to change the serial device string below depending on your distro.
  9. ser = serial.Serial("/dev/ttyUSB0", 9600);
  10.  
  11. while 1:
  12.         # Windows users: Change the "php check.php" to the path to your PHP install. ex: "C:\php\php.exe check.php"
  13.         data = os.popen("php check.php");
  14.         ser.write(data.read());
  15.         time.sleep(60);

Then that just sends the data from check.php to this program running on the Arduino:

  1. #include <Servo.h>
  2.  
  3. Servo servo;
  4.  
  5. void setup()
  6. {
  7.   servo.attach(9);
  8.   Serial.begin(9600);
  9. }
  10.  
  11. void loop()
  12. {
  13.   if ( Serial.available() )
  14.   {
  15.     switch(Serial.read())
  16.     {
  17.       case ’1′:
  18.       // One Hour
  19.       servo.write(60);
  20.       break;
  21.      
  22.       case ’2′:
  23.       // Two Hour
  24.       servo.write(120);
  25.       break;
  26.      
  27.       case ’3′:
  28.       // Closed
  29.       servo.write(180);
  30.       break;
  31.      
  32.       case ’4′:
  33.       // Nothing
  34.       servo.write(0);
  35.       break;
  36.      
  37.       default:
  38.       return;
  39.       break;
  40.     }
  41.   }
  42. }

This code was tested on Ubuntu, but by modifying the serial device string and downloading PHP, Python, and PySerial, it should work on Windows. If you’re running it on a debian-based OS ( like Ubuntu ) just install python, php5-cli and python-serial. Then run the python script and it will check every minute.
Note: You’ll need to edit the PHP script to work for your school district.

Before running: You should use the code in this zip file, otherwise the indents of the Python file won’t be right, and you’ll get an error. Click to Download If you have trouble, just read some of the comments in daemon.py.
Verified to on OS X and Linux, also will run with some edits on Windows.