custom sensor with 4 inputs Topic is solved

Moderator: leecollings

Post Reply
adeibiza
Posts: 27
Joined: Friday 19 October 2018 18:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

custom sensor with 4 inputs

Post by adeibiza »

i have an ec probe which gives me conductivity, ppm, salinity & specific gravity

i can't see a way of sending all 4 to one dummy sensor ?

i could set up 4 custom sensors & then split the data in the one script and send 4 times but that seems inefficiant
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: custom sensor with 4 inputs

Post by waltervl »

No, unfortunately not possible.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
peterbos
Posts: 79
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: custom sensor with 4 inputs

Post by peterbos »

With a text device instead of a sensor device and a script that puts all 4 values in the text field of that text device?

Peter
adeibiza
Posts: 27
Joined: Friday 19 October 2018 18:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom sensor with 4 inputs

Post by adeibiza »

an idea, but i ended up with 4 custom sensors in the end & some copying and pasting ;)

Code: Select all

            ecvalue1 =values[0]
            ecvalue2 =values[1]
            ecvalue3 =values[2]
            ecvalue4 =values[3]

            cmd1 = url_json1  + str(idx1) + "&nvalue=0&svalue="+ecvalue1+";"
            print (cmd1)
            hf1 = urllib2.urlopen(cmd1)
            hf1.close
            time.sleep(1)

            cmd2 = url_json2  + str(idx2) + "&nvalue=0&svalue="+ecvalue2+";"
            print (cmd2)
            hf2 = urllib2.urlopen(cmd2)
            hf2.close
            time.sleep(1)

            cmd3 = url_json3  + str(idx3) + "&nvalue=0&svalue="+ecvalue3+";"
            print (cmd3)
            hf3 = urllib2.urlopen(cmd3)
            hf3.close
            time.sleep(1)          

            cmd4 = url_json4  + str(idx4) + "&nvalue=0&svalue="+ecvalue4+";"
            print (cmd4)
            hf24 = urllib2.urlopen(cmd4)
            hf4.close
            time.sleep(1)  
peterbos
Posts: 79
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: custom sensor with 4 inputs

Post by peterbos »

Hi,

This is just a snippet. In which of the script languages? In dzVents your problem can be solved, I think. The other languages I don't know or don't use.

Peter
adeibiza
Posts: 27
Joined: Friday 19 October 2018 18:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom sensor with 4 inputs

Post by adeibiza »

the full code for you, python2.7 on a raspberry pi using an atlas scientific K.1 ec probe :)

Code: Select all

import io # used to create file streams
import fcntl # used to access I2C parameters like addresses
import time
import string # helps parse strings
import datetime as dt

import urllib2



idx1 = 19
url_json1 = "http://192.168.1.170:8080/json.htm?type=command&param=udevice&idx=" 

idx2 = 20
url_json2 = "http://192.168.1.170:8080/json.htm?type=command&param=udevice&idx=" 

idx3 = 21
url_json3 = "http://192.168.1.170:8080/json.htm?type=command&param=udevice&idx="

idx4 = 22
url_json4 = "http://192.168.1.170:8080/json.htm?type=command&param=udevice&idx="



class atlas_i2c:
    long_timeout = 3 # the timeout needed to query readings and calibrations
    short_timeout = .5 # timeout for regular commands
    default_bus = 1 # the default bus for I2C on the newer Raspberry Pis, certain older boards use bus 0
    default_address = 100 # the default address for the pH sensor

    def __init__(self, address = default_address, bus = default_bus):
        # open two file streams, one for reading and one for writing
        # the specific I2C channel is selected with bus
        # it is usually 1, except for older revisions where its 0
        # wb and rb indicate binary read and write
        self.file_read = io.open("/dev/i2c-"+str(bus), "rb", buffering = 0)
        self.file_write = io.open("/dev/i2c-"+str(bus), "wb", buffering = 0)

        # initializes I2C to either a user specified or default address
        self.set_i2c_address(address)

    def set_i2c_address(self, addr):
        # set the I2C communications to the slave specified by the address
        # The commands for I2C dev using the ioctl functions are specified in
        # the i2c-dev.h file from i2c-tools
        I2C_SLAVE = 0x703
        fcntl.ioctl(self.file_read, I2C_SLAVE, addr)
        fcntl.ioctl(self.file_write, I2C_SLAVE, addr)

    def write(self, string):
        # appends the null character and sends the string over I2C
        string += "\00"
        self.file_write.write(string)

    def read(self, num_of_bytes = 31):
        
        current= dt.datetime.now().strftime('%m%d%H%M')
        current2 =dt.datetime.now().strftime('%d/%m %H:%M:%S')



        # reads a specified number of bytes from I2C, then parses and displays the result
        res = self.file_read.read(num_of_bytes) # read from the board
        response = [x for x in res if x != '\x00'] # remove the null characters to get the response
        if(ord(response[0]) == 1): # if the response isnt an error
            char_list = [chr(ord(x) & ~0x80) for x in list(response[1:])] # change MSB to 0 for all received characters except the first and get a list of characters
            # NOTE: having to change the MSB to 0 is a glitch in the raspberry pi, and you shouldn't have to do this!
            

            
            ecvalue = ''.join(char_list)
            print(ecvalue)
            values = ecvalue.split(",")
            print(values[0]) # output: 406700
            print(values[1]) 
            print(values[2]) 
            print(values[3]) 

            ecvalue1 =values[0]
            ecvalue2 =values[1]
            ecvalue3 =values[2]
            ecvalue4 =values[3]




            cmd1 = url_json1  + str(idx1) + "&nvalue=0&svalue="+ecvalue1+";"
            print (cmd1)
            hf1 = urllib2.urlopen(cmd1)
            hf1.close
            time.sleep(1)

            cmd2 = url_json2  + str(idx2) + "&nvalue=0&svalue="+ecvalue2+";"
            print (cmd2)
            hf2 = urllib2.urlopen(cmd2)
            hf2.close
            time.sleep(1)

            cmd3 = url_json3  + str(idx3) + "&nvalue=0&svalue="+ecvalue3+";"
            print (cmd3)
            hf3 = urllib2.urlopen(cmd3)
            hf3.close
            time.sleep(1)          

            cmd4 = url_json4  + str(idx4) + "&nvalue=0&svalue="+ecvalue4+";"
            print (cmd4)
            hf24 = urllib2.urlopen(cmd4)
            hf4.close
            time.sleep(1)            



        else:
            return "Error " + str(ord(response[0]))

    def query(self, string):
        # write a command to the board, wait the correct timeout, and read the response
        self.write(string)

        # the read and calibration commands require a longer timeout
        if((string.upper().startswith("R")) or
           (string.upper().startswith("CAL"))):
            time.sleep(self.long_timeout)
        elif((string.upper().startswith("SLEEP"))):
            return "sleep mode"
        else:
            time.sleep(self.short_timeout)

        return self.read()

    def close(self):
        self.file_read.close()
        self.file_write.close()

    def domo():

    	print(("loop")+''.join(char_list))
    	return



def main():
    device = atlas_i2c() # creates the I2C port object, specify the address or bus if necessary

    i=0
    timestamp = 0
    output = "0.00"
    phvalue = 7

    while True:

        if (time.time() > timestamp + 10):
			
            try:

               output = device.query("R")
               #print(output);
            except IOError:
               print("Query failed")

            timestamp = time.time()
            i = i+1

if __name__ == '__main__':
    main()
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: custom sensor with 4 inputs

Post by gizmocuz »

Just send it to 4 different sensors and be done with it (yes it might take you 5 minutes more coding)
Quality outlives Quantity!
peterbos
Posts: 79
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: custom sensor with 4 inputs

Post by peterbos »

I suppose the four values are ecvalue1 to ecvalue4. Why don't you make a string like

Code: Select all

str = "Value 1 = " + ecvalue1 + "\nValue 2 = " + ecvalue2 + "\nValue 3 = " + ecvalue3 + "\nValue 4 = " + ecvalue4
(assuming \n is a new line just as in dzVents)
and write that string to a text device:

Code: Select all

cmd1 = "http://192.168.1.170:8080/json.htm?type=command&param=udevice&idx="  + str(idxTxtdevice) + "&nvalue=0&svalue="+str+";"
Peter
adeibiza
Posts: 27
Joined: Friday 19 October 2018 18:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: custom sensor with 4 inputs

Post by adeibiza »

because i'm using a time series graph on grafana :)
2023-05-18_20-35-01.png
2023-05-18_20-35-01.png (105.89 KiB) Viewed 2501 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest