QIoT & ESP8266 & MQTT

QIoT technology to allow you to explore potential IoT business opportunities.
Smart home/Smart building/Smart City/
Post Reply
dsz123
New here
Posts: 4
Joined: Tue Apr 25, 2017 1:05 am

QIoT & ESP8266 & MQTT

Post by dsz123 » Wed Dec 13, 2017 8:38 am

Hi - I'm wondering if anyone has successfully connected an ESP8266 device to the QIoT (QIoT Suite Lite, specifically) using MQTT? I'm close! But having some issues.

I have a program running on a Sparkfun ESP8266 Thing using the Arduino environment. I have installed the PubSubClient (https://github.com/knolleary/pubsubclient) library, in addition to the ESP8266 libraries. I can compile code, connect to the server, and even publish data. Unfortunately, the data isn't correctly received / processed by the QIoT server.

When I look at the Things tab within my application, it does show that data is coming in:

ThingDataLog.png


But they do not show up on the dashboard.

If I make a debug object in the Rule tab, I do see some kind of error when I have the "auto save" checkbox enabled.

Rules.png


It seems like the messages are received, but maybe it has some trouble parsing the value?

Can anyone help? Has anyone succeeded in connecting an Arduino (not running linux or python) to the QIoT Suite Lite?

Code is:

Code: Select all

// Library
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <PubSubClient.h>

#define MQTT_SERVER   "192.168.1.201"
#define MQTT_PORT     21883
#define MQTT_CLIENTID "ChestFreezerIOTemp_1513054490"
#define T_TOPIC_PUB   "qiot/things/admin/ChestFreezerIOTemp/chestfreezer"
#define V_TOPIC_PUB   "qiot/things/admin/ChestFreezerIOTemp/batterylevel"
#define USERNAME      "d69e8bab-4ae6-4344-ab28-23b049972bc6"
#define PASSWORD      "r:ba6066f950b4805751d06c0356765bbe"

ADC_MODE(ADC_VCC)

// WiFi settings
const char* ssid = "networkname";
const char* password = "wifipassword";

// Time to sleep (in seconds):
const int sleepTimeS = 10;
const int TEMP_PIN=4, wifiTries = 15, psTries = 5;

OneWire ds(TEMP_PIN);
WiFiClient espClient;
PubSubClient psClient(espClient);

void setup()
{
  Serial.begin(9600);
  int wifiTry = 0;
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while ((WiFi.status() != WL_CONNECTED) && wifiTry < wifiTries ) {
    delay(500);
    wifiTry++;
  }
  if ( wifiTry < wifiTries ) {
    Serial.println("Wifi connected.");
    psClient.setServer(MQTT_SERVER, MQTT_PORT);
    reconnect();
    if (psClient.connected()) {
      psClient.loop();
      char tStr[15], vStr[15];
      getTempStr(tStr, 15);
      Serial.print("Temp read: ");
      Serial.println(tStr);
      Serial.println("Publishing...");
      int r= psClient.publish(T_TOPIC_PUB, tStr);
      Serial.print("Publish() returned ");
      Serial.println(r);

      psClient.loop();
     
      getVStr(vStr, 15);
      Serial.print("Voltage read: ");
      Serial.println(vStr);
      Serial.println("Publishing...");
      r= psClient.publish(V_TOPIC_PUB, vStr);
      Serial.print("Publish() returned ");
      Serial.println(r);
     
      psClient.loop();
    }
   
  }

  // Sleep
  ESP.deepSleep(sleepTimeS * 1000000);
 
}

void loop()
{

}

void reconnect() {
  int psTry = 0;
  Serial.print("Attempting MQTT connection...");
  while (!psClient.connected() && psTry < psTries) {
    // Attempt to connect
    if (psClient.connect(MQTT_CLIENTID, USERNAME, PASSWORD)) {
      Serial.print("connected");
    } else {
      Serial.print(".");
      delay(1000);
      psTry++;
    }
  }
  Serial.println();
}

bool getTempStr(char *str, int n) {
  //returns the temperature from one DS18B20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    //no more sensors on chain, reset search
    ds.reset_search();
    str = "-1000";
    return false;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
    Serial.println("CRC is not valid!");
    str = "-1001";
    return false;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
    Serial.print("Device is not recognized");
    str = "-1002";
    return false;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end
  delay(1000);

  byte present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE); // Read Scratchpad

  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  int16_t tempRead = ((MSB << 8) | LSB); //using two's compliment
  byte cfg = (data[4] & 0x60);
  if ( cfg == 0x00 ) {
    tempRead = tempRead & ~7;
  } else if ( cfg == 0x20 ) {
    tempRead = tempRead & ~3;
  } else if ( cfg == 0x40 ) {
    tempRead = tempRead & ~1;
  }

  float TemperatureSum = (float)tempRead / 16;
  int intPart=0, decPart=0;
  splitFloat(TemperatureSum, &intPart, &decPart);

  snprintf(str, n, "%d.%02d", intPart, decPart);
  Serial.print("Read temp: ");
  Serial.println(str);
 
  return true;
}

void splitFloat(float val, int *integer, int *decimal) {
  int intPart = 0, decPart = 0;
  *integer = (int)val;
  *decimal = (int)round((val-*integer)*100);
  *decimal = abs(*decimal);
  if ( *decimal >= 100 ) {
    *decimal=0;
    if ( *integer >= 0 ) {
      *integer+=1;
    } else {
      *integer-=1;
    }
  }
}

void getVStr(char *str, int n) {
  float v = ESP.getVcc();
  int intPart =0, decPart=0;
  splitFloat(v, &intPart, &decPart);
  snprintf(str, n, "%d.%02d", intPart, decPart);
  Serial.print("Read voltage: ");
  Serial.println(str);
}


Rules.png
You do not have the required permissions to view the files attached to this post.

dsz123
New here
Posts: 4
Joined: Tue Apr 25, 2017 1:05 am

Re: QIoT & ESP8266 & MQTT

Post by dsz123 » Fri Dec 15, 2017 2:35 pm

I was able to figure this out eventually!

The trick is that the MQTT protocol doesn't actually care what "payload" it sends, but QIoT Suite Lite does care what the format of the payload is.

Instead of just sending the value of the sensor as a string (like

Code: Select all

12.345
, I had to format it as

Code: Select all

{"value":12.345}


The relevant line from the code that changes is:

Code: Select all

snprintf(str, n, "{\"value\":%d.%02d}", intPart, decPart);


Once I changed that, the values show up in the dashboard as they are supposed to.

User avatar
andersoncheng
Getting the hang of things
Posts: 65
Joined: Mon Dec 25, 2017 10:48 am
Contact:

Re: QIoT & ESP8266 & MQTT

Post by andersoncheng » Fri Jun 15, 2018 10:30 am

add QNAP tutorial:
How to use an ESP8266 to connect to QNAP QIoT Suite Lite?
https://www.qnap.com/en/how-to/tutorial ... suite-lite

sliawatimena
First post
Posts: 1
Joined: Fri Dec 28, 2018 7:03 pm

Re: QIoT & ESP8266 & MQTT

Post by sliawatimena » Fri Jan 18, 2019 7:26 pm

Dear all,

How do you define and testing connection to an esp8266? What is username and password for it?
Thank you very much on advance.

Warmest regards
Suryadi

timhsu
New here
Posts: 8
Joined: Wed Nov 15, 2017 12:12 pm

Re: QIoT & ESP8266 & MQTT

Post by timhsu » Mon Jan 28, 2019 9:55 am

Hi sliawatimena,

You reference this tutorial :
How to connect to QIoT Suite Lite using MQTT?
https://www.qnap.com/en/how-to/tutorial ... using-mqtt
and
How to use an ESP8266 to connect to QNAP QIoT Suite Lite?
https://www.qnap.com/en-us/how-to/tutor ... uite-lite/

Post Reply

Return to “QIoT”