I don't know if this is the right place, but i managed to create a Particle Photon temperature logger with a DHT22 sensor. If someone has any suggestions to the code, please give them!

Code: Select all
#include "application.h"
#include "PietteTech_DHT/PietteTech_DHT.h"
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 3 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 10000 // Sample every minute
#define LUXPIN A1 // LDR Pin
#define READ_INTERVAL 10000
//Setup DHT Sensor reading
void dht_wrapper();
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
//Setup Global Variables
unsigned int DHTnextSampleTime; // Next time we want to start sample
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
int lux;
char VERSION[64] = "1.0";
char resultstr[64]; //String to store the sensor data
TCPClient client;
IPAddress remoteIP(192,168,178,196);
void setup() {
DHTnextSampleTime = 0; // Start the first sample immediately
}
void dht_wrapper() {
DHT.isrCallback();
}
void loop() {
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) { // start the sample
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
char tempInChar[32];
char humidInChar[32];
//Calculate Temperature
float temp = (float)DHT.getCelsius();
int temp1 = (temp - (int)temp) * 100;
//Calculate Humidity
float humid = (float)DHT.getHumidity();
int humid1 = (humid - (int)humid) * 100;
//Calculate Light
float light_measurement = analogRead(LUXPIN);
lux = (int)(light_measurement/4096*100);
//Put Variables in Char
sprintf(tempInChar,"%0d.%d", (int)temp, temp1);
sprintf(humidInChar,"%0d.%d", (int)humid, humid1);
if (client.connect(remoteIP, 8080)) {
Particle.publish("Connection Domoticz", "Success", 60, PRIVATE);
client.println("GET /json.htm?type=command¶m=udevice&idx=1&nvalue=0&svalue=" + String(temp) + ";" + String(humid) + ";1 HTTP/1.1");
client.println("Host: "+ remoteIP);
client.println("Authorization: Basic <Authentication..>");
client.println("Connection: close");
client.println();
}
n++; // increment counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
}