ampel-firmware.ino 7.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/***
 *       ____ ___ ____       _                         _
 *      / ___/ _ \___ \     / \   _ __ ___  _ __   ___| |
 *     | |  | | | |__) |   / _ \ | '_ ` _ \| '_ \ / _ \ |
 *     | |__| |_| / __/   / ___ \| | | | | | |_) |  __/ |
 *      \____\___/_____| /_/__ \_\_| |_| |_| .__/ \___|_|         _
 *     | | | |/ _|_   _| / ___|| |_ _   _| |_| |_ __ _  __ _ _ __| |_
 *     | |_| | |_  | |   \___ \| __| | | | __| __/ _` |/ _` | '__| __|
 *     |  _  |  _| | |    ___) | |_| |_| | |_| || (_| | (_| | |  | |_
 *     |_| |_|_|   |_|   |____/ \__|\__,_|\__|\__\__, |\__,_|_|   \__|
 *                                               |___/
 */

14
#include "ampel-firmware.h"
15
16
17
18
19

/*****************************************************************
 * GPL License                                                   *
 *****************************************************************/
/*
Eric Duminil's avatar
Eric Duminil committed
20
21
 * This file is part of the "CO2 Ampel" project ( https://transfer.hft-stuttgart.de/gitlab/co2ampel and
 * https://transfer.hft-stuttgart.de/gitlab/co2ampel/ampel-firmware )
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 * Copyright (c) 2020 HfT Stuttgart.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/*****************************************************************
 * Authors                                                       *
 *****************************************************************/
/*
 * Eric Duminil
 * Robert Otto
 * Myriam Guedey
 * Tobias Gabriel Erhart
 * Jonas Stave
46
 * Michael Käppler
47
48
49
50
51
52
53
54
55
56
57
58
 */

/*****************************************************************
 * Configuration                                                 *
 *****************************************************************/
/*
 * Please define settings in 'config.h'.
 * There's an example config file called 'config.example.h'.
 * You can copy 'config.public.h' (stored in Git) to 'config.h' (not stored in Git),
 * and define your credentials and parameters in 'config.h'.
 */

59
/*****************************************************************
60
 * PreInit                                                       *
61
62
 *****************************************************************/
void preinit() {
63
64
65
#if !defined(AMPEL_WIFI) && defined(ESP8266)
  // WiFi would be initialized otherwise (on ESP8266), even if unused.
  // see https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
66
67
68
69
  ESP8266WiFiClass::preinitWiFiOff();
#endif
}

70
71
72
73
/*****************************************************************
 * Setup                                                         *
 *****************************************************************/
void setup() {
74
75
  led_effects::setupOnBoardLED();
  led_effects::onBoardLEDOff();
76
77
78

  Serial.begin(BAUDS);

79
  pinMode(0, INPUT); // Flash button (used for forced calibration)
80

81
  led_effects::setupRing();
82
83
84
85

  sensor::initialize();

  Serial.print(F("Sensor ID: "));
86
  Serial.println(ampel.sensorId);
87
88
89
  Serial.print(F("Board    : "));
  Serial.println(BOARD);

Eric Duminil's avatar
Eric Duminil committed
90
#ifdef AMPEL_WIFI
91
  WiFiConnect(ampel.sensorId);
92

Eric Duminil's avatar
Eric Duminil committed
93
  Serial.print(F("WiFi - Status: "));
94
95
96
  Serial.println(WiFi.status());

  if (WiFi.status() == WL_CONNECTED) {
Eric Duminil's avatar
Eric Duminil committed
97
#  ifdef AMPEL_HTTP
98
    web_server::initialize();
Eric Duminil's avatar
Eric Duminil committed
99
#  endif
100
101
102

    ntp::initialize();

103
    if (MDNS.begin(ampel.sensorId.c_str())) { // Start the mDNS responder for SENSOR_ID.local
104
105
106
107
108
109
      MDNS.addService("http", "tcp", 80);
      Serial.println(F("mDNS responder started"));
    } else {
      Serial.println(F("Error setting up MDNS responder!"));
    }

Eric Duminil's avatar
Eric Duminil committed
110
#  ifdef AMPEL_MQTT
111
    mqtt::initialize("CO2sensors/" + ampel.sensorId);
Eric Duminil's avatar
Eric Duminil committed
112
#  endif
113
  }
Eric Duminil's avatar
Eric Duminil committed
114
#endif
Eric Duminil's avatar
Eric Duminil committed
115

Eric Duminil's avatar
Eric Duminil committed
116
#ifdef AMPEL_CSV
117
  csv_writer::initialize();
118
#endif
Eric Duminil's avatar
Eric Duminil committed
119

Eric Duminil's avatar
Eric Duminil committed
120
#if defined(AMPEL_LORAWAN) && defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
121
122
  lorawan::initialize();
#endif
123
124
125
126
127
128
129
}

/*****************************************************************
 * Main loop                                                     *
 *****************************************************************/

void loop() {
Eric Duminil's avatar
Eric Duminil committed
130
#if defined(AMPEL_LORAWAN) && defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
131
132
133
134
135
136
137
138
  //LMIC Library seems to be very sensitive to timing issues, so run it first.
  lorawan::process();

  if (lorawan::waiting_for_confirmation) {
    // If node is waiting for join confirmation from Gateway, nothing else should run.
    return;
  }
#endif
139
  //NOTE: Loop should never take more than 1000ms. Split in smaller methods and logic if needed.
Eric Duminil's avatar
Notes    
Eric Duminil committed
140
  //NOTE: Only use millis() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
141
142
143
  uint32_t t0 = millis();

  keepServicesAlive();
144

145
146
147
  // Short press for night mode, Long press for calibration.
  checkFlashButton();

Eric Duminil's avatar
Eric Duminil committed
148
  checkSerialInput();
149

Eric Duminil's avatar
Eric Duminil committed
150
  if (sensor::processData()) {
Eric Duminil's avatar
Eric Duminil committed
151
#ifdef AMPEL_CSV
Eric Duminil's avatar
Eric Duminil committed
152
153
154
    csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#endif

Eric Duminil's avatar
Eric Duminil committed
155
#if defined(AMPEL_WIFI) && defined(AMPEL_MQTT)
Eric Duminil's avatar
Eric Duminil committed
156
157
158
    mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#endif

Eric Duminil's avatar
Eric Duminil committed
159
#if defined(AMPEL_LORAWAN) && defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
160
    lorawan::preparePayloadIfTimeHasCome(sensor::co2, sensor::temperature, sensor::humidity);
Eric Duminil's avatar
Eric Duminil committed
161
162
#endif
  }
163
164
165
166

  uint32_t duration = millis() - t0;
  if (duration > max_loop_duration) {
    max_loop_duration = duration;
Eric Duminil's avatar
Eric Duminil committed
167
    Serial.print(F("Debug - Max loop duration : "));
168
    Serial.print(max_loop_duration);
169
    Serial.println(F(" ms."));
170
171
172
  }
}

Eric Duminil's avatar
Eric Duminil committed
173
174
175
176
177
178
void checkSerialInput() {
  while (Serial.available() > 0) {
    sensor_console::processSerialInput(Serial.read());
  }
}

179
180
181
182
183
184
185
186
/**
 * Checks if flash button has been pressed:
 *   If not, do nothing.
 *   If short press, toggle LED display.
 *   If long press, start calibration process.
 */
void checkFlashButton() {
  if (!digitalRead(0)) { // Button has been pressed
187
    led_effects::onBoardLEDOn();
188
189
190
    delay(300);
    if (digitalRead(0)) {
      Serial.println(F("Flash has been pressed for a short time. Should toggle night mode."));
191
      led_effects::toggleNightMode();
192
193
    } else {
      Serial.println(F("Flash has been pressed for a long time. Keep it pressed for calibration."));
194
      if (led_effects::countdownToZero() < 0) {
195
196
197
        sensor::startCalibrationProcess();
      }
    }
198
    led_effects::onBoardLEDOff();
199
200
201
202
  }
}

void keepServicesAlive() {
Eric Duminil's avatar
Eric Duminil committed
203
#ifdef AMPEL_WIFI
204
  if (WiFi.status() == WL_CONNECTED) {
Eric Duminil's avatar
Eric Duminil committed
205
#  if defined(ESP8266)
206
207
208
    //NOTE: Sadly, there seems to be a bug in the current MDNS implementation.
    // It stops working after 2 minutes. And forcing a restart leads to a memory leak.
    MDNS.update();
Eric Duminil's avatar
Eric Duminil committed
209
#  endif
210
    ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s.
Eric Duminil's avatar
Eric Duminil committed
211
#  ifdef AMPEL_HTTP
212
    web_server::update();
Eric Duminil's avatar
Eric Duminil committed
213
214
#  endif
#  ifdef AMPEL_MQTT
215
    mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s.
Eric Duminil's avatar
Eric Duminil committed
216
#  endif
217
  }
Eric Duminil's avatar
Eric Duminil committed
218
#endif
219
}