Skip to content

Commit

Permalink
Merge pull request #133 from lahirumenik/main
Browse files Browse the repository at this point in the history
iot connection cloud -testing
  • Loading branch information
lahirumenik authored Jan 28, 2024
2 parents 3816f49 + 881e968 commit 1ce3dc3
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
118 changes: 118 additions & 0 deletions code/iotconnect/main.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11

#define AWS_IOT_PUBLISH_TOPIC "gun/1/test"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"


float h ;
float t;



WiFiClient net;;
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

// size_t privateKeySize = AWS_CERT_CA.size();
// char *privateKey = new char[privateKeySize + 1];

// AWS_CERT_CA.readBytes(privateKey, privateKeySize);
// privateKey[privateKeySize] = '\0';

// net.loadCACert(privateKey);
// net.loadCertificate(AWS_CERT_CRT);
// net.loadPrivateKey(AWS_CERT_PRIVATE);

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


Serial.println("AWS IoT Connected!");
}

void publishMessage()
{

client.publish(AWS_IOT_PUBLISH_TOPIC, "he");
}

void messageHandler(char* topic, byte* payload, unsigned int length)
{
Serial.print("incoming: ");
Serial.println(topic);

}

void setup()
{
Serial.begin(115200);
connectAWS();

}

void loop()
{
h = 9;
t =7;


if (isnan(h) || isnan(t) ) // Check if any reads failed and exit early (to try again).
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));

publishMessage();
client.loop();
delay(1000);
}
111 changes: 111 additions & 0 deletions code/iotconnect/secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@


#include <pgmspace.h>

#define SECRET
#define THINGNAME "gun1" //change this

const char WIFI_SSID[] = "HUAWEI nova 7i"; //change this
const char WIFI_PASSWORD[] = "04131107"; //change this
const char AWS_IOT_ENDPOINT[] = "a2leuqp8y2i70g-ats.iot.ap-southeast-1.amazonaws.com"; //change this

// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6
b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL
MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv
b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj
ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM
9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw
IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6
VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L
93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm
jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC
AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA
A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI
U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs
N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv
o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU
5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy
rqXRfboQnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----

)EOF";
// Device Certificate //change this
static const char AWS_CERT_CRT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
MIIDFDCCAfwCFGU2z949uv1HP8riFmv6Dz8A7hG7MA0GCSqGSIb3DQEBCwUAMEgx
CzAJBgNVBAYTAlNMMRMwEQYDVQQIDApTb21lLVN0YXRlMREwDwYDVQQKDAhhcmNv
bWJhdDERMA8GA1UEAwwIYXJjb21iYXQwHhcNMjQwMTI2MTM1NDA2WhcNMjUwMTI1
MTM1NDA2WjBFMQswCQYDVQQGEwJTTDETMBEGA1UECAwKU29tZS1TdGF0ZTERMA8G
A1UECgwIYXJjb21iYXQxDjAMBgNVBAMMBWd1bi8xMIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAuvpavNKBHa5iuDXbccEQ4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ggEiMA0GCSqG
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQCazSiNESFFAhFTcLL0GuYhqugjU1IZmP95
RaB6QuNAMK8OQ754yF6tDlWxc6ZN7l49nSJuHuKPs4PoAGJr+ydokQQh+1zX4Q4s
PbW44rsfc3gE5b4jZda5b7E/J5cy2hQjNnzfQwiwpAemL+ye3HcESwY9VXMEEXCB
oqKeFqSAsghKMYdKlB71qsL8koAXbETEwbutVYnXBa02FGUhVd6m/0QAuMdY8dK3
2oIshSBVVzIbwN/u/Ofj+FCoT+HN72k0QFXLUlHr03/eKyKeIBcVJxOLgy7AgzCD
oa/I1ERPxfcBK1l2MYSHirIidboizaR7tw6ZBCx3PYEyeSD7BQPbAgMBAAGjUzBR
MB0GA1UdDgQWBBTK/LIGqAK29+fp6WmHreTCEnVaOzAfBgNVHSMEGDAWgBTK/LIG
qAK29+fp6WmHreTCEnVaOzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUA
A4IBAQBr/LXSOWrocuEyDuwP4VzrDaYmsDpRZ0AGc71so889F2jUsof3MJSn0iap
Dpw3t/YaREAr3ytuXM188tQYHMMrFzdZRTg7C8SWbmvsGqYCq4nkbwDOPVuL4iPS
3+5Tex6MKPdROQLvmUiMc5O6NmE6tJU4nh965H8q0jwZQfGSXOwDo0tQ0DSN2OzB
6DSvlKA38raArCvMqfQiK+D2N79kk5We+pLh1f92yPV2vbEEN+QWUTyvsu4l5MOn
GMRjV4n9HgZEXQjjcHm64mHGkZZHXZ87xZ7vTzLOrN+xoBhaOaM7iblxSAL9nQ0O
UhhnT477jo51rwYv4uvzfLvrQvX6
-----END CERTIFICATE-----
)KEY";
// Device Private Key //change this
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY(
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6+lq80oEdrmK4
NdtxwRDhn4GnRPi/4j+CviNe4iVnFLwsP1nRmP9rG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