OpenSprinkler Forums Hardware Questions OpenSprinkler Starting OS from Data from Thingspeak

  • This topic has 11 replies, 3 voices, and was last updated 4 years ago by Ray.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #48774

    gt244
    Participant

    Hi All,

    I have an moisture and temp sensor setup that sends data to Thingspeak.

    I would like to have OS start when the Temp is at and certain level (This would be for frost protection when water is sprayed over trees and freezes on the buds which creates latent heat) and also when the moisture is at an certain KPA reading.

    I have all this data in ThingSpeak but how do I get OS to start when these conditions are meet?

    Any ideas much appreciated? I was thinking something with IFTTT but not sure where to start.

    Many Thanks,

    Ben

    #48775

    Mike
    Participant

    I upload soil moisture readings to ThingSpeak and use the readings to start a zone on my OS. I included the command to start the irrigation in the Python script that uploads the readings to my ThingSpeak account. The OS API covers how to use the commands. Since I don’t want to miss an irrigation cycle if my internet connection is lost, I make sure to start watering even if the upload to ThingSpeak fails.

    Mike

    #48776

    gt244
    Participant

    Hi Mike,

    Thanks for that.

    Are you able to provide an copy of the code that you are using? Or point me towards the correct place in the OS API.

    I am running an RPI that picks up data from the sensor stations. The RPI then uploads this data to thingspeak.

    So I should just be able to add the command to start the OS in my script that uploads to thingspeak.

    It will be starting an program.

    Cheers,

    Ben

    #48779

    Mike
    Participant

    If you’re starting a program, take a look at section 12. Manually Start a Program. That’s the same way I’m doing it.

    #48783

    gt244
    Participant

    Hi Mike,

    Thanks for your help.

    Still a little confused. You include the code in section 12 in your python script that uploads to Thingspeak. But how does that talk to the OS?

    Thanks,

    Ben

    #48794

    Mike
    Participant

    Ben,

    The lines of code in my Python script that turn on the irrigation look like this:

    import requests
        <snip>
        if min(SoilMoistureReadings) < WateringThreshold:
          #Manually start an OpenSprinkler program
          r = requests.get('http://127.0.0.1:8080/mp?pw=' + MD5_PW + '&pid=' + pid + '&uwt=' + uwt)
    

    You’ll need to substitute your own values in – like the IP address of your OS. Does this help?

    Mike

    #48820

    gt244
    Participant

    Cheers Mike!

    Will give that a go.

    #49138

    gt244
    Participant

    Hi Mike,

    Still having some trouble hopefully you can look at the below and see how I am going wrong. Just tacked your code on at the end.

    #!/usr/bin/python3
    
    from time import localtime, strftime
    import serial
    import time
    import thingspeak
    import requests
    
    channel_id = ()
    write_key  = ()
    
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=900)
    #ser = serial.Serial('/dev/cu.usbmodem1421', 9600, timeout=900)
    
    while True:
        line=ser.readline()
        if len(line)==0:
            print("Time Out")
            sys.exit()
        try:
            line=line.decode("utf-8")
        except:
            line=""
            print ("unreadable character received")
        
        print (line)
        print (len(line.split(",")))
    
        try:
           
            start_char = line.split(",")[0]
            write_key = line.split(",")[1]
            field_1 = line.split(",")[2]
            field_2 = line.split(",")[3]
            field_3 = line.split(",")[4]
            field_4 = line.split(",")[5]
            v_batt = (line.split(",")[6])
            temperature = line.split(",")[7]
            humidity = line.split(",")[8]
            aux = line.split(",")[9]
            stop_char = line.split(",")[10]
            
        except:
            if start_char != "$":
                print ("no start character detected")
           
    
        try:
    
            channel = thingspeak.Channel(id=channel_id,write_key=write_key)
            print (write_key, field_1, field_2, field_3, field_4, v_batt, temperature , humidity, aux)
            response = channel.update({1:field_1, 2:field_2, 3:field_3, 4:field_4, 5:v_batt, 6:temperature, 7:humidity,})
            print (strftime("%a, %d %b %Y %H:%M:%S", localtime()))
            print (response)
            
        except:        print ("connection failed",)
    
        
        if min(temperature) < 25.00:
          #Manually start an OpenSprinkler program
          r = requests.get('http://122.60.XXX.XXX:8080/mp?pw=a6d82bced638de3def1XXXXbb4983225c&pid=0&uwt=0)

    Cheers,

    Ben

    #49141

    Ray
    Keymaster

    @gt244: when you said “Still having some trouble” — can you be more specific? Does the Python script report any error? Generally you have to do a bit of debugging work to figure out what’s going wrong.

    #49159

    gt244
    Participant

    Hi Ray,

    Just need some time to get my head around it.

    I think with the min function I need an list.

    Thanks,

    Ben

    #49172

    gt244
    Participant

    Thanks all for the help it is all up and going and here is the code below if anyone else is having trouble.

    list1 = (line.split(",")[7])
        frost = str(30)
    
        if (list1 < frost):
              #Manually start an OpenSprinkler program
              r = requests.get('http://12X.60.XXX.2X5/mp?pw=a6d82bcedXXXef1e9bbXXX83225c&pid=0&uwt=0')
              print (r.text)
    #50043

    Ray
    Keymaster

    Thanks for sharing.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Starting OS from Data from Thingspeak