Gooday!
I have a small project requiring soil moisture, humidity, temp and a lumens sensor to send data to Azure IOT Hub. I get a NULL response in the event console. Here is the code I’m having trouble with. Having trouble replacing Ubidots.
#include "SparkFunRHT03/SparkFunRHT03.h"
const int RHT03_DATA_PIN = D3; // RHT03
const int LIGHT_PIN = A0; // Photocell
const int LED_PIN = D7; // LED
RHT03 rht; // RTH03 object
// UbiDots Creds
#include <Ubidots.h>
#define TOKEN "BBFF-jsTngKYRdS8B03gA0Npc22w5IuMC9D"
// UbiDots Creds
unsigned int minimumLight = 65536;
unsigned int maximumLight = 0;
float minimumTempC = 5505;
float maximumTempC = 0;
float minimumTempF = 9941;
float maximumTempF = 0;
float minimumHumidity = 100;
float maximumHumidity = 0;
int val = 0;//soil value
int soil = A2;
int soilPower = D6;
#define PRINT_RATE 5000 // Global Sensors Update time in ms.
const int speakerPin = D2;
const int songLength = 18;
char notes[] = "d c d c d c d c d c";
int beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int tempo = 150;
Ubidots ubidots(TOKEN);
void setup()
{
Serial.begin(9600);
rht.begin(RHT03_DATA_PIN);
pinMode(LIGHT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Mositure Sensor
pinMode(speakerPin, OUTPUT);
pinMode(soilPower, OUTPUT);
digitalWrite(soilPower, LOW);
}
void loop()
{
digitalWrite(LED_PIN, HIGH);
int update = rht.update();
if (update == 1)
{
unsigned int light = analogRead(LIGHT_PIN);
if (light > maximumLight) maximumLight = light;
if (light < minimumLight) minimumLight = light;
float humidity = rht.humidity();
if (humidity > maximumHumidity) maximumHumidity = humidity;
if (humidity < minimumHumidity) minimumHumidity = humidity;
float tempF = rht.tempF();
if (tempF > maximumTempF) maximumTempF = tempF;
if (tempF < minimumTempF) minimumTempF = tempF;
float tempC = rht.tempC();
if (tempC > maximumTempC) maximumTempC = tempC;
if (tempC < minimumTempC) minimumTempC = tempC;
// serial prints
// Moisture Sensor
Serial.print("Plant Moisture = ");
Serial.print(readSoil());
Serial.println();
Serial.print("Light: "); // Print "Light: "
Serial.print(light); // Print the light reading
Serial.print(" ("); // Print " ("
Serial.print(minimumLight); // Print the minimum light reading
Serial.print('/'); // Print a '/' -- single quotes mean we're only sending one character
Serial.print(maximumLight); // Print the maximum light reading.
Serial.println(") (min/max)"); // Finish the line by printing ") (min/max)"
// The full line will look something like: "Light: 545 (8/791) (min/max)"
// Print the temperature in °C:
// Example printout: "Temp: 24.9 °C (23.5/24.5) (min/max)"
Serial.print("Temp: ");
Serial.print(tempC, 1); // Printing a float, we can set the precision (number of decimal points) with the second parameter
Serial.print(" ");
// `write()` can be used to write a SINGLE BYTE value over the serial line:
Serial.write(248); // 248 is the ASCII value for the ° symbol. We're fancy.
Serial.print("C (");
Serial.print(minimumTempC, 1);
Serial.print('/'); // Print a '/'
Serial.print(maximumTempC, 1);
Serial.println(") (min/max)");
// Print the temperature in °F:
// Example printout: "Temp: 76.1 °F (74.3/76.1) (min/max)"
Serial.print("Temp: "); // Print "Temp: "
Serial.print(tempF, 1); // Print the tempF variable -- 1 decimal point
Serial.print(' '); // Print a space
Serial.write(248); // Print ASCII value 248 (the ° symbol)
Serial.print("F ("); // Print "F ("
Serial.print(minimumTempF, 1); // Print minimum temperature -- 1 decimal point
Serial.print('/'); // Print a '/'
Serial.print(maximumTempF, 1); // Print maximum temp -- 1 decimal point
Serial.println(") (min/max)"); // Finsh the line by printing ") (min/max)"
// Print the relative humidity:
// Example printout: Humidity: 29.7 % (29.10/41.80) (min/max)
Serial.print("Humidity: ");
Serial.print(humidity, 1);
Serial.print(" %");
Serial.print(" (");
Serial.print(minimumHumidity, 1);
Serial.print("/");
Serial.print(maximumHumidity, 1);
Serial.println(") (min/max)");
Serial.println(); // Print a blank line:
//ubidots commit
ubidots.add("temp", tempF);
ubidots.add("light", light);
ubidots.add("humid", humidity);
ubidots.add("moist", readSoil());-
ubidots.sendAll();
//notify particle console of update
Spark.publish("sensor-update-status", "success");
}
else // If the update failed, give the sensor time to reset:
{
RGB.control(true);
RGB.color(255, 0, 0);
delay(500);
RGB.control(false);
Serial.println("Error reading from the RHT03."); // Print an error message
Serial.println(); // Print a blank line
ubidots.add("errors", 1);
ubidots.sendAll();
Spark.publish("sensor-update-status", "error, bad reading");
delay(RHT_READ_INTERVAL_MS); // The RHT03 needs about 1s between read attempts
}
digitalWrite(LED_PIN, LOW); // Turn the LED off
delay(PRINT_RATE); // delay for 1s, printing too much will make the output very hard to read.
if(readSoil() < 3200)
{
// take control of the RGB LED
RGB.control(true);
RGB.color(255, 0, 0);//set RGB LED to Red
///////////ALARM SOUND///////////////
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(speakerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
///////////ALARM SOUND///////////////
}
else
{
// turn led green for good status
RGB.control(true);
RGB.color(0, 255, 0);
}
}
//This is a function used to get the soil moisture content
int readSoil()
{
digitalWrite(soilPower, HIGH);//turn D6 "On"
delay(10);//wait 10 milliseconds
val = analogRead(soil);
digitalWrite(soilPower, LOW);//turn D6 "Off"
return val;
}
///////////// sound data ///////////////////
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
//end file