mirror of
https://github.com/grillbaer/esp32-geiger-counter.git
synced 2025-12-21 13:23:15 +01:00
Better WiFi connect and reconnect, online WiFi<->low power switching
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
/Release/
|
/Release/
|
||||||
/sloeber.ino.cpp
|
/sloeber.ino.cpp
|
||||||
/spec.d
|
/spec.d
|
||||||
/credentials.h
|
/credentials.cpp
|
||||||
|
|||||||
@@ -169,6 +169,8 @@ void loop() {
|
|||||||
ingestCountdown = ingestInterval;
|
ingestCountdown = ingestInterval;
|
||||||
ingest(geigerData, ingestInterval);
|
ingest(geigerData, ingestInterval);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
deinitIngest();
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine current value, average 6 seconds
|
// determine current value, average 6 seconds
|
||||||
|
|||||||
8
credentials.h
Normal file
8
credentials.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef CREDENTIALS_H_
|
||||||
|
#define CREDENTIALS_H_
|
||||||
|
|
||||||
|
extern const char *wifiSsid;
|
||||||
|
extern const char *wifiPassword;
|
||||||
|
extern const char *thingspeakApiKey;
|
||||||
|
|
||||||
|
#endif /* CREDENTIALS_H_ */
|
||||||
66
ingest.cpp
66
ingest.cpp
@@ -2,44 +2,53 @@
|
|||||||
|
|
||||||
#include "GeigerData.h"
|
#include "GeigerData.h"
|
||||||
|
|
||||||
// WiFi and thingspeak credentials:
|
|
||||||
//const char *wifiSsid = "...";
|
|
||||||
//const char *wifiPassword = "...";
|
|
||||||
//const char *thingspeakApiKey = "...";
|
|
||||||
#include "credentials.h"
|
#include "credentials.h"
|
||||||
|
|
||||||
const char *thingsPeakUrl = "api.thingspeak.com";
|
const char *thingsPeakUrl = "api.thingspeak.com";
|
||||||
|
|
||||||
void connect() {
|
bool connect() {
|
||||||
wl_status_t status = WL_DISCONNECTED;
|
uint16_t retries = 3;
|
||||||
|
while (WiFi.status() != WL_CONNECTED && (--retries) > 0) {
|
||||||
while (status != WL_CONNECTED) {
|
Serial.print("Trying to connect to ");
|
||||||
WiFi.begin(wifiSsid, wifiPassword);
|
|
||||||
Serial.print("Connecting to WiFi '");
|
|
||||||
Serial.print(wifiSsid);
|
Serial.print(wifiSsid);
|
||||||
Serial.println("' ...");
|
Serial.print(" ... ");
|
||||||
uint16_t waitRemaining = 10;
|
WiFi.begin(wifiSsid, wifiPassword);
|
||||||
while ((status = WiFi.status()) != WL_CONNECTED && waitRemaining > 0) {
|
uint16_t waitRemaining = 8;
|
||||||
Serial.print(" status=");
|
while (WiFi.status() != WL_CONNECTED && (--waitRemaining) > 0) {
|
||||||
Serial.println(status);
|
|
||||||
delay(500);
|
delay(500);
|
||||||
waitRemaining--;
|
}
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
Serial.println("successful");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
Serial.print("failed status=");
|
||||||
|
Serial.println(WiFi.status());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("WiFi connected!");
|
return WiFi.status() == WL_CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initIngest() {
|
void initIngest() {
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void deinitIngest() {
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
Serial.println("Disconnecting WiFi");
|
||||||
|
WiFi.disconnect(true, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ingest(GeigerData &geigerData, uint16_t intervalSamples) {
|
void ingest(GeigerData &geigerData, uint16_t intervalSamples) {
|
||||||
|
if (!connect())
|
||||||
|
return;
|
||||||
|
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
if (!client.connect(thingsPeakUrl, 80)) {
|
if (!client.connect(thingsPeakUrl, 80)) {
|
||||||
Serial.print("Connecting to '");
|
Serial.print("Connecting to ");
|
||||||
Serial.print(thingsPeakUrl);
|
Serial.print(thingsPeakUrl);
|
||||||
Serial.println("' failed");
|
Serial.println(" failed");
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const uint32_t pulses = geigerData.getPreviousPulses(1,
|
const uint32_t pulses = geigerData.getPreviousPulses(1,
|
||||||
@@ -52,9 +61,13 @@ void ingest(GeigerData &geigerData, uint16_t intervalSamples) {
|
|||||||
intervalSamples);
|
intervalSamples);
|
||||||
|
|
||||||
const String content = "api_key=" + String(thingspeakApiKey)
|
const String content = "api_key=" + String(thingspeakApiKey)
|
||||||
+ "&field1=" + String(cpm) + "&field2=" + String(uSph);
|
+ "&field1=" + String(cpm) + "&field2=" + String(uSph, 3);
|
||||||
|
|
||||||
Serial.println("Ingesting");
|
Serial.print("Ingesting cpm=");
|
||||||
|
Serial.print(cpm);
|
||||||
|
Serial.print(" uSph=");
|
||||||
|
Serial.print(uSph, 3);
|
||||||
|
Serial.print(" ... ");
|
||||||
|
|
||||||
client.print("POST /update HTTP/1.1\n");
|
client.print("POST /update HTTP/1.1\n");
|
||||||
|
|
||||||
@@ -70,9 +83,16 @@ void ingest(GeigerData &geigerData, uint16_t intervalSamples) {
|
|||||||
client.print("\n\n");
|
client.print("\n\n");
|
||||||
|
|
||||||
client.print(content);
|
client.print(content);
|
||||||
delay(1000);
|
|
||||||
|
|
||||||
Serial.print("Response: ");
|
uint16_t timeout = 40;
|
||||||
|
while (client.available() == 0 && (--timeout) > 0) {
|
||||||
|
delay(50);
|
||||||
|
}
|
||||||
|
if (client.available() == 0) {
|
||||||
|
Serial.println("failed (no response)");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("response:");
|
||||||
while (client.available()) {
|
while (client.available()) {
|
||||||
char c = client.read();
|
char c = client.read();
|
||||||
Serial.write(c);
|
Serial.write(c);
|
||||||
|
|||||||
1
ingest.h
1
ingest.h
@@ -4,6 +4,7 @@
|
|||||||
#include "GeigerData.h"
|
#include "GeigerData.h"
|
||||||
|
|
||||||
void initIngest();
|
void initIngest();
|
||||||
|
void deinitIngest();
|
||||||
void ingest(GeigerData &geigerData, uint16_t intervalSamples);
|
void ingest(GeigerData &geigerData, uint16_t intervalSamples);
|
||||||
|
|
||||||
#endif /* INGEST_H_ */
|
#endif /* INGEST_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user