Hey Guys,
I've been struggling with this for a few hours now, I'm writing a program that needs to have a serial command interface, and accept and save a few different values to the EEProm.
The Temprature is saving correctly, but I'm not sure whats happening with the phone numbers.
I believe it may be because the phone numbers are a character array?
#include <SoftwareSerial.h>
#include <SerialCommand.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
#define arduinoLED 13 // Arduino LED on board
#define EEPROMStart 32 // Start write and read for EEPROM
#define TX 1
#define RX 0
SoftwareSerial SoftSerial = SoftwareSerial(RX,TX); // The SoftwareSerial Object
SerialCommand SCmd(SoftSerial); // The demo SerialCommand object, using the SoftwareSerial Constructor
struct config_t
{
int alertHigh;
int alertLow;
char* phone1;
char* phone2;
} configuration;
void setup()
{
//Read our last state from EEPROM
EEPROM_readAnything(EEPROMStart, configuration);
pinMode(arduinoLED,OUTPUT); // Configure the onboard LED for output
digitalWrite(arduinoLED,LOW); // default to LED off
Serial.begin(9600);
SoftSerial.begin(9600);
// Setup callbacks for SerialCommand commands
SCmd.addCommand("temp",setTemp); // Converts two arguments to integers and echos them back
SCmd.addCommand("phone",setPhone); // Converts two arguments to integers and echos them back
SCmd.addCommand("config",showConfig); // Converts two arguments to integers and echos them back
SCmd.addCommand("default",clearEeprom); // Converts two arguments to integers and echos them back
SCmd.addDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
showConfig();
Serial.println("Ready");
}
void loop()
{
SCmd.readSerial(); // We don't do much, just process serial commands
}
void setTemp()
{
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
configuration.alertHigh=atoi(arg); // Converts a char string to an integer
Serial.print("HIGH: ");
Serial.println(configuration.alertHigh);
}
else {
Serial.println("TEMP <HIGH> <LOW>");
return;
}
arg = SCmd.next();
if (arg != NULL)
{
configuration.alertLow=atol(arg);
Serial.print("LOW: ");
Serial.println(configuration.alertLow);
}
else {
Serial.println("TEMP <HIGH> <LOW>");
return;
}
EEPROM_writeAnything(EEPROMStart, configuration);
}
void setPhone()
{
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
configuration.phone1=arg;
Serial.println(configuration.phone1);
}
else {
Serial.println("phone <number1> <number2>");
return;
}
arg = SCmd.next();
if (arg != NULL)
{
configuration.phone2=arg;
Serial.println(configuration.phone2);
}
else {
Serial.println("phone <number1> <number2>");
return;
}
EEPROM_writeAnything(EEPROMStart, configuration);
}
void showConfig(){
Serial.println("Reading Config from EEPROM:");
EEPROM_readAnything(EEPROMStart, configuration);
Serial.println("Current Config:");
Serial.print(" Alert High: ");
Serial.println(configuration.alertHigh);
Serial.print(" Alert Low: ");
Serial.println(configuration.alertLow);
Serial.print(" Phone 1: ");
Serial.println(configuration.phone1);
Serial.print(" Phone 2: ");
Serial.println(configuration.phone2);
}
void clearEeprom(){
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
Serial.print("eepromCleared");
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized()
{
Serial.println("Unrecognized");
}