Init commit

This commit is contained in:
2020-04-10 11:53:47 +02:00
commit 55b69b9a8d
12 changed files with 1053 additions and 0 deletions

197
src/main.cpp Normal file
View File

@@ -0,0 +1,197 @@
#include <NTPClient.h>
// change next line to use with another board/shield
#include <WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <DateTime.h>
#include <IotWebConf.h>
#include "calendar.h"
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
#define COMMAND_PARAMETER_LENGTH 30
#define PIN 15
#include <WiFiUdp.h>
void handleRoot();
void processCommand();
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "BADThing";
// -- Initial password to connect to the Thing, when it creates an own Access Point.
const char wifiInitialApPassword[] = "BADesp32";
DNSServer dnsServer;
WebServer server(80);
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword);
enum Lines_names { TIME_LINE, DATE_LINE, WEATHER_LINE, NAMES_LINE, LAST_LINE };
String Lines[LAST_LINE];
Adafruit_NeoMatrix *matrix;
const uint16_t colors[] = {
Adafruit_NeoMatrix::Color(255, 0, 0), Adafruit_NeoMatrix::Color(0, 255, 0), Adafruit_NeoMatrix::Color(0, 0, 255) };
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
HTTPClient client;
DynamicJsonDocument doc(1024);
String get_name_of_day(int m,int d) {
for (int i=0;i<sizeof(cal_names)/sizeof(cal_names[0]); i++) {
if (cal_names[i].m == m && cal_names[i].d ==d)
return String(cal_names[i].name);
}
return String("Sviatok");
}
String get_weather_line()
{
client.begin("http://api.openweathermap.org/data/2.5/weather?q=Trnava,sk&lang=sk&APPID=d15c61931a053026e98769dd9fc46ba3");
int code = client.GET();
if (code > 0) {
if(code == HTTP_CODE_OK) {
float temp;
String w;
String desc;
String payload = client.getString();
deserializeJson(doc, payload);
JsonObject obj = doc.as<JsonObject>();
temp = obj["main"]["temp"].as<float>();
w = obj["weather"][0]["main"].as<String>();
desc = obj["weather"][0]["description"].as<String>();
temp -= 273.15;
return String(temp) + String("C ") + desc;
}
}
return "Error";
}
String get_time_line(){
return String(DateTime.format("%H:%M"));
}
String get_date_line(){
return String(DateTime.format("%d.%m"));
}
String get_names_line() {
int d = DateTime.getParts().getMonthDay();
int m = DateTime.getParts().getMonth();m++;
return get_name_of_day(m,d);
}
void setup_matrix() {
matrix = new Adafruit_NeoMatrix(32, 8, PIN,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GBR + NEO_KHZ800);
matrix->begin();
matrix->setTextWrap(false);
matrix->setBrightness(1);
matrix->setTextColor(colors[1]);
}
void setup(){
Serial.begin(115200);
//iotWebConf.setStatusPin(PIN);
iotWebConf.init();
// -- Set up required URL handlers on the web server.
server.on("/", handleRoot);
server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); });
setup_matrix();
}
void scroll_line(String line) {
int x = 0, mx = 32;
int y = 0;
if (line.length() * 6 > mx) {
for (x = 0; x < line.length()*6 - mx; x++) {
matrix->fillScreen(0);
matrix->setCursor(-x, y);
matrix->setTextColor(colors[1]);
matrix->print(line);
matrix->show();
if (x == 0) delay(300);
else delay(100);
}
delay(1000);
} else {
matrix->fillScreen(0);
matrix->setCursor(x, y);
matrix->setTextColor(colors[1]);
matrix->print(line);
matrix->show();
delay(3000);
}
}
void handleRoot()
{
// -- Let IotWebConf test and handle captive portal requests.
if (iotWebConf.handleCaptivePortal())
{
// -- Captive portal request were already served.
return;
}
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>IotWebConf 02 Status and Reset</title></head><body>Hello world!";
s += "Go to <a href='config'>configure page</a> to change settings.";
s += "</body></html>\n";
server.send(200, "text/html", s);
}
void loop() {
unsigned long sec;
unsigned long n=0;
// -- doLoop should be called as frequently as possible.
iotWebConf.doLoop();
if (WiFi.status() == WL_CONNECTED) {
timeClient.begin();
timeClient.update();
sec = timeClient.getEpochTime();
DateTime.setTime(sec);
if (n++ % 25 == 0) Lines[WEATHER_LINE] = get_weather_line();
Lines[TIME_LINE] = get_time_line();
Lines[DATE_LINE] = get_date_line();
Lines[NAMES_LINE] = get_names_line();
for (int i=0;i<LAST_LINE;i++) {
Serial.println(Lines[i]);
scroll_line(Lines[i]);
}
}
}