iot base changes

This commit is contained in:
Siwat Sirichai 2023-12-28 11:41:20 +07:00
parent 822eb66285
commit 908c3f9c0e
13 changed files with 80 additions and 2 deletions

View File

@ -80,3 +80,8 @@ bool AnalogCard::begin()
void AnalogCard::loop()
{
}
uint8_t AnalogCard::getType()
{
return CARD_TYPE_ANALOG;
}

View File

@ -3,6 +3,8 @@
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#define CARD_TYPE_ANALOG 0x02
#define ANALOG_INPUT_BANK_A_ADDRESS 0x48
#define ANALOG_INPUT_BANK_B_ADDRESS 0x49
#define DAC0_ADDRESS 0x60
@ -17,6 +19,7 @@ class AnalogCard : public ExpansionCard {
uint16_t analogRead(uint8_t pin);
bool begin();
void loop();
uint8_t getType();
private:
MCP4725 dac0;
MCP4725 dac1;

View File

@ -193,3 +193,8 @@ void DigitalInputCard::loadPinMap(uint8_t pinMap[16])
this->virtualPinMap[pinMap[i]] = i;
}
}
uint8_t DigitalInputCard::getType()
{
return CARD_TYPE_DIGITAL_INPUT;
}

View File

@ -2,6 +2,8 @@
#include <ExpansionCard.hpp>
#include <PCF8574.h>
#define CARD_TYPE_DIGITAL_INPUT 0x01
class DigitalInputCard : public ExpansionCard {
public:
// Instantiate the card with the specified address
@ -28,6 +30,8 @@ class DigitalInputCard : public ExpansionCard {
void unregisterCallback();
// Load a new pin map
void loadPinMap(uint8_t pinMap[16]);
// Get type of card
uint8_t getType();
private:
PCF8574 inputBankA;
PCF8574 inputBankB;

View File

@ -39,3 +39,8 @@ void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) {
// Dummy loop function
void DigitalOutputCard::loop() {
}
// Get type of card
uint8_t DigitalOutputCard::getType() {
return CARD_TYPE_DIGITAL_OUTPUT;
}

View File

@ -1,6 +1,9 @@
#pragma once
#include <ExpansionCard.hpp>
#include <Adafruit_PWMServoDriver.h>
#define CARD_TYPE_DIGITAL_OUTPUT 0x00
class DigitalOutputCard : public ExpansionCard
{
public:
@ -16,6 +19,8 @@ public:
void digitalWrite(uint8_t pin, bool state);
// Set the output to the specified pwm value
void analogWrite(uint8_t pin, uint16_t value);
// Get type of card
uint8_t getType();
private:
Adafruit_PWMServoDriver pwm;
uint8_t address;

View File

@ -0,0 +1,4 @@
#pragma once
#include <ExpansionCard.hpp>
class DigitalOutputIoT : p

View File

@ -0,0 +1,34 @@
#include <ESPMegaIoT.hpp>
void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) {
// Create a null terminated string from the payload
memcpy(payload_buffer, payload, length);
payload_buffer[length] = '\0';
// Remove the base topic from the topic
char *topic_without_base = topic + strlen(base_topic) + 1;
// Call the respective card's mqtt callback
// Note that after the base topic, there should be the card id
// /base_topic/card_id/...
// First, get the card id in integer form
char *card_id_str = strtok(topic_without_base, "/");
uint8_t card_id = atoi(card_id_str);
// Check if the card is registered
if (expansionCards[card_id] == NULL) {
return;
}
// Get the card type
uint8_t card_type = expansionCards[card_id]->getType();
// Cast the card to the respective type
switch(card_type) {
case CARD_TYPE_ANALOG_INPUT:
((AnalogIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
case CARD_TYPE_DIGITAL_INPUT:
((DigitalInputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
case CARD_TYPE_DIGITAL_OUTPUT:
((DigitalOutputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
}
}

View File

@ -1,7 +1,10 @@
#include <ExpansionCard.hpp>
#include <AnalogCard.hpp>
#include <AnalogIoT.hpp>
#include <DigitalInputCard.hpp>
#include <DigitalInputIoT.hpp>
#include <DigitalOutputCard.hpp>
#include <DigitalOutputIoT.hpp>
#include <PubSubClient.h>
#include <ETH.h>
@ -25,12 +28,16 @@ class ESPMegaIoT
void disconnectFromMqtt();
void publishToTopic(char *topic, char *payload);
void registerMqttCallback(void (*callback)(char *, char *));
void checkCardType(uint8_t card_id);
private:
void sessionKeepAlive();
void mqttReconnect();
void wifiReconnect();
void mqttCallback(char *topic, byte *payload, unsigned int length);
PubSubClient mqtt;
ExpansionCard *expansionCards[255];
bool card_publish_enabled[255];
}
char payload_buffer[200];
char base_topic[100];
};

View File

@ -8,4 +8,6 @@ class ExpansionCard {
virtual bool begin() {}
// Preform a loop to refresh the input buffers
virtual void loop() {}
// Get the card type
virtual uint8_t getType() {}
};

View File

@ -0,0 +1,2 @@
class

View File

@ -23,4 +23,6 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
robtillaart/FRAM_I2C@^0.6.1
paulstoffregen/Time@^1.6.1
paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033
knolleary/pubsubclient@^2.8.0
monitor_speed = 115200