Byot

the bug is very bad as i am sending info to the dashboard and the dashboard keeps on disconnecting the device

  • Device & model you are using Raspberry Pi 3 B

  • What dashboard am I using? WEB, IOS & ANDRIOD

here is the code as it is for wind speed. i know it is messy but it worked fine till Friday

#################################################################################################################
#Author : Shailendra Singh                                                                                      # 
#email-id : srj0408@gamil.com                                                                                   #
#Code to convert number of clicks to wind speed								                                    #	
#Technique used here is simple 											                                        #			
#Speed = Distance / time											                                            #
#now here distance is Pie * r (where r is the radius i.e. from center of anemometer to the center of cup)	    #
#In this anemometer, one click is caused when one cup of anemometer cover half the circumference, so when	    #
#that click happen, we measure time taken 									                                    #
#and divide the pie * r / time taken 										                                    #	
#now when you use this code, please change .25 to the radius and that too in meter. 				            #
#################################################################################################################

import time
import datetime  
import RPi.GPIO as GPIO
import cayenne.client
import logging

GPIO.setmode(GPIO.BCM)  
  
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

#variable initialisations    
count = 0
count_ch1=0
count_ch2=0
count_ch3=0

timestamp = 0
wind_km_float = 0
wind_km_ch1_float = 0
wind_km_ch2_float = 0
wind_km_ch3_float = 0

#timestamp variables
s_time = time.time()
s_ch1_time = time.time()
s_ch2_time = time.time()
s_ch3_time = time.time()

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


def on_message(message):
  logger.info('started the cayenne MQTT client')
  logger.debug('Message received :%s',str(message))
  print("message received: " + str(message))

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

i=0

def mine_callback_function(channel):
    global s_time
    time_now_in_callback = time.time()
    time_diff = time_now_in_callback - s_time
    wind_speed = .25 / time_diff
    wind_float = "%3.2f"% wind_speed #we are not assuming wind speed to be more than 999 km/hour i.e. 3 digit
    #convert wind speed from meter/hour to km/hour
    wind_km = float(wind_float) * 3.6
    global wind_km_float
    wind_km_float = "%3.2f"%wind_km
    print "wind speed of channel 0 i.e. Anemometer on PIN23  in meter/hour is : {0} and in km/hour is : {1}".format(wind_float, wind_km_float)
    global count
    count = count + 1
    print "count before the time comparison  is {0}".format(count)
    s_time = time.time()
    wind_km_float = 0

def mine_callback_function_ch1(channel):
    global s_ch1_time
    time_now_in_callback_ch1 = time.time()
    time_diff_ch1 = time_now_in_callback_ch1 - s_ch1_time
    wind_speed_ch1 = .25 / time_diff_ch1 #wind speed in meter /second
    wind_float_ch1 = "%3.2f"% wind_speed_ch1 #we are not assuming wind speed to be more than 999 km/hour i.e. 3 digit
    wind_km_ch1 = float(wind_float_ch1) * 3.6
    global wind_km_ch1_float
    wind_km_ch1_float = "%3.2f"%wind_km_ch1
    print "wind speed of channel1 i.e. Anemometer on PIN24 in  meter/hour is : {0} and in km/hour is : {1}".format(wind_float_ch1, wind_km_ch1_float)
    global count_ch1
    count_ch1 = count_ch1 + 1
    print "count before the time comparison  is {0}".format(count_ch1)
    s_ch1_time = time.time()
    wind_km_ch1_float = 0

def mine_callback_function_ch2(channel):
    global s_ch2_time
    time_now_in_callback_ch2 = time.time()
    time_diff_ch2 = time_now_in_callback_ch2 - s_ch2_time
    wind_speed_ch2 = .25 / time_diff_ch2
    wind_float_ch2 = "%3.2f"% wind_speed_ch2 
    wind_km_ch2 = float(wind_float_ch2) * 3.6
    global wind_km_ch2_float
    wind_km_ch2_float = "%3.2f"%wind_km_ch2
    print "wind speed of channel2 i.e. Anemometer on PIN25 in meter/hour is : {0} and in km/hour is : {1}".format(wind_float_ch2, wind_km_ch2_float)
    global count_ch2
    count_ch2 = count_ch2 + 1
    print "count before the time comparison  is {0}".format(count_ch2)
    s_ch2_time = time.time()
    wind_km_ch2_float = 0

def mine_callback_function_ch3(channel):
    global s_ch3_time
    time_now_in_callback_ch3 = time.time()
    time_diff_ch3 = time_now_in_callback_ch3 - s_ch3_time
    wind_speed_ch3 = .25 / time_diff_ch3
    wind_float_ch3 = "%3.2f"% wind_speed_ch3
    wind_km_ch3 = float(wind_float_ch3) * 3.6
    global wind_km_ch3_float
    wind_km_ch3_float = "%3.2f"%wind_km_ch3
    print "wind speed of channel3 i.e. Anemometer on PIN22  in meter/hour is : {0} and in km/hour is : {1}".format(wind_float_ch3, wind_km_ch3_float)
    global count_ch3
    count_ch3 = count_ch3 + 1
    print "count before the time comparison  is {0}".format(count_ch3)
    s_ch3_time = time.time()
    wind_km_ch3_float = 0

GPIO.add_event_detect(23, GPIO.FALLING, callback=mine_callback_function, bouncetime=200)  
GPIO.add_event_detect(24, GPIO.FALLING, callback=mine_callback_function_ch1, bouncetime=200)  
GPIO.add_event_detect(25, GPIO.FALLING, callback=mine_callback_function_ch2, bouncetime=200)  
GPIO.add_event_detect(22, GPIO.FALLING, callback=mine_callback_function_ch3, bouncetime=200)  

def main(): 
	try:
	    while True:
		client.loop()
		global timestamp
		if (time.time() > timestamp + 10):
			print wind_km_ch3_float
			client.windspeedWrite(1, wind_km_float)
			client.windspeedch1Write(2, wind_km_ch1_float)
			client.windspeedch2Write(3, wind_km_ch2_float)
			#print "windspeed of channel 3 is {0}".format(wind_km_ch3_float)
			client.windspeedch3Write(4, wind_km_ch3_float)
			timestamp = time.time()
	except KeyboardInterrupt:  
	    GPIO.cleanup(23)       
	    GPIO.cleanup(24)      
	    GPIO.cleanup(25)      
	    GPIO.cleanup(22)      
	GPIO.cleanup()


if __name__ == "__main__":
	main()

Hi @it1,

Apologies for the delay in our response. If the code hasn’t changed at all, it’s likely not an issue there, potentially something on our server side or on your side connectivity wise?

What (if any) error messages are you seeing on the client side? How is this device connected to the internet? If you run our example python client on the same device as a test, does it maintain connection where your above-posted code does not?

Hi,

i have installed it now i will leave it for the weekend and see what happens but the connection looks stable with the sample devices

1 Like

its not running by its self i have to start it every-time is there any coding that i need to do to make it run automatically

Glad to hear the connection is stable for you at the moment!

The client software should loop indefinitely once launched once. If you’re interested in running it automatically you could set the Python script to run at Raspberry Pi boot.