Skip to content

Commit

Permalink
Merge pull request #142 from OPEnSLab-OSU/connection-patch
Browse files Browse the repository at this point in the history
Small fix for external function access
  • Loading branch information
WL-Richards authored Mar 6, 2024
2 parents 7e51dbf + 4fcb466 commit 39e158e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
59 changes: 29 additions & 30 deletions src/Internet/Connectivity/Loom_Wifi/Loom_Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/**
* Communication mode for routing traffic between the feather and Max client
*/
*/
enum CommunicationMode{
CLIENT, // Connect to a remote router to handle traffic
AP // Set the feather itself as an access point
Expand All @@ -26,110 +26,109 @@ typedef struct {

/**
* WiFi 101 library integrated with the manager to allow for easy sleep
*
*
* @author Will Richards
*/
*/
class Loom_WIFI : public NetworkComponent{
protected:

/* These aren't used with the Wifi manager */
void measure() override {};
void measure() override {};

bool getNetworkTime(int* year, int* month, int* day, int* hour, int* minute, int* second, float* tz) override;

// Initialize the device and connect to the network
void initialize() override;
void package() override;
void package() override;

// Reconnect to the network
void power_up() override;

// Disconnect from the network
void power_down() override;

// Overridden isConnected for the NetworkComponent
bool isConnected() override;

public:

/**
* Construct a new WiFi Manager
* @param man Reference to the manager to control all aspects of every module
* @param mode Whether or not to try to connect to an access point or create our own
* @param name The name of the WiFi access point we are going to connect to
* @param password The password (if applicable) to connect to the access point
*/
* @param password The password (if applicable) to connect to the access point
*/
Loom_WIFI(Manager& man, CommunicationMode mode, const char* name = "", const char* password = "", int connectionRetries = 5);

/**
* Construct a new WiFi manager, passing the credentials in as a json document
* @param man Reference to the manager
* @param man Reference to the manager
* @param jsonString JSON string to pull the credentials from
*/
*/
Loom_WIFI(Manager& man);

// Overridden isConnected for the NetworkComponent
bool isConnected() override;

/**
* Load the Wifi credentials from a JSON string, used to pull credentials from a file
*
*
* This automatically frees the input json
*
* @param jsonString JSON formatted string containing the SSID and password
*
* @param jsonString JSON formatted string containing the SSID and password
*/
void loadConfigFromJSON(char* json);

/**
* Returns a reference to the WifiClient
* @return wifiClient
*/
*/
WiFiClient& getClient() { return wifiClient; };

/**
* Get a reference to the UDP communication handler
*/
*/
WiFiUDP* getUDP() { return new WiFiUDP(); };

/**
* Attempt to ping Google, this tests if we are truly connected to the internet
*/
*/
bool verifyConnection();

/**
* Connect to an already existing access point
*/
*/
void connect_to_network();

/**
* Create our own access point
*/
*/
void start_ap();

/* Take in new WiFi information store it to flash and restart the WiFi chip to put the new credentials into effect*/
void storeNewWiFiCreds(const char* name, const char* password);

/**
* Get the IP address of the WiFi module
*/
*/
IPAddress getIPAddress();

/**
* Get the subnet mask of the connected network
*/
*/
IPAddress getSubnetMask();

/**
* Get the gateway IP of the network
*/
*/
IPAddress getGateway();

/**
* Get the broadcast IP of the network
*/
*/
IPAddress getBroadcast();


/**
* Called by max to ignore WiFi verification requests
*/
*/
void useMax() {usingMax = true; };

/**
Expand All @@ -144,8 +143,8 @@ class Loom_WIFI : public NetworkComponent{

/**
* Convert an IP address to a string
*/
void ipToString(IPAddress ip, char array[16]) {
*/
void ipToString(IPAddress ip, char array[16]) {
snprintf(array, 16, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
};

Expand All @@ -165,7 +164,7 @@ class Loom_WIFI : public NetworkComponent{
bool firstInit = true;
CommunicationMode mode; // Current WiFi mode we are in

IPAddress remoteIP; // IP address to send the UDP requests to
IPAddress remoteIP; // IP address to send the UDP requests to



};
48 changes: 24 additions & 24 deletions src/Internet/Logging/MQTTComponent/MQTTComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* MQTT Abstraction class that provides basic MQTT communciation functionality
*
*
* @author Will Richards
*/
class MQTTComponent : public Module{
Expand All @@ -21,63 +21,57 @@ class MQTTComponent : public Module{

/**
* Publish an MQTT message to a given topic and broker
*
*
* @param topic The MQTT topic we want to publish our message to
* @param message The message we want to publish to the given topic
* @param retain Whether or not we want to the message to be retained on the specified topic (default = false)
* @param qos What quality-of-service we want to upload the message with (default = 2)
*
*
* @return The status of the publish attempt
*/
bool publishMessage(const char* topic, const char* message, bool retain = false, int qos = 2);
bool publishMessage(const char* topic, const char* message, bool retain = false, int qos = 2);

/**
* Subscribe to a given topic to get the retained message and then immediately unsubscribe
*
*
* @param topic The topic we want to retrieve the message from
* @param message The buffer we want to store the retrieved message in
*
*
* @param The status of the retrieval attempt
*/
bool getCurrentRetained(const char* topic, char message[MAX_JSON_SIZE]);
bool getCurrentRetained(const char* topic, char message[MAX_JSON_SIZE]);

/**
* Publishes a message to a given topic with size 0 and the retain flag set to true so that the current retained message is removed
*
*
* @param topic The topic we want to delete the retained message on
*
*
* @return The result of the deletion attempt
* */
* */
bool deleteRetained(const char* topic);

/**
* Length of time the broker should keep the connection open for default
* Length of time the broker should keep the connection open for default
* @param time Length of time in MILLISECONDS the connection will be kept open
*/
*/
void setKeepAlive(int time) { keep_alive = time; };

/**
* Set the maximum number of reconnection attempts to make before failing
* @param retries The number of retries we want to make
*/
void setMaxRetries(int retries) { maxRetries = retries; };

/**
* Set the client ID for the specific MQTT connection
*
*
* @param id The new ID to set
*/
void setClientID(const char* id) { mqttClient.setId(id); };

/**
* Wrapper for specifying that we want to use a clean session or not (defaults to true)
*
*
* @param cleanSession Wether or not the session should be clean
*/
void setCleanSession(bool cleanSession) { mqttClient.setCleanSession(cleanSession); };

/* On power up we want to connect to the broker, this can be overridden but you will need to call connectToBroker again */
virtual void power_up() override { connectToBroker(); };
virtual void power_up() override { connectToBroker(); };

/* On initialize we just want to call the power_up function to connect to the broker, this can also be overriden but you should call power_up or connectToBroker again */
virtual void initialize() override;
Expand All @@ -92,9 +86,9 @@ class MQTTComponent : public Module{

/**
* Constructor for base MQTTComponent
*
*
* @param compName Name for the underlying module
* @param internet_client The Client object from a internet platform
* @param internet_client The Client object from a internet platform
*/
MQTTComponent(const char* compName, Client& internet_client) : Module(compName), mqttClient(internet_client) {
/* Clear all connection parameters */
Expand All @@ -113,9 +107,15 @@ class MQTTComponent : public Module{
*/
virtual void loadConfigFromJSON(char* json) = 0;

/**
* Set the maximum number of reconnection attempts to make before failing
* @param retries The number of retries we want to make
*/
void setMaxRetries(int retries) { maxRetries = retries; };

private:
MqttClient mqttClient; // Instance of the MQTT client

int keep_alive = 60000; // How long the broker should keep the connection open, defaults to a minute
int maxRetries = 4; // How many times we want to retry the connection
};
};

0 comments on commit 39e158e

Please sign in to comment.