I am using a serial wireless transfer of temperature data,
Components used
1.lm35 temperature sensor ( signal pin connected to A0 of arduino uno)
2. hc12 serial wireless communicator( rx(hc12) to tx (uno) and tx(hc12) to rx(uno)
3. arduino uno
My arduino tx code goes like this
// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0
void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// Get the voltage reading from the LM35
int reading = analogRead(sensorPin);
// Convert that reading into voltage
float voltage = reading * (5.0 / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = voltage * 100;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println("C | ");
delay(1000); // wait a second between readings
}
and on the receiver side i have
- arduino uno
- hc 12 alone
my receiver code is
// Example 4 - Receive a number as text and convert it to an int
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
int dataNumber = 0; // new for this version
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewNumber();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
dataNumber = 0; // new for this version
dataNumber = atoi(receivedChars); // new for this version
Serial.print("This just in ... ");
Serial.println(receivedChars);
Serial.print("Data as Number ... "); // new for this version
Serial.println(dataNumber); // new for this version
newData = false;
}
}
This works really well, my temperature is sent wirelessly without any problem, My next aim is to make this secured, like for example for my project if temperature goes high beyond 45 deg celsius i need to switch on the dc motor fan (just digitalWrite(x, HIGH); AND many serial communications are present in my test area, I need help in the receiver side that the data received is only from my transmitter (first check) and if first check is fine, then it has to check for temperature >45 deg, if yes switch on tiny dc fan, if not do nothing.
I tried creating a string, tried to send constant , but not able to put in the form of code, any help would be greatly appreciated, Let God reward you guys