Last active
June 16, 2022 10:57
-
-
Save Dushyantsingh-ds/ef14c2f15e94663c8a2c0e134c58796f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <ESP8266WiFi.h> | |
| #include <WiFiClientSecure.h> | |
| #include <PubSubClient.h> | |
| #include <ArduinoJson.h> | |
| #include <time.h> | |
| #include "secrets.h" | |
| #define AWS_IOT_PUBLISH_TOPIC "ESP8266/pub" | |
| #define AWS_IOT_SUBSCRIBE_TOPIC "ESP8266/sub" | |
| String Massage= "Hello from ESP8266 IoT Module" ; | |
| unsigned long lastMillis = 0; | |
| unsigned long previousMillis = 0; | |
| const long interval = 5000; | |
| struct tm timeinfo; | |
| WiFiClientSecure net; | |
| BearSSL::X509List cert(AWS_CERT_CA); | |
| BearSSL::X509List client_crt(AWS_CERT_CRT); | |
| BearSSL::PrivateKey key(AWS_CERT_PRIVATE); | |
| PubSubClient client(net); | |
| time_t now; | |
| time_t nowish = 1510592825; | |
| void NTPConnect(void) | |
| { | |
| Serial.print("Setting time using SNTP"); | |
| configTime(TIME_ZONE * 3600, 0 * 3600, "pool.ntp.org", "time.nist.gov"); | |
| now = time(nullptr); | |
| while (now < nowish) | |
| { | |
| delay(500); | |
| Serial.print("."); | |
| now = time(nullptr); | |
| } | |
| Serial.println("done!"); | |
| gmtime_r(&now, &timeinfo); | |
| Serial.print("Current time: "); | |
| Serial.print(asctime(&timeinfo)); | |
| } | |
| void messageReceived(char *topic, byte *payload, unsigned int length) | |
| { | |
| Serial.print("Received ["); | |
| Serial.print(topic); | |
| Serial.print("]: "); | |
| for (int i = 0; i < length; i++) | |
| { | |
| Serial.print((char)payload[i]); | |
| } | |
| Serial.println(); | |
| } | |
| void connectAWS() | |
| { | |
| delay(3000); | |
| WiFi.mode(WIFI_STA); | |
| WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
| Serial.println(String("Attempting to connect to SSID: ") + String(WIFI_SSID)); | |
| while (WiFi.status() != WL_CONNECTED) | |
| { | |
| Serial.print("."); | |
| delay(1000); | |
| } | |
| NTPConnect(); | |
| net.setTrustAnchors(&cert); | |
| net.setClientRSACert(&client_crt, &key); | |
| client.setServer(AWS_IOT_ENDPOINT, 8883); | |
| client.setCallback(messageReceived); | |
| Serial.println("Connecting to AWS IOT"); | |
| while (!client.connect(THINGNAME)) | |
| { | |
| Serial.print("."); | |
| delay(1000); | |
| } | |
| 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; | |
| gmtime_r(&now, &timeinfo); | |
| doc["Time"] = asctime(&timeinfo); | |
| doc["Massage"] = Massage; | |
| char jsonBuffer[512]; | |
| serializeJson(doc, jsonBuffer); // print to client | |
| client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer); | |
| } | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| connectAWS(); | |
| } | |
| void loop() | |
| { | |
| gmtime_r(&now, &timeinfo); | |
| Serial.print("Current time: "); | |
| Serial.print(asctime(&timeinfo)+'\n'); | |
| Serial.print(F("Massage: ")); | |
| Serial.print(Massage+'\n'); | |
| delay(2000); | |
| now = time(nullptr); | |
| if (!client.connected()) | |
| { | |
| connectAWS(); | |
| } | |
| else | |
| { | |
| client.loop(); | |
| if (millis() - lastMillis > 5000) | |
| { | |
| lastMillis = millis(); | |
| publishMessage(); | |
| } | |
| } | |
| } |
This file contains hidden or 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 <pgmspace.h> | |
| #define SECRET | |
| #define THINGNAME "ESP8266" | |
| const char WIFI_SSID[] = "Local"; | |
| const char WIFI_PASSWORD[] = "Local123"; | |
| const char AWS_IOT_ENDPOINT[] = "xxxxxxxx-ats.iot.ap-south-1.amazonaws.com"; | |
| int8_t TIME_ZONE = -5; //NYC(USA): -5 UTC | |
| // Amazon Root CA 1 | |
| static const char AWS_CERT_CA[] PROGMEM = R"EOF( | |
| -----BEGIN CERTIFICATE----- | |
| -----END CERTIFICATE----- | |
| )EOF"; | |
| // Device Certificate | |
| static const char AWS_CERT_CRT[] PROGMEM = R"KEY( | |
| -----BEGIN CERTIFICATE----- | |
| -----END CERTIFICATE----- | |
| )KEY"; | |
| // Device Private Key | |
| static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( | |
| -----BEGIN RSA PRIVATE KEY----- | |
| -----END RSA PRIVATE KEY----- | |
| )KEY"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment