103 lines
1.8 KiB
C++
103 lines
1.8 KiB
C++
/*
|
|
* production.cpp
|
|
*
|
|
* Created on: Aug 20, 2017
|
|
* Author: wn
|
|
*/
|
|
#include "defines.h"
|
|
|
|
#define MQTT_MAX_PACKET_SIZE 256
|
|
|
|
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include <mqttHandling.h>
|
|
|
|
#include "configuration.h"
|
|
|
|
|
|
|
|
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
extern uint32_t startTime;
|
|
|
|
|
|
|
|
void setupApplication() {
|
|
mqttSetup();
|
|
|
|
sensors.begin();
|
|
#ifdef DEBUG
|
|
uint8_t num = sensors.getDeviceCount();
|
|
Serial.print("device count: ");
|
|
Serial.println(num);
|
|
#endif
|
|
|
|
sensors.setResolution(12);
|
|
}
|
|
|
|
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, "{\"location\":\"%s\", \"temperature\":%d.%d, \"battery\":%d.%d, \"duration\":%ld}", configBlock.mqttClientId, t1, t2, v1, v2, duration);
|
|
#ifdef DEBUG
|
|
Serial.println(payload);
|
|
#endif
|
|
mqttClient.publish(configBlock.mqttTopic, payload, true);
|
|
|
|
|
|
}
|
|
|
|
void loopApplication() {
|
|
static uint32_t lastMillis = 0;
|
|
mqttLoop();
|
|
|
|
uint32_t currentMillis = millis();
|
|
if (currentMillis - lastMillis > 1000) {
|
|
lastMillis = currentMillis;
|
|
|
|
read_thermometer();
|
|
|
|
|
|
|
|
#ifdef SLEEP
|
|
#ifdef DEBUG
|
|
Serial.println("Sleeping");
|
|
#endif
|
|
mqttClient.disconnect();
|
|
ESP.deepSleep(configBlock.measurePeriod * 1000000);
|
|
#endif
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|