Frederic, these two lines you have highlighted
        self.username = stn_dict.get('username', 'XXX')
        self.password = stn_dict.get('password', 'password')
should pick up the username and password from the python "dictionary
<https://learnpythonthehardway.org/book/ex39.html>" that is populated when
weewx reads your weewx.conf file on startup.

Do you get any sort of error message, either from weewx or Mosquitto? In
weewx.conf, you can set

> # Set to 1 for extra debug info, otherwise comment it out or set to zero.
> debug = 1

and it will log much more information about what it is doing.

Try adding this line right after the two highlighted
        self.username = stn_dict.get('username', 'XXX')
        self.password = stn_dict.get('password', 'password')
        logdbg("Set username to %s and password to %s" % (self.username,
self.password))

In Mosquitto, you have to explicitly set logging to go to syslog, otherwise
it is only to stdout. Also, I recall having problems with the wrong version
of the MQTT protocol when I started. I might have had to build a newer
version of Mosquitto manually, instead of using the one which came with the
running version of the OS.

I'm attaching a small python script which should watch everything going
through your Mosquitto broker.

On Mon, Feb 20, 2017 at 5:24 PM, wysiwyg <[email protected]> wrote:

> Bill,
>
> I have some troubles to make the driver work.
> While look at the code, I have the feeling that the username / password
> from weewx.conf are not used ?
>
> I tried something, but seems not working better.
>
> here is my test (it's based on other lines of the driver, I try to do
> something similar, but as I said, I know nothing about python)
>
>
>  def __init__(self, **stn_dict):
>         # where to find the data file
>         self.host = stn_dict.get('host', 'localhost')
>         self.topic = stn_dict.get('topic', 'weather')
>         self.username = stn_dict.get('username', 'XXX')
>         self.password = stn_dict.get('password', 'password')
>         # how often to poll the weather data file, seconds
>         self.poll_interval = float(stn_dict.get('poll_interval', 5.0))
>         # mapping from variable names to weewx names
>         self.label_map = stn_dict.get('label_map', {})
>
>
>         loginf("host is %s" % self.host)
>         loginf("topic is %s" % self.topic)
>         loginf("polling interval is %s" % self.poll_interval)
>         loginf('label map is %s' % self.label_map)
>
>
>         self.payload = "Empty"
>         #self.payloadList = [payload]
>         self.client = mqtt.Client(client_id="XXX", protocol=mqtt.MQTTv31)
>
>
>         #self.client.on_connect = self.on_connect
>         self.client.on_message = self.on_message
>
>
>         self.client.username_pw_set(self.username, self.password)
>         self.client.connect(self.host, 1883, 60)
>         self.client.subscribe(self.topic, qos=1)
>
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "weewx-user" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/weewx-user/zhl4I7oRtt8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
import time
import thread
import paho.mqtt.client as mqtt


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    #print("In thread %s" % thread.get_ident())
    userdata[0] = str(msg.topic)+"->"+str(msg.payload)

data = "                       data                                                      "
list = [data]
print "Initializing list to %s" % list[0]
client = mqtt.Client(client_id="surveyLooper", userdata = list, protocol=mqtt.MQTTv31)

client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set("user", "passwd")
client.connect("localhost", 1883, 60)
client.loop_start()
oldmsg = ''

while(1) :
  time.sleep(1)
  #print("Do some other stuff in this thread %s" % thread.get_ident() )
  if list[0] != oldmsg :
      oldmsg = list[0]
      print "List is now %s " % list[0]

Reply via email to