Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFiClientSecure.h>
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include "mqtt_crt.h"
- #include "mqtt_key.h"
- #include "time.h"
- #include <time.h>
- #define DEBUG_TLS_MEM
- const char* ssid = "X";
- const char* password = "X";
- const char* mqtt_server = "192.168.254.8";
- const char* my_name = "esp-ctrl-5ccf7f0167f0";
- const uint8_t IP[4] = {192, 168, 254, 15};
- const uint8_t GATEWAY[4] = {192, 168, 254, 1};
- const uint8_t SUBNET[4] = {255, 255, 255, 0};
- const uint8_t DNS[4] = {8, 8, 8, 8};
- int timezone = 0;
- int dst = 0;
- WiFiClientSecure espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- void callback(char* topic, byte* payload, unsigned int length);
- void setup() {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- WiFi.mode(WIFI_STA);
- WiFi.hostname(my_name);
- WiFi.config(IP, GATEWAY, SUBNET, DNS);
- WiFi.begin(ssid, password);
- Serial.println("\nConnecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(1000);
- }
- configTime(timezone, dst, "pool.ntp.org", "time.nist.gov");
- Serial.println("\nWaiting for time");
- while (!time(nullptr)) {
- Serial.print(".");
- delay(100);
- }
- Serial.println("");
- espClient.setCertificate(esp_crt, esp_crt_len);
- espClient.setPrivateKey(esp_key, esp_key_len);
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- // Switch on the LED if an 1 was received as first character
- if ((char)payload[0] == '1') {
- digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is acive low on the ESP-01)
- } else {
- digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP8266Client")) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("outTopic", "hello world");
- // ... and resubscribe
- client.subscribe("inTopic");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void loop() {
- time_t n = time(nullptr);
- Serial.println(ctime(&n));
- delay(1000);
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- long now = millis();
- if (now - lastMsg > 2000) {
- lastMsg = now;
- ++value;
- snprintf (msg, 75, "hello world #%ld", value);
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("outTopic", msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement