Browse Source

refactor code

plus_tomsk_weather
Vladimir Smagin 4 months ago
parent
commit
9a6800db5d
1 changed files with 135 additions and 94 deletions
  1. 135
    94
      tm1640-ntp.ino

+ 135
- 94
tm1640-ntp.ino View File

@@ -54,7 +54,8 @@ int outside_termo = 0;
//////// CALLBACK FUNCTIONS ////////

//callback config mode
void configModeCallback (WiFiManager *myWiFiManager) {
void configModeCallback(WiFiManager *myWiFiManager)
{
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
@@ -62,23 +63,29 @@ void configModeCallback (WiFiManager *myWiFiManager) {
}

//callback notifying us of the need to save config
void saveConfigCallback () {
void saveConfigCallback()
{
Serial.println("Should save config");
shouldSaveConfig = true;
}

void processSyncEvent(NTPSyncEvent_t ntpEvent) {
if (ntpEvent) {
void processSyncEvent(NTPSyncEvent_t ntpEvent)
{
if (ntpEvent)
{
ntp_online = false;
Serial.print("Time Sync error: ");
if (ntpEvent == noResponse) {
if (ntpEvent == noResponse)
{
Serial.println("NTP server not reachable");
}
else if (ntpEvent == invalidAddress) {
else if (ntpEvent == invalidAddress)
{
Serial.println("Invalid NTP server address");
}
}
else {
else
{
ntp_online = true;
Serial.print("Got NTP time: ");
get_outside_termo();
@@ -86,23 +93,42 @@ void processSyncEvent(NTPSyncEvent_t ntpEvent) {
}
}

//////// MAIN BODY ////////
//////// PRIMITIVES /////////

void setup() {
Wire.begin();
pinMode(ONBOARDLED, OUTPUT); // Onboard LED
digitalWrite(ONBOARDLED, HIGH); // Switch off LED
first_start();
Serial.println("mounting FS...");
void reset_device()
{
// connection failed, reset
Serial.println("Fatal error! Resetting device");
display.clear();
display.showString("resetting...");
delay(3000);
ESP.reset();
delay(5000);
}

// read wifi manager config
if (SPIFFS.begin()) {
void hello()
{
display.clear();

for (uint8_t l = 0; l <= 7; l++)
{
display.setBrightness(l); //set the diplay to maximum brightness
display.showString("hello int 21h");
delay(200);
}
}

void config_load() {
if (SPIFFS.begin())
{
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
if (SPIFFS.exists("/config.json"))
{
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
if (configFile)
{
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
@@ -110,26 +136,61 @@ void setup() {

configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
JsonObject &json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
if (json.success())
{
Serial.println("\nparsed json");

strcpy(ntp_server, json["ntp_server"]);
strcpy(ntp_timezone, json["ntp_timezone"]);
strcpy(wifi_hostname, json["wifi_hostname"]);

} else {
}
else
{
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
}
else
{
Serial.println("failed to mount FS");
}
//end read
}

void config_save() {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["ntp_server"] = ntp_server;
json["ntp_timezone"] = ntp_timezone;
json["wifi_hostname"] = wifi_hostname;

File configFile = SPIFFS.open("/config.json", "w");
if (!configFile)
{
Serial.println("failed to open config file for writing");
}

json.printTo(Serial);
json.printTo(configFile);
configFile.close();
}

//////// MAIN BODY ////////

void setup()
{
Wire.begin();
pinMode(ONBOARDLED, OUTPUT); // Onboard LED
digitalWrite(ONBOARDLED, HIGH); // Switch off LED
hello();
Serial.println("mounting FS...");

config_load();

Serial.println("Wifi manager configuring...");

// custom wifi parameters
@@ -140,7 +201,8 @@ void setup() {
wifiManager.addParameter(&custom_hostname);

Serial.println("Wifi connecting...");
if (!wifiManager.autoConnect("clock-x16-1-config")) {
if (!wifiManager.autoConnect("clock-x16-1-config"))
{
reset_device();
}

@@ -154,26 +216,13 @@ void setup() {
strcpy(wifi_hostname, custom_hostname.getValue());

// check hostname, set if needed
if (wifi_hostname != "clock-x16-1") WiFi.hostname("clock-x16-1");
if (wifi_hostname != "clock-x16-1")
WiFi.hostname("clock-x16-1");

//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["ntp_server"] = ntp_server;
json["ntp_timezone"] = ntp_timezone;
json["wifi_hostname"] = wifi_hostname;

File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}

json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
if (shouldSaveConfig)
{
config_save();
}

Serial.print("local ip ");
@@ -183,43 +232,24 @@ void setup() {
ntpEvent = event;
syncEventTriggered = true;
});

}

void get_outside_termo() {
void get_outside_termo()
{
http.begin("http://termo.tomsk.ru/data.txt");
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
String result = http.getString();
outside_termo = round(result.toInt());
}
}
}

void reset_device() {
// connection failed, reset
Serial.println("Fatal error! Resetting device");
display.clear();
display.showString("resetting...");
delay(3000);
ESP.reset();
delay(5000);
}

void first_start() {
display.clear();

for (uint8_t l = 0; l <= 7; l++) {
display.setBrightness(l); //set the diplay to maximum brightness
display.showString("hello int 21h");
delay(200);
}
}

void starting() {
NTP.begin(ntp_server, atoi(ntp_timezone), true);
NTP.setInterval(10, 600);
void starting()
{
Serial.print("TZ: ");
Serial.println(atoi(ntp_timezone));

@@ -227,12 +257,16 @@ void starting() {

//Serial.println("Starting NTP client");

while (ntp_online == false) {
processSyncEvent (ntpEvent);
syncEventTriggered = true;
while (ntp_online == false)
{
display.setBrightness(1);
display.showString("connecting...");
delay(5000);
NTP.stop();
NTP.begin(ntp_server, atoi(ntp_timezone), true);
NTP.setInterval(3600);
processSyncEvent(ntpEvent);
syncEventTriggered = true;
delay(30000);
}
get_outside_termo();
display.setBrightness(7);
@@ -242,21 +276,28 @@ void starting() {

char timebuff[17];

void loop() {
void loop()
{

if (WiFi.status() != WL_CONNECTED) {
if (WiFi.status() != WL_CONNECTED)
{
wifi_connected = false;
}

// if wifi suddenly disconnected wait, if failed run configurator
if (wifi_connected == true) {
if (wifi_connected == true)
{
// if ntp server is down try to reconnect
if (ntp_online == false) {
if (ntp_online == false)
{
starting();
} else {
}
else
{
// all fine, working here
if (syncEventTriggered) {
processSyncEvent (ntpEvent);
if (syncEventTriggered)
{
processSyncEvent(ntpEvent);
syncEventTriggered = false;
}

@@ -265,13 +306,16 @@ void loop() {
// draw temperature
String nozero = "";
String sign;
if (outside_termo < 0) {
if (outside_termo < 0)
{
sign = "";
} else
}
else
{
sign = " ";
}
if (outside_termo > -10 && outside_termo < 10) {
if (outside_termo > -10 && outside_termo < 10)
{
nozero = " ";
}

@@ -279,22 +323,19 @@ void loop() {

result.toCharArray(timebuff, 17);

display.showString( timebuff );
display.showString(timebuff);

delay(1000);
}
} else {
NTP.stop();
}
else
{
delay(10000);
Serial.println("Wifi connecting...");
if (wifiManager.autoConnect("clock-x16-1-config")) {
if (wifiManager.autoConnect("clock-x16-1-config"))
{
wifi_connected = true;
starting();
};
}
}






Loading…
Cancel
Save