#include "secrets.h"
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#define BUTTON_PIN 15 // Digital pin connected to the push button
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
int buttonState = HIGH; // Initial button state
int lastButtonState = HIGH;
WiFiClientSecure net;
PubSubClient client(net);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "id.pool.ntp.org");
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client.setServer(AWS_IOT_ENDPOINT, 8883);
// Create a message handler
client.setCallback(messageHandler);
Serial.println("Connecting to AWS IoT");
while (!client.connected())
{
Serial.print(".");
if (client.connect(THINGNAME))
{
Serial.println("Connected to AWS IoT");
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
}
else
{
Serial.println("AWS IoT Connection Failed! Retrying...");
delay(1000);
}
}
}
void publishMessage()
{
StaticJsonDocument<200> doc;
doc["timestamp"] = timeClient.getEpochTime() + (7 * 3600);
doc["buttonState"] = buttonState;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer);
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
void messageHandler(char *topic, byte *payload, unsigned int length)
{
Serial.print("Incoming message from topic: ");
Serial.println(topic);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload, length);
const char *message = doc["message"];
Serial.println(message);
}
void setup()
{
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
connectAWS();
Wire.begin();
timeClient.begin();
timeClient.setTimeOffset(0);
}
void loop()
{
int currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState != lastButtonState)
{
if (currentButtonState == LOW)
{
Serial.println("Switch pressed");
}
else
{
Serial.println("Switch unpressed");
}
buttonState = currentButtonState;
publishMessage();
}
lastButtonState = currentButtonState;
timeClient.update();
client.loop();
delay(100); // Adjust the delay as needed
}