I followed this example to use MQTT from my ESP32:
Lets make some fake variables for discussion:
long channelID = 1234567;char mqttPass[] = "ABCDEFGHIJKLM"; // MQTT API KEY (generated from user profile)char apiKey[] = "APIKEY"; //Channel Write API Key (generated from channel API keys tab)
I can connect with the example code (sometimes it fails, but it reconnects and then holds the connection well).
After the postingInterval has passed, the publish function is called:
void mqttPublishFeed(float field1, float field2, float field3, float field4, float field5, float field6, float field7, float field8) { // Create data string to send to ThingSpeak. String data = "field1="; data += String(field1, DEC); data += "&field2="; data += String(field2, DEC); data += "&field3="; data += String(field3, DEC); data += "&field4="; data += String(field4, DEC); data += "&field5="; data += String(field5, DEC); data += "&field6="; data += String(field6, DEC); data += "&field7="; data += String(field7, DEC); data += "&field8="; data += String(field8, DEC); int length = data.length(); const char *msgBuffer; msgBuffer=data.c_str(); Serial.println(msgBuffer); // Create a topic string and publish data to ThingSpeak channel feed. String topicString = "channels/" + String( channelID ) + "/publish/"+String(apiKey); Serial.println(topicString); length = topicString.length(); const char *topicBuffer; topicBuffer = topicString.c_str(); mqttClient.publish( topicBuffer, msgBuffer ); lastConnectionTime = millis();}
Here is an example of the topicString and msgBuffer variables that are printed to the serial (using the fake variables state above)
msgBuffer:field1=829.0000000000&field2=477.0000000000&field3=28.0499992371&field4=35.0566406250&field5=23.0000000000&field6=50.9949913025&field7=0.0000000000&field8=995.9716186523topicString:channels/1234567/publish/APIKEY
Problem:
I have confirmed that (on my serial) the channelID and the channel API write key are correct and data seems to publish without error (though it is QOS 0 so its really hard to tell). Despite this, my channel does not update. I have previously used HTTPS and had no issues with API keys or channel updating so I know they aren't the issue.
Best Answer