* ------ [Ut_12] Waspmote Using auxiliary UART Example --------
* Explanation: This example shows how to use some UART library functions
* to send commands to an external device using the auxiliar serial 1 and 2
* Tips: Use a Waspmote USB Gateway to receive serial packets from the
* auxiliar pins and send answers to the Waspmote
* Open the Waspmote technical guide, page 59, section I/O
* to see how auxiliar serial 1 and 2 pins are located
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
* Implementation: Luis Miguel Martí
// Create WaspUART object
WaspUART uart = WaspUART();
// Variable to store function returns
// Variable to store connection to UART
////////////////////////////////////////////////////////////
// 1. Set UART configuration to communicate with the device
////////////////////////////////////////////////////////////
USB.println(F("Waspmote Using auxiliary UART Example"));
uart.setBaudrate(115200);
if (auxiliar == 1) Utils.setMuxAux1();
if (auxiliar == 2) Utils.setMuxAux2();
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Connect device Vcc pin to the 3V3 SENSOR POWER pin
// If the device requiers 5V power supply, the 5V SENSOR
// POWER pin may be enabled with:
// PWR.setSensorPower(SENS_5V, SENS_ON);
////////////////////////////////////////////////////////
PWR.setSensorPower(SENS_3V3, SENS_ON);
/////////////////////////////////////////////////////////
// 2. Send a command to a device and wait for a response
/////////////////////////////////////////////////////////
// Define command to send to the device
char message[] = {"Command #\r\n"};
// Define possible answers to the command
char answer1[] = {"OK answer\r\n"};
char answer2[] = {"ERROR command\r\n"};
char answer3[] = {"ERROR config\r\n"};
// Timeout for the function to return error if there is no response
uint32_t timeout = 10000;
////////////////////////////////////////////////////////
// Send command to the device
////////////////////////////////////////////////////////////
// The sendCommand function, sends "message" to the device
// and waits for "timeout" milliseconds to get any of
// the answers defined in "answer1", "answer2" or "answer3"
// The function returns: 0 if timeout
// 1 if answer 1 was found
// 2 if answer 2 was found
// 3 if answer 3 was found
// The function can expect up to 4 answers
////////////////////////////////////////////////////////////
answer = uart.sendCommand(message,answer1,answer2,answer3,timeout);
USB.println(F("Answer was OK"));
// Answer was ERROR command
USB.println(F("Answer was ERROR command"));
// Answer was ERROR config
USB.println(F("Answer was ERROR config"));
// There was no answer (timeout)
USB.println(F("Timeout"));
/////////////////////////////////////////////////////////
// 3. Wait for a command or a second answer that might
// take some time to be generated by the device, like
/////////////////////////////////////////////////////////
// Wait for a command from the device
printString("Command #\r\n",1);
char sensor_reading[] = {"\r\n"};
char answer4[] = {"ERROR reading sensor\r\n"};
////////////////////////////////////////////////////////
// Send command to the device
////////////////////////////////////////////////////////////
// The waitFor function, waits for "timeout" milliseconds to
// get any of the answers defined in "sensor_reading" or
// The function returns: 0 if timeout
// 1 if answer 1 was found
// 2 if answer 2 was found
// The function can expect up to 4 answers
////////////////////////////////////////////////////////////
answer = uart.waitFor(sensor_reading,answer4,timeout);
USB.print(F("Parse sensor info: "));
USB.println((char*)uart._buffer);
// Answer was ERROR reading sensor
USB.println(F("Answer was ERROR reading sensor"));