4

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?

3
  • 4
    Hi Balsam Qassem, deleting your old question and reposting it is not the way to handle disputes about your questions closure. You should wait for the reopen queue to handle it or flag it for mod attention if we hammered it shut.
    – Helmar
    Commented Jan 15, 2018 at 19:11
  • 1
    You haven't explained what you've tried to debug this problem yourself. e.g. try adding extra print statements to track the flow through the program. Hint: your Main loop is still never getting executed
    – hardillb
    Commented Jan 15, 2018 at 21:06
  • 1
    really, this is a very helpful hint . i am still a beginner with python, i almost create this one depending on this hackster.io/SibiSank/virtual-post-man-e8e9ff @hardillb Commented Jan 16, 2018 at 4:39

1 Answer 1

4

I will post an answer, just to make it clear what your problem is.

However, this is not really my answer - you ought to have been able to see it when @hardillb said "Hint: your Main loop is still never getting executed".

Since it is never getting executed, I looked at the line above it which says mqttc.loop_forever(). Then I Googled for that function ans the 2nd or 3rd answer was Understanding The Loop -Using The Python MQTT Client, which clearly says

The loop_forever() method blocks the program, and is useful when the program must run indefinitely.

So, obviously the following statement will never be reached. There is your answer.


What I am not going to do, is to fix your program for you, but I will give you some hints (teach a man to fish).

  • perhaps you don't need to call mqttc.loop_forever(). I don't know enough about what you want your program to do to figure it out (receive one laser signal, or loop receiving many). Maybe you can remove that. If you need it, then you need to learn about threading. Some people have difficulty with the subject at first, but sometimes you just have to use it.

  • someone mentioned debugging by inserting print statements. DO NOT do this. Get yourself a proper IDE. For Python, I do not know of any better than the excellent Pycharm. It is free, even for commercial use. Feel free to look for other options on our sister site.

    Once you have a good IDE, learn about setting breakpoints, which is the core concept of good debugging. The code will run until the breakpoint and stop. Then you can examine and even change the values of your variables, examine the call stack, seeing which function called which, and with which parameters, and you can step through the code a line at a time.

    In this case, you would have seen that the code would not step beyond that call to mqttc.loop_forever(), which should have caused you to examine he documentation and discover that your problem is that it is a blocking call.

  • actually, this looks like a pure software problem, rather than anything IoT specific. I do understand that it is difficult to tell that, and to know which site to post on, but, in general, if I am going to post code, then I think that Stack Overflow is the correct site.

    When posting there, you should post your code, explain what it is supposed to do, and what it is doing wrongly. Only with that information can others help you.

  • and, finally, when your code is working correctly, if you are new to the language, you might want to post it to our code review site. DO NOT post non-working code. If you post working code, people there will help you to understand how it could have been more efficient, better laid out, easier to understand and maintain, etc, which can be a really good learning aid.

I hope that this has been of some help, and look forward to seeing you at the various SE sites :-)

2
  • 2
    Print statement flow checking is still worth trying, especially when working on small platforms like a pi, it works everywhere even when you can't install a proper debugger to step through the code.
    – hardillb
    Commented Jan 16, 2018 at 13:27
  • I admit to being tempted sometimes, but don't want to set a bad examples to others who might be looking to learn here :-) I will say, though, that I won't ever print unless it is impossible to use a debugger, or quite complicated (maybe because of timing) to breakpoint.
    – Mawg
    Commented Jan 16, 2018 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.