#include <WiFi.h>
const int p_LED = 2;
#include <PubSubClient.h>
const char* mqtt_server = "broker.hivemq.com";
const int mqtt_port = 1883;
//WiFi Credentials
const char* ssid = "Wokwi-GUEST"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = ""; // The password of the Wi-Fi network
WiFiClient wifi_client;
struct Schedule {
const char* name;
const int pin;
unsigned long T;
unsigned long t_last;
};
Schedule sch_table[] = {
{"blue", 27, 200, 0}, //blue
{"green", 26, 123, 0}, //green
{"yellow", 14, 333, 0}, //yellow
{"red", 13, 421, 0}, //red
};
int N_sch = sizeof(sch_table)/sizeof(Schedule);
//pre declare call back
void mqtt_callback(char* topic, byte* payload, unsigned int length);
//PubSubClient constructor object
PubSubClient mqtt_client(mqtt_server, mqtt_port, mqtt_callback, wifi_client);
// Topics
// to publish
// char* pub_topic = "apu/cs452/yeh/button";
// to subscribe
char* sub_topic = "apu/cubesat/antenna/switch/1";
// char* sub_topic = "apu/#";
// char* sub_topic_2 = "apu/cs452/yeh/message";
//random cliend ID
char* cliend_id = "apu_cubesat_antenna_switch_1";
void handleSwitch(byte* payload, uint16_t length);
// void handleMessage(byte* payload, uint16_t length);
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Pub Client for LED");
Serial.println("");
Serial.println("(c) 2023 James Yeh");
Serial.println();
// wifiMulti.addAP("APU-MYDEVICES");
Serial.println("Connecting...");
WiFi.begin(ssid); // Connect to the network
connectWiFi();
for (int i = 0; i < N_sch; i++) {
pinMode(sch_table[i].pin, OUTPUT);
digitalWrite(sch_table[i].pin, LOW);
}
}
//MQTT callback
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
//only print the payload - can modify code to call other functions based on topic
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, sub_topic) == 0) {
handleSwitch(payload, length);
}
// if (strcmp(topic, sub_topic_2) == 0) {
// handleMessage(payload, length);
// }
}
void handleSwitch(byte* payload, uint16_t length) {
if (length <= 0)
return;
char c = (char)payload[0];
// Serial.println(c);
if ((c >= '1') && (c <= '4')) {
int j = c - '1';
// Serial.println(j);
for (int i = 0; i < N_sch; i++) {
if (i == j)
digitalWrite(sch_table[i].pin, HIGH);
else
digitalWrite(sch_table[i].pin, LOW);
}
}
}
void loop() {
//reconnect if disconnected
if (!mqtt_client.connected()) {
mqtt_reconnect();
}
mqtt_client.loop();
}
//(re)connect to MQTT broker
void mqtt_reconnect() {
// Loop until we're reconnected
while (!mqtt_client.connected()) {
Serial.print("Attempting MQTT connection...");
// Connect MQTT
// if (client.connect(cliend_id, username, password)) {
if (mqtt_client.connect(cliend_id)) {
Serial.println("connected");
//Subscribe
mqtt_client.subscribe(sub_topic, 1);
// mqtt_client.subscribe(sub_topic_2, 1);
} else {
Serial.print("failed, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void connectWiFi() {
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
// while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
digitalWrite(p_LED, !digitalRead(p_LED));
}
digitalWrite(p_LED, LOW);
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}