Wednesday, October 21, 2015

Pebble Time Watch - Shutting Down a Server

Here's my story: I have this server computer that I turn on every morning. It runs some basic server stuff, syncs my FitBit to the internet and charges my phone. I turn it off before I leave to school or work in the morning because it cannot be accessed from outside my LAN network and would otherwise waste energy with no one there to use it.

Here's my problem: To turn it off, I normally have to SSH into the server and run the command "sudo poweroff". This normally takes longer than I would like, especially if I happen to be running late.

Here's my solution: Make an app for my Pebble Time smart watch that could turn my server off easily.

My Implementation: I decided the best way to do this was to take advantage of the PHP part of the web server, running Apache 2 on an Ubuntu Server 14.04 box. I could make a web page that runs the Linux shutdown command and replies something through HTML. Then I could make an app for the Pebble Time smart watch that calls the page and displays the status.
The PHP code looks like this:
Poweroff ---
<?php
  echo(exec("sudo poweroff"));
?>

It is just a small PHP script called poweroff.php that runs the poweroff command and returns "Poweroff ---" plus whatever might be appended from the poweroff command. Running it is as simple as just calling "http://IPofServer/poweroff.php".
It's important that the Apache 2 web server is given admin permissions, otherwise it cannot run the "sudo poweroff" command. To give it admin permissions, run this command in terminal: sudo visudo and write this line to the end of the file: www-data ALL=NOPASSWD: ALL then press Ctrl+x, press y, press enter and you should be good. Type sudo service apache2 restart to restart the Apache 2 server and apply the changes, or just reboot the whole server.
The watch app itself was written using the Pebble.js method, this way I didn't have to waste a lot of time making it in C, but I might port it later. The code is very simple:
var UI = require("ui");
var ajax = require("ajax");


var card = new UI.Card({ //init the card
  title: "Shutdown",
  subtitle: "USERV2",
  body: "LOADING..."
});
 
card.show(); //show the card
 
ajax( //send the shutdown request
  {url: "http://IPofServer/poweroff.php"},
  function(data){ //success function
    card.body("STATUS: "+data);
  },
  function(error){ //error function
    card.body("ERROR - Cannot Reach Server: "+error);
  }
);


How it works: The app just initializes a card screen and all the parameters are hard coded, but I'm planning on changing that in a possible future update. The app then sends an AJAX request to my server (where it says IPofServer, change to an actual IP) and calls the shutdown page. The shutdown page return some text, in my case "Poweroff ---", and proceeds to send the shutdown command to the system. The watch receives the output of the server and replaces the "LOADING..." (on the image) with "STATUS: Poweroff ---". If it cannot reach the server or something goes wrong, then the watch says "LOADING..." for a while and then returns "ERROR - Cannot Reach Server: " with any errors appended to it. Once you see "Poweroff ---" then you know the server is shutting down and you can press the back button on your Pebble and proceed with whatever you were doing previously.

Screenshot of emulator with shutdown app.

Friday, October 16, 2015

Sale on GearBest.com!!!

Hey guys, there's a sale happening on GearBest.com right now: http://www.gearbest.com/promotion-electrical-amp-tools-special-233.html

In case you happen to miss it, just remember that GearBest.com always has cool stuff at rock bottom prices! And keep an eye out for any possible sales, they are somewhat frequent.

Thursday, August 20, 2015

Simple Python Command Line Calculator

Hello world, today I have something that is actually useful :p

So I commonly do a lot of small math, but I also prefer to use the command line to do everything... not sure why though. I later found out that there was a similar utility called bc, but it doesn't seem as functional as I would need it to be.

Anyway, I wrote a tiny Python script that is capable of doing everything I need it to... and more. Essentially it's a Python command line interface that is geared towards math. You type in a mathematical function, and it spits out the answer. It also saves the answer to a variable called "a" for answer. You can use that variable in your math normally in case you need the previous answer for your next calculation. You can also define your own variables just like "b = 5+3" or something, and still use them for calculations.

One of the biggest features, is the ability to use the Python interface instead of just doing math. Say you want to exit, all you have to do is type in "exit()" just as you would to exit a normal Python shell. Anything that works in a normal Python shell should work in this one also. But it's geared more for speed of calculations than running Python functions.

*Note* It is recommended you have experience with Python to use this as the math functions might not be so intuitive for someone who's never used Python before. Everything is calculated as an integer unless it becomes a float (by adding ".0" at the end of a number); 25/4 comes out as 6, but 25.0/4 comes out as 6.25. You can use the traditional math functions like cos() and sin() and sqrt() and all those functions that are found in the Python math library.

I didn't attach code because those 7 lines in the picture below are all that's needed and it shouldn't take more than a minute to copy it. The terminal shows how things look/work.


Monday, August 3, 2015

Present from China!

Hello world, today a package from The People's Republic of China came in the mail for me.


A few weeks ago, a representative from GearBest.com reached out to me regarding a generous offer. I was told that I could receive free items in exchange for documenting a project and giving a review. I accepted, chose my products and waited for the package to arrive. It finally came, I was pretty happy.

Inside was an Arduino Nano clone, a DHT22 temperature/humidity sensor, a 16x2 LCD module, a 433 MHz transmitter and receiver, and an MB102 breadboard power supply. In total, it cost $21.08, an extremely generous amount for such a simple task.

I'm planning on making a clock type thing that also displays temperature and humidity. Afterwards I might make an autonomous robot with the Clocky robot!

These things will be documented on Instructables.com

Second Week at New Job

Hello world, today I finished my first day of the second week of my new job. I got hired as a programmer at J2 Innovations on July 23.
I like my new job a lot! I get to sit in a chair and write small scripts for eight hours a day! :p
I plan to work here for a while, until I'm at least out of university. This'll help to pay for that.
I'll let you guys know how that goes in a while.

Friday, July 17, 2015

Reading a Wikipedia Dump

So I wanted to embark on a journey to find out if I could make a natural language processor by analysing how grammar works, but I needed some examples. I needed to see how sentences were put together in terms of the parts of speech and also see which words can be removed from a sentence without altering the meaning but making it easier to process. I figured Wikipedia has the largest source of text and context that I could simply download and scan to answer my questions. Problem is, the Wikipedia Dump is encoded in XML so that it can be easily parsed and used offline, but is not what I am looking for to analyse the language.

My solution to the problem was to parse the Wikipedia dump myself and turn each page into its own file. In order to do the initial testing, I decided to use a dump for Simple Wiki since it's quite a few gigabytes less. I also wrote a small python script to go through the XML file at intervals of 1000 characters at a time. This allowed me to get some insight into how the XML file was structured.

Between the <page> and </page> tags was the page which included the title and sections and subsections and references and all those things. The title of each page was found between the <title> and </title> tags. The main text, which houses relevant information, is kept between the <text> and </text> tags. Words surrounded by three single quotes (''' ''') appeared to be what signalled bolded words. Words between two sets of square brackets ([[ ]]) are links to other pages. Sections and subsections are signalled by words between two (== ==) and three (=== ===) equal signs consecutively. The asterisk symbol (*) indicated a bullet point. And it appeared that {{ndash}} was simply a dash (-).

Knowing all this, I was able to construct a program that made Python list of every page in the dump and then made a text file with the title of the page title and the content of the page text.

Turns out some of the text files only contained text that was only one file and described a redirect. Otherwise those files were empty, so I had to write a little Python script that went through and removed all of those, leaving only files with actually useful text.

Otherwise, I had to go through all the files individually and remove files with names in foreign languages and names of things that aren't useful for my purposes... 140,000+ text files... it's fun...

Anyway, the code for the program that got each page and turned it into a text file is at the far bottom and the general algorithm is below this block of text. The program was designed to parse the entire 400+ MB simplewiki file without loading it all into memory... not sure why... made sense at the time. But it scans through the whole file line by line to find the page, title and text tags. Just remember to actually download the Wiki dump if you want to try this, use SimpleWiki since it's quite a few gigabytes less than the actual EnWiki dump.

------------------------------

open simplewiki.xml
set text variable to " "
while lines haven't run out{
    page content = ""
    while you haven't found the <page> tag{
        text variable = readline()
    }
    while you haven't hit the </page> tag{
        add the current line to the page text variable
    }
    set a title variable equal to the page title by searching between <title> tags
    open a file for writing with the title variable's title
    set a text variable equal to the  text between <text> tags
    write that text to the file
    close the file}
close the simplewiki.xml file

------------------------------

#!/usr/bin/env python
f = open("simplewiki.xml")
x = " "
while x != "": #while still reading
    page = "" #hold page
    while x.find("<page") == -1: #skip through lines until find page start
        x = f.readline()
        if x == "": break
    while x.find("</page") == -1: #get lines between <page> and </page>
        x = f.readline()
        page += x
        if x == "": break
    title = page[page.find("<title")+7:page.find("</title")].replace("/","-") #get title
    g = open(title+".txt","w") #open file with title
    text = page[page.find("<text"):page.find("</text")]
    g.write(text)
    g.close()
f.close()
print "Done" #give confirmation of finishing

------------------------------

Here are pictures. This first one is a screenshot of the finished code in my favorite text editor: Gedit. I also use Ninja-IDE in extreme cases where I'm working on complicated projects.

This second picture is a picture of all of my processed text files. Since there are so many, I actually sorted them out based on the letter they start with and put them in folders. All the text files you can see have weird/foreign characters at the start and have been sorted manually after this picture was taken. But there you go, I now house every topic of SimpleWiki on my computer in text file format... come at me internet blackout.

Wednesday, July 15, 2015

Gravity Visualizer

Hello world, today I will be sharing a little project I have been working on involving physics, C++ and a crazy question.

A few months ago, I was talking to a friend about the destruction of Earth through a huge meteor impact and I had a crazy idea. What if a meteor shot through Earth and split the earth perfectly in half so you had two hemispheres, had the two parts moved away due to the energy of the impact and then had the two parts attract each other and collide; what would be the energy and destruction level of something like that? Now I know it sounds crazy, but it's a purely theoretical question that could possibly be simulated.

In order to begin to answer my question, I had to know how non-spherical objects attract each other and I figured a square would be a pretty easy shape to experiment with. I am working with 2D shapes because I don't know how to program 3D graphics and the math for it would get pretty messy.

Initially I just wanted to get a shape, have a small particle circle around it and collect information regarding the force exerted by the shape; all simulated of course.

The first version was coded in Python using the PyGame graphics library since I was already familiar with it and Python is cross-compatible with the Linux computers I use at home and the Windows computers I used in the classes that I developed the first version in.

Below is an image of the square I used in the first version. The square is 50x50 pixels, each being 1 mass unit for a total of 2500 mass units. Forces ≥20 are a light blue, forces between 10 and 20 are yellow, forces between 4 and 10 are a hot pink(?), forces between 3 and 4 are red, forces between 2 and 3 are blue and forces between 1 and 2 are green.
This helped solve part of the question, shapes have gravity fields that are roughly the same shape at very close distances, but as you get farther, the shape of the gravity field becomes spherical. Other shapes were tested and the same was true, but the distance at which it became more spherical was dependant on how odd the shape was. Below is a hemisphere, it has a mostly spherical gravity field, but it is just a bit lopsided.


This was all well and good for a while, but the output looked horrible and took forever to render in Python. So, about two days ago, I wrote it in C++ and made a few changes. I made it so instead of scanning in a circular pattern, it would just calculate the force present in every pixel of the 600x600 space. It would also represent the colors as a modulus function of 256 to retain a grayscale color between 0 and 255, as dictated by most RGB colors; this means that once the color surpasses 255, it starts again at 0, represented as the alternating rings. The result of the new output is shown below.
Below are more shapes which have had their gravity fields calculated, notice how the fields become more or less spherical as they go farther.



For the more code-savy people out there, I will post the general algorithm for the C++ gravity visualizer:

make an empty array for the coordinates and mass of the points
add points to the array corresponding to the shape you want
calculate the center of mass and move the shape so center is at origin
draw your shape on the screen
for every x pixel{
    for every y pixel{
        set force2 to 0
        for every point{
            calculate distance of pixel and point
            set force = (gconst * point mass) / distance
            set force2 = force2 + force
        }
        set pixel intensity to force2 % 256
    }
    update screen
}
draw your shape on the screen
save screenshot

The source code can be found here.

Tuesday, July 14, 2015

It has begun...

Hello world, today I am beginning my blog.

This blog will mostly be about my adventures in writing software and designing hardware in an attempt to spread ideas around. This blog will also be used as somewhat of a portfolio to document my projects in computer science. I will also be occasionally exploring algorithms and theory involved in programming.

A lot of my programming projects will be based around Python and C++, but there may also be a few in other languages as I do know around 8 languages fluently and have tinkered around with a few others.