Skip to content

Commit

Permalink
Added Subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
whatisbyandby committed Jul 5, 2024
1 parent e5c1288 commit 3bb09d3
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 111 deletions.
30 changes: 19 additions & 11 deletions src/common/thermostat_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#define THERMOSTAT_VERSION "1.0.0"


typedef enum ThermostatError {
typedef enum ThermostatError
{
THERMOSTAT_OK = 0,
THERMOSTAT_NO_DATA,
THERMOSTAT_ERROR,
Expand Down Expand Up @@ -35,7 +35,8 @@ inline const char *thermostatErrorToString(ThermostatError error)
}
}

enum ThermostatCommandType {
enum ThermostatCommandType
{
SET_TEMPERATURE,
SET_MODE,
SET_UNITS,
Expand All @@ -48,22 +49,26 @@ enum ThermostatCommandType {
INVALID_COMMAND
};

struct ThermostatCommand {
class ThermostatCommand
{
public:
ThermostatCommandType command_type;
double parameter;
std::string command_string;
std::string resultString;
};

typedef enum TemperatureState {
typedef void (*CommandCallback)(ThermostatCommand *command, void *arg);

typedef enum TemperatureState
{
OVER_TEMPERATURE,
UNDER_TEMPERATURE,
UNDER_TEMPERATURE_IN_RANGE,
OVER_TEMPERATURE_IN_RANGE,
IN_RANGE
} TemperatureState;


inline const char *temperatureStateToString(TemperatureState state)
{
switch (state)
Expand All @@ -83,7 +88,8 @@ inline const char *temperatureStateToString(TemperatureState state)
}
}

typedef enum ThermostatState {
typedef enum ThermostatState
{
HEATING,
COOLING,
FAN_ON,
Expand All @@ -108,7 +114,8 @@ inline const char *hvacStateToString(ThermostatState state)
}
}

typedef enum ThermostatMode {
typedef enum ThermostatMode
{
HEAT,
COOL,
FAN_ONLY,
Expand Down Expand Up @@ -146,7 +153,8 @@ inline const char *thermostatModeToString(ThermostatMode mode)
}
}

typedef enum TemperatureUnits {
typedef enum TemperatureUnits
{
FAHRENHEIT,
CELSIUS
} TemperatureUnits;
Expand All @@ -164,7 +172,6 @@ inline const char *temperatureUnitsToString(TemperatureUnits units)
}
}


double convertFahrenheitToCelsius(double fahrenheit);
double convertCelsiusToFahrenheit(double celsius);

Expand All @@ -178,7 +185,8 @@ inline double convertCelsiusToFahrenheit(double celsius)
return celsius * 9.0 / 5.0 + 32;
}

struct ThermostatData {
struct ThermostatData
{
double currentTemperature;
double targetTemperature;
double temperatureRange;
Expand Down
36 changes: 25 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@
#include "wifi.hpp"
#include "mqtt.hpp"
#include "watchdog.hpp"
#include "thermostat_context.hpp"

bool thermostat_timer_callback(struct repeating_timer *t) {
ThermostatContext *ctx = (ThermostatContext *) t->user_data;
ctx->thermostat->update();
ThermostatData data;
ctx->thermostat->getData(&data);
ctx->producer->update(&data);
return true;
}

void commandLoop()
{

void command_callback(ThermostatCommand *command, void *arg) {
ThermostatContext *ctx = (ThermostatContext *) arg;
ctx->commandParser->parseString(command);
ctx->thermostat->executeCommand(command);
}

int main()
Expand All @@ -45,9 +55,9 @@ int main()
Mqtt mqtt;
Watchdog watchdog;
Producer producer;
Thermostat thermostat;
CommandParser commandParser;
Repl repl;
Thermostat thermostat;


// Set up the application context
Expand All @@ -67,24 +77,28 @@ int main()
context.producer = &producer;
context.commandParser = &commandParser;
context.repl = &repl;
context.thermostat = &thermostat;


ThermostatError err = context.initialize();
thermostat.initialize(&context);


err = wifi.connect();
err = mqtt.connect();

err = mqtt.subscribe("home/thermostat/command", command_callback, &context);

err = thermostat.connect();
struct repeating_timer timer;
add_repeating_timer_ms(-5000, thermostat_timer_callback, &context, &timer);

while (true) {
ThermostatCommand command;
ThermostatError err = repl.read(&command);
sleep_ms(10);
if (err == THERMOSTAT_OK) {
thermostat.executeCommand(&command);
repl.print(&command);
}
thermostat.update();
ThermostatData currentData;
thermostat.getData(&currentData);
producer.update(&currentData);
sleep_ms(1000);

}
}
42 changes: 41 additions & 1 deletion src/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
#include "mqtt.hpp"
#include "lwip/apps/mqtt.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"


static mqtt_client_t *client;


static void publish_callback(void *arg, const char *topic, u32_t tot_len)
{
Mqtt *mqtt = (Mqtt *)arg;
printf("Publish callback\n");
mqtt->commandBuffer[0] = '\0';
mqtt->messageLength = tot_len;
mqtt->currentWriteIndex = 0;
}

static void data_callback(void *arg, const u8_t *data, u16_t len, u8_t flags)
{
Mqtt *mqtt = (Mqtt *)arg;
printf("Data callback\n");
for (int i = 0; i < len; i++)
{
mqtt->commandBuffer[mqtt->currentWriteIndex++] = data[i];
}
mqtt->commandBuffer[mqtt->currentWriteIndex] = '\0';
if (flags & MQTT_DATA_FLAG_LAST)
{
ThermostatCommand *command = new ThermostatCommand();
std::string inputString = std::string((char *)mqtt->commandBuffer);
command->command_string = inputString;
mqtt->executeCallback(command);
}
}


Mqtt::Mqtt()
{

Expand All @@ -19,6 +49,7 @@ ThermostatError Mqtt::initialize(Configuration *newConfig)
{
configuration = newConfig;
client = mqtt_client_new();
mqtt_set_inpub_callback(client, publish_callback, data_callback, this);
return THERMOSTAT_OK;
}

Expand Down Expand Up @@ -64,11 +95,20 @@ ThermostatError Mqtt::publish(const char *topic, char *message)
return THERMOSTAT_OK;
}

ThermostatError Mqtt::subscribe(const char *topic)
ThermostatError Mqtt::subscribe(const char *topic, CommandCallback callback, void *arg)
{
commandCallback = callback;
callbackArg = arg;
err_t err = mqtt_subscribe(client, topic, 0, NULL, NULL);
return THERMOSTAT_OK;
}

void Mqtt::executeCallback(ThermostatCommand *command)
{
commandCallback(command, callbackArg);
}


bool Mqtt::isConnected()
{
return mqtt_client_is_connected(client);
Expand Down
8 changes: 7 additions & 1 deletion src/mqtt/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ class Mqtt {
ThermostatError initialize(Configuration *configuration);
ThermostatError connect();
ThermostatError publish(const char *topic, char *message);
ThermostatError subscribe(const char *topic);
ThermostatError subscribe(const char *topic, CommandCallback, void *arg);
bool isConnected();
void executeCallback(ThermostatCommand *command);
uint8_t commandBuffer[1024];
uint32_t messageLength;
uint32_t currentWriteIndex;
private:
bool initalized;
Configuration *configuration;
CommandCallback commandCallback;
void *callbackArg;

};

Expand Down
2 changes: 1 addition & 1 deletion src/repl/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ ThermostatError Repl::read(ThermostatCommand *command)

std::string inputString = std::string((char *)buffer);
command->command_string = inputString;

commandParser->parseString(command);

return THERMOSTAT_OK;
}
return THERMOSTAT_NO_DATA;
Expand Down
49 changes: 0 additions & 49 deletions src/test_main.cpp

This file was deleted.

Loading

0 comments on commit 3bb09d3

Please sign in to comment.