Unable to upload sensor data from arduino to thingspeak using MQTT | 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
Unable to upload sensor data from arduino to thingspeak using MQTT
Avatar
Lakshana
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
January 26, 2018 - 6:22 am
sp_Permalink sp_Print sp_EditHistory

I had used the below code to send data using Arduino PubSubclient library. The serial monitor shows data being sent to the Thingspeak MQTT broker but the thingspeak dashboard is not getting updated....please help meCry

#include <SPI.h>
#include <Ethernet.h>

#define USE_ETHERNET_SHIELD
byte mac[] = {x,x,x,x,x,x};
IPAddress ip(192,168,x,x);
EthernetClient client;

#include
char mqttUserName[] = "x";
char mqttPass[] = "x";
char writeAPIKey[] = "x";
long channelID = x;

PubSubClient mqttClient(client);
const char* server = "mqtt.thingspeak.com";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 20L * 1000L;

//#include "ThingSpeak.h"
//unsigned long myChannelNumber =x;
//const char * myWriteAPIKey = "x";

#include "DHT.h"
#define DHTPIN 2
DHT dht;
const int sensor_pin = A1;

void setup() {
Serial.begin(4800);
//ThingSpeak.begin(client);
Ethernet.begin(mac,ip);
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Unable to configure Ethernet using DHCP"));
for (;;);
}
Serial.println("Ethernet configured via DHCP");
Serial.println("Connected to internet");
mqttClient.setServer(server, 1883);
dht.setup(DHTPIN);
}

void loop() {
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
if (millis() - lastConnectionTime > postingInterval)
{
mqttpublish();
}
}

void reconnect()
{

while (!mqttClient.connected())
{
Serial.print("Attempting MQTT connection...");
// Generate ClientID
/*for (int i = 0; i < 8; i++) {
clientID[i] = alphanum[random(51)];
}*/
char clientID[]="x";
if (mqttClient.connect(clientID,mqttUserName,mqttPass))
{
Serial.print("Connected with Client ID: ");
Serial.print(String(clientID));
Serial.print(", Username: ");
Serial.print(mqttUserName);
Serial.print(" , Passwword: ");
Serial.println(mqttPass);
} else
{
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

void mqttpublish() {

delay(dht.getMinimumSamplingPeriod());
float moisture_percentage;
int sensor_analog;
sensor_analog = analogRead(sensor_pin);
moisture_percentage = ( 100 - ( (sensor_analog/1023.00) * 100 ) );
float t = dht.getTemperature();
float h = dht.getHumidity();

// Create data string to send to ThingSpeak
String data = String("field1=" + String(moisture_percentage, DEC) + "&field2=" + String(t, DEC) + "&field3=" + String(h, DEC));
int length = data.length();
char msgBuffer[length];
data.toCharArray(msgBuffer,length+1);
Serial.println(msgBuffer);

// Create a topic string and publish data to ThingSpeak channel feed.
String topicString ="channels/" + String( channelID ) + "/publish/"+String(writeAPIKey);
length=topicString.length();
char topicBuffer[length];
topicString.toCharArray(topicBuffer,length+1);

mqttClient.publish( topicBuffer, msgBuffer );

lastConnectionTime = millis();
}
// Use this function instead to publish to a single field directly. Don't forget to comment out the above version.
/*
void mqttpublish() {

float t = dht.getTemperature(); // Read temperature from DHT sensor.

String data = String(t, DEC);
int length = data.length();
char msgBuffer[length];
data.toCharArray(msgBuffer,length+1);
Serial.println(msgBuffer);

// Create a topic string and publish data to ThingSpeak channel feed.
String topicString ="channels/" + String( channelID ) + "/publish/"+String(writeAPIKey);
length=topicString.length();
char topicBuffer[length];
topicString.toCharArray(topicBuffer,length+1);

mqttClient.publish( topicBuffer, msgBuffer );

lastConnectionTime = millis();
}
*/

Avatar
cstapels
Moderator
Forum Posts: 158
sp_UserOfflineSmall Offline
2
February 1, 2018 - 3:44 pm
sp_Permalink sp_Print sp_EditHistory

Does the serial monitor show a successful connection to the thingspeak server? i.e. do you get "...Connected with Client ID:..."

I would double check your channel number and write API key, I have definitely made that mistake before.  

One debugging method that I use is to copy the topic output and the publish command from the serial monitor and try to publish the same content using MQTT.fx.  There is a demo for it here.  That will halp make sure you are publishing correctly.

Another thing is that the declaration for the pubsub client library is missing, but you wouldn't be able to compile without that.

What hardware are you using specifically?

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
35 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

ibgedubo

aogomaeqaza

ureomug

itixuwidouzuv

eilimir

memozupi

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee