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

1
/*
2
* ------ [Ut_12] Waspmote Using auxiliary UART Example --------
3
*
4
* Explanation: This example shows how to use some UART library functions
5
* to send commands to an external device using the auxiliar serial 1 and 2
6
* pins.
7
*
8
* Tips: Use a Waspmote USB Gateway to receive serial packets from the
9
* auxiliar pins and send answers to the Waspmote
10
* Open the Waspmote technical guide, page 59, section I/O
11
* to see how auxiliar serial 1 and 2 pins are located
12
*
13
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
14
* http://www.libelium.com
15
*
16
* This program is free software: you can redistribute it and/or modify
17
* it under the terms of the GNU General Public License as published by
18
* the Free Software Foundation, either version 3 of the License, or
19
* (at your option) any later version.
20
*
21
* This program is distributed in the hope that it will be useful,
22
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
* GNU General Public License for more details.
25
*
26
* You should have received a copy of the GNU General Public License
27
* along with this program. If not, see .
28
*
29
* Version: 3.0
30
* Design: David Gascón
31
* Implementation: Luis Miguel Martí
32
*/
33
34
// Create WaspUART object
35
WaspUART uart = WaspUART();
36
37
// Variable to store function returns
38
uint8_t answer;
39
40
// Variable to store connection to UART
41
// AUX1(1) or AUX2(2)
42
uint8_t auxiliar = 1;
43
44
void setup()
45
{
46
////////////////////////////////////////////////////////////
47
// 1. Set UART configuration to communicate with the device
48
////////////////////////////////////////////////////////////
49
USB.ON();
50
USB.println(F("Waspmote Using auxiliary UART Example"));
51
52
// Open UART 1
53
uart.setBaudrate(115200);
54
uart.setUART(SOCKET1);
55
uart.beginUART();
56
57
// Select UART 1
58
if (auxiliar == 1) Utils.setMuxAux1();
59
if (auxiliar == 2) Utils.setMuxAux2();
60
61
serialFlush(1);
62
63
////////////////////////////////////////////////////////
64
// Power on the socket
65
////////////////////////////////////////////////////////
66
// Connect device Vcc pin to the 3V3 SENSOR POWER pin
67
// If the device requiers 5V power supply, the 5V SENSOR
68
// POWER pin may be enabled with:
69
// PWR.setSensorPower(SENS_5V, SENS_ON);
70
////////////////////////////////////////////////////////
71
PWR.setSensorPower(SENS_3V3, SENS_ON);
72
}
73
74
void loop()
75
{
76
/////////////////////////////////////////////////////////
77
// 2. Send a command to a device and wait for a response
78
/////////////////////////////////////////////////////////
79
80
// Define command to send to the device
81
char message[] = {"Command #\r\n"};
82
// Define possible answers to the command
83
char answer1[] = {"OK answer\r\n"};
84
char answer2[] = {"ERROR command\r\n"};
85
char answer3[] = {"ERROR config\r\n"};
86
// Timeout for the function to return error if there is no response
87
uint32_t timeout = 10000;
88
89
////////////////////////////////////////////////////////
90
// Send command to the device
91
////////////////////////////////////////////////////////////
92
// The sendCommand function, sends "message" to the device
93
// and waits for "timeout" milliseconds to get any of
94
// the answers defined in "answer1", "answer2" or "answer3"
95
// The function returns: 0 if timeout
96
// 1 if answer 1 was found
97
// 2 if answer 2 was found
98
// 3 if answer 3 was found
99
// The function can expect up to 4 answers
100
////////////////////////////////////////////////////////////
101
102
answer = uart.sendCommand(message,answer1,answer2,answer3,timeout);
103
104
if (answer == 1)
105
{
106
// Answer was OK
107
USB.println(F("Answer was OK"));
108
}
109
if (answer == 2)
110
{
111
// Answer was ERROR command
112
USB.println(F("Answer was ERROR command"));
113
}
114
if (answer == 3)
115
{
116
// Answer was ERROR config
117
USB.println(F("Answer was ERROR config"));
118
}
119
if (answer == 0)
120
{
121
// There was no answer (timeout)
122
USB.println(F("Timeout"));
123
}
124
125
/////////////////////////////////////////////////////////
126
// 3. Wait for a command or a second answer that might
127
// take some time to be generated by the device, like
128
// sensor readings
129
/////////////////////////////////////////////////////////
130
131
// Wait for a command from the device
132
printString("Command #\r\n",1);
133
char sensor_reading[] = {"\r\n"};
134
char answer4[] = {"ERROR reading sensor\r\n"};
135
136
////////////////////////////////////////////////////////
137
// Send command to the device
138
////////////////////////////////////////////////////////////
139
// The waitFor function, waits for "timeout" milliseconds to
140
// get any of the answers defined in "sensor_reading" or
141
// "answer4"
142
// The function returns: 0 if timeout
143
// 1 if answer 1 was found
144
// 2 if answer 2 was found
145
// The function can expect up to 4 answers
146
////////////////////////////////////////////////////////////
147
answer = uart.waitFor(sensor_reading,answer4,timeout);
148
149
if (answer == 1)
150
{
151
// Parse sensor info
152
USB.print(F("Parse sensor info: "));
153
USB.println((char*)uart._buffer);
154
}
155
if (answer == 2)
156
{
157
// Answer was ERROR reading sensor
158
USB.println(F("Answer was ERROR reading sensor"));
159
}
160
161
}
Copied!
Last modified 1yr ago