This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
legionlabs's profile picture

COSM updater for CC

Started by legionlabs, 04 January 2013 - 09:00 PM
legionlabs #1
Posted 04 January 2013 - 10:00 PM
I wrote a primitive python server/script that lets you send/receive data from a CC computer to COSM, and also your computer's serial port. COSM is an online data feed service for internet enabled devices.

It works by taking the URL from a HTTP GET request and cutting out the main server directory. For example, if you request www.example.com/i101 it will send the data "101" to the COSM feed using your server at example.com.

This lets you easily control "things" inside minecraft from anything connected to the internet: PCs, cellphones, hacked routers (OPENWRT), even that 'arduino' thing (I program in ASM, but I hear it works with COSM). Big red buttons sound fun.

You can also control things outside minecraft from inside the game. Want to get an SMS when your powerplant is heating up? Control blinkenlights? Just requires a little engineering.

Oh, and license = public domain. You can do anything with it except claim you wrote it.

To set it up:

Download Python 2.7.
Use IDLE to open the script.
Add your COSM username, password, feed, and API key. If you don't have one, register for one.
Add the COM port for your serial port (or /dev/tty for linux, see pyserial docs)
Set your serial baud rate
Change the port (default 4242) if you like
Run it
Install dependencies if it doesn't run (google is your friend, or use Linux)

Python dependencies are:
BaseHTTPServer
cgi, sys
eeml
time
csv
urllib2
serial
decimal

If you use windows, I think you'll need:
lxml (2.3 or higher)
libxml2
libxslt

Here is how to use it:

send a HTTP GET with http://[server IP:port number]/[command][data]

–>Server IP is the IP address of the server running the script. If it's the same machine that is hosting minecraft, you can use 127.0.0.1
–>Port number is the port the script is configured to use, default is 4242
–>Commands are: i (input), o (output), s (serial port send), r (serial port receive). COSM commands have a latency of around 8 seconds. Serial commands are faster.
–>The data field is what you're actually performing the command on. Valid data is different for each command:

Input: Enter the address to read from on COSM. For example, for address 42…. http://127.0.0.1:4242/i42
Output: Enter the target address, followed by the data… http://127.0.0.1:4242/o42your_data. Strings, numbers, and some punctuation is supported.
Serial send: Enter the data to send. Must be a number 0-255… http://127.0.0.1:4242/s42
Serial receive: No parameters. Converts all ASCII to numbers, so receiving '~' will get you the number 126… http://127.0.0.1:4242/r

So far I have a real life voltmeter I can read from inside minecraft.

Enjoy!
wesley009 #2
Posted 04 January 2013 - 10:30 PM
Wow that's awesome i want that so bad for my MFFS control system and doors etc.

Awesome work/Idea
legionlabs #3
Posted 04 January 2013 - 11:53 PM
OK, I guess I'll clean up the code a little and post what I've got tomorrow. It runs in Linux trivially, but if you install a couple of libraries and some python packages it can probably run in windows.

It's much easier to send data out of minecraft right now. Receiving data currently vomits XML at you, I think. I'll fix that in my script, because coding an XML parser in CC seems like a waste of processing resources, it can be fixed client-side… maybe without a parser. Everything also needs way more testing than I've done so far.

-S
bjornir90 #4
Posted 05 January 2013 - 02:45 AM
Looks so cool ! Can you list all the dependencies for it to work on Windows please ?
legionlabs #5
Posted 05 January 2013 - 04:57 AM
Well, you asked for it… to get it running in windows 7 (may work in other versions):

First, back up important files. I don't think these are risky instructions, but it's a good idea anyway.

You'll need libxml2 and libxslt binaries for windows (and configured to be in your PATH), python-lxml, python-eeml, and python-time.

You'll need to install python 2.7+IDLE, but the verion of lxml for python 2.3 ($easy_install lxml==2.3). For eeml, let easy_install pick the best version. While you're changing your OS PATH variables, you may as well add python there too.

I suggest navigating to Python27/scripts and using easy_install from command line for these and any other missing Python packages.

I got it working, but your mileage may vary, and I may have missed dependencies that I already had installed.

Long story short: I thought "Dependency Hell" was bad in linux… until I tried to develop software in windows.
legionlabs #6
Posted 05 January 2013 - 05:00 AM
Well, I was bored and never sleep so here is the proof of concept code. This is now an old version, but perhaps easier to read than the finished version.

I logged in to a local minecraft server and wrote this short program copied from the computercraft example code for HTTP GET:

local sExample = http.get("http://192.168.0.102:4242/data_sent_from_minecraft") –Get contents of pagewrite
(sExample.readAll()) –Read and print contents of pages
Example.close() –Just in case

That IP address is the local address of another computer on my network running the server. Running it locally (127.0.0.1 as IP) works fine too.
legionlabs #7
Posted 05 January 2013 - 03:49 PM
I'm tentatively going for a data format of [2 digits for datapoint number][arbitrary amount of data].

So if you HTTP GET www.example.com/i07, it will look up datapoint 07 on COSM and return it's value to CC.

If you HTTP GET www.example.com/o7342, it will write the value '42' to datapoint 73

Is 100 datapoints (addresses) enough? How much cloud storage do you need? It's not hard for me to make it 1000 or 10,000 addresses at this point (though it makes usage more cumbersome I think). I suppose this can be used to send data from one CC computer to another (slowly) through HTTP, though I imagine there are faster ways to do this already.

Oh also… python-csv will shortly be a dependency. If you want serial communication to hardware (blinkenlights, big red buttons, arduino), you'll probably need pyserial too… but your hardware can be on a different continent than the computer running this script so it's not really a dependency.

-S
legionlabs #8
Posted 05 January 2013 - 10:16 PM
OK, here is the less primitive version [attachment=875:server0.txt]. Sending and fetching data works, and 100 addresses are supported. It's not lightning fast… but response time is less than 10 seconds for me. This version is obsolete now, as I've updated the software to support serial access. However, I'll leave it here in case someone wants this old version without serial access

I've also attached an image of the result of reading address 42 (which contained the string 'webbrowserdata'), then writing the string 'minecraftdata' to it (which sent 16 characters… 'o42minecraftdata'), then reading the address again. It handles strings and numbers fine, some punctuation will break it (just like some punctuation breaks URLs).