Internet of Things/Testprogramma MQTT

Uit Lab
Naar navigatie springen Naar zoeken springen

Versie met sensoren

import time
import grovepi
import grove_barometer_lib
import paho.mqtt.client as mqtt

led = 4     # led-relais op grovepi D4 
valve = 3   # klep-relais op grovepi D3
valveOn = 1 # klep aan (initieel)
ledOn = 0   # lamp uit (initeel)

moisturePin = 0 # grovepi A0
# barometer op grovepi I2C-2

now = 0

grovepi.pinMode(led, "OUTPUT")
grovepi.pinMode(valve, "OUTPUT")

## MQTT Callbacks
def on_connect(client, userdata, flags, rc):
    if rc != 0:
        print "Connection failed. RC: {}".format(rc)
    else:
        print "Connected successfully"

def on_publish(client, userdata, mid):
    print "Message {} published.".format(mid)

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print "Client disconnected unexpectedly, trying to reconnect."
        hasDisconnected = True
        mqttClient.reconnect()

def init_mqtt():
    mqttClient = mqtt.Client("testClient123") # je eigen ID
    mqttClient.on_connect = on_connect
    mqttClient.on_publish = on_publish
    mqttClient.on_disconnect = on_disconnect
    mqttClient.loop_start()
    mqttClient.connect("192.168.178.130", 1883) # local: 192.168.178.130, global: 82.161.55.218

def send_sensordata(sensorValues):
    mqttClient.publish("test/123", json.dumps(sensorValues))

sensorValues = dict()

# The main loop:

while True:
  try:
    print("hi")
    b = grove_barometer_lib.barometer()
    print("Pressure: " + str(b.pressure) + " temperature: " + str(b.temperature))
    sensorValues["Pressure"] = b.pressure
    sensorValues["Temperature"] = b.temperature

    moisture = grovepi.analogRead(moisturePin)
    print("Moisture: " + str(moisture))
    sensorValues["Moisture"] = moisture

    send_sensordata(sensorValues)

    # grovepi.digitalWrite(led, ledOn)
    # ledOn = 1 - ledOn

    # grovepi.digitalWrite(valve, valveOn)
    # valveOn = 1 - valveOn

    time.sleep(10)
    now += 10

  except KeyboardInterrupt:
    # reset outputs
    grovepi.digitalWrite(led, 0)
    grovepi.digitalWrite(valve, 0)
    break
  except IOError:
    print("error")

Eenvoudige versie

Een eenvoudige versie zonder sensoren:

import time
import paho.mqtt.client as mqtt

now = 0

## MQTT Callbacks
def on_connect(client, userdata, flags, rc):
    if rc != 0:
        print "Connection failed. RC: {}".format(rc)
    else:
        print "Connected successfully"

def on_publish(client, userdata, mid):
    print "Message {} published.".format(mid)

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print "Client disconnected unexpectedly, trying to reconnect."
        hasDisconnected = True
        mqttClient.reconnect()

def init_mqtt():
    mqttClient = mqtt.Client("testClient123") # je eigen ID
    mqttClient.on_connect = on_connect
    mqttClient.on_publish = on_publish
    mqttClient.on_disconnect = on_disconnect
    mqttClient.loop_start()
    mqttClient.connect("192.168.178.130", 1883) # local: 192.168.178.130, global: 82.161.55.218

def send_sensordata(sensorValues):
    mqttClient.publish("test/123", json.dumps(sensorValues))

sensorValues = dict()

# The main loop:

while True:
  try:
    print("hi at " + str(now))

    sensorValues["Pressure"] = 200 + now
    sensorValues["Temperature"] = 20 + now
    sensorValues["Moisture"] = 300 + now

    send_sensordata(sensorValues)

    time.sleep(10)
    now += 10

  except KeyboardInterrupt:
    # reset outputs
    break
  except IOError:
    print("error")

Programma met aansturing en mqtt

import time
import paho.mqtt.client as mqtt

now = 0

## MQTT Callbacks
def on_connect(client, userdata, flags, rc):
    if rc != 0:
        print "Connection failed. RC: {}".format(rc)
    else:
        print "Connected successfully"

def on_publish(client, userdata, mid):
    print "Message {} published.".format(mid)

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print "Client disconnected unexpectedly, trying to reconnect."
        hasDisconnected = True
        mqttClient.reconnect()

def init_mqtt():
    mqttClient = mqtt.Client("testClient123") # je eigen ID
    mqttClient.on_connect = on_connect
    mqttClient.on_publish = on_publish
    mqttClient.on_disconnect = on_disconnect
    mqttClient.loop_start()
    mqttClient.connect("192.168.178.130", 1883) # local: 192.168.178.130, global: 82.161.55.218

def send_sensordata(sensorValues):
    mqttClient.publish("test/123", json.dumps(sensorValues))

sensorValues = dict()

# The main loop:

while True:
  try:
    print("hi at " + str(now))

    if (now % (5*60) == 0):
      moisture = grovepi.analogRead(moisturePin)
      

    if now >= ledStart:
      grovepi.digitalWrite(ledPin, 1)
      print("led ON")
      ledStop = now + ledDuration      # end activity this period
      ledStart = ledStart + ledPeriod  # start next period
      print("next Stop: " + str(ledStop))
      print("next Start: " + str(ledStart))

    if now >= ledStop:
      print("led OFF")
      grovepi.digitalWrite(ledPin, 0)
      ledStop = ledStart - 1

    if now >= waterStart:
      moisture = grovepi.analogRead(moisturePin)
      print("check moisture: " + str(moisture))
      if moisture < 550:
        grovepi.digitalWrite(waterPin, 1)
        print("water ON")
      waterStop = now + waterDuration        # end activity this period
      waterStart = waterStart + waterPeriod  # start next period     
      print("next waterStop: " + str(waterStop))
      print("next waterStart: " + str(waterStart))

    if now >= waterStop:
      grovepi.digitalWrite(waterPin, 0)
      waterStop = waterStart - 1
      print("water OFF")

    sensorValues["Pressure"] = 200 + now
    sensorValues["Temperature"] = 20 + now
    sensorValues["Moisture"] = 300 + now

    send_sensordata(sensorValues)

    time.sleep(10)
    now += 10

  except KeyboardInterrupt:
    grovepi.digitalWrite(ledPin, 0)   # reset LED
    grovepi.digitalWrite(waterPin, 0) # reset water valve
    break
  except IOError:
    print("Error")

import time import grovepi

waterPin = 3 ## Valve relay: digital port D3 ledPin = 4 ## LED relay: digital port D4 moisturePin = 0 ## analog port A0: moisture sensor

    1. barometer: connected to I2C, e.g. port I2C-1

grovepi.pinMode(waterPin, "OUTPUT") grovepi.pinMode(ledPin, "OUTPUT")

    1. pinMode for analog port not needed
    1. Timing
    2. Periodic events: every xxxPeriod seconds
    3. active for xxxDuration seconds (from xxxStart to xxxStop)

now = 0

waterPeriod = 15 * 60 # every 15 minutes waterDuration = 1 # active for 1 seconds (only if needed) waterStart = now + 1 * 60 waterStop = now

ledPeriod = 60 * 60 # every hour ledDuration = 45 * 60 # active for 45 minutes ledStart = now + 5 ledStop = now


while True:

 try:
   if (now % (5*60) == 0):
     moisture = grovepi.analogRead(moisturePin)
     print("moisture: " + str(moisture))
   if now >= ledStart:
     grovepi.digitalWrite(ledPin, 1)
     print("led ON")
     ledStop = now + ledDuration      # end activity this period
     ledStart = ledStart + ledPeriod  # start next period
     print("next Stop: " + str(ledStop))
     print("next Start: " + str(ledStart))
   if now >= ledStop:
     print("led OFF")
     grovepi.digitalWrite(ledPin, 0)
     ledStop = ledStart - 1
   if now >= waterStart:
     moisture = grovepi.analogRead(moisturePin)
     print("check moisture: " + str(moisture))
     if moisture < 550:
       grovepi.digitalWrite(waterPin, 1)
       print("water ON")
     waterStop = now + waterDuration        # end activity this period
     waterStart = waterStart + waterPeriod  # start next period     
     print("next waterStop: " + str(waterStop))
     print("next waterStart: " + str(waterStart))
   if now >= waterStop:
     grovepi.digitalWrite(waterPin, 0)
     waterStop = waterStart - 1
     print("water OFF")
   time.sleep(1)
   now += 1
 except KeyboardInterrupt:
   grovepi.digitalWrite(ledPin, 0)   # reset LED
   grovepi.digitalWrite(waterPin, 0) # reset water valve
   break
 except IOError:
   print("Error")