Voltage measuring included

This commit is contained in:
starcalc 2018-09-13 21:44:51 +02:00
parent 79344f971e
commit 3c23bfa0c9
1 changed files with 20 additions and 12 deletions

View File

@ -12,13 +12,14 @@
#include <OneWire.h>
#include <DallasTemperature.h>
const char* ssid = "ENTER_YOUR_SSID_HERE";
const char* password = "ENTER_YOUR_WLAN_PASS_HERE";
const char* ssid = "CTDO-IoT";
const char* password = "12345678";
#define DEVICENAME "maintopic/devicename"
#define DEVICENAME "haus/bad"
#define TOPIC DEVICENAME"/temperature"
#define TOPICVOLTAGE DEVICENAME"/voltage"
#define ONLINETOPIC DEVICENAME"/online"
#define MQTTSERVER IPAddress(37, 187, 106, 16) // test.mosquitto.org = 37.187.106.16
#define MQTTSERVER IPAddress(195, 160, 169, 11) // test.mosquitto.org = 37.187.106.16
const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life
#define VCCPIN D7
@ -36,6 +37,10 @@ float tempC;
AsyncMqttClient mqttClient;
uint16_t packetId1Pub;
bool packet1Ack = false;
uint16_t packetId2Pub;
bool packet2Ack = false;
float voltage = 0.0;
int raw = 0;
bool ready = false;
@ -54,14 +59,14 @@ char *ftoa( double f, char *a, int precision)
}
void onMqttPublish(uint16_t packetId) {
Serial.println("** Publish acknowledged **");
Serial.print(" packetId: ");
Serial.println(packetId);
// TODO: Only when both packages were acknowledged...
// TODO: Only when both packages were acknowledged...
if (packetId == packetId1Pub) {
packet1Ack = true;
}
if (packet1Ack) {
if (packetId == packetId2Pub) {
packet2Ack = true;
}
if (packet1Ack && packet2Ack) {
ready = true;
}
}
@ -69,9 +74,9 @@ void onMqttPublish(uint16_t packetId) {
void onMqttConnect(bool sessionPresent) {
char buf[7];
packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2));
packetId2Pub = mqttClient.publish(TOPICVOLTAGE, 1, true, ftoa(voltage, buf, 2));
}
void setup() {
pinMode(GNDPIN, OUTPUT);
pinMode(VCCPIN, OUTPUT);
@ -92,6 +97,9 @@ void loop() {
// 0 refers to the first IC on the wire
Serial.println("Requesting Temperature from Device 0");
tempC = sensors.getTempCByIndex(0);
raw = analogRead(A0); // 2.428V = 739
// Je Tick = 0,003285521V
voltage = raw * 0.003285521;
Serial.println("Connecting to WIFI");
// Connect to WiFi