I have a Solar panel for my pool. Last year I had my core monitoring 3 temperature sensors (ambient, water going in to panel, water coming out of panel.) I also had a relay controlling my pump for the collector which i can turn on or off thru IFTTT using a SMS message. The pool temperatures are also published to a google doc and Spark variable to allow me to monitor it using FreeBoard. Everything worked great. Winter came and i repurposed my core.
Now i want to flash my old code to the same core with the same setup, I am getting a Hard Fault error immediately after loading my code. I have 2 cores and have tried flashing both with the same result.
I’m using the web IDE.
I have reloaded tinker and that works however loading my code again results in a hard fault. (using CLI, Windows10)
Below is my code if someone could please look to see if some syntax has changed since i originally Programmed this.
char resultstr[120];
int x, RecordNum;
int AirVar, OutVar, InVar, PumpMem, Delta, DeltaCount;
float AirMem, OutMem, InMem;
float Air,Out,In;
int NewData = 2;
unsigned long oldTime = 0;
char ChangeAir = ' ';
char ChangeOut = ' ';
char ChangeIn = ' ';
char ChangePump = ' ';
int PumpState = 1;
void setup() {
pinMode(A0, INPUT); // setup A0 as analog input
pinMode(A1, INPUT); // setup A1 as analog input
pinMode(A2, INPUT); // setup A2 as analog input
pinMode(D0, OUTPUT); // setup D2 as digital output
pinMode(D7, OUTPUT); // setup D7 as digital output
// expose your char buffer to the Cloud API
Particle.variable("result", resultstr);
Particle.variable("Air", AirVar);
Particle.variable("Out", OutVar);
Particle.variable("In", InVar);
Particle.variable("Record",RecordNum);
Particle.variable("PumpState", PumpState);
Particle.function("Text", FuncText);
}
void FuncPumpPower() {
EEPROM.put(10,PumpState); //Record to eeprom the Pumpstate to recall on power failure
digitalWrite(D0,!PumpState); // Turn Pump ON or OFF. High = OFF / Low = ON
if (PumpState != PumpMem) {
NewData = 1;
ChangePump = 'P';
if(PumpState) // Print to dashboard Power State
Particle.publish("PumpState","On", PRIVATE);
else if(!PumpState)
Particle.publish("PumpState","Off", PRIVATE);
}
}
// this function automagically gets called upon a matching POST request from IFTTT
int FuncText(String command)
{
// look for the matching argument "off" <-- max of 64 characters long
if (command == "Off") {
PumpState = 0;
Particle.publish("TextMsgRecvd","Pump Off", PRIVATE);
FuncPumpPower();
return 1;
}
if (command == "On") {
PumpState = 1;
Particle.publish("TextMsgRecvd","Pump On", PRIVATE);
FuncPumpPower();
return 1;
}
else
return -1;
}
void loop() {
if ((millis() - oldTime) >= 1000) {
oldTime = millis();
++x;
switch (x) {
case(1): {
digitalWrite(D7,1);
Air = analogRead(A1);
delay(100);
Air = analogRead(A1)*3.3/4095.0;
Air = ((Air - 0.5)*100)+1;
break;
}
case(6): {
digitalWrite(D7,1);
In = analogRead(A2);
delay(100);
In = analogRead(A2)*3.3/4095.0;
In = ((In - 0.5)*100)+1;
break;
}
case(11): {
digitalWrite(D7,1);
Out = analogRead(A0);
delay(100);
Out = analogRead(A0)*3.3/4095.0;
Out = ((Out - 0.5)*100)+1;
break;
}
case(16): {
digitalWrite(D7,1);
FuncPumpPower();
break;
}
case(21): {
digitalWrite(D7,1);
if (((Air - AirMem) > 1) || ((AirMem - Air) > 1)) {
NewData = 1;
ChangeAir = 'A';
String strTemp = String(AirMem);
Particle.publish("OldAirTemp",strTemp, PRIVATE);
delay(10);
strTemp = String(Air);
Particle.publish("NewAirTemp",strTemp, PRIVATE);
}
if (((In - InMem) > 1) || ((InMem - In) > 1)) {
NewData = 1;
ChangeIn = 'I';
String strTemp = String(InMem);
Particle.publish("OldInTemp",strTemp, PRIVATE);
delay(10);
strTemp = String(In);
Particle.publish("NewInTemp",strTemp, PRIVATE);
}
if (((Out - OutMem) > 1) || ((OutMem - Out) > 1)) {
NewData = 1;
ChangeOut = 'O';
String strTemp = String(OutMem);
Particle.publish("OldOutTemp",strTemp, PRIVATE);
delay(10);
strTemp = String(Out);
Particle.publish("NewOutTemp",strTemp, PRIVATE);
}
if (NewData == 1) {
AirMem = Air;
OutMem = Out;
InMem = In;
PumpMem = PumpState;
AirVar = Air;
OutVar = Out;
InVar = In;
if ((Out - In) > 1) { // Delta needs to only show a positive for sms reasons must be 2 degrees or more to change
++DeltaCount;
if(DeltaCount > 100) {
Delta = Out - In;
Particle.publish("Delta","Heating", PRIVATE);
}
}
if (((Out-In) <= 0) && (Delta != 0)) { // delta shows zero
Delta = 0;
DeltaCount = 0;
Particle.publish("Delta","Cooling", PRIVATE);
}
++RecordNum;
EEPROM.put(9,RecordNum); //Record to eeprom the RecordNum to recall on power failure
// format your data as JSON, don't forget to escape the double quotes
sprintf(resultstr, "%d,%d,%.1f,%.1f,%.1f,%c%c%c%c", RecordNum, PumpState, Air, In, Out, ChangePump, ChangeAir, ChangeIn, ChangeOut);
Particle.publish("NewRecord",resultstr, PRIVATE);
NewData = 0;
ChangeAir = ' ';
ChangeOut = ' ';
ChangeIn = ' ';
ChangePump = ' ';
}
else {
sprintf(resultstr, "%d,%d,%.1f,%.1f,%.1f", RecordNum, PumpState, Air, In, Out);
Particle.publish("RecPumpAirInOut",resultstr, PRIVATE);
}
break;
}
case(30): {
x = 0;
break;
}
default: {
digitalWrite(D7,0);
}
}
}
}
void Initalize() {
EEPROM.get(10,PumpState);
EEPROM.get(9,RecordNum);
FuncPumpPower();
}
STARTUP( Initalize() );