// successfully send data local to aws and receive data from server to IAM user Project_2024_2025 things
#include <WiFi.h>
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal.h>
// AWS IoT topics
#define AWS_IOT_PUBLISH_TOPIC "raj/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "raj/sub"
// LCD pin definitions
const int rs = 12, en = 14, d4 = 27, d5 = 26, d6 = 25, d7 = 33;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// WiFi and AWS IoT
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
// Relay pin definition
const int relayPin = 15; // Connect the relay IN pin to digital pin D1
// Password to match
const String Line_on = "1234";
const String Line_off = "1235";
String enteredPassword = "";
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.connect(THINGNAME)) {
Serial.print(".");
delay(100);
}
if (!client.connected()) {
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
void publishMessage(const String& status) {
StaticJsonDocument<200> doc;
doc["password_status"] = status;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
void messageHandler(char* topic, byte* payload, unsigned int length) {
Serial.print("incoming: ");
Serial.println(topic);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
const String message = doc["message"];
Serial.println(message);
if (message == Line_on) {
digitalWrite(relayPin, HIGH);
publishMessage("Correct Password for Line ON from server -- Line ON");
Serial.println("Line ON");
}
else if(message == Line_off){
digitalWrite(relayPin, LOW);
publishMessage("Correct Password for Line OFF from server -- Line OFF");
Serial.println("Line OFF");
}
else {
publishMessage("Wrong Password from server");
Serial.println("Wrong Password from server");
}
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); // Initialize the LCD
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
pinMode(relayPin, OUTPUT); // Set the relay pin as an output
digitalWrite(relayPin, LOW); // Ensure the relay is off initially
connectAWS();
}
void loop() {
client.loop();
// Check if data is available on the Serial Monitor
if (Serial.available() > 0) {
enteredPassword = Serial.readStringUntil('\n'); // Read the password from the Serial Monitor
// Remove any trailing newline or carriage return characters
enteredPassword.trim();
// Check the entered password
checkPassword();
}
}
void checkPassword() {
if (enteredPassword == Line_on) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Line ON");
digitalWrite(relayPin, HIGH); // Turn on the relay (and thus the LED)
publishMessage("Correct Password for Line ON from local-- Line ON");
Serial.println("Line ON");
}
else if (enteredPassword == Line_off){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Line OFF");
digitalWrite(relayPin, LOW); // Turn Off the relay (and thus the LED)
publishMessage("Correct Password for Line OFF from local-- Line OFF");
Serial.println("Line OFF");
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Password");
publishMessage("Wrong Password from local");
Serial.println("Wrong Password");
}
enteredPassword = ""; // Clear entered password
delay(2000); // Display message for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
delay(2000);
Serial.println("Enter Password:");
}