Skip to main content
added syntax highlight
Source Link
Bence Kaulics
  • 7.8k
  • 8
  • 42
  • 90

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit and publish MQTT messages. I am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldrLDR sensor will be 1, then the code must publish an MQTT message.

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

#Main Loop

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
import ssl

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

#Main Loop

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit and publish MQTT messages. I am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldr sensor will be 1, then the code must publish an MQTT message.

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

#Main Loop

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit and publish MQTT messages. I am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of LDR sensor will be 1, then the code must publish an MQTT message.

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

#Main Loop

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

Connect Program with MQTT hanging after starting, LDR sensor to IOTprocessing is not working with no MQTT messages

I am trying to connect the LDR sensor to IoTIoT by using GPIO pins (pin 4) of Raspberry Pi kit ,iand publish MQTT messages. I am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldr sensor will be 1, then the code must publish aan MQTT message so,.

I tried this code:

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()
 
    
#Main Loop
 

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

But whenWhen I run it I only gotsee the printed output text "successful connection" nothing elseand I am not seeing any of the expected MQTT messages. 

Can you please tell me what's wrong with this code? i create the main loop depending on this idea: input_value = GPIO.input(4) should interenter the signal if the value of signal is 1 then it should publish the message what's wrong with thatmy code?

Connect LDR sensor to IOT

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit ,i am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldr sensor will be 1, then the code must publish a message so, I tried this code:

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()
 
    
#Main Loop
 

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

But when I run it I only got "successful connection" nothing else. Can you please tell me what's wrong with this code? i create the main loop depending on this idea: input_value = GPIO.input(4) should inter the signal if the value of signal is 1 then it should publish the message what's wrong with that

Program with MQTT hanging after starting, LDR processing is not working with no MQTT messages

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit and publish MQTT messages. I am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldr sensor will be 1, then the code must publish an MQTT message.

I tried this code:

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

#Main Loop

 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

When I run it I only see the printed output text "successful connection" and I am not seeing any of the expected MQTT messages. 

Can you please tell me what's wrong with this code? i create the main loop depending on this idea: input_value = GPIO.input(4) should enter the signal if the value of signal is 1 then it should publish the message what's wrong with my code?

Source Link

Connect LDR sensor to IOT

I am trying to connect the LDR sensor to IoT by using GPIO pins (pin 4) of Raspberry Pi kit ,i am using a laser as a transmitter and the LDR sensor as a receiver, the output signal from LDR sensor is (0 or 1), if something passed through laser line, the output of ldr sensor will be 1, then the code must publish a message so, I tried this code:

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
MQTT_HOST = "iot.eclipse.org"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
mqttc.loop_forever()

    
#Main Loop


 while True       
    input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            print ("message published")
            time.sleep(1)

But when I run it I only got "successful connection" nothing else. Can you please tell me what's wrong with this code? i create the main loop depending on this idea: input_value = GPIO.input(4) should inter the signal if the value of signal is 1 then it should publish the message what's wrong with that