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

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
MQTT connection to ThingSpeak from local Mosquitto broker - Not authorized
No permission to create posts
August 11, 2017
3:31 pm
Avatar
Boggie
New Member
Members
Forum Posts: 4
Member Since:
October 20, 2016
sp_UserOfflineSmall Offline

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? 🙂

August 14, 2017
3:39 pm
Avatar
Boggie
New Member
Members
Forum Posts: 4
Member Since:
October 20, 2016
sp_UserOfflineSmall Offline

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

August 16, 2017
2:35 am
Avatar
RobO
New Member
Members
Forum Posts: 2
Member Since:
August 16, 2017
sp_UserOfflineSmall Offline

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

August 16, 2017
10:38 am
Avatar
JasonW

MathWorks
Members
Forum Posts: 54
Member Since:
October 21, 2016
sp_UserOfflineSmall Offline

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.

August 20, 2017
11:25 pm
Avatar
RobO
New Member
Members
Forum Posts: 2
Member Since:
August 16, 2017
sp_UserOfflineSmall Offline

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

March 16, 2018
6:01 pm
Avatar
robvk
New Member
Members
Forum Posts: 1
Member Since:
March 13, 2018
sp_UserOfflineSmall Offline

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?

April 5, 2018
1:53 pm
Avatar
marcinnap
New Member
Members
Forum Posts: 1
Member Since:
April 5, 2018
sp_UserOfflineSmall Offline

Hi 

I am new to mqtt and thingspeak. I try to do the bridge from my raspberry pi to thingspeak through mqtt. i setup everything like it is said here and my topic is

topic # out 0 home/temperatura/ channels/<chnumber>/publish/fields/field1/<mymqttapi>

the bridge config:

onnection thingspeak
address mqtt.thingspeak.com:1883
notifications false
cleansession true
#remote_clientid thing
start_type automatic
log_type all
remote_username  
remote_password

the log shows :

Bridge raspberrypi.thingspeak sending CONNECT
1522950371: Received CONNACK on connection local.raspberrypi.thingspeak.
1522950371: Bridge local.raspberrypi.thingspeak sending UNSUBSCRIBE (Mid: 1, Topic: channels/<mychnumber>/publish/fields/field1/<mywritekey>/#)
1522950371: Received UNSUBACK from local.raspberrypi.thingspeak

Sending PUBLISH to local.raspberrypi.thingspeak (d0, q0, r1, m0, 'channels/<mychnumber>/publish/fields/field1/<mymqttapi>/home/temperatura',

so it adds local topic to my remote topic

so I don't see anything in thingspeak in my channel  

What am i doing wrong?

how to change so remote broker won't change to topic with local topic?

Thanks for help

April 24, 2018
9:46 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 792
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I'm not clear which topics are on your pi and which you are sending to thingspeak, but this topic name is not right for ThingSpeak MQTT
"channels//publish/fields/field1//home/temperatura"

The api key should be the write api key. You would use the mqttapi key for connecting, not for publish. The channel write api key is at the end of the topic and then the field information goes in the message body.

Have a look at the publish and subscribe pages at the top of the MQTT API page for the correct structure.
https://www.mathworks.com/help/thingspeak/mqtt-api.html

December 21, 2018
12:00 pm
Avatar
nowayray
New Member
Members
Forum Posts: 2
Member Since:
December 21, 2018
sp_UserOfflineSmall Offline

Hi,

I know this is old, but I also wanted to bridge messages from my esp via mosquitto to a single field in thingspeak like the threadstarter.
It seems mosquitto connects to thingspeak, but there is no data recieved.
I took the configuration of @Boggie but see no publish messages in the logs.
I just see
1545410627: Bridge woodstock.thingspeak sending CONNECT
1545410627: Received CONNACK on connection local.woodstock.thingspeak.
1545410627: Bridge local.woodstock.thingspeak sending UNSUBSCRIBE (Mid: 1, Topic: channels/ChannelID/publish/fields/field1/writeapikey_/#)
1545410627: Received UNSUBACK from local.woodstock.thingspeak
1545410687: Sending PINGREQ to local.woodstock.thingspeak
1545410687: Received PINGRESP from local.woodstock.thingspeak

but no publish...?
Here is my config:

connection thingspeak
address mqtt.thingspeak.com:1883
notifications false
cleansession true
start_type automatic
topic # out 0 esp12e/data channels/ChannelID/publish/fields/field1/writeapikey_/
remote_username nowayray
remote_password MQTTAPIKey_
log_type all

If I publish the same topic "channels/ChannelID/publish/fields/field1/writeapikey_" (without slash in the end) via mqtt-spy and credentials directly to thingspeak it shows up on thingsepak.
I am running mosquitto 1.4.10 under debian linux.
I can subscribe to messages on the mosquitto under the topic "esp12e/data" via mqtt-spy and see values as payload. So this works.
What is wrong here? Where else can I search ?

Regards,

Mike

December 21, 2018
12:50 pm
Avatar
nowayray
New Member
Members
Forum Posts: 2
Member Since:
December 21, 2018
sp_UserOfflineSmall Offline

Ok, got it myself, maybe by typing it down before... 🙂

Mosquitto is not able to remap topics. So there is allways the original topic included in the publish topic name to thingspeak.
The key is to name the topic on esp like this:

ChannelID/publish/fields/field1/writeapikey_/

and then change mosquitoot config to:

topic # out 0 ChannelID/publish/fields/field1/writeapikey_/ channels/

And BAM it works.... this was a hard one...Laugh

Mike

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

titan83, Svetoslav, abubakar, shindepavan551@gmail.com, fet61-fakohse, Mario Marquez

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457