UT 12: Auxiliary UARTS

This example shows how to use auxiliary UARTS

Required Materials

1 x Waspmote 1 x MiniUSB wire 1 x Battery

Notes

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 where auxiliar serial 1 and 2 pins are located

This example can be executed in Waspmote v12 and Waspmote v15

Code

/*
 *  ------ [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
 *  pins.
 *  
 *  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 . 
 *  
 *  Version:           3.0
 *  Design:            David Gascón 
 *  Implementation:    Luis Miguel Martí
 */

 // Create WaspUART object 
 WaspUART uart = WaspUART(); 
 
 // Variable to store function returns
 uint8_t answer;

 // Variable to store connection to UART
 // AUX1(1) or AUX2(2)
 uint8_t auxiliar = 1;
 
void setup()
{
  ////////////////////////////////////////////////////////////
  // 1. Set UART configuration to communicate with the device
  ////////////////////////////////////////////////////////////
  USB.ON();
  USB.println(F("Waspmote Using auxiliary UART Example"));

  // Open UART 1
  uart.setBaudrate(115200);
  uart.setUART(SOCKET1);
  uart.beginUART();
  
  // Select UART 1
  if (auxiliar == 1) Utils.setMuxAux1();
  if (auxiliar == 2) Utils.setMuxAux2();
  
  serialFlush(1);

  ////////////////////////////////////////////////////////
  // Power on the socket
  ////////////////////////////////////////////////////////
  // 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);
}

void loop()
{
  /////////////////////////////////////////////////////////
  // 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);
  
  if (answer == 1)
  {
    // Answer was OK
    USB.println(F("Answer was OK"));
  }
  if (answer == 2)
  {
    // Answer was ERROR command
    USB.println(F("Answer was ERROR command"));
  }
  if (answer == 3)
  {
    // Answer was ERROR config
    USB.println(F("Answer was ERROR config"));
  }
  if (answer == 0)
  {
    // 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
  //    sensor readings
  /////////////////////////////////////////////////////////
  
  // 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 
  // "answer4"
  // 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);
  
  if (answer == 1)
  {
    // Parse sensor info 
    USB.print(F("Parse sensor info: "));
    USB.println((char*)uart._buffer);
  }
  if (answer == 2)
  {
    // Answer was ERROR reading sensor
    USB.println(F("Answer was ERROR reading sensor"));
  }
  
}

Last updated