230 lines
4.5 KiB
C++
230 lines
4.5 KiB
C++
/*
|
|
* productionMode.cpp
|
|
*
|
|
* Created on: Jan 24, 2018
|
|
* Author: wn
|
|
*/
|
|
|
|
|
|
#include "defines.h"
|
|
|
|
#define MQTT_MAX_PACKET_SIZE 256
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <PubSubClient.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include "configuration.h"
|
|
|
|
|
|
|
|
volatile uint32_t cnt = 0;
|
|
uint32_t uptime = 0;
|
|
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
WiFiClientSecure espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
|
|
void count() {
|
|
static uint32_t lastEvent = 0;
|
|
uint32_t currentEvent = micros();
|
|
if (currentEvent > (lastEvent + configBlock.debounce)) {
|
|
lastEvent = currentEvent;
|
|
cnt++;
|
|
}
|
|
}
|
|
|
|
void setup_wifi() {
|
|
delay(10);
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
// We start by connecting to a WiFi network
|
|
#ifdef DEBUG
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(configBlock.wifiSsid);
|
|
#endif
|
|
|
|
WiFi.begin(configBlock.wifiSsid, configBlock.wifiKey);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(50);
|
|
#ifdef DEBUG
|
|
Serial.print(".");
|
|
#endif
|
|
}
|
|
Serial.println("!");
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
#ifdef DEBUG
|
|
Serial.print("Attempting MQTT connection...");
|
|
#endif
|
|
// Attempt to connect
|
|
//char clientId[128];
|
|
//snprintf(clientId, 127, "esp%s", WiFi.macAddress().c_str());
|
|
if (client.connect(configBlock.mqttClientId, configBlock.mqttUser, configBlock.mqttPass)) {
|
|
#ifdef DEBUG
|
|
Serial.println("connected");
|
|
#endif
|
|
// client.setCallback(callback);
|
|
|
|
// Once connected, publish an announcement...
|
|
client.publish(configBlock.mqttDebugTopic, "hello world");
|
|
client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str());
|
|
} else {
|
|
#ifdef DEBUG
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
#endif
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool mqtt_connect() {
|
|
bool reconnected = false;
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
reconnected = true;
|
|
}
|
|
client.loop();
|
|
return reconnected;
|
|
}
|
|
|
|
|
|
void setupProduction() {
|
|
pinMode(HALL_PIN, INPUT_PULLUP);
|
|
|
|
attachInterrupt(HALL_PIN, count, FALLING);
|
|
|
|
setup_wifi();
|
|
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
|
|
|
sensors.begin();
|
|
#ifdef DEBUG
|
|
uint8_t num = sensors.getDeviceCount();
|
|
Serial.print("device count: ");
|
|
Serial.println(num);
|
|
#endif
|
|
|
|
sensors.setResolution(12);
|
|
}
|
|
|
|
|
|
float batteryCalc(uint16_t n) {
|
|
const float u_ref = 3.5;
|
|
const float n_max = 1023;
|
|
const float r_1 = 150e3;
|
|
const float r_2 = 10e3;
|
|
|
|
float n_i = (float) n;
|
|
float u_2 = (n_i / n_max) * u_ref;
|
|
float u_1 = ((r_1 + r_2) / r_2) * u_2;
|
|
|
|
return u_1;
|
|
}
|
|
|
|
void loopProduction() {
|
|
bool reconnected = mqtt_connect();
|
|
static uint32_t reconnectTime = 0;
|
|
if (reconnected) {
|
|
reconnectTime = millis();
|
|
}
|
|
|
|
uint32_t currentMillis = millis();
|
|
|
|
static uint32_t lastUptimeMillis = 0;
|
|
if (currentMillis > (lastUptimeMillis + 1000)) {
|
|
lastUptimeMillis = currentMillis;
|
|
uptime++;
|
|
}
|
|
|
|
static uint32_t lastMillis = 0;
|
|
if (currentMillis > (lastMillis + (configBlock.period * 1000))) {
|
|
lastMillis = currentMillis;
|
|
|
|
// rain sensor
|
|
uint32_t rainCnt = 0;
|
|
noInterrupts();
|
|
rainCnt = cnt;
|
|
cnt = 0;
|
|
interrupts();
|
|
#ifdef DEBUG
|
|
Serial.print(rainCnt);
|
|
Serial.println();
|
|
#endif
|
|
/*
|
|
float rainVol = rainCnt * configBlock.spoonfill;
|
|
int16_t r1 = (int)rainVol;
|
|
int16_t r2 = (int)((rainVol - r1)*100);
|
|
#ifdef DEBUG
|
|
Serial.print(rainVol);
|
|
Serial.println();
|
|
#endif
|
|
*/
|
|
|
|
// battery voltage
|
|
float battery = batteryCalc(analogRead(A0));
|
|
#ifdef DEBUG
|
|
Serial.print(battery);
|
|
Serial.println();
|
|
#endif
|
|
|
|
int16_t b1 = (int)battery;
|
|
int16_t b2 = (int)((battery - b1)*100);
|
|
|
|
// temperature
|
|
sensors.requestTemperatures();
|
|
|
|
float t = sensors.getTempCByIndex(0);
|
|
#ifdef DEBUG
|
|
Serial.print(t);
|
|
Serial.println();
|
|
#endif
|
|
|
|
int16_t t1 = (int)t;
|
|
int16_t t2 = (int)((t - t1)*100);
|
|
|
|
|
|
// MQTT publishing
|
|
#define BUF_SIZE 256
|
|
char buf[BUF_SIZE];
|
|
/* snprintf(buf, BUF_SIZE-1, "{\"rainvol\":%d.%d, \"battery\":%d.%d, \"uptime\":%ld, \"temperature\":%ld.%ld}", r1, r2, b1, b2, uptime, t1, t2); */
|
|
snprintf(buf, BUF_SIZE-1, "{\"raincnt\":%ld, \"battery\":%d.%d, \"uptime\":%ld, \"temperature\":%ld.%ld}", rainCnt, b1, b2, uptime, t1, t2);
|
|
client.publish(configBlock.mqttTopic, buf);
|
|
|
|
|
|
#ifdef DEBUG
|
|
Serial.println();
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|