void IOToaster::setup() { // Set the pin configuration setupPins(); // Open serial communication Serial.begin(9600); Serial.setTimeout(5000); // Load the configuration if (isConfigured()) { // Normal mode _setupMode = false; loadConfiguration(); connectServer(); setActivityLedState(HIGH); } else { // Setup mode setActivityLedState(LOW); _setupMode = true; createServer(); } }
byte Radioino::receiveCommand() { // Check the setup mode command if (digitalRead(_setupButtonPin)==HIGH && !_setupMode) { _setupMode = true; _previousTime = 0; _setupModeCount = 0; } if (_setupMode) { // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentTime = millis(); if(currentTime - _previousTime > RADIOINO_SETUP_LED_BLINK_MS) { // save the last time you blinked the LED _previousTime = currentTime; setActivityLedState(!_activityLedState); _setupModeCount++; } // Exit the setup mode if (_setupModeCount > RADIOINO_SETUP_ATTEMPTS) { _setupMode = false; setActivityLedState(LOW); } } //Wait for a new command _commandResult = RADIOINO_NO_COMMAND; if (Serial.available() > 0) { if (receive()) { if (execute()) { // Build the response message beginResponse(true); // Add the sensors status sendSensorsStatus(); _commandResult = RADIOINO_COMMAND_OK; } else { // Build the error response message beginResponse(false); _commandResult = RADIOINO_COMMAND_ERROR; } // wait for another command resetCommand(); } } // end activity setActivityLedState(LOW); return _commandResult; }
byte IOToaster::receiveCommand() { if (_setupMode) { // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentTime = millis(); if (currentTime - _previousTime > IOTOASTER_SETUP_LED_BLINK_MS) { // save the last time you blinked the LED _previousTime = currentTime; // if the LED is off turn it on and vice-versa: if (_activityLedState == LOW) setActivityLedState(HIGH); else setActivityLedState(LOW); } } //Wait for a new command _commandResult = IOTOASTER_NO_COMMAND; if (Serial.available() > 0) { if (receive()) { if (_command.startsWith("+IPD")) // Check for data sent from the server { _command = _command.substring(_command.lastIndexOf(':') + 1); _customResponse = ""; if (execute()) { // Build the response message beginResponse(true); // Add the sensors status send(getInputSensorsStatus()); _commandResult = IOTOASTER_COMMAND_OK; } else { // Build the error response message beginResponse(false); _commandResult = IOTOASTER_COMMAND_ERROR; } } // wait for another command resetCommand(); } } return _commandResult; }
// Build the incoming command in a ascii-string boolean Radioino::receive() { while (Serial.available() > 0) { int value = Serial.read(); // show activity setActivityLedState(!_activityLedState); switch (value) { case RADIOINO_COMMAND_END : // Command arrived. Check if we can accept this command command address if (_command.startsWith(_startHeader) || _command.startsWith(RADIOINO_SETUP_HEADER) && _setupMode) { // Check if is not message to send the latest result again if (_command == _startHeader+"C") { // Send the latest response again Serial.println(_response); resetCommand(); return false; } // It's a regular command. execute-it return true; } else { resetCommand(); return false; } case 'R' : resetCommand(); _command = _command + char(value); return false; default: _command = _command + char(value); return false; } } return false; }