Hello. My next issue in this project is decoding the DHT22 environment data from the payload. at the Arduino MQTT Client
Here is the output of the compiler -
Using board ‘mega’ from platform in folder: C:\Users\johno\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Using core ‘arduino’ from platform in folder: C:\Users\johno\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Detecting libraries used…
“C:\Users\johno\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR “-IC:\Users\johno\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino” “-IC:\Users\johno\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\variants\mega” “C:\Users\johno\AppData\Local\Temp\arduino-sketch-AAB4E1EA55727EB640A0AF04A530CE7B\sketch\sketch_apr5_v9.0.ino.cpp” -o nul
Alternatives for LMIC.h: []
ResolveLibrary(LMIC.h)
→ candidates: []
Compilation error: Error: 2 UNKNOWN: exit status 1
Here is my code (hope I’ve presented it in the right way.).
The issue is the decoding loop in the callback area. It just doesn’t work.
*
Arduino MQTT Client v9.0
Extracting DHT22 data from payload.
Arduino Mega + genuine Arduino Ethernet Shield2
Subscribes to topic “application/+/device/+/+”
IPaddress RAK7249 in-built server 192.168.1.227 (Barn) or 192.168.1.250
(Network Room) Arduino ethernet shield MAC address is A8:61:0A:AE:6A:0B
Arduino PubSubClient - MQTT Client Library Encyclopedia
*/
//#include <Adafruit_Sensor.h>
//#include “DHT.h”
#include <LMIC.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <string.h>
// float temperature;
// float humidity;
int data[5];
int dataStart;
int dataEnd;
float rawTemp;
float rawHumid;
int i;
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x6A, 0x0B}; // MAC address Arduino Ethernet Shield MQTT client
IPAddress ip(192, 168, 1, 51); // Static IP address Arduino MQTT client
IPAddress server(192, 168, 1, 227); // Static IP address RAK7249 built-in LoRa server Barn
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("\nMessage arrived\nTopic\n [");
Serial.print(topic); // Print topic
Serial.print("]\n");
Serial.print(“Payload\n “);
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]); // Print payload
}
Serial.print(”\n”);
if (strstr(payload, “01f3b8fb5a1c1050”)) // DTH22 environment message? Search for devEUI.
{
dataStart = int(strchr(payload, ‘data":"’)); // point to the start of the environment data
dataEnd = int(strchr(payload, ‘data_encode’) - 14); // point to the end of the environment data
for (i = dataStart; i < dataEnd; i++)
{
data[i] = payload[i]; // extract the environment data to a new string
}
// isolate temperature
rawTemp = data[0] + data[1] * 256;
// float -> int: this uses the sflt16 datum (https://github.com/mcci-catena/arduino-lmic#sflt16)
uint16_t payloadTemp = f2sflt16(rawTemp) * 100; //alt code: temperature = sflt162f(rawTemp) * 100;
// isolate humidity
rawHumid = data[2] + data[3] * 256;
uint16_t payloadHumid = f2sflt16(rawHumid)* 100;
//humidity = sflt162f(rawHumid) * 100;
Serial.print("\n Environment: ");
Serial.print("Temperature: ");
Serial.print(payloadTemp);
Serial.print("° C");
Serial.print(" Humidity: ");
Serial.print(payloadHumid);
Serial.print("% rH");
}
}
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void reconnect()
{
while (!mqttClient.connected()) // Loop until we’re reconnected
{
Serial.print("\nAttempting MQTT connection…");
if (mqttClient.connect("arduinoClient9")) // Attempt to connect
{
Serial.print("\nThe client is connected.");
mqttClient.subscribe("application/+/device/+/+");
}
else
{
Serial.print("\nFailed, rc = ");
Serial.print(mqttClient.state());
Serial.print("\nTry again in 5 seconds.");
delay(5000);} // Wait 5 seconds before retrying
}
}
void setup()
{
Serial.begin(115200);
Ethernet.begin(mac, ip);
delay(5000); // Allow the hardware to sort itself out
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);
}
void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}
I’m hoping that some one can point out where I’m going wrong. Thank you. Regards