Hi Guys,
New to arduino, and my ocd is running overtime. Please can someone help or guide me in the right direction.
I will attach full code below, but I want to highlight one section of code that consumes almost 20% of storage space on my Arduino Uno. I am sending readings from a dc voltage sensor and ac voltage sensor to my mqtt server using pubsubclient library. I have tried a few things with my limited knowledge to no avail.
Problematic code:
void sendStats(){
float dcv = getDCVoltage();
float acv = getACVoltage();
String msg = "{DCOffice;" + String(dcv) + ";" + String(acv) + ";}";
client.publish("stat", (char*) msg.c_str());
}
Storage jumps from 74% to 93%. Is there anyway I can limit the damage here?
Full project:
#include <SPI.h>
#include <EthernetENC.h>
#include <PubSubClient.h>
#include <EmonLib.h>
#define VOLT_CAL 620
#define Client_ID "DCOffice"
EnergyMonitor emon1;
// Update these with values suitable for your network.
const byte mac[] PROGMEM = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress server(***, ***, ***, ***); //Constant, does not need to change during runtime
int interval=30000; //Is adjustable by sending topic: device/looptime msg: millisecs
unsigned long previousMillis=0;
void callback(char* topic, byte* payload, unsigned int length) {
//Check id, then command, then pin number
if (strcmp(topic,"DCOffice/cyclerelay")==0) {
cycleRelay(atoi(payload));
} else if (strcmp(topic,"DCOffice/switchrelayon")==0){
switchOnRelay(atoi(payload));
} else if (strcmp(topic,"DCOffice/switchrelayoff")==0){
switchOffRelay(atoi(payload));
} else if (strcmp(topic,"DCOffice/relaystatus")==0){
getRelayStatus(atoi(payload));
} else if (strcmp(topic,"DCOffice/looptime")==0){
setLoopTime(atoi(payload));
} else if (strcmp(topic,"DCOffice/updatestats")==0){
sendStats();
}
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
while (!client.connected()) {
Serial.print(F("Connecting..."));
if (client.connect("DCOffice")) {
Serial.println(F("Success"));
client.publish("NewDeviceConnected", "DCOffice");
client.subscribe("DCOffice/cyclerelay");
client.subscribe("DCOffice/switchrelayon");
client.subscribe("DCOffice/switchrelayoff");
client.subscribe("DCOffice/relaystatus");
client.subscribe("DCOffice/looptime");
client.subscribe("DCOffice/updatestats");
} else {
Serial.println(F("Retrying"));
delay(5000);
}
}
}
void setup()
{
pinMode(2, OUTPUT); //relay 1 AC/DC
pinMode(3, OUTPUT); //relay 2 AC/DC
pinMode(4, OUTPUT); //relay 3 AC/DC
pinMode(5, OUTPUT); //relay 4 AC/DC
pinMode(6, OUTPUT); //relay 5 AC/DC
pinMode(7, OUTPUT); //relay 6 AC/DC
pinMode(8, OUTPUT); //relay 7 AC/DC
pinMode(9, OUTPUT); //relay 8 AC/DC
//pinMode(A0, INPUT); //Sensor 1 N/A 14
pinMode(A1, INPUT); //Sensor 2 AC Volts 15
pinMode(A2, INPUT); //Sensor 3 DC Volts 16
//pinMode(A3, INPUT); //Sensor 4 N/A 17
//pinMode(A4, INPUT); //Sensor 5 N/A 18
//pinMode(A5, INPUT); //Sensor 6 N/A 19
//analogReference(EXTERNAL);
Serial.begin(9600);
emon1.voltage(A1, VOLT_CAL, 1.7);
client.setServer(server, 1883);
client.setCallback(callback);
//Ethernet.begin(mac, ip); //
if (Ethernet.begin(mac) == 0) {
while (true) {
delay(1);
}
}
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
//Send Stats every 30 seconds to server
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
sendStats();
previousMillis = currentMillis;
}
}
void sendStats(){
float dcv = getDCVoltage();
float acv = getACVoltage();
String msg = "{DCOffice;" + String(dcv) + ";" + String(acv) + ";}";
client.publish("stat", (char*) msg.c_str());
}
void cycleRelay(int pin){
digitalWrite(pin, HIGH);
delay(5000);
digitalWrite(pin, LOW);
//client.publish("NewDeviceConnected", "Relay Cycled");
}
void switchOffRelay(int pin){
digitalWrite(pin, LOW);
//client.publish("NewDeviceConnected", "Relay Switched Off");
}
void switchOnRelay(int pin){
digitalWrite(pin, HIGH);
//client.publish("NewDeviceConnected", "Relay Switched On");
}
void getRelayStatus(int pin){
char buffer [33];
itoa(digitalRead(pin), buffer, 10);
//client.publish("NewDeviceConnected", buffer);
}
float getACVoltage(){
emon1.calcVI(25, 1000);
return emon1.Vrms;
}
float getDCVoltage(){
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float ref_voltage = 5;
int adc_value = 0;
adc_value = analogRead(A2);
adc_voltage = (adc_value * ref_voltage) / 1024.0;
return in_voltage = adc_voltage / (R2/(R1+R2));
}
void setLoopTime(int time){
interval = time;
}
Any help or guidance would be greatly appreciated. If you also maybe can give my full code a once over and make suggestions of any pitfalls or things I am doing wrong in terms of memory/storage usage that would also be appreciated.
Thanks in advance