Using Boron for temp humidity sensor

I have the Boron printing serial data to the serial port. But i am having a devil of a time getting it to a platform to view data over timer. Whether that is Adafruit or thingspeak or whatever. Could someone please point me to the right code to do this?
I have tried some that do both but it keeps throwing errors when I try to verify it.
SO if anyone knows of a good chunk of code that will do it I will try that.
It must be fairly easy to do.
Thanks for the help folks.

I am trying this code to use webhook. So I just need to publish the variable. But how do I do that with this? Or is there something easier?

#include "Adafruit_DHT.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
 #define DHTTYPE DHT22		// DHT 22 (AM2302)
//#define DHTTYPE DHT21		// DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

SYSTEM_MODE(AUTOMATIC);

void setup() {
	Serial.begin(115200); 
	Serial.println("DHTxx test!");

	dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
	delay(2500);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	// if (isnan(h) || isnan(t) || isnan(f)) {
	if (isnan(h)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

    Particle.publish("temp", t, PRIVATE);
// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.print("*K - ");
	Serial.print("DewP: ");
	Serial.print(dp);
	Serial.print("*C - ");
	Serial.print("HeatI: ");
	Serial.print(hi);
	Serial.println("*C");
	Serial.println(Time.timeStr());
}

Have you tried searching the forum?
One thing to note though is that Particle.publish() only takes strings as data payload - no floats.
So this cannot work

When using Particle.publish() you should also check console.particle.io/events to see what is being sent.

Yes I searched for anything Boron and DHT related. There was ONE post and it only dealt with getting a DHT to work. Which I did. But now I want to publish it and not sure how.

I see in that code it is using “t” to store the temperature value as a float. Now how do I make that equal to “temp” and send it?

This is the key part of my answer before

float is a numeric data type like int and only stores the bit pattern of the value itself.
A string is a the human readable representaion of such a number.

So some quick basics first
e.g. take the number 1225. That would be stored as int16_t (two byte integer) like this

0000010011001001

which isn’t that easy to read and comprehend, so for easier reading the this number needs to be converted into the four decmial digits 1, 2, 2 and 5 and these need to be stored as individual bytes an a block of memory we call string.

A convenient way to convert numeric values into strings is snprintf() which you should have found an many code fragments all over this forum.

  float f = 123.4;
  char str[8];
  snprintf(str, sizeof(str), "%.2f", f); 
  // after that str can be used for publishing

This translates a float variable into a string with two decimal places.
For more info how to format different kinds of datatypes have a look here

Once you get the hang of that kind of formatting, you can replace your multiple Serial.print() statements with one single line of Serial.printlnf(".......", h, t, f, k, dp, hi, (const char*)Time.timeStr()) too.

Thanks ScruffyDude…I just did find some code chucks I reused. I still don’t fully understand it but it’s coming along. Here is what I Have now…I turned off the serial printing for now as I was wondering if it was messing with something. So it’s publishing the temp fine but it fails to read the humidity. Weird that’s where I checked the serial output and it reads it fine. I looked ot see if I was missing a period or whatever but it looks the same to me.

#include "Adafruit_DHT.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
 #define DHTTYPE DHT22		// DHT 22 (AM2302)
//#define DHTTYPE DHT21		// DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

//int temp = (t);

DHT dht(DHTPIN, DHTTYPE);

SYSTEM_MODE(AUTOMATIC);

char tempString[30] = "";
char humString[30] = "";

void setup() {
//	Serial.begin(115200); 
//	Serial.println("DHTxx test!");

	dht.begin();
	
//Particle.variable("temp", temp);
}

void loop() {
// Wait a few seconds between measurements.
   
    
	delay(2500);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
	
	//int temp = (t);
	
	sprintf(tempString, "%f", t);
    sprintf(humString, "%h", h);
  
// Check if any reads failed and exit early (to try again).
	// if (isnan(h) || isnan(t) || isnan(f)) {
	if (isnan(h)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

    Particle.publish("t", tempString, PRIVATE);
    Particle.publish("h", humString, PRIVATE);
// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

//	Serial.print("Humid: "); 
//	Serial.print(h);
//	Serial.print("% - ");
//	Serial.print("Temp: "); 
//	Serial.print(t);
//	Serial.print("*C ");
//	Serial.print(f);
//	Serial.print("*F ");
//  Serial.print(k);
//	Serial.print("*K - ");
//	Serial.print("DewP: ");
//	Serial.print(dp);
//	Serial.print("*C - ");
//	Serial.print("HeatI: ");
//	Serial.print(hi);
//	Serial.println("*C");
//	Serial.println(Time.timeStr());
}

Also what’s the deal with the “char tempString[30] = “”;” why would you need so many places? I tried the 8 like you had in your answer but then it wont publish anything. IS that what is messing with the humidity publish?

You haven’t quite got the meaning of the format strings right :wink:

%f stands for float but I don’t know about %h - where have you found that in the printf() link I provided above?
Since your variable h is also a float you’d use the same %f (or better as suggested above %.2f) for it.

Because that string can hold more than just one value. This way you could pack all the variables into one single Particle.publish() statement.
That’s what I indicated with this too

Serial.printlnf() uses the same printf() format as snprintf() does.

Probably because you used sprintf() instead of snprintf() and %f instead of %.2f as I suggested. Without the limitatin of decimal places your float will probably render more than 7 characters.

Usually every character has meaning and counts in coding :wink:

Ok I see what you (I) did there…lol. I thought it was referring to the “t” and “h” so I changed the f to and h…anyway ya I get it now. That’s good info! Ok I am going back to change a few things.

Well what do you know, it reads the h value now. TADA…So I want to get this to thngspeak. I followed the tutorial on webhooks. So I have this in the console, which says it is kind of working.


I am thinking the DATA part should be my temp reading. But it isnt getting it.

and then this in Thingspeak
thingspeak

I know I just have something named wrong or pointing to the wrong this. But I have changed a few things and now not sure where to start again.

Thanks

As I repeatedly said or indicated: You should put your data into one single event.
You are still creating two and your webhook is only listening for one (t):

Ok I didn’t understand it’s something that you SHOULD do , I thought it was something you COULD do. I am only listening for one yes, but it doesn’t work. Plus I wasn’t sure how to get the one data point working I figured two would be more complicated. I changed it over to the %.2f and it gives me the 2 decimal points.
So what does the multiple publish statement look like? Also you can setup a webhook to listen for two at a time?

So…Particle.publish(“t”, tempString, PRIVATE); actually I don’t know how to format that for multiple items.
Can you give me a hand with that please?

I haven’t played with ThingSpeak yet, so I don’t know whether the parsing of a mulit-part payload can be done there, but the Particle integrations support JSON format and Handlebars notation to package/parse multiple values into/out of a single event.
Hence I’d go with a format like this

  char data[64];
  snprintf(data, sizeof(data), "{ \"temp\":%.2f, \"hum\":%.2f }", t, h);

and then in the webhook definition I’d go with this

Sweet, let me try that. Thanks. Oh and so I am sure, this replaces my other sprintf statements where I have two, one for temp and one for humidity…? It compiles.so I’ll upload and see
All the data changed to “null” so it’s not reading it anymore. DO I need to change my Publish statement?

Here is what I changed…

  //sprintf(tempString, "%.2f", t);
  //sprintf(humString, "%.2f", h);
  char data[64];
  snprintf(data, sizeof(data), "{ \"temp\":%.2f, \"hum\":%.2f }", t, h);`

The Publish is still

    Particle.publish("t", tempString, PRIVATE);
    Particle.publish("h", humString, PRIVATE);

So if anyone has some code that lets a Boron publish to a cloud platform please chime in . I would love to see it. There are many ways to do it, but I don’t know enough code yet to modify for my needs. Thanks

If you compared your previous sprintf() statements with your Particle.publish() statements the answer to that should be clear.

Coding is a matter of - at least trying - to understand what the individual code lines are roughly doing - just copy/pasting randomly collected code from various sources and somehow stitching that together will not suffice.

When you combine your observation

with the knowledge about what you changed the reason for this should become perfectly clear: You are not filling data into the published strings anymore so it’s to be expected that nothing (null) will be seen on the other end, right?
On the other hand you are filling a previously unknown variable data but never send that, so how on earth would one expect it to arrive in the cloud?

BTW, that’s a wrong assumption

The data is still being read from the sensor but after filling that into data it’s never used again and hence will vanish unseen.

Here ya go:
https://community.particle.io/t/boron-rc26-stability-random-reboots-how-to-debug/46423/14?u=rftop

Each Boron will need it’s own Thingspeak Channel ( API Key, saved as myWriteAPIKey ).
Each Boron will publish the API Key, so only 1 webhook is required for unlimited Boron’s.
You can update all 8 ThingSpeak fields for a channel in 1 publish, if you wish.
Note: The Webhook doesn’t care what you name your Field 1, Field 2, etc inside ThingSpeak’s Channel Settings.

.