more files moved and adjusted to new structure
This commit is contained in:
106
sketch/application.cpp
Normal file
106
sketch/application.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
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
|
||||
client.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
|
||||
client.disconnect();
|
||||
ESP.deepSleep(configBlock.measurePeriod * 1000000);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user