Hello everyone.
I have been searching in many places to no avail.
Long story short:
I have a remote Arduino placed outside with a DHT11 sensor and it is outputting the temperature and humidity over 433MHz RF.
The code compiles fine but I am at a loss.
How in the world do I turn an LED on at a given temperature??????????
Thank you in advance.
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h> // Reciever to Pin 11
// Include dependant SPI Library
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define LED_PERFECT 2 // LED output Pin 2
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define output strings
String str_humid;
String str_temp;
String str_out;
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
int a=32; // Set fom 0 to 255 for a given brightness via PWM
const int ledPin = 3; // the pin that the LED is attached to PWM pin 3
void setup() {
pinMode(ledPin, OUTPUT); // declare pin 3 to be an output
pinMode (2 , OUTPUT); // Set LED pin
// Initialize ASK Object
rf_driver.init();
// Setup Serial Monitor
Serial.begin(9600);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
}
void loop() {
analogWrite(ledPin, a); // set the brightness of pin 9:
delay(100); // needed?
// Set buffer to size of expected message
uint8_t buf[11];
uint8_t buflen = sizeof(buf);
// Check if received packet is correct size
if (rf_driver.recv(buf, &buflen))
{
// Message received with valid checksum
// Get values from string
// Convert received data into string
str_out = String((char*)buf);
// Split string into two values
for (int i = 0; i < str_out.length(); i++) {
if (str_out.substring(i, i + 1) == ",") {
str_humid = str_out.substring(0, i);
str_temp = str_out.substring(i + 1);
break;
}
}
// Print values to Serial Monitor
Serial.print("Humidity: ");
Serial.print(str_humid);
Serial.print(" - Temperature: ");
Serial.println(str_temp);
// Print values to LCD
lcd.setCursor(3, 0);
lcd.print("Humidity: ");
lcd.print(str_humid);
lcd.setCursor(1, 1);
lcd.print("Temperature: ");
lcd.print(str_temp);
// if temp goes above X, turn the relay ON
if (str_temp = 100) {
digitalWrite(2, HIGH);// set pin 2 HIGH
}else{
digitalWrite(2, LOW);// set pin 2 LOW
}
}
}
Hi,
str_temp is a string variable being defined as a substring of a string (str_out), which is earlier declared as a string. Near the end of your loop you try to test the string variable with a decimal value (100) which is not a string and will fail. You can use the toInt() function which allows you to convert a String to an integer number. You could then test the value as an integer.
Thank you for the reply. I am baffled as to why the string is automatically converted via serial monitor and LCD without having to convert the string into a numeric value. I'm going to implement the string to int conversion process. Does the serial interface do that automatically through some embedded (unseen) protocol?
Okay. I implemented the string to int function accordingly to no avail.
The serial monitor says 0 for the function.
My ability to extrapolate only from those helpful code-snippets is fairly limited.
Could someone please explain to me exactly WHERE and HOW to insert the string to int function within my code that otherwise compiles?
All I want to do is light an LED at a set numeric value that the serial protocol seems to automatically decode into the displayed numeric value.
I'll save the code-snippets for the code-snippets page.
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h> // Reciever to Pin 11
// Include dependant SPI Library
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define LED_PERFECT 2 // LED output Pin 2
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define output strings
String str_humid;
String str_temp;
String str_out;
String inString = "str_out"; // string to hold input
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
int a=32; // Set fom 0 to 255 for a given brightness via PWM
const int ledPin = 3; // the pin that the LCD LED is attached to PWM pin 3
void setup() {
pinMode(ledPin, OUTPUT); // declare pin 3 to be an output
pinMode (2 , OUTPUT); // Set LED pin
// Initialize ASK Object
rf_driver.init();
// Setup Serial Monitor
Serial.begin(9600);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
}
void loop() {
analogWrite(ledPin, a); // set the brightness of pin 9:
delay(100); // needed?
// Set buffer to size of expected message
uint8_t buf[11];
uint8_t buflen = sizeof(buf);
// Check if received packet is correct size
if (rf_driver.recv(buf, &buflen))
{
// Message received with valid checksum
// Get values from string
// Convert received data into string
str_out = String((char*)buf);
// Split string into two values
for (int i = 0; i < str_out.length(); i++) {
if (str_out.substring(i, i + 1) == ",") {
str_humid = str_out.substring(0, i);
str_temp = str_out.substring(i + 1);
break;
}
}
// Print values to Serial Monitor
Serial.print("Humidity: ");
Serial.print(str_humid);
Serial.print(" - Temperature: ");
Serial.print(str_temp);
// Print values to LCD
lcd.setCursor(3, 0);
lcd.print("Humidity: ");
lcd.print(str_humid);
lcd.setCursor(1, 1);
lcd.print("Temperature: ");
lcd.print(str_temp);
Serial.println(inString.toInt()); // String to int function
// if temp goes above X, turn the relay ON
if (inString.toInt() > 60) {
digitalWrite(2, HIGH);// set pin 2 HIGH
}else{
digitalWrite(2, LOW);// set pin 2 LOW
}
}
}
I can't see where inString is updated. It was declared = "str_out" which is itself a literal string value. It can't be tested as a decimal because it is a string. It's printed value looks like 0 because the value of inString which is the literal string "str_out" cannot be converted by toInt() to a decimal value.
example:
intTemp = str_out.toInt(); // Now the value in str_out (say it is 59(an ascii string)) is an integer in inTemp whose value 59
// Now you can test the integer value in intTemp against a decimal (e.g. >60 or <60)
Note that you still have to update the value of intTemp after str_out has been updated by the incoming signal and before you test intTemp. Your code at present never updates inString after it is declared and given a string value. "str_out" is a string not an integer, regardless of the value of str_out.
It just became clear to me now.
// if temperature goes below X, turn the relay ON
if (str_temp.toInt() < 35.00) {
digitalWrite(4, HIGH);// set pin 4 HIGH
} else {
digitalWrite(4, LOW);// set pin 4 LOW
}
It seems self explanatory now but it didn't a week ago. Thank you all for your help. 