1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <WiFiClientSecure.h> #include <PubSubClient.h> #include <time.h>
uint LED = 16; const char *ssid = "regen"; const char *password = "regen's pwd";
const int mqtt_port = 8883; const char *mqtt_broker = "server ip or domain"; const char *mqtt_topic = "/esp38266/test"; const char *mqtt_username = "uasername"; const char *mqtt_password = "passwd";
const char *mqtt_start = "start"; const char *mqtt_stop = "stop";
const char *ntp_server = "pool.ntp.org"; const long gmt_offset_sec = 0; const int daylight_offset_sec = 0;
WiFiClientSecure espClient; PubSubClient mqtt_client(espClient);
static const char ca_cert[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE-----
-----END CERTIFICATE----- )EOF";
void connectToWiFi(); void connectToMQTT(); void syncTime(); void mqttCallback(char *topic, byte *payload, unsigned int length); bool judgeEqual(char *str1, const char *str2, int length);
void setup() { pinMode(LED, OUTPUT); Serial.begin(115200); connectToWiFi(); syncTime(); mqtt_client.setServer(mqtt_broker, mqtt_port); mqtt_client.setCallback(mqttCallback); connectToMQTT(); }
void connectToWiFi() { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); }
void syncTime() { configTime(gmt_offset_sec, daylight_offset_sec, ntp_server); Serial.print("Waiting for NTP time sync: "); while (time(nullptr) < 8 * 3600 * 2) { delay(1000); Serial.print("."); } Serial.println("Time synchronized"); struct tm timeinfo; if (getLocalTime(&timeinfo)) { Serial.print("Current time: "); Serial.println(asctime(&timeinfo)); } else { Serial.println("Failed to obtain local time"); } }
void connectToMQTT() { espClient.setCACert(ca_cert); while (!mqtt_client.connected()) { String client_id = "esp32-client-" + String(WiFi.macAddress()); Serial.printf("Connecting to MQTT Broker as %s.....\n", client_id.c_str()); if (mqtt_client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { Serial.println("Connected to MQTT broker"); mqtt_client.subscribe(mqtt_topic); mqtt_client.publish(mqtt_topic, "Hi EMQX I'm ESP32 ^^"); } else { Serial.print("Failed to connect to MQTT broker, rc="); Serial.println(mqtt_client.state()); delay(5000); } } }
void mqttCallback(char *topic, byte *payload, unsigned int length) { Serial.print("Message received on topic: "); Serial.print(topic); Serial.print("]: "); char judge[5]; for (int i = 0; i < length; i++) { if (i >= 12) { judge[i - 12] = payload[i]; } Serial.print((char)payload[i]); } if (judgeEqual(judge, mqtt_start, 5)) { Serial.print("Start signal received, turning LED on"); digitalWrite(LED, HIGH); } if (judgeEqual(judge, mqtt_stop, 4)) { Serial.print("Stop signal received, turning LED off"); digitalWrite(LED, LOW); } Serial.println(); }
bool judgeEqual(char *str1, const char *str2, int length) { for (int i = 0; i < length; i++) { if (str1[i] != str2[i]) return false; } return true; }
void loop() { if (!mqtt_client.connected()) { connectToMQTT(); } mqtt_client.loop(); }
|