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:
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.
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);
}