Arduino uno connection problem

#1

I have a connection problem with arduino uno. losant platform appears : waiting for data.

Here is my Arduino source code.

#include <Losant.h>

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

/**
 * Example for sending temperature and humidity
 * to the cloud using the DHT22 and ESP8266
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */


#include "DHT.h"

#define DHTPIN A1     // what digital pin the DHT22 is conected to
#define DHTTYPE DHT11   // There are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);

// WiFi credentials.
#define MQTT_MAX_TRANSFER_SIZE 128  //this was done as a result of troubleshooting
EthernetClient client;
// Losant credentials.
const char* LOSANT_DEVICE_ID = "5adaa6d7d17c7600067badea";
const char* LOSANT_ACCESS_KEY = "5c35d778-dea5-4f5c-a137-890dffdff111";
const char* LOSANT_ACCESS_SECRET = "removed";
byte mac[] = {  0x00, 0x21, 0x5C, 0xD7, 0x78, 0xEF };
IPAddress ip(192, 168, 1, 150);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running DHT!");
  Serial.println("-------------------------------------");
Serial.print(F("Starting ethernet..."));
 Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
  }
  // Register the command handler to be called when a command is received
  // from the Losant platform.
  //device.onCommand(&handleCommand);
  connect();
}

LosantDevice device(LOSANT_DEVICE_ID);

void connect() {

 // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    Serial.println(device.mqttClient.state()); 
    Serial.print(".");
  }

  Serial.println("Connected!");
}

void report(double humidity, double tempC, double tempF, double heatIndexC, double heatIndexF) {
  StaticJsonBuffer<500> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["humidity"] = humidity;
  root["tempC"] = tempC;
  root["tempF"] = tempF;
  root["heatIndexC"] = heatIndexC;
  root["heatIndexF"] = heatIndexF;
  device.sendState(root);
  Serial.println("Reported!");
}

int timeSinceLastRead = 0;
void loop() {
   bool toReconnect = false;

 

  if (!device.connected()) {
    Serial.println("Disconnected from MQTT");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();
  }

  device.loop();

  // Report every 2 seconds.
  if(timeSinceLastRead > 2000) {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" % ");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C ");
    //Serial.print(f);
    //Serial.print(" *F\t");
    /*Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    report(h, t, f, hic, hif);*/

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
}

What is the problem?

Nick

0 Likes

#2

Does your Application Communication Log show any messages when your device is powered on?

0 Likes

#3

ARDUINO CONNECTION PROBLEM.

DEAR BRANDON

THE LOSANT PLATFORM APPEARS :WAITING FOR DATA
THE APPLICATION LOG SHOWS THE FOLLOWING MESSAGE:

One or more of your payload properties was rejected.
Payload must be in JSON format.
Attributes outside your data object are ignored.

0 Likes

#4

Can you please paste a screenshot of the communication log, so we can see what was reported?

0 Likes

#5

DEAR BRANDON

HERE IS THE PRINT SCREEN OF THE APPLICATION LOG

0 Likes

#6

DEAR BRANDON

MY ARDUINO CODE IS:


#include <Losant.h>

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

/**
 
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */


#include "DHT.h"



#define DHTPIN A1     // what digital pin the DHT11 is conected to
#define DHTTYPE DHT11   // There are multiple kinds of DHT sensors

DHT dht(DHTPIN, DHTTYPE);

// WiFi credentials.
#define MQTT_MAX_TRANSFER_SIZE 256  //this was done as a result of troubleshooting
EthernetClient client;
// Losant credentials.
const char* LOSANT_DEVICE_ID = "5adaa6d7d17c7600067badea";

const char* LOSANT_ACCESS_KEY = "2796f76a-ffca-4d2c-b23c-b84a3ca01fd2";
const char* LOSANT_ACCESS_SECRET = "removed";
byte mac[] = {  0x00, 0x21, 0x5C, 0xD7, 0x78, 0xEF };
IPAddress ip(192, 168, 1, 150);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running DHT!");
  Serial.println("-------------------------------------");
Serial.print(F("Starting ethernet..."));
 Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.println(Ethernet.localIP());
      Serial.println(Ethernet.gatewayIP());
  }
  // Register the command handler to be called when a command is received
  // from the Losant platform.
  //device.onCommand(&handleCommand);
  connect();
}

LosantDevice device(LOSANT_DEVICE_ID);

void connect() {

 // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    Serial.println(device.mqttClient.state()); 
    Serial.print(".");
  }

  Serial.println("Connected!");
}



void report(double humidity, double tempC, double tempF, double heatIndexC, double heatIndexF) {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root= jsonBuffer.createObject();
  root["humidity"] = humidity;
  root["tempC"] = tempC;
  root["tempF"] = tempF;
  root["heatIndexC"] = heatIndexC;
  root["heatIndexF"] = heatIndexF;
  device.sendState(root);
  Serial.println("Reported!");
}

int timeSinceLastRead = 0;
void loop() {
   bool toReconnect = false;

 

  if (!device.connected()) {
    Serial.println("Disconnected from MQTT");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();
  }

  device.loop();

  // Report every 2 seconds.
  if(timeSinceLastRead > 2000) {
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" % ");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C ");
    Serial.print(f);
    Serial.print(" *F");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    report(h, t, f, hic, hif);

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
}
0 Likes

#7

Can you adjust the screenshot to show the information below the error message?

0 Likes

#8

Can you also print the contents of the JSON object to Serial?

root.printTo(Serial);
0 Likes

#9

DEAR BRANDON

THE APPLICATION SCREENSHOT IS:

AND THE PRINT OF THE CONTENTS OF THE JSON OBJECT TO SERIAL IS:

0 Likes

#10

There’s definitely a big difference between what your device is printing vs. what’s being received by Losant. We’ll have to attempt to reproduce this issue. Using the Arduino’s Library Manager, can you please provide the version of the pubsubclient and ArduinoJSON libraries?

0 Likes

#11

DEAR BRANDON

ARDUINO JSON LIBRARY VERSION : 5.13.1

PubSubClient LIBRARY VERSION: 2.6.0

LOSANT-MQTT-ARDUINO : 1.3.0

0 Likes

#12

DEAR BRANDON

YOU ASKED ME TO PROVIDE THE VERSIONS OF PubSubClient
AND ArduinoJSON.WHAT IS THE NEXT STEP TO FIX THIS PROBLEM?

0 Likes

#13

I’m using the same code, but I’m using json buffer of 512 for root. It shows my device connected, but at my dashboard it doesn’t show any data.

0 Likes

#14

Thanks Paulo_Renato_Xavier.

0 Likes

#15

Hi nick_bfas, your code it is working?

0 Likes

#16

To solve my problem, I had to edit MQTT_MAX_PACKET_SIZE at ~/Documents/Arduino/libraries/pubsubclient/src/PubSubClient.h to 256 or something larger.

0 Likes

#17

Hi Paulo.My code is working ,sends data to serial monitor and shows connected to Losant. I have changed PubSubClient.h to 256 according to your advice but my dashboard still shows :waiting for data.Did this change to 256 worked for you?

0 Likes

#18

Sorry for aswer you this late. For my application I have to use 512. I’m doing upload of 9 parameters.

0 Likes