Sunday, 22 January 2017

#Hackpack blog: MineGraph

How to create a real time graph using the sense hat emulator and Minecraft on Raspberry Pi


Firstly if you are just looking for the code: here it is https://raw.githubusercontent.com/ncscomputing/Hackpack/Volume-5/MinegraphSHEmuv1.py

Here is a straight copy of the code:

"""
Written by @ncscomputing v2 to work  with Pi Sense hat emulator on Pi not web based.
"""

import mcpi.minecraft as minecraft
import mcpi.block as block
import time
import random
from sense_emu import SenseHat

sense = SenseHat()

mc = minecraft.Minecraft.create()
orx,ory,orz = mc.player.getPos()

mc.postToChat("Start Graph")

blockTypeList=[35,45,3,4,5,6]
DataStreamList = []

DataStreamCount= 0

def BuildDataBlock():
    temp = int(sense.temp)
    DataStreamList.append(temp)
    orx,ory,orz = mc.player.getPos()
    block = random.choice(blockTypeList)

    for i in range (0,temp):
        x,y,z = mc.player.getPos()
        mc.setBlock(x,i,z,block)
    mc.player.setPos(orx,ory,orz+1)
    msg = "Temp currently is:",temp 
    print(msg)
        
while True:
    BuildDataBlock()
    time.sleep(4)




Here is a simple intro to how to get it to work:

  • It works with the sensehat emulator which is included by default on the Raspian OS. You can find it in Menu > Programming > Sense Emu.
  • Once that is loaded open Minecraft Pi which can be accessed Menu > Games > Minecraft
  • Create a new world and then using the space bar position the character up in the sky. If you press space down for about 5-6 seconds that should be high enough.
  • Open Python 3 Menu > Programming > Python3 and open the Python Code above and press F5
  • The code should then run.
  • It should start drawing bars as above. If you are not happy turn it off and retry. The temp is adjustable by the SenseHatEmu interface their is a temp slider adjust it up or down accordingly.
  • N.B. The code is not perfect but it does work last time I tested.
Enjoy :) Over

Chris Penn

No comments:

Post a Comment