Skip to content

Commit

Permalink
Begin moving posts back to semaphore/loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Feb 26, 2020
1 parent baf5fd1 commit 01b5099
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
41 changes: 12 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void setup() {

_delay(200); // Let pins settle, else detect is inconsistent
pinMode(RESETWIFI, INPUT_PULLUP);
// DEBUG: Commented startup lines
// if (digitalRead(RESETWIFI) == LOW) {
// Log.notice(F("%s low, presenting portal." CR), RESETWIFI);
// doWiFi(true);
Expand Down Expand Up @@ -66,8 +67,6 @@ void loop() {
JsonConfig *config = JsonConfig::getInstance();
WebServer *server = WebServer::getInstance();
Bubbles *bubble = Bubbles::getInstance();
URLTarget *target = URLTarget::getInstance();
// BFTarget *bfTarget = BFTarget::getInstance();

// Bubble loop to create 60 second readings
Ticker bubUpdate;
Expand All @@ -76,11 +75,11 @@ void loop() {
// Target timer
Ticker urlTarget;
// config->targetfreq * 60
urlTarget.attach(5, [target](){ target->push(); });
urlTarget.attach(5, [](){doURLTarget = true;});

// Brewer's friend timer
// Ticker bfTimer;
// bfTimer.attach(config->bffreq * 60, [bfTarget](){ bfTarget->push(); });
// bfTimer.attach(config->bffreq * 60, [](){doBFTarget = true;});

// mDNS Reset Timer - Helps avoid the host not found issues
Ticker mDNSTimer;
Expand All @@ -92,43 +91,27 @@ void loop() {
rebootTimer.attach(REBOOTTIMER, reboot);

while (true) {
// Handle semaphores - No radio work in a Ticker
tickerLoop();

// Handle JSON posts
// if (doBF) { // Do BF post
// doBF = false;
// bfPost();
// }
// Regular loop handlers
server->handleLoop(); // Handle HTML requests
MDNS.update(); // Handle mDNS requests

// If timers needs to be updated, update timers
// If target frequencies needs to be updated, update here
if (config->updateTargetFreq) {
Log.notice(F("Resetting URL Target frequency timer to %l minutes." CR), config->targetfreq);
urlTarget.detach();
urlTarget.attach(config->targetfreq * 60, [target](){ target->push(); });
urlTarget.attach(config->targetfreq * 60, [](){doURLTarget = true;});
config->updateTargetFreq = false;
}
// if (config->updateBFFreq) {
// Log.notice(F("Resetting Brewer's Friend frequency timer to %l minutes." CR), config->bffreq);
// bfTimer.detach();
// bfTimer.attach(config->bffreq * 60, [bfTarget](){ bfTarget->push(); });
// bfTimer.attach(config->bffreq * 60, [](){doBFTarget = true;});
// config->updateBFFreq = false;
// }

// Handle the board LED
if (digitalRead(COUNTPIN) == HIGH) { // Non-interrupt driven LED logic
digitalWrite(LED, LOW); // Turn LED on when not obstructed
} else {
digitalWrite(LED, HIGH); // Make sure LED turns off after a bubble4
}

if (bubble->doBub) { // Serial log for bubble detect
#ifdef LOG_LEVEL
Log.verbose(F("॰ₒ๐°৹" CR)); // Looks like a bubble, right?
#endif
bubble->doBub = false;
}

// Regular loop handlers
server->handleLoop(); // Handle HTML requests
MDNS.update(); // Handle mDNS requests
yield();
}
}
67 changes: 54 additions & 13 deletions src/pushtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,21 @@ SOFTWARE. */

#include "pushtarget.h"

IPAddress resolveHost(PushTarget *target) {
LCBUrl lcburl;
lcburl.setUrl(String(target->url));
Log.verbose(F("Host lookup: %s." CR), lcburl.getHost().c_str());
IPAddress resolveHost(const char *hostname) {
Log.verbose(F("Host lookup: %s." CR), hostname);
IPAddress returnIP = INADDR_NONE;
if (WiFi.hostByName(lcburl.getHost().c_str(), returnIP, 10000) == 0) {
if (WiFi.hostByName(hostname, returnIP, 10000) == 0) {
Log.error(F("Host lookup error." CR));
returnIP = INADDR_NONE;
}
return returnIP;
}

bool pushTarget(PushTarget *target) {
bool pushTarget(PushTarget *target, IPAddress targetIP, int port) {
Log.notice(F("Posting to: %s" CR), target->url);
LCBUrl lcburl;
lcburl.setUrl(String(target->url));

if (target->ip == INADDR_NONE) {
Log.error(F("Name resolution failed for %s." CR), lcburl.getHost().c_str());
return false;
}

Bubbles *bubble = Bubbles::getInstance();
JsonConfig *config = JsonConfig::getInstance();
const size_t capacity = JSON_OBJECT_SIZE(8) + 210;
Expand All @@ -61,8 +54,6 @@ bool pushTarget(PushTarget *target) {
String json;
serializeJson(doc, json);

IPAddress targetIP = target->ip;
int port = lcburl.getPort();
// Use the IP address we resolved if we are connecting with mDNS
Log.verbose(F("Connecting to: %s @ %s on port %l" CR),
lcburl.getHost().c_str(),
Expand Down Expand Up @@ -137,3 +128,53 @@ bool pushTarget(PushTarget *target) {
return false;
}
}

bool testConnect(IPAddress targetIP, int port) {
WiFiClient client;
if (client.connect(targetIP, port)) {
Log.notice(F("Connected to: %s port %l." CR),
targetIP.toString().c_str(), port
);
} else {
Log.warning(F("Connection failed to: %s, Port: %l (Err: %d)" CR),
targetIP.toString().c_str(), port, client.connected()
);
return false;
}
client.stop();
}

void tickerLoop() {
Bubbles *bubble = Bubbles::getInstance();
URLTarget *urlTarget = URLTarget::getInstance();

// Handle JSON posts
//
// Do URL Target post
if (doURLTarget) {
doURLTarget = false;
urlTarget->push();
}
//
// Do Brewer's Friend Post
if (doBFTarget) { // Do BF post
doBFTarget = false;
// urlTarget->push(); // TODO - Attach BF Target
}

// Handle the board LED status
// Smarter to do it in the loop than in the ISR
if (digitalRead(COUNTPIN) == HIGH) { // Non-interrupt driven LED logic
digitalWrite(LED, LOW); // Turn LED on when not obstructed
} else {
digitalWrite(LED, HIGH); // Make sure LED turns off after a bubble4
}

// Some just for fun serial logging
if (bubble->doBub) { // Serial log for bubble detect
#ifdef LOG_LEVEL
Log.verbose(F("॰ₒ๐°৹" CR)); // Looks like a bubble, right?
#endif
bubble->doBub = false;
}
}
11 changes: 9 additions & 2 deletions src/pushtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOFTWARE. */

#include "bubbles.h"
#include "jsonconfig.h"
#include "urltarget.h"
#include <LCBUrl.h>
#include <ArduinoLog.h>
#include <ESP8266mDNS.h>
Expand Down Expand Up @@ -55,7 +56,13 @@ class PushTarget {
pushPoint bpm; // Bubbles per minute
};

IPAddress resolveHost(PushTarget*);
bool pushTarget(PushTarget*);
IPAddress resolveHost(const char hostname[129]);
bool pushTarget(PushTarget*, IPAddress, int);
bool testConnect(IPAddress, int);
void tickerLoop();

static bool __attribute__((unused)) doURLTarget = false; // Semaphore for Target timer
static bool __attribute__((unused)) doBFTarget = false; // Semaphore for BF timer
static bool __attribute__((unused)) doBRFTarget = false; // Semaphore for BRF timer

#endif // _PUSHTARGET_H
6 changes: 3 additions & 3 deletions src/urltarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ URLTarget* URLTarget::getInstance() {

bool URLTarget::push() {
Log.verbose(F("Triggered %s push." CR), single->target->target.name);
LCBUrl lcburl;
if (single->target->apiName.enabled) {
LCBUrl lcburl;
if (lcburl.setUrl(String(single->target->url))) {
IPAddress resolvedIP = resolveHost(single->target);
IPAddress resolvedIP = resolveHost(lcburl.getHost().c_str());
if (resolvedIP == INADDR_NONE) {
if (single->target->ip == INADDR_NONE) {
Log.error(F("Unable to resolve host %s to IP address." CR), lcburl.getHost().c_str());
Expand All @@ -92,7 +92,7 @@ bool URLTarget::push() {
return true;
}

if (pushTarget(single->target)) {
if (pushTarget(single->target, target->ip, lcburl.getPort())) {
Log.notice(F("%s post ok." CR), single->target->target.name);
Serial.println(); // DEBUG
return true;
Expand Down

0 comments on commit 01b5099

Please sign in to comment.