#!/usr/bin/python
#
#    This program demonstrates how to convert the raw values from an accelerometer to Gs
#
#
#    Both the BerryIMUv1 and BerryIMUv2 are supported
#
#    Feel free to do whatever you like with this code
#    Distributed as-is; no warranty is given.
#
#    http://ozzmaker.com/



import time
import IMU
import paho.mqtt.client as mqtt

topic = "jumptie"

IMU.detectIMU()  # Detect if BerryIMUv1 or BerryIMUv2 is connected.
IMU.initIMU()  # Initialise the accelerometer, gyroscope and compass

def on_connect(client, userdate, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe(topic)


def on_message(client, userdate, msg):
    if (msg.topic == topic):
        payload = str(msg.payload)
        print(msg.topic + " - " + payload)

while True:
    # Read the accelerometer,gyroscope and magnetometer values
    ACCx = IMU.readACCx()
    ACCy = IMU.readACCy()
    ACCz = IMU.readACCz()

    print("##### X = %f G  #####" % ((ACCx * 0.244) / 1000)),
    print(" Y =   %fG  #####" % ((ACCy * 0.244) / 1000)),
    print(" Z =  %fG  #####" % ((ACCz * 0.244) / 1000))

    # slow program down a bit, makes the output more readable
    time.sleep(0.3)

    if ACCy < 0.3:
        print("Jump")
        client.publish(topic, "Jump")

    client = mqtt.Client()
    client.connect("192.168.0.100", 1883, 60)

    client.on_connect = on_connect
    client.on_message = on_message





