MQTT connection to ThingSpeak from local Mosquitto broker - Not authorized | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
MQTT connection to ThingSpeak from local Mosquitto broker - Not authorized
Avatar
Boggie
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
1
August 11, 2017 - 3:31 pm
sp_Permalink sp_Print

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):

thingspeak.conf:
-----------------------------
connection thingspeak
address mqtt.thingspeak.com:1883
cleansession true
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? 🙂

Avatar
Boggie
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
2
August 14, 2017 - 3:39 pm
sp_Permalink sp_Print

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 🙂

-----
connection thingspeak
address mqtt.thingspeak.com
notifications false
cleansession true
topic # out 0 remote/thingspeak/ channels/
-----

Happy MQTT'ing

Avatar
RobO
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
3
August 16, 2017 - 2:35 am
sp_Permalink sp_Print

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. 

 

Rob

Avatar
JasonW

Forum Posts: 23
sp_UserOfflineSmall Offline
4
August 16, 2017 - 10:38 am
sp_Permalink sp_Print

RobO,

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.

Avatar
RobO
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
5
August 20, 2017 - 11:25 pm
sp_Permalink sp_Print

JasonW,

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......

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
16 Guest(s)

Currently Browsing this Page:
2 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

donj51

trilee

Bfletche

stuartWAI

shivmkatiyar

AidanThing

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee