12 #define LWD_TIMEOUT 15*1000
26 ret = String(bytes) +
"B";
28 else if (bytes < (1024 * 1024))
30 ret = String(bytes / 1024.0) +
"KB";
32 else if (bytes < (1024 * 1024 * 1024))
34 ret= String(bytes / 1024.0 / 1024.0) +
"MB";
46 float tempfloat = value;
56 for (i = 0; i < places; i++)
66 while ((tens * 10.0) <= tempfloat)
79 for (i=0; i< tenscount; i++)
81 digit = (int) (tempfloat/tens);
82 Serial.print(digit, DEC);
83 tempfloat = tempfloat - ((float)digit * tens);
95 for (i = 0; i < places; i++)
98 digit = (int) tempfloat;
99 Serial.print(digit,DEC);
101 tempfloat = tempfloat - (float) digit;
107 unsigned int stringLength = str.length();
109 if (stringLength == 0)
return false;
111 for(
unsigned int i = 0; i < stringLength; ++i)
113 if (isDigit(str.charAt(i)))
127 Serial.println(
"Entered error state.");
128 digitalWrite(ledRed, LOW);
129 digitalWrite(ledGreen, LOW);
133 Serial.println(
"!! Entering error state - No WiFi.");
137 digitalWrite(ledRed, HIGH);
138 digitalWrite(ledGreen, LOW);
140 digitalWrite(ledRed, LOW);
141 digitalWrite(ledGreen, LOW);
146 Serial.println(
"Entered errorstate 1. Press reset");
155 digitalWrite(ledRed, HIGH);
156 digitalWrite(ledGreen, LOW);
158 digitalWrite(ledRed, LOW);
159 digitalWrite(ledGreen, LOW);
161 digitalWrite(ledRed, HIGH);
162 digitalWrite(ledGreen, LOW);
164 digitalWrite(ledRed, LOW);
165 digitalWrite(ledGreen, LOW);
170 Serial.println(
"Entered error state 2. Could not connect to MQTT broker. Press reset");
176 Serial.println(
"!! Entering error state - No One Wire temp sensor.");
180 digitalWrite(ledRed, HIGH);
181 digitalWrite(ledGreen, LOW);
183 digitalWrite(ledRed, LOW);
184 digitalWrite(ledGreen, LOW);
189 Serial.println(
"Entered errorstate 1. Press reset");
198 digitalWrite(ledRed, HIGH);
199 digitalWrite(ledGreen, LOW);
201 digitalWrite(ledRed, LOW);
202 digitalWrite(ledGreen, LOW);
207 Serial.println(
"Entered error state 4. Not possible to start SPIFFS. Press reset");
213 Serial.println(
"!! Entering error state - Could not transmit data to emoncms, but we need to continue regulate.");
221 Serial.println(
"** Hit a key to continue");
222 while(Serial.available() == 0){}
223 while(Serial.available()){Serial.read();}
239 Serial.println(
"WARNING loop Blocked! check for deadlock!");
260 "movi %0, 0x60000200\n\t"
261 "l32i %0, %0, 0x118\n\t"
266 return ((bootmode >> 0x10) & 0x7);
269 int startWiFi(IPAddress _ip, IPAddress _dns, IPAddress _gateway, IPAddress _subnet,
char *_ssid,
char *_password,
char *_host)
274 Serial.print(
"** Connecting to ");
275 Serial.println(_ssid);
277 WiFi.enableSTA(
true);
278 WiFi.setAutoConnect (
true);
279 WiFi.setAutoReconnect (
true);
281 WiFi.config(_ip, _gateway, _subnet, _dns);
282 WiFi.hostname(_host);
283 WiFi.begin(_ssid, _password);
285 while (WiFi.status() != WL_CONNECTED)
293 Serial.println(
"\nNo WiFi available. Check configuration.\n");
302 Serial.print(
" ** Connected to..: "); Serial.println(_ssid);
303 Serial.print(
" ** IP address....: "); Serial.println(WiFi.localIP());
304 Serial.print(
" ** Gateway.......: "); Serial.println(WiFi.gatewayIP());
305 Serial.print(
" ** DNS...........: "); Serial.println(WiFi.dnsIP());
306 Serial.print(
" ** RSSI..........: "); Serial.println(WiFi.RSSI());
307 Serial.print(
" ** WiFi.status...: ");
showWiFiStatus(WiFi.status());
317 Serial.println(
"WL_NO_SHIELD");
320 Serial.println(
"WL_IDLE_STATUS");
323 Serial.println(
"WL_NO_SSID_AVAIL");
326 Serial.println(
"WL_SCAN_COMPLETED");
329 Serial.println(
"WL_CONNECTED");
332 Serial.println(
"WL_CONNECT_FAILED");
335 Serial.println(
"WL_CONNECTION_LOST");
338 Serial.println(
"WL_DISCONNECTED");
341 Serial.println(
"_UNKNOWN");
348 if(
DEBUG ) Serial.println(
"** Reconnecting WLAN...");
349 Serial.print(
" ** WiFi.status: ");
showWiFiStatus( WiFi.status() );
352 WiFi.begin(_ssid, _password);
355 while (WiFi.status() != WL_CONNECTED)
358 if (waitCounter == 30)
360 Serial.println(
"** ERROR: could not reconnect WiFi");
367 Serial.print(
" ** waitCounter: "); Serial.println(waitCounter);
369 Serial.print(
"** WiFi reconnected with status: ");
showWiFiStatus( WiFi.status() );
377 if (_mqtt->connected())
379 if(
DEBUG ) Serial.println(
" ** Connected to MQTT.");
384 Serial.print(
"Connecting to MQTT... ");
387 while ((ret = _mqtt->connect()) != 0)
389 Serial.println(_mqtt->connectErrorString(ret));
390 Serial.println(
"Retrying MQTT connection in 5 seconds...");
400 Serial.println(
"MQTT Connected!");
void showWiFiStatus(int i)
Utility function Display WiFi status in clear text.
boolean isNumeric(String str)
Utility function Checks if a string is numeric. Original code found here: http://tripsintech....
int getBootDevice(void)
Utility function See https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs2_en....
void lwdtFeedInit(void)
Utility function Initializes the loop watchdog interrupt routine. To be used in setup() at the end.
void printFloat(float value, int places)
Utility function: Prints value with places of decimal places determine by precision.
bool doWifiConnect(char *_ssid, char *_password)
Utility function Tries to reconnect the WiFi connection Software found here: https://github....
void ICACHE_RAM_ATTR lwdtcb(void)
Utility function lwdTicker callback routine Code found here: https://www.sigmdel.ca/michel/program/es...
String formatBytes(size_t bytes)
Utility function Original code found here: https://tttapa.github.io/ESP8266/Chap16%20-%20Data%20Loggi...
int MQTT_connect(Adafruit_MQTT_Client *_mqtt)
Utility function Function to connect and reconnect as necessary to the MQTT server.
void keypressWait(void)
Utility function Halt the program and wait for "key press" at the serial port. For DEBUG purpose only...
void lwdtFeed(void)
Utility function Resets the loop watchdog counter also known as "kicking the watchdog".
int errorState(int state, int ledRed, int ledGreen)
Utility function Visual error indication via LED at pin D2 This is a dead end. You will need to fix t...
int startWiFi(IPAddress _ip, IPAddress _dns, IPAddress _gateway, IPAddress _subnet, char *_ssid, char *_password, char *_host)
Utility function Starts everything related to WiFi and Internet connections.