-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaareDisplay.ino
234 lines (201 loc) · 4.8 KB
/
aareDisplay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Display Aare temperature on a screen
*
* Digit selectors D1-D4 are connected on GPA0-GPA3
*
* Segments are connected on GPB0-GPB8
*/
#include <ESP8266WiFi.h> // ESP8266 Core WiFi Library
#include <DNSServer.h> // Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> // Local WebServer used to serve the configuration portal
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ESP8266HTTPClient.h> // GET client
#include <Wire.h> // I2C bus library
#include <ArduinoJson.h> // Decode JSON responses
// Local config
String API_URL = "http://aare.schwumm.ch/api/current?app=ch.existenz.aaredisplay&version=1.0.0 ";
String API_temperaturkey = "temperature";
int DTime = 4; // [ms] Display refresh time
float temperature = 0.0; // [°C] Temperature value cache
long mydelay = 300 * 1000; // [ms] Time between updates from webservice (5 min)
long timer = millis(); // Global refresh timer
void setup()
{
// Setup serial port and I2C bus
Serial.begin(115200);
pinMode(16, OUTPUT); // Turn on internal LED
digitalWrite(16, LOW);
Wire.pins(5, 4); // Corresponds to D2, D1
Wire.begin(); // Defaults to address 0
Wire.beginTransmission(0x20);
Wire.write(0x00); // A Port
Wire.write(0x00); // Set entire port to output
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x01); // B Port
Wire.write(0x00); // Set entire port to output
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x13); // B Port
Wire.write(B10000000); // Show dots everywhere
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x12); // A Port
Wire.write(B00000000); // Turn on all digits
Wire.endTransmission();
// Setup WiFi
WiFiManager wifiManager;
wifiManager.autoConnect("AareDisplay");
// Output WiFi information
Serial.print("Connected to WiFi: ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Using API: ");
Serial.println(API_URL);
digitalWrite(16, HIGH); // Turn off connection LED
}
void loop()
{
if (millis() > timer)
{ // Fetch new value from API
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
http.begin(API_URL);
int httpCode = http.GET();
if (httpCode > 0)
{
String payload = http.getString();
Serial.println(payload);
// Parse JSOn
StaticJsonBuffer<500> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(payload);
temperature = root[API_temperaturkey];
Serial.println(temperature);
}
http.end();
}
timer = millis() + mydelay;
}
showValue(temperature);
}
/**
* Show float value
*
* Note: This is sometimes off by a hundreth degree...
*
* @param float value
*/
void showValue(float value)
{
int newValue = (value)*100;
int hundrets = newValue % 10;
int tenths = (newValue / 10) % 10;
int ones = (newValue / 100) % 10;
int tenners = (newValue / 1000) % 10;
if (tenners > 0)
{
showNumber(4, tenners, false);
}
else
{
showNothing(4);
}
showNumber(3, ones, true);
showNumber(2, tenths, false);
showNumber(1, hundrets, false);
}
/**
* Show number on a single digit
*
* @param int digit (1-4)
* @param int x
* @param bool dot
*/
void showNumber(int digit, int x, bool dot)
{
showPattern(digit, pickNumber(x, dot));
}
/**
* Show nothing here
*
* @param int digit (1-4)
*/
void showNothing(int digit)
{
showPattern(digit, 0);
}
/**
* Show this bit pattern
*
* @param int digit [1-4]
* @param int pattern
*/
void showPattern(int digit, int pattern)
{
int digitBits = 0;
bitSet(digitBits, digit - 1);
Wire.beginTransmission(0x20);
Wire.write(0x13); // B Port
Wire.write(pattern); // Set pattern
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x12); // A Port
Wire.write(~digitBits); // Turn on this digit
Wire.endTransmission();
delay(DTime);
Wire.beginTransmission(0x20);
Wire.write(0x12); // A Port
Wire.write(0x0F); // Turn off all digits
Wire.endTransmission();
}
/**
* Return pinout for this number, including the dot
*
* @param int x
* @param bool dot
* @return int
*/
int pickNumber(int x, bool dot)
{
byte r = B0;
switch (x)
{ //.GFEDCBA
case 1:
r = B00000110;
break;
case 2:
r = B01011011;
break;
case 3:
r = B01001111;
break;
case 4:
r = B01100110;
break;
case 5:
r = B01101101;
break;
case 6:
r = B01111101;
break;
case 7:
r = B00000111;
break;
case 8:
r = B01111111;
break;
case 9:
r = B01101111;
break;
default:
r = B00111111;
break;
}
if (dot)
{
r = r | B10000000;
}
return r;
}