Sorry error with posting:
this was the start --
Hi there guys.
I am building a smart home system and currently working on the master slave rs485 part of it. I would love some tips on my code to try and make it run smoother. At the moment it is a bit frankenstein code. But it seems like good stuff.
So the Master Arduino mega with esp8266 connected receives GET request and then sends an rs485 message to the slave, which then checks its hall current sensor to see if the switch is open, if it is it will either reset or set a dual latch relay.
This is my code:
SLAVE
/*
Measuring AC Current Using ACS712
*/
const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
/*
Measuring AC Current Using ACS712
*/
//RS485 Intros
/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
//RS485 Intros
const byte setPin = 6; // connect to relay coil "S" pin
const byte resetPin = 7; // connect to relay coil "R" pin
int ReplyCommand = 0;
void setup() {
Serial.begin(9600);
Serial.println("SerialRemote"); // Can be ignored
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
pinMode(setPin, OUTPUT);
pinMode(resetPin, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(9600); // set the data rate
}
void loop() {
Voltage = getVPP();
VRMS = (Voltage / 2.0) * 0.707;
AmpsRMS = ((VRMS * 1000) / mVperAmp) - 0.08;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
//Copy input data to output
if (RS485Serial.available())
{
byteSend = RS485Serial.read(); // Read the byte
digitalWrite(Pin13LED, HIGH); // Show activity
delay(10);
digitalWrite(Pin13LED, LOW);
if (byteSend == '4')
{
digitalWrite (setPin, HIGH);
delay (40);
digitalWrite (setPin, LOW);
ReplyCommand = 1;
}
if (byteSend == '3')
{
digitalWrite (resetPin, HIGH);
delay (40);
digitalWrite (resetPin, LOW);
ReplyCommand = 1;
}
if (byteSend == '5')
{
if (AmpsRMS > 0.09)
{
digitalWrite (setPin, HIGH);
delay (40);
digitalWrite (setPin, LOW);
ReplyCommand = 1;
}
else
{
digitalWrite (resetPin, HIGH);
delay (40);
digitalWrite (resetPin, LOW);
ReplyCommand = 1;
}
}
// delay(100);
}// End If RS485SerialAvailable
if (ReplyCommand == 1) {
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(byteSend); // Send the byte back
RS485Serial.write(AmpsRMS); // Send the byte back
delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
ReplyCommand = 0;
}
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while ((millis() - start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5.0) / 1024.0;
return result;
}
Thank you in advance