// Arduino IDE 2.3.4, board set: NodeMCU-32S // remember // for UPLOAD press right button ( when WIFI antenna UP ) // for RESET press left button #include "secrets.h" // ______________________________________ here find the router login SSID PASSWORD ... //#include #include #if MQTT_SECURE #include WiFiClientSecure wifiClient; #else #include WiFiClient wifiClient; #endif const char *ssid = SECRET_SSID; const char *password = SECRET_PASSWORD; IPAddress thisip(FIXIP); // ___________________________________ fix IP bool WIFI_OK = false; unsigned long lastWIFIms = millis(); unsigned long nowWIFIms=lastWIFIms; #include // __________________________________________ ESP32 internal MQTT with TLS MQTTClient mqttclient; // the selection what broker is used / and if TLS / see secrets.h const char *mqtt_broker = SECRET_MQTTBROKER; // ___________________________________ MQTT IP broker // SECRET_LMQTTBROKER not work const char *mqtt_clientid = MQTT_CLIENTID; const int mqtt_port = MQTT_port; const char *mqtt_user = SECRET_MQTTUSER; const char *mqtt_pass = SECRET_MQTTPW; const char *mqtt_stopic = sTOPIC; // mqtt init status const char *mqtt_dtopic = dTOPIC; // data reporting const char *mqtt_rtopic = rTOPIC; // remote operation bool MQTT_OK = false; // change because i not want to get it stuck if no broker online BUT now, if start broker later, must reset board int MQTT_count = 0; String MQTTs = " MQTTs "; String msg = ""; char MQTTc[250]; String REMOTE_CMD=""; bool REMOTE_IN = false; // _________________________________________________________________ ONBOARD LED on NODEMCU ESP32S V1.1 is BLUE and not need pin declaration bool thisLED_status = 0; void LED_toggle() { thisLED_status = !thisLED_status; digitalWrite(LED_BUILTIN, thisLED_status); Serial.println("gpio LED_TOGGLE"); } void callback(String &atopic, String &apayload) { // try new MQTT.h callback // what MQTT commands we might get: TOGGLE if (apayload == "TOGGLE") { LED_toggle(); } else { // something not understood? Serial.print("\nmqtt Message arrived in topic: "); Serial.print(atopic); Serial.print(" : "); Serial.println(apayload); } } void setup_MQTT(){ msg="mqtt setup_MQTT"; #if MQTT_SECURE wifiClient.setInsecure(); // secure but not check server cert #endif mqttclient.begin(mqtt_broker, mqtt_port,wifiClient); mqttclient.setKeepAlive(100); mqttclient.onMessage(callback); if ( !MQTT_OK ) { msg+="\nmqtt try connect to MQTT broker ..."; if (mqttclient.connect(mqtt_clientid, mqtt_user, mqtt_pass)) { msg+="\nmqtt broker: "; msg+=mqtt_broker; msg+=" connected"; MQTT_OK = true; } else { msg+="failed "; } } Serial.println(msg); if ( MQTT_OK ) { // Publish and subscribe mqttclient.publish(mqtt_stopic, MQTT_STATUS); msg ="mqtt publish to topic: \n" + String(mqtt_stopic); msg+="\n" + String(mqtt_dtopic); msg+="\nmqtt subscribe to: \n" + String(mqtt_rtopic); mqttclient.subscribe(mqtt_rtopic); Serial.println(msg); } } void setup_WIFI(){ WiFi.mode(WIFI_STA); WiFi.setMinSecurity(WIFI_AUTH_WEP); // should help on certain router problems WiFi.config(thisip); if ( !WIFI_OK ) { Serial.println(); //WiFi.disconnect(); //delay(1000); msg = "www WIFI_connect / begin returns: "; msg += String( WiFi.begin(ssid, password) )+"\n"; while ( ( !WiFi.isConnected() ) && ( lastWIFIms + 30000 > nowWIFIms ) ) { delay(1000); Serial.print("w"); } if ( WiFi.status() == WL_CONNECTED ){ WIFI_OK = true; } else { WIFI_OK = false; } Serial.println(msg); } } void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); // _______________________________ later operated by webpage and local MQTT LED_toggle(); // _________________________________________________ see LED change to know board working Serial.begin(115200); // __________________________________________ talk to serial while (!Serial && (millis() < 10000)); Serial.println(); msg="ESP32 on Arduino IDE 2\nmodel: "; msg += ESP.getChipModel(); Serial.println(msg); setup_WIFI(); setup_MQTT(); } unsigned long lastTms = millis(); int lastTsec = 0; void loop() { if ( lastTms +1000 < millis() ) { // __________________________ 1 sec job lastTms = millis(); mqttclient.loop(); Serial.print("."); lastTsec += 1; if ( lastTsec >= 60 ) { // __________________________________ 1 min job lastTsec = 0; MQTT_count += 1; msg = "min_rep "; msg += String(MQTT_count); Serial.println(msg); mqttclient.publish(mqtt_dtopic,msg); } } } // need a file: secrets.h /* #define SECRET_SSID "" #define SECRET_PASSWORD "" #define FIXIP {192,168,1,214} // REMOTE BROKER #define SECRET_MQTTBROKER "" #define SECRET_MQTTUSER "" #define SECRET_MQTTPW "" #define MQTT_port 8883 #define MQTT_SECURE true #define MQTT_CLIENTID "ESP32s D214" #define MQTT_STATUS "ESP32s ArduinoIDE" #define sTOPIC "ESP32S/D214/status" #define bTOPIC "ESP32S/D214/log" #define dTOPIC "ESP32S/D214/data" #define rTOPIC "ESP32S/D214/set" //'LED TOGGLE' */