Energenie

#!/usr/bin/env python
import cayenne.client
import time
import logging
from energenie import switch_on, switch_off

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""


# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.
    
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

while True:
    client.loop()
    
    if (time.time() > timestamp + 10):
        client.celsiusWrite(1, i)
        client.luxWrite(2, i*10)
        client.hectoPascalWrite(3, i+800)
        timestamp = time.time()
def on_message(message):
    print("message received: " + str(message))
    if (message.channel == 4) and (message.value == 1):
        switch_on(1)
    else:
        switch_on(0)




i = i+1

why are you having two on_message function?

Ah yes. I was only using one and that was the last one.
The other must have been an error i made when moving that block around.

so what is the issue you are facing? did you try the energenie code without cayenne code to check whether it is working or not?

Yes. The energenie code works fine but when i included it in the other code it didnt respond to the value changing from 0 to 1 ,I can see that value change but it doesnt pick that up.
.Thanks

can you share the code you are using (the correct one with only one on message function)

Thanks for your assistance.
Ive found that since running the code it makes the raspberry pi go offline.
Bit frustrating ,so I think i will find a diferent software to run my project on.
Yours looks very good but maybe not for DIY people.

Thanks agin

i dont think the reason you gave for shifting to a different software is not quite justifying.
you are free to move to a different software and all the best for your journey.
before that let me give the reason why the your code is not working: you had two on_message function in your code and when you run the cayenne code (which is an excellent code for people who knows python) you call the first on_message function and this function you have not included the energenie code. so the 2nd function function never gets executed.
And for you kind information, we will shortly add energenie as a plugin support. So users like you who are struggling with coding can directly add their energenie device to pi agent without need of any code.

1 Like