Skip to content

Instantly share code, notes, and snippets.

@CroneKorkN
Last active March 26, 2017 11:37
Show Gist options
  • Save CroneKorkN/70ddecda495ffc7eb291d9416f1bd1bd to your computer and use it in GitHub Desktop.
Save CroneKorkN/70ddecda495ffc7eb291d9416f1bd1bd to your computer and use it in GitHub Desktop.
// init
#include <elapsedMillis.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <elapsedMillis.h>
#define WATTS_PER_SIGNAL 0.5
#define RESETBUTTON_DELAY 1500
//
// app
//
bool old_s0_state = false;
char server_address[200];
char sensor_id[200];
elapsedMicros timeElapsed;
int cycles;
void setup() {
// serial setup
Serial.begin(115200);
delay(10);
// initialize WiFiManager
WiFiManager wifiManager;
// id/name, placeholder/prompt, default, length
WiFiManagerParameter wifimanager_server_address("server", "server address", "10.0.2.80:3000", 16);
wifiManager.addParameter(&wifimanager_server_address);
WiFiManagerParameter wifimanager_sensor_id("server", "sensor id", "strom_1234", 16);
wifiManager.addParameter(&wifimanager_sensor_id);
// reset WifiManager (D7 -> GND)
pinMode(13, INPUT_PULLUP);
if(resetbutton_pressed()) {
wifiManager.resetSettings();
delay(100);
Serial.println("RESET BUTTON PRESSED");
}
// run WiFiManager
wifiManager.autoConnect();
// save custom values
strcpy(server_address, wifimanager_server_address.getValue());
strcpy(sensor_id, wifimanager_sensor_id.getValue());
// s0 setup (D8 -> 3.3V)
pinMode(15, INPUT);
Serial.println("setup done");
}
void loop() {
if (check_for_impulse()) {
post_measurement();
}
}
// lib
bool resetbutton_pressed() {
// D7 -> GND
int countdown = RESETBUTTON_DELAY;
Serial.print(countdown);
Serial.print(digitalRead(13));
while(countdown > 0) {
if (digitalRead(13) == 0) {
return true;
}
countdown -= 10;
Serial.print(countdown);
Serial.print("; ");
delay(10);
}
return false;
}
bool check_for_impulse() {
int state = digitalRead(15);
bool result = false;
if(old_s0_state == 0 && state == 1) {
result = true;
delay(10);
}
old_s0_state = state;
return result;
}
bool post_measurement() {
// calc watts
int micros_elapsed = timeElapsed;
float hours_elapsed = (1.0*micros_elapsed)/60/60/1000000;
float watts = WATTS_PER_SIGNAL/hours_elapsed;
// format watts
char watts_string[50];
sprintf(watts_string, "%d.%04d", (int)watts, (int)(watts*10000)%10000);
// build server_url
char server_url[200];
strcpy(server_url, "http://");
strcat(server_url, server_address);
strcat(server_url, "/measurements");
// build query
char query[100];
strcpy(query, "sensor_id=");
strcat(query, sensor_id);
strcat(query, "&value=");
strcat(query, watts_string);
// debug
Serial.print(micros_elapsed);
Serial.print("ms; query:");
Serial.print(server_url);
Serial.print("?");
Serial.print(query);
Serial.println("");
// post
HTTPClient http;
http.begin(server_url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(query);
// http.writeToStream(&Serial);
http.end();
// reset timer
timeElapsed = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment