Created
November 17, 2024 22:21
-
-
Save djom202/d1660f4c9c5b9fa038b753dc6057c7bf to your computer and use it in GitHub Desktop.
AWS Connction with ArduinoCloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef AwsManager_h | |
#define AwsManager_h | |
void setupAws(MQTTClient &client, WiFiClientSecure &net); | |
void subscribeTopic(MQTTClient &client); | |
void setCretifications(WiFiClientSecure &net); | |
void publishMessage(MQTTClient &client, String payload); | |
void messageHandler(String &topic, String &payload); | |
const char* getSerial(); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "AwsManager.h" | |
#include <ESPping.h> | |
#include <MQTTClient.h> | |
// Topic MQTT | |
const char AWS_IOT_PUBLISH_TOPIC[] = "esp32/test"; | |
const char AWS_IOT_SUBSCRIBE_TOPIC[] = "esp32/test"; | |
// AWS MQTT Info | |
const char AWS_ENDPOINT[] = "NAME.iot.us-east-1.amazonaws.com"; | |
const int AWS_PORT = 8883; | |
static const char AWS_ROOT_CA[] PROGMEM = R"EOF( | |
-----BEGIN CERTIFICATE----- | |
-----END CERTIFICATE----- | |
)EOF"; | |
static const char AWS_CLIENT_CRT[] PROGMEM = R"KEY( | |
-----BEGIN CERTIFICATE----- | |
-----END CERTIFICATE----- | |
)KEY"; | |
static const char AWS_PRIVATE_KEY[] PROGMEM = R"KEY( | |
-----BEGIN RSA PRIVATE KEY----- | |
-----END RSA PRIVATE KEY----- | |
)KEY"; | |
void setupAws(MQTTClient &client, WiFiClientSecure &net) { | |
Serial.println("Setting up the server for AWS"); | |
setCretifications(net); | |
client.begin(AWS_ENDPOINT, AWS_PORT, wifiClient); | |
client.onMessage(messageReceived); | |
Serial.println("Connecting to AWS IOT"); | |
while (!client.connect(getSerial())) { | |
isAwsConnected = false; | |
Serial.print("Error al conectar a AWS IoT Core: "); | |
Serial.println(client.returnCode()); | |
Serial.println(client.lastError()); | |
delay(10000); | |
} | |
Serial.println(); | |
if(!client.connected()){ | |
Serial.println("AWS IoT Timeout!"); | |
return; | |
} | |
client.subscribe(AWS_IOT_PUBLISH_TOPIC); | |
Serial.println("AWS IoT Connected!"); | |
isAwsConnected = true; | |
} | |
void setCretifications(WiFiClientSecure &net) { | |
net.setCACert(AWS_ROOT_CA); | |
net.setCertificate(AWS_CLIENT_CRT); | |
net.setPrivateKey(AWS_PRIVATE_KEY); | |
} | |
char* getStateConnection(int state) { | |
if (state == -4){ | |
return "Timeout"; | |
} else if (state == -3){ | |
return "ConnectionLost"; | |
} else if (state == -2){ | |
return "ConnectionFailed"; | |
} else if (state == -1){ | |
return "Disconnected"; | |
} else if (state == 0){ | |
return "Connected"; | |
} else if (state == 1){ | |
return "BadProtocol"; | |
} else if (state == 2){ | |
return "BadClientId"; | |
} else if (state == 3){ | |
return "Unavailable"; | |
} else if (state == 4){ | |
return "BadCredentials"; | |
} else if (state == 5){ | |
return "Unauthorized"; | |
} | |
} | |
void messageReceived(String &topic, String &payload) { | |
Serial.println("Message received!"); | |
Serial.println("Topic: " + topic); | |
Serial.println("Payload: " + payload); | |
if (topic == INCOMMING_ATTACK_SUBSCRIBE_TOPIC) { | |
Serial.println("Will be attacked"); | |
} else if (topic == CHANGE_TURN_SUBSCRIBE_TOPIC) { | |
Serial.println("Now it your turn or the opponent"); | |
} else if (topic == LIFE_POINTS_SUBSCRIBE_TOPIC) { | |
Serial.println("new life points to show"); | |
} | |
} | |
const char* getSerial() { | |
String clientId = "ESP32Client-"; | |
clientId += DEVICE_LOGIN_NAME; | |
return clientId.c_str(); | |
} | |
void subscribeTopic(MQTTClient &client) { | |
if (!client.connected()) return; | |
if (client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC)) { | |
Serial.println("Successful subscription."); | |
} else { | |
Serial.println("Subscription error."); | |
} | |
} | |
void publishMessage(MQTTClient &client, String msg) { | |
static unsigned long lastMsg = 0; | |
unsigned long now = millis(); | |
if (now - lastMsg > 5000) { | |
lastMsg = now; | |
Serial.print("Publishing message: "); | |
Serial.println(msg); | |
client.publish(AWS_IOT_PUBLISH_TOPIC, msg.c_str()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "arduino_secrets.h" | |
#include "thingProperties.h" | |
#include <ArduinoJson.h> | |
#include <WiFiClientSecure.h> | |
#include <MQTT.h> | |
#include "AwsManager.h" | |
#define LEVEL_LOGS 2 | |
WiFiClientSecure wifiClient = WiFiClientSecure(); | |
MQTTClient mqttClient = MQTTClient(256); | |
void setup() { | |
Serial.begin(19200); | |
delay(1500); | |
initProperties(); | |
ArduinoCloud.begin(ArduinoIoTPreferredConnection); | |
setDebugMessageLevel(LEVEL_LOGS); | |
ArduinoCloud.printDebugInfo(); | |
isDeviceConnected = false; | |
isAwsConnected = false; | |
} | |
void loop() { | |
ArduinoCloud.update(); | |
mqttClient.loop(); | |
if (ArduinoCloud.connected()) { | |
Serial.print("IP: "); | |
Serial.println(WiFi.localIP()); | |
setCretifications(wifiClient); | |
setupAws(mqttClient, wifiClient); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment