I had code working a few days ago, now I just get 301 errors from slack that the url has moved. There is nothing different from slack on the url, i have checked my webhook info. This is running on the wifi version of opta. I cannot figure out what the heck is going on.
#include <WiFi.h>
#include <WiFiSSLClient.h>
#include <ArduinoHttpClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Wi-Fi network details.
const char* ssid = "myssid";
const char* password = "mypassword";
// Slack webhook URL
const char* slackWebhookHost = "hooks.slack.com";
const int slackWebhookPort = 443;
const char* slackWebhookPath = "/services/the slack id...";
// Wi-Fi client instance for secure communication.
WiFiSSLClient wifiClient;
HttpClient slackClient(wifiClient, slackWebhookHost, slackWebhookPort);
// NTP Client settings
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", -4 * 3600, 60000); // Update every 60 seconds, ET (UTC-4 for daylight saving time)
void setup() {
// Begin serial communication at a baud rate of 115200.
Serial.begin(115200);
// Wait for the serial port to connect,
// This is necessary for boards that have native USB.
while (!Serial) {
delay(100);
}
// Start the Wi-Fi connection using the provided SSID and password.
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait for WiFi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Wi-Fi connected!");
logWiFiDetails();
// Initialize NTP client
timeClient.begin();
while (!timeClient.update()) {
timeClient.forceUpdate();
}
Serial.println("NTP time synchronized.");
Serial.print("Current time: ");
Serial.println(getCurrentTime());
// Send a Slack message to indicate the system is active
sendSlackNotification("Arduino Opta is active and connected to WiFi at " + getCurrentTime());
}
void loop() {
// Just keep looping and potentially add more functionality here
delay(10000); // Delay to avoid spamming
}
/**
Send a notification to Slack
@param message The message to send
@return none
*/
void sendSlackNotification(String message) {
if (WiFi.status() == WL_CONNECTED) {
if (!wifiClient.connect(slackWebhookHost, slackWebhookPort)) {
Serial.println("Connection to Slack failed.");
return;
}
String payload = "{\"text\":\"" + message + "\"}";
Serial.print("Payload: ");
Serial.println(payload);
// Send the HTTP POST request
slackClient.beginRequest();
slackClient.post(slackWebhookPath);
slackClient.sendHeader("Content-Type", "application/json");
slackClient.sendHeader("Content-Length", payload.length());
slackClient.beginBody();
slackClient.print(payload);
slackClient.endRequest();
int statusCode = slackClient.responseStatusCode();
String response = slackClient.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
slackClient.stop();
} else {
Serial.println("WiFi not connected, unable to send Slack message");
}
}
/**
Get the current time as a formatted string
@return the current time in the format "YYYY-MM-DD HH:MM:SS"
*/
String getCurrentTime() {
time_t epochTime = timeClient.getEpochTime();
struct tm* localTime = localtime(&epochTime);
char buffer[20];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
return String(buffer);
}
/**
Log WiFi connection details
*/
void logWiFiDetails() {
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
}
1 post - 1 participant