hey
starting out with mqtt and arduino. Decided to delve in and try out a DHT22 sensor as I have an application that best suits this sensor to any other due to its temperature ranges and accuracy.
Using some examples from the web, and a bit of fiddling, I managed to get MQTT working with an alternative sensor. I then checked with MQTTlens and had a good constant stream of data
Changing the code slowly, bit by bit, I added code from the DHT22 library which has been posted on the arduino site.
Unfortunately I am now only receiving an initial MQTT post to say that the device is online, and then a single post with the message '1' for temperature and and no post for humidity.
Would someone mind looking at my code and pointing out where my code is wrong please?
Also, I've previously struggled posting digital output commands to the arduino. Anyone able to post an example?
Many thanks
//Â ARDUINO 01 - Loft
// All includes
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <dht.h>
// Ethernet Information
byte mac[]Â Â = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };Â
byte server[] = { 172, 16, 1, 230 };Â
byte ip[]Â Â = { 172, 16, 1, 235 };
// All definitions
// --DHT--
dht DHT;
#define DHT22_PIN 6
// --Other Temp Sensor---
int sensorPin=5;
int temperature = (DHT.temperature, 1);
int humidity = (DHT.humidity, 1);
int lastTemperature;
int lastHumidity;
unsigned long lastTime;
char buffer[10];
// Read MQTT messages
void callback(char* topic, byte* payload, unsigned int length) {
 // handle message arrived (no messages expected though)
}
// Setup Ethernet Client to talk to MQTT broker
EthernetClient ethClient;Â
PubSubClient client(server, 1883, callback, ethClient);
void setup() {Â
 Ethernet.begin(mac, ip);
 if (client.connect("Arduino 01 (235) Loft")) {
  client.publish("Ourplace/Nodes/Arduino-01", "Online");
  lastTemperature = 0;
  lastHumidity = 0;
  lastTime = 0;
 }
}
void loop() {Â
 // Other Temp Sensor settings
 int reading = analogRead(sensorPin);
 int temperature = ((reading * 0.004882) - 0.50) * 100;
 //if (temperature != lastTemperature) {
 // if (millis()>(lastTime+1000)) {
 //  sprintf(buffer,"%d",temperature);
 //  client.publish("Ourplace/Loft/Temperature", buffer);
 //  lastTemperature = temperature;
 //  lastTime = millis();
 // }
//Â }
 // DHT22 Sensor
 // --Read Data--
 int chk = DHT.read22(DHT22_PIN);
 switch (chk)
 if (temperature != lastTemperature) {
  if (millis()>(lastTime+1000)) {
   sprintf(buffer,"%d",temperature);
   client.publish("Ourplace/Loft/Temperature", buffer);
   lastTemperature = temperature;
   lastTime = millis();
  }
 }
 if (humidity != lastHumidity) {
  if (millis()>(lastTime+1000)) {
   sprintf(buffer,"%d",humidity);
   client.publish("Ourplace/Loft/Temperature", buffer);
   lastHumidity = humidity;
   lastTime = millis();
  }
 }
Â
 client.loop();
}