Reading analog moisture signal from Arduino over USB to RPi3 and publishing via paho MQTT This program is meant to read a moisture sensor attached to an Arduino UNO board via USB to a Raspberry Pi3 which broadcasts it over MQTT.

I am looking for any constructive criticism.

import serial
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt


def main():
    try:
        while 1:
            sendMQTT(read_sensor("clean"),"Planty/moisture_readings")
            alerts()
    except KeyboardInterrupt:
        print "\n\nExiting via Keyboard Interrupt"


def debugger():
    # This is just to test each reading
    moisture_clean = read_sensor("clean")
    output = "Soil Moisture is " + str(moisture_clean) + "%"
    moisture_raw_adjusted = read_sensor("raw_adjusted")
    output += "\nRaw_adjusted is " + str(moisture_raw_adjusted)
    moisture_raw = read_sensor("raw")
    output += "\nRaw is " + str(moisture_raw)
    sendMQTT(output, "Planty/debug")


def alerts():
    # Generates alerts that are humanly readable for certain moisture thresholds
    moisture = read_sensor("clean")
    alert_low = 10
    alert_high = 90
    alert_threshold = 30
    if moisture &lt;= alert_low:
        output = "**Moisture is below 10%**"
    if moisture &lt;= alert_threshold and moisture &gt; alert_low:
        output = "***Moisture is between 10%` and 30%, watering***"
        relay(14)
        time.sleep(60*1)
    if moisture &gt;= alert_high:
        output = "***Moisture is above 90%***"
    sendMQTT(output, "Planty/alerts")


def read_sensor(choice):
    # Read analog moisture data from moisture probe via Arduino UNO plugged into
    # the RPi via USB
    min = 330
    max = 970
    ser = serial.Serial('/dev/ttyACM0', 9600)
    #Line below handles the problem of the Arduino returning more than 1 reading
    raw_data = int(str.split(ser.readline())[0])
    if choice == "raw":
        moisture = raw_data
    #Stop readings going over or under 0 or 100%
    if raw_data &lt; min:
        raw_data = min
    if raw_data &gt; max:
        raw_data = max
    if choice == "raw_adjusted":
        moisture = raw_data
    if choice == "clean":
        #Get moisture as  % reading
        moisture = int(round(100-(100/float(max-min))*(raw_data-min), 0))
    return moisture


def relay(pin):
    # Turn on relay via PIN.HIGH
    try:
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(pin,GPIO.OUT,initial=1)
        sendMQTT("Turning Relay1 ON", "Planty/relay")
        GPIO.output(pin,0)
        time.sleep(5)
        sendMQTT("Turning Relay1 OFF", "Planty/relay")
        GPIO.output(pin,1)
    finally:
        GPIO.cleanup()


def sendMQTT(output, topic):
    # Publish to MQTT for given topic
    host = "MQTTBroker.local"
    user = "user"
    password = "password"
    mqttc = mqtt.Client()
    mqttc.username_pw_set(user, password)
    mqttc.connect(host, 1883)
    mqttc.publish(topic, output)


if __name__ == '__main__':
    main()
Here're some changes that I'd personally do:
PEP8 Guidlines

Be consistent when writing code! You used both camelCase and snake_case conventions when you declared your functions. Chose one of them and keep it that way. PEP8 recommends the second one.
Talking about consistency, you should also have a space after each #. More, when you want to write what your function is doing, you should use the following:


def read_sensor(choice):
    &quot;&quot;&quot;
    Read analog moisture data from moisture probe via Arduino UNO plugged into
    the RPi via USB
    &quot;&quot;&quot;
    # ...


After each comma (or any other operator) you should have a space
This: if moisture &lt;= alert_threshold and moisture &gt; alert_low: can be simplified using a chained expression:


if alert_low &lt; moisture &lt;= alert_threshold:
    ....


Use string format() instead of using the old + operator.
Constants should be UPPER_CASED (but not inside functions). I just took them out.

Personal anecdote:

It feels a bit weird to have the main() function at the beginning and everything within that function after. I'd just move it to the end as it will look more naturally.
You have several magic numbers which should be declared at the beginning. Doing this, you'll be able to modify their value faster than going through all the code and trying to remember what does what.
Don't store variables if you use them only once. It will make you're code harder to follow and it will speed it down (don't think of it as a big speed down but even so...)

The above will lead our code to the following:
import serial
import time
import RPi.GPIO as gpio
import paho.mqtt.client as mqtt

ALERT_LOW = 10
ALERT_HIGH = 90
ALERT_TRESHOLD = 30
RELAY_TIME = 14
TIME_TO_SLEEP = 60 * 1
MIN = 330
MAX = 970
RATE = 9600


def read_sensor(choice):
    &quot;&quot;&quot;
    Read analog moisture data from moisture probe via Arduino UNO plugged into
    the RPi via USB
    &quot;&quot;&quot;
    ser = serial.Serial('/dev/ttyACM0', RATE)

    # Line below handles the problem of the Arduino returning more than 1 reading
    raw_data = int(str.split(ser.readline())[0])
    if choice == &quot;raw&quot;:
        moisture = raw_data

    # Stop readings going over or under 0 or 100%
    if raw_data &lt; MIN:
        raw_data = MIN
    if raw_data &gt; MAX:
        raw_data = MAX
    if choice is &quot;raw_adjusted&quot;:
        moisture = raw_data
    if choice == &quot;clean&quot;:
        # Get moisture as  % reading
        moisture = int(round(100 - (100 / float(MAX - MIN)) * (raw_data - MIN), 0))

    return moisture


def send_mqtt(output, topic):
    &quot;&quot;&quot;Publish to MQTT for given topic&quot;&quot;&quot;
    host, user, password, port = &quot;MQTTBroker.local&quot;, &quot;user&quot;, &quot;password&quot;, 1883

    mqttc = mqtt.Client()
    mqttc.username_pw_set(user, password)
    mqttc.connect(host, port)
    mqttc.publish(topic, output)


def alerts():
    &quot;&quot;&quot; Generates alerts that are humanly readable for certain moisture thresholds&quot;&quot;&quot;
    moisture, output = read_sensor(&quot;clean&quot;), ''

    if moisture &lt;= ALERT_LOW:
        output = &quot;**Moisture is below 10%**&quot;
    if ALERT_LOW &lt; moisture &lt;= ALERT_TRESHOLD:
        output = &quot;***Moisture is between 10%` and 30%, watering***&quot;
        relay(RELAY_TIME)
        time.sleep(TIME_TO_SLEEP)
    if moisture &gt;= ALERT_HIGH:
        output = &quot;***Moisture is above 90%***&quot;
    send_mqtt(output, &quot;Planty/alerts&quot;)
    
    
def debugger():
    &quot;&quot;&quot;This is just to test each reading&quot;&quot;&quot;
    output = &quot;Soil Moisture is: {}%\n&quot; \
             &quot;Raw_adjusted is: {}\n&quot; \
             &quot;Raw is: {}&quot;.format(read_sensor(&quot;clean&quot;), read_sensor(&quot;raw_adjusted&quot;), read_sensor(&quot;raw&quot;))

    send_mqtt(output, &quot;Planty/debug&quot;)


def relay(pin):
    &quot;&quot;&quot;Turn on relay via PIN.HIGH&quot;&quot;&quot;
    try:
        gpio.setmode(gpio.BCM)
        gpio.setwarnings(False)
        gpio.setup(pin, gpio.OUT, initial=1)
        send_mqtt(&quot;Turning Relay1 ON&quot;, &quot;Planty/relay&quot;)
        gpio.output(pin, 0)
        time.sleep(5)
        send_mqtt(&quot;Turning Relay1 OFF&quot;, &quot;Planty/relay&quot;)
        gpio.output(pin, 1)
    finally:
        gpio.cleanup()


def main():
    try:
        while 1:
            send_mqtt(read_sensor(&quot;clean&quot;), &quot;Planty/moisture_readings&quot;)
            alerts()
    except KeyboardInterrupt:
        print(&quot;\n\nExiting via Keyboard Interrupt&quot;)


if __name__ == '__main__':
    main()

Since you're using the try/except in main only for KeyboardInterrupt I'd completely remove it as it seems redundant.
def main():
    while 1:
        send_mqtt(read_sensor(&quot;clean&quot;), &quot;Planty/moisture_readings&quot;)
        alerts()