Hi all
I'm building a remote system for my garden lights and wireless camera. The system consists of two parts: A small hand transmitter (Arduino Nano Every) and a box (Arduino Uno SMD) that will be placed outside. I programmed all the remote switching functions and the display functions. The code has a wireless feedback function, so I get to see on the display, whether the lights or the camera are on or off.
Now I'd like to add a function which sends the battery voltage in the same 1minute intervall. The analog reading an converting is no problem. But the handling in the char array read processing is an other thing. I guess I'd have to first compare the part of the char string ("Voltage") as a condition, then substracting this char string and finally printing the part with the float variable.
Here's the code of the transmitter:
#include <SoftwareSerial.h>
#include <U8g2lib.h>
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /*data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 4);
SoftwareSerial HC12(2, 3); // RX | TX
int button1 = 6;
int button2 = 7;
int button3 = 8;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
char command1[] = "ActivateTimer1!";
char command2[] = "StopTimer1!";
char command3[] = "ToggleRelais3!";
char keyWord4[] = "VideoLinkON";
char keyWord5[] = "VideoLinkOFF";
char keyWord6[] = "LightsON";
char keyWord7[] = "LightsOFF";
const byte numChars = 32;
char messageReceived[numChars];
char VideoLinkStatus[] = "OFF";
char LightsStatus[] = "OFF";
float BattVoltage;
void draw(){
u8g2.setFont(u8g_font_profont12);
u8g2.drawStr(0, 12, "VIDEO LINK:");
u8g2.drawStr(0, 36, "GARDEN LIGHTS:");
u8g2.drawStr(0, 60, "BATTERY:");
u8g2.setCursor(90,12);
u8g2.println(VideoLinkStatus);
u8g2.setCursor(90,36);
u8g2.println(LightsStatus);
u8g2.setCursor(90,60);
u8g2.println(BattVoltage);
delay(10);
}
void setup() {
pinMode(button1, INPUT);
pinMode (button2, INPUT);
pinMode (button3, INPUT);
Serial.begin(9600);
HC12.begin(9600);
u8g2.begin();
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
static byte ndx = 0;
char charReceived;
char endMarker1 = '!';
char endMarker2 = '\r';
if (buttonState1 == HIGH) {
HC12.print(command1);
}
else if (buttonState2 == HIGH) {
HC12.print(command2);
}
else if (buttonState3 == HIGH) {
HC12.print(command3);
}
while (HC12.available() > 0) {
charReceived = HC12.read();
if (charReceived != endMarker1 && charReceived != endMarker2) {
messageReceived[ndx] = charReceived;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
messageReceived[ndx] = '\0';
ndx = 0;
delay(100);
while (HC12.available() > 0) HC12.read(); // Read incoming
if(strcmp(messageReceived, keyWord4) == 0) {
strncpy( VideoLinkStatus, "ON", sizeof(VideoLinkStatus) );
VideoLinkStatus[sizeof(VideoLinkStatus)-1] = 0;
delay(100);
}
else if(strcmp(messageReceived, keyWord5) == 0) {
strncpy( VideoLinkStatus, "OFF", sizeof(VideoLinkStatus) );
VideoLinkStatus[sizeof(VideoLinkStatus)-1] = 0;
delay(100);
}
else if(strcmp(messageReceived, keyWord6) == 0) {
strncpy( LightsStatus, "ON", sizeof(LightsStatus) );
LightsStatus[sizeof(LightsStatus)-1] = 0;
delay(100);
}
else if(strcmp(messageReceived, keyWord7) == 0) {
strncpy( LightsStatus, "OFF", sizeof(LightsStatus) );
LightsStatus[sizeof(LightsStatus)-1] = 0;
delay(100);
}
}
}
u8g2.firstPage();
do
{
draw();
}
while( u8g2.nextPage() );
delay(50);
}
And the code for the box outside:
#include <SoftwareSerial.h>
int Relais1 = 12;
int Relais2 = 13;
int Relais3 = 8;
int LightsState = 11;
int VideoLinkState = 9;
float BatteryVoltage;
float BatteryValue;
float VoltagePrint;
const byte numChars = 32;
char messageReceived[numChars];
char keyWord1[] = "ActivateTimer1";
char keyWord2[] = "StopTimer1";
char keyWord3[] = "ToggleRelais3";
char command1[] = "VideoLinkON!";
char command2[] = "VideoLinkOFF!";
char command3[] = "LightsON!";
char command4[] = "LightsOFF!";
unsigned long last = millis(); //set timer
const unsigned long INTERVAL = 1000L*60*1; // Intervall 1 Min.
unsigned long lastRun = 0 - INTERVAL; //
int StateOfLights;
int StateOfVideoLink;
SoftwareSerial HC12(2, 3);
void setup() {
HC12.begin(9600);
Serial.begin(9600); // set up Serial library at 9600 bps - this is the speed the serial interface will work all
pinMode (Relais1, OUTPUT);
pinMode (Relais2, OUTPUT);
pinMode (Relais3, OUTPUT);
pinMode (LightsState, INPUT);
pinMode (VideoLinkState, INPUT);
}
void loop() {
boolean Relais3state = digitalRead(Relais3); //check if Relais3 ist on or off. Returns 1 or 0
static byte ndx = 0;
char charReceived;
char endMarker = '!';
while (HC12.available() > 0) {
charReceived = HC12.read();
if (charReceived != endMarker) {
messageReceived[ndx] = charReceived;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
messageReceived[ndx] = '\0';
ndx = 0;
delay(100);
while (HC12.available() > 0) HC12.read();
if(strcmp(messageReceived, keyWord1) == 0) {
digitalWrite(Relais1, HIGH);
delay(100);
digitalWrite(Relais1, LOW);
}
else if(strcmp(messageReceived, keyWord2) == 0) {
digitalWrite(Relais2, HIGH);
delay(100);
digitalWrite(Relais2, LOW);
}
else if(millis() - last > 250){
if(Relais3state == 0 && (strcmp(messageReceived, keyWord3) == 0)){
digitalWrite(Relais3, HIGH);
}
else if(Relais3state == 1 && (strcmp(messageReceived, keyWord3) == 0)){
digitalWrite(Relais3, LOW);
}
}
last = millis();
}
}
if (millis() - lastRun >= INTERVAL) {
StateOfLights = digitalRead(LightsState);
StateOfVideoLink = digitalRead(VideoLinkState);
Serial.print(StateOfVideoLink);
BatteryValue = analogRead(BatteryVoltage); //reading Voltage of Battery
VoltagePrint = ((BatteryValue * 5.0) / 1024.0) * 3; //converting Voltage according to Resistor Divider
//HC12.print("Voltage");
//HC12.print(VoltagePrint); // send Voltage over RF
if(StateOfLights == HIGH) {
HC12.print(command3);
delay(500);
}
else if(StateOfLights == LOW) {
HC12.print(command4);
delay(500);
}
if(StateOfVideoLink == HIGH) {
HC12.print(command1);
delay(500);
}
else if(StateOfVideoLink == LOW) {
HC12.print(command2);
}
lastRun += INTERVAL;
}
}
Any help is very much appreciated!