UT 06: Stack EEPROM
This example shows how to use the Stack EEPROM library of Waspmote in order to store and get pending frames using the EEPROM memory of the Waspmote device

Required Materials

1 x Waspmote 1 x Battery 1 x MiniUSB wire

Notes

- Note that it is possible to specify the block size to be used within the EEPROM. Greater block size implies less frame slots. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [Ut_06] - Stack EEPROM Basic operation --------
3
*
4
* Explanation: This example shows how to use the Stack EEPROM
5
* library of Waspmote in order to store and get pending frames
6
* using the EEPROM memory of the Waspmote device
7
*
8
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
9
* http://www.libelium.com
10
*
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
20
*
21
* You should have received a copy of the GNU General Public License
22
* along with this program. If not, see .
23
*
24
* Version: 3.0
25
* Design: David Gascon
26
* Implementation: Alejandro Gallego
27
*/
28
29
30
#include
31
#include
32
33
int answer;
34
35
void setup()
36
{
37
38
// set Waspmote ID
39
frame.setID("node_001");
40
41
//////////////////////////////////////////////////
42
// 1. inits the block size and the stack
43
//////////////////////////////////////////////////
44
45
// init block size
46
USB.println(F("Set Block Size to '100' Bytes"));
47
stack.initBlockSize(100);
48
49
// clear stack initializing to zeros
50
USB.println(F("Clearing Stack..."));
51
answer = stack.initStack();
52
if (answer == 1)
53
{
54
USB.println(F("Stack initialized"));
55
}
56
else
57
{
58
USB.println(F("Stack NOT initialized"));
59
}
60
61
USB.print(F("Number of stored frames:"));
62
USB.println(stack.getStoredFrames());
63
64
}
65
66
67
68
void loop()
69
{
70
71
//////////////////////////////////////////////////
72
// 2. Create a frame
73
//////////////////////////////////////////////////
74
75
// Create new frame (ASCII)
76
frame.createFrame(ASCII);
77
// set frame fields (String - char*)
78
frame.addSensor(SENSOR_STR, (char*) "Stack_example");
79
// set frame fields (Battery sensor - uint8_t)
80
frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
81
82
USB.println(F("Frame to store:"));
83
frame.showFrame();
84
85
86
//////////////////////////////////////////////////
87
// 3. push frame into the stack
88
//////////////////////////////////////////////////
89
answer = stack.push( frame.buffer, frame.length);
90
if (answer == 1)
91
{
92
USB.println(F("Frame pushed"));
93
}
94
else if (answer == 2)
95
{
96
USB.println(F("Stack is full, frame not pushed"));
97
}
98
else if (answer == 3)
99
{
100
USB.println(F("Block size too small, frame not pushed"));
101
}
102
else
103
{
104
USB.println(F("Error pushing the frame"));
105
}
106
107
108
USB.print(F("Number of stored frames:"));
109
USB.println(stack.getStoredFrames());
110
111
//////////////////////////////////////////////////
112
// 4. pop frame from the stack
113
//////////////////////////////////////////////////
114
frame.length = stack.pop( frame.buffer );
115
116
if (frame.length > 2)
117
{
118
USB.println(F("Frame extracted:"));
119
frame.showFrame();
120
}
121
else if (frame.length == 0)
122
{
123
USB.println(F("Stack is empty, frame not extracted"));
124
}
125
else if (frame.length == 1)
126
{
127
USB.println(F("Error reading the EEPROM, frame not extracted"));
128
}
129
else
130
{
131
USB.println(F("Error updating the new pointer"));
132
}
133
134
USB.print(F("Number of stored frames:"));
135
USB.println(stack.getStoredFrames());
136
137
USB.println();
138
USB.println();
139
delay(5000);
140
141
}
142
143
144
145
146
147
Copied!

Output

1
H#
2
Set Block Size to '100' Bytes
3
Clearing Stack...
4
Stack initialized
5
Number of stored frames:0
6
Frame to store:
7
===============================
8
Current ASCII Frame:
9
Length: 52
10
Frame Type: 128
11
frame (HEX): 3C3D3E800223333934373631383435236E6F64655F3030312330235354523A537461636B5F6578616D706C65234241543A393823
12
frame (STR): <=>€#394761845#node_001#0#STR:Stack_example#BAT:98#
13
===============================
14
Frame pushed
15
Number of stored frames:1
16
Frame extracted:
17
===============================
18
Current ASCII Frame:
19
Length: 52
20
Frame Type: 128
21
frame (HEX): 3C3D3E800223333934373631383435236E6F64655F3030312330235354523A537461636B5F6578616D706C65234241543A393823
22
frame (STR): <=>€#394761845#node_001#0#STR:Stack_example#BAT:98#
23
===============================
24
Number of stored frames:0
25
Frame to store:
26
===============================
27
Current ASCII Frame:
28
Length: 52
29
Frame Type: 128
30
frame (HEX): 3C3D3E800223333934373631383435236E6F64655F3030312331235354523A537461636B5F6578616D706C65234241543A393823
31
frame (STR): <=>€#394761845#node_001#1#STR:Stack_example#BAT:98#
32
===============================
33
Frame pushed
34
Number of stored frames:1
35
Frame extracted:
36
===============================
37
Current ASCII Frame:
38
Length: 52
39
Frame Type: 128
40
frame (HEX): 3C3D3E800223333934373631383435236E6F64655F3030312331235354523A537461636B5F6578616D706C65234241543A393823
41
frame (STR): <=>€#394761845#node_001#1#STR:Stack_example#BAT:98#
42
===============================
43
Number of stored frames:0
44
...
Copied!
Last modified 1yr ago