start integration of mqtt

This commit is contained in:
hg 2015-05-09 16:31:49 +02:00
parent 472b9628c4
commit 558711bb2b
5 changed files with 136 additions and 2 deletions

View File

@ -27,6 +27,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/NetMeterBusMaster2/Streaming}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/NetMeterBusMaster2/MQTT}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/NetMeterBusMaster2/PString}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/NetMeterBusMaster2/Metro}&quot;"/>
</option>
<inputType id="it.baeyens.arduino.compiler.cpp.sketch.input.1988694850" name="CPP source files" superClass="it.baeyens.arduino.compiler.cpp.sketch.input"/>
</tool>
@ -45,10 +46,11 @@
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="PString|MQTT|EEPROM|Streaming|SPI|Ethernet|Libraries/*/?xamples" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="Metro|PString|MQTT|EEPROM|Streaming|SPI|Ethernet|Libraries/*/?xamples" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="EEPROM"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Ethernet"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="MQTT"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Metro"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="PString"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="SPI"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Streaming"/>

73
Metro/Metro.cpp Normal file
View File

@ -0,0 +1,73 @@
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Metro.h"
Metro::Metro(unsigned long interval_millis)
{
this->autoreset = 0;
interval(interval_millis);
reset();
}
// New creator so I can use either the original check behavior or benjamin.soelberg's
// suggested one (see below).
// autoreset = 0 is benjamin.soelberg's check behavior
// autoreset != 0 is the original behavior
Metro::Metro(unsigned long interval_millis, uint8_t autoreset)
{
this->autoreset = autoreset; // Fix by Paul Bouchier
interval(interval_millis);
reset();
}
void Metro::interval(unsigned long interval_millis)
{
this->interval_millis = interval_millis;
}
// Benjamin.soelberg's check behavior:
// When a check is true, add the interval to the internal counter.
// This should guarantee a better overall stability.
// Original check behavior:
// When a check is true, add the interval to the current millis() counter.
// This method can add a certain offset over time.
char Metro::check()
{
if (millis() - this->previous_millis >= this->interval_millis) {
// As suggested by benjamin.soelberg@gmail.com, the following line
// this->previous_millis = millis();
// was changed to
// this->previous_millis += this->interval_millis;
// If the interval is set to 0 we revert to the original behavior
if (this->interval_millis <= 0 || this->autoreset ) {
this->previous_millis = millis();
} else {
this->previous_millis += this->interval_millis;
}
return 1;
}
return 0;
}
void Metro::reset()
{
this->previous_millis = millis();
}

26
Metro/Metro.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef Metro_h
#define Metro_h
#include <inttypes.h>
class Metro
{
public:
Metro(unsigned long interval_millis);
Metro(unsigned long interval_millis, uint8_t autoreset);
void interval(unsigned long interval_millis);
char check();
void reset();
private:
uint8_t autoreset;
unsigned long previous_millis, interval_millis;
};
#endif

View File

@ -10,6 +10,7 @@
#include <PubSubClient.h>
#include <PString.h>
#include <Streaming.h>
#include <Metro.h>
byte MQTT_BROKER[] = { 192, 168, 75, 1 };
@ -26,10 +27,22 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
Metro secondTick = Metro(1000);
MqttClient::MqttClient(RequestSender *meterBusMaster) :
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
m_disconnectState(3), m_disconnectTime(millis())
{
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
m_mbusDevTuple[i].address = 0;
m_mbusDevTuple[i].queryPeriod = 0;
m_mbusDevTuple[i].timer = 0;
}
m_mbusDevTuple[0].address = 0x53;
m_mbusDevTuple[0].queryPeriod = 60;
m_mbusDevTuple[0].timer = m_mbusDevTuple[0].queryPeriod;
}
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength) {
@ -92,6 +105,18 @@ void MqttClient::exec() {
}
if (secondTick.check() == 1) {
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
m_mbusDevTuple[i].timer -= 1;
if (m_mbusDevTuple[i].timer == 0) {
m_mbusDevTuple[i].timer = m_mbusDevTuple[i].queryPeriod;
Serial << "Issue request for device " << m_mbusDevTuple[i].address << endl;
}
}
}
// m_client = m_server.available();
// if (m_client) {
// uint16_t sendBufLen = 0;

View File

@ -11,8 +11,16 @@
#include <Ethernet.h>
#include <PubSubClient.h>
#include "mBusDialog.h"
#include <Metro.h>
#define NUM_OF_DEVICES 10
typedef struct {
uint8_t address;
uint16_t queryPeriod;
uint16_t timer;
} mbusDevTuple_t;
class MqttClient : public ResponseCallback {
public:
@ -27,7 +35,7 @@ private:
PubSubClient m_mqttClient;
uint8_t m_disconnectState;
uint32_t m_disconnectTime;
mbusDevTuple_t m_mbusDevTuple[NUM_OF_DEVICES];
};