I have a a problem here connecting with my AWS server and ESP32. I have previously connected my esp32 and AWS IoT node a many number of times, but last night as I was trying to connect my server again, there was nothing to be shown on the serial plot monitor like previously what happens is like it shows connecting to wifi and then connection successfull, but serial monitor was empty this time. The code was compiling and running but serial monitor showed me nothing. I tried other piece of code just to check if serial monitor is working fine, it is it shows me readings and other things. I have tried restarting my arduino IDE, using different ESP32 and even different USB cable none of it seems to help me. I am attaching the code for reference although I know the code is correct as previously I have connected with AWS multiple times. Any possible help will be appreciated.
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#define AWS_IOT_PUBLISH_TOPIC "ESP32_Arduino/Pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "ESP32_Arduino/Sub"
// Sensor pins
#define sensorPower 27
#define sensorPin 34 // Analog pin
//#define sensorPin_D 26 // Digital pin
int output_value;
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
//-------------------------------------------------//
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()
{
StaticJsonDocument<200> doc;
doc["Moisture"] = output_value;
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 char* message = doc["message"];
Serial.println(message);
}
//----------------------------------------------//
int readSensor() {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(10); // Allow power to settle
int val = analogRead(sensorPin); // Read the analog value form sensor
// int val = digitalRead(sensorPin_D); // Read the Digital value form sensor
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return analog moisture value
}
void setup() {
// put your setup code here, to run once:
connectAWS();
pinMode(sensorPower, OUTPUT);
// Initially keep the sensor OFF
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//get the reading from the function below and print it
// Serial.print("Analog output: ");
// Serial.println(readSensor());
output_value= readSensor();
output_value = map(output_value,1400,0,0,100);
Serial.print("Mositure : ");
Serial.print(output_value);
Serial.println("%");
//---------------------------//
publishMessage();
client.loop();
//---------------------------//
delay(1000);
//--------------------------------------// This is for analog Output
// int val = readSensor();
// Serial.print("Digital Output: ");
// Serial.println(val);
//
// // Determine status of our soil moisture situation
// if (val) {
// Serial.println("Status: Soil is too dry - time to water!");
// } else {
// Serial.println("Status: Soil moisture is perfect");
// }
//
// delay(1000); // Take a reading every second for testing
// // Normally you shoul take reading perhaps every 12 hours
// Serial.println();
//-----------------------------------// This is for digital output
}