· 5 years ago · Jun 17, 2020, 05:20 PM
1#include <Arduino.h>
2
3//WiFi
4#include <ESP8266WiFi.h>
5#include <ESP8266HTTPClient.h>
6#include <WiFiUdp.h>
7
8#include <WiFiManager.h>
9#include <DNSServer.h>
10#include <ESP8266WebServer.h>
11
12//NTP
13#include <TimeLib.h> // library for keeping time
14#include <NTPClient.h> // helper functions for requesting ntp time
15
16//Json parsing
17//#include <JsonStreamingParser.h>
18//#include <JsonListener.h>
19#include <OpenWeatherMapCurrent.h>
20//#include <CurrencyRate.h>
21
22//fastLED and GFX libs
23#include <SPI.h>
24#define FASTLED_ESP8266_RAW_PIN_ORDER
25#include <FastLED.h>
26#include <Adafruit_GFX.h>
27#include <FastLED_NeoMatrix.h>
28
29//frame class for creating and displaying frames;
30#include <Frame.h>
31
32//Misc
33#include <sprites.h> //sprite bitmaps
34
35//#define sensorPin 0
36
37// WiFi ---------------------------
38//const char *WIFI_SSID = " "; //wifi ssid if you don't want to use wifimanager
39//const char *WIFI_PWD = " "; //wifi pass
40// WiFi ---------------------------
41
42
43
44//Json parsing -----------------------------------
45WiFiUDP Udp; //wificlient for json
46
47OpenWeatherMapCurrent wClient; //initialize client for parsing weather data
48OpenWeatherMapCurrentData weatherData; //initialize data structure for holdng weather data
49const String OPEN_WEATHER_MAP_APP_ID = "9759e82eeb731cf943a728dff200ff90"; //your openweathermap api key
50const String OPEN_WEATHER_MAP_LOCATION_ID = "3054643"; //your city code
51const String OPEN_WEATHER_MAP_LANGUAGE = "en"; //english
52const boolean IS_METRIC = true; //get metric, false for imperial units
53
54//CurrencyRate cClient;
55//CurrencyRateData ratesData;
56//const String CurrencyRateUrl = "http://www.floatrates.com/daily/sek.json"; //url json for currencyrates of chosen currency,
57//Json parsing ------------------------------------
58
59
60
61//NTP----------------------------------------------
62const int NTP_PACKET_SIZE = 48; //NTP time is in the first 48 bytes of message
63byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
64const char *ntpServerName = "1.hu.pool.ntp.org"; //server name for ntp server
65const int UTC_OFFSET = 2; //set utc offset
66unsigned int localPort = 8888; //local port to listen for UDP packets
67
68time_t getNtpHelper() //passing function without argument to return time_t for setsyncinterval
69{
70 return getNtpTime(Udp);
71}
72//NTP----------------------------------------------
73
74
75//FastLED NeoMatrix -------------------------------
76#define mw 32 //matrix width
77#define mh 8 //matrix height
78#define NUMMATRIX (mw * mh) //number of leds
79
80#define DATA_PIN D2 //data for led comms
81#define BRIGHTNESS 6 //starting brightness [0,255]
82
83CRGB leds[NUMMATRIX]; //initialize led array
84
85FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, mw, mh, //matrix initialization
86 NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
87 NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG);
88//FastLED NeoMatrix -------------------------------
89
90
91
92//Frame----------------------------------------------
93uint8_t Frame::sensorPin = 0; //set sensorPin for all frames (static member)
94
95TimeFrame timeFrame(30, 0xFFFF); // create object of subclasses of frame(duration, textcolor, sensorpin for brightness control)
96
97//StaticFrame dateFrame(5, 0x07E0); //green
98//StaticFrame dollarFrame(5,0x07FF); //cyan
99//StaticFrame euroFrame(5, 0xFEE0); //yellow
100
101AnimatedFrame weatherFrame(5, 0xFBE0); //orange
102frameData fdata; //create data structure containing pointer to array of animation frames and number of frames
103//Frame-----------------------------------------------
104
105
106void setup()
107{
108
109 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUMMATRIX); //Matrix setup
110 FastLED.setBrightness(BRIGHTNESS);
111 matrix->setTextWrap(false);
112 matrix->setTextSize(1);
113
114 Serial.begin(9600);
115
116 /*
117 WiFi.begin(WIFI_SSID, WIFI_PWD); //Wifi setup if you want to use hardcoded SSID and pass
118
119 //Serial.print("Connecting");
120 uint8_t count = 0;
121
122 while (WiFi.status() != WL_CONNECTED)
123 {
124 matrix->drawRGBBitmap(9, 0, wifi_sprite[count], 13, 8);
125 matrix->show();
126 delay(200);
127 matrix->clear();
128 count = (count + 1) & 3;
129 }
130
131 matrix->clear();
132 matrix->show();
133 */
134
135 WiFiManager wifiManager; //remove if you decide to use hardcoded SSID and password instead
136 delay(1000);
137
138 wifiManager.autoConnect();
139
140 Udp.begin(localPort); //ntp
141
142 setSyncProvider(getNtpHelper); //get NTP time
143 setSyncInterval(30000); //resync time every 10 mins (300s)
144
145 matrix->drawRGBBitmap(12, 1, checkmark, 6, 6); // draw checkmark to indicate successfull connection
146 matrix->show();
147
148 ///cClient.updateCurrent(&ratesData, CurrencyRateUrl); //get currency data
149 wClient.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
150 wClient.setMetric(IS_METRIC);
151 wClient.updateCurrentById(&weatherData, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID); //get weather data
152
153 matrix->clear();
154
155 //Serial.println();
156 //Serial.print("Connected, IP address: ");
157 //Serial.println(WiFi.localIP());
158
159 //dateFrame.setBitmap(calendar[day()-1], 9, 8); //bitmap array, width, height
160 //dateFrame.setText(getDay(), 3); //text to be displayed, offset from bitmap
161
162 //dollarFrame.setBitmap(currencySprite[0], 8, 8);
163 //dollarFrame.setText(String(ratesData.USD) + "." + String(ratesData.usdF), 0);
164 //dollarFrame.digitFont();
165
166 //euroFrame.setBitmap(currencySprite[1], 8, 8);
167 //euroFrame.setText(String(ratesData.EUR) + "." + String(ratesData.eurF), 0);
168 //euroFrame.digitFont();
169
170 fdata = weatherBitmap(weatherData.weatherId); //choose bitmap array based on weatherId and get nrOfFrames(rows) from the array, defined in Frames.cpp
171 weatherFrame.setBitmap(fdata.bitmap, fdata.frameCount, 200, 8, 8); //setBitmap(2dim array with animation frames, nr of frames, fps, width, height)
172 weatherFrame.setText(String(weatherData.temp) + "'" + "C", 2);
173 weatherFrame.digitFont();
174
175}
176
177
178void loop()
179{
180
181 timeFrame.display();
182 //dateFrame.display();
183 //dollarFrame.display();
184 //euroFrame.display();
185 weatherFrame.display();
186
187 //fix method to update day more frequently later on
188
189 EVERY_N_MINUTES(10) //display something for this?
190 {
191 // dateFrame.setText(getDay(), 3);
192 // dateFrame.setBitmap(calendar[day()-1], 9 , 8); //update day can also be placed in a more frequently running loop, every 15mins is good enough for me
193 wClient.updateCurrentById(&weatherData, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_ID);
194 fdata = weatherBitmap(weatherData.weatherId);
195 weatherFrame.setBitmap(fdata.bitmap, fdata.frameCount, 200, 8, 8);
196 weatherFrame.setText(String(weatherData.temp) + "'" + "C", 2);
197 }
198
199 /*EVERY_N_HOURS(24)
200 {
201 cClient.updateCurrent(&ratesData, CurrencyRateUrl); //refresh currencydata every 24h
202 dollarFrame.setText(String(ratesData.USD) + "." + String(ratesData.usdF), 0);
203 euroFrame.setText(String(ratesData.EUR) + "." + String(ratesData.eurF), 0);
204 }*/
205
206}