173 lines
3.2 KiB
C++
173 lines
3.2 KiB
C++
![]() |
/*
|
||
|
* production.cpp
|
||
|
*
|
||
|
* Created on: Aug 20, 2017
|
||
|
* Author: wn
|
||
|
*/
|
||
|
#include "defines.h"
|
||
|
|
||
|
#define MQTT_MAX_PACKET_SIZE 256
|
||
|
|
||
|
|
||
|
#include <ESP8266WiFi.h>
|
||
|
#include <ESP8266WebServer.h>
|
||
|
#include <PubSubClient.h>
|
||
|
#include <OneWire.h>
|
||
|
#include <DallasTemperature.h>
|
||
|
|
||
|
#include "configuration.h"
|
||
|
|
||
|
|
||
|
|
||
|
WiFiClientSecure espClient;
|
||
|
PubSubClient client(espClient);
|
||
|
|
||
|
#define ONE_WIRE_BUS 2
|
||
|
OneWire oneWire(ONE_WIRE_BUS);
|
||
|
DallasTemperature sensors(&oneWire);
|
||
|
|
||
|
extern uint32_t startTime;
|
||
|
|
||
|
|
||
|
void setup_wifi() {
|
||
|
delay(10);
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
#ifdef DEBUG
|
||
|
Serial.println("");
|
||
|
Serial.println("WiFi connected");
|
||
|
Serial.println("IP address: ");
|
||
|
Serial.println(WiFi.localIP());
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void setupProduction() {
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
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
|
||
|
// Once connected, publish an announcement...
|
||
|
//client.publish("IoT/espThermometer/status", "hello world");
|
||
|
} 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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void mqtt_connect() {
|
||
|
if (!client.connected()) {
|
||
|
reconnect();
|
||
|
}
|
||
|
client.loop();
|
||
|
}
|
||
|
|
||
|
|
||
|
void read_thermometer() {
|
||
|
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);
|
||
|
|
||
|
|
||
|
uint16_t vcc = ESP.getVcc();
|
||
|
uint16_t v1 = vcc / 1000;
|
||
|
uint16_t v2 = (vcc - v1 * 1000);
|
||
|
#ifdef DEBUG
|
||
|
Serial.print(vcc);
|
||
|
Serial.println();
|
||
|
#endif
|
||
|
|
||
|
|
||
|
uint32_t duration = millis() - startTime;
|
||
|
|
||
|
//char topic[128];
|
||
|
//snprintf(topic, 127, "IoT/espThermometer2/%s/measurement", WiFi.macAddress().c_str());
|
||
|
char payload[128];
|
||
|
snprintf(payload, 127, "%s %d.%d %d.%d %ld", configBlock.mqttClientId, t1, t2, v1, v2, duration);
|
||
|
#ifdef DEBUG
|
||
|
Serial.println(payload);
|
||
|
#endif
|
||
|
client.publish(configBlock.mqttTopic, payload, true);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
void loopProduction() {
|
||
|
static uint32_t lastMillis = 0;
|
||
|
mqtt_connect();
|
||
|
|
||
|
uint32_t currentMillis = millis();
|
||
|
if (currentMillis - lastMillis > 1000) {
|
||
|
lastMillis = currentMillis;
|
||
|
|
||
|
read_thermometer();
|
||
|
|
||
|
|
||
|
|
||
|
#ifdef SLEEP
|
||
|
#ifdef DEBUG
|
||
|
Serial.println("Sleeping");
|
||
|
#endif
|
||
|
client.disconnect();
|
||
|
ESP.deepSleep(configBlock.sleepTime * 1000000);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|