I'm trying to set up a connection from my local mosquitto mqtt broker to thingspeak, so I can run everything through my local broker and still log some of the data to thingspeak by creating a topic subtree starting with remote/thingspeak.
I have Mosquitto up and running and can pub and sub as expected, but when I try to configure the thingspeak connection things start to get weird.
Having read the mqtt documentation on thingspeaks api pages, I have come up with this mosquitto.conf file (I put in the cleansession true, everything I found about will-something appears to be 0 by default):
topic # out 0 remote/thingspeak/ channels/
When I start my server, it tries to connect, but the connection gets refused:
1502479267: Connecting bridge thingspeak (mqtt.thingspeak.com:1883)
1502479267: Bridge raspberrypi.thingspeak sending CONNECT
1502479267: Received CONNACK on connection raspberrypi.thingspeak.
1502479267: Connection Refused: not authorised
1502479267: Socket error on client raspberrypi.thingspeak, disconnecting.
This is before I even try to publish a message through the connection.
What I was hoping this configuration would allow me to do, was publish a message with values to remote/thingspeak/<channelid>/publish/<write key> and make Mosquitto forward it to thingspeak on the topic channels/<channelid>/publish/<write key>
Does anyone out there has a working thingspeak configuration for Mosquitto they can share? 🙂
Just to answer my own question, in case anyone else with the same challenge finds it.
After a lot of trial, error and looking at other bridge configurations on the internet, I found out that I needed to turn off notifications. The following bridge configuration will allow you to bridge messages from a local mosquitto server to thingspeak 🙂
topic # out 0 remote/thingspeak/ channels/
This may be related to an issue I am having. I am running HomeAssistant on a RaspberryPI. It uses Paho, or Mosquitto or some sort of MQTT broker. Any way, I am sending & logging some weather data from an ESP8266 to HomeAssistant, through the MQTT broker. I read the data, send it mqtt to home assistant, then post it to thingspeak. I noticed that when I "ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);" , it drops my MQTT connection to HomeAssistant and must reconnect.
I use the lastwill feature and when they drop "OFFLINE", then I get a notification. The weird thing is that the keep alive time is about 5 minutes. I was getting a offline and reconnected notifications about every 30 seconds or so. I edited out the thingspeak.writefields line and the issue went away. Then I searched and your post came up. I am thinking its the two different MQTT services causing my problem.
So, do you think there is a workaround for this issue? Can I somehow separate the two mqtt messages being sent to two different mqtt services on the same device?
I am pretty new to mqtt & these little microcontrollers. I have been curious if there was a way to send the same data to two different HomeAssistant installations from the same device, but I dont have enough knowledge about it. After looking at the code, it looks like two different mqtt servers could be declared in the variables, but the thingspeak server is already declared as something different so I am still confused.
Anyway, if you could offer some suggestions I would greatly appreciate it. Right now my setup is working, just for some reason it is dropping my mqtt connection to home assistant every time I send a post to my thingspeak channel. I have to turn off the notifications for the dropped connections or I constantly get them.
I don't think your problem is related to Boggie's post. He is referring to publishing messages to the mqtt interface of ThingSpeak. As far as I know, the ThingSpeak HomeAssistant component is using the HTTP REST interface.
It sounds like there is a problem with your ThingSpeak configuration. If the ThingSpeak call errors out, it may be causing your HomeAssistant process to restart and in the process booting your ESP8266 mqtt client.
You may be correct. I probably didnt explain my setup well enough. I use an Adafruit Huzzah ESP8266 to send the mqtt messages to home assistant. Which sends fine. Then when I call the ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); function within my program, it kills the mqtt connection to my home assistant broker. I do not use the ThingSpeak component of home assistant. I tried that first and could not get it to work. So, I saw that there is an arduino library and went that route. When I tried to add the ThingSpeak component, I kept getting errors and I couldnt figure it out.
I figured this out because I get notifications when it drops the connection to the mqtt broker at the home assistant address. It should not fall off for at least 5 minutes because that is the keep alive time. If I comment out that line ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);... the program works fine and does not drop the mqtt connection. If I leave it in there and compile it and upload the program, it works fine, it just drops the mqtt connection to home assistant and reconnects at the top of the program again. So, based on my trial and error, when I call the ThingSpeak.writeFields function, it apparently forces the lastwill message to be sent to home assistant broker. I immediately get an "OFFLINE" message, which is what I have it set to do. Then right away I get an "ONLINE" reconnect message. It wont do it if I comment out that ThingSpeak.writeFields function or remove it.
When I read that it was a mqtt connection to thingspeak, I figured this was some sort of conflict between the mqtt client setup to send messages to home assistant and the mqtt client setup to send messages to thingspeak. I dont know enough about it other than there is something going on when I try doing them both.
I kind of figured there was a way to declare two different mqtt setups on the same micro and it work. I may try to set one up and do some tests.
Thanks for Replying......
I am posting to Mosquitto and syncing to Thingspeak just as in the startingpost. I post to Mosquitto from an ESP-board. I also had to set retain=false in my code:
pubsubclient.publish(thingspeak_topic, msgTS.c_str(), false);
With 'mosquitto_pub' from command line retain is false by default. Enabling it also blocks the message from being synced to Thingspeak.
Maybe this helps somebody.
btw I can only sync when I post values in the form of "field1=5&field2=10" to a specific topic. Does anybody know a way to sync a topic with "normal" values, like topic "home/temp" that contains temperature-values to a Thingspeak-channel?
Most Users Ever Online: 114
Currently Browsing this Page:
Guest Posters: 1
Administrators: Hans, lee