Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiManager.h>
- #include <PubSubClient.h>
- // Setările pentru Azure IoT Hub
- const char* iotHubEndpoint = "licentaDrulaAlex.azure-devices.net";
- const char* deviceId = "1";
- const char* deviceKey = "HostName=licentaDrulaAlex.azure-devices.net;DeviceId=1;SharedAccessKey=5itKG9VL/duaqiELaaF8JWbqtHVnaQtjPtEiX4mfCBM=";
- WiFiClientSecure wifiClient;
- PubSubClient mqttClient(wifiClient);
- #define TRIGGER_PIN 0
- bool wm_nonblocking = false; // change to true to use non blocking
- WiFiManager wm; // global wm instance
- WiFiManagerParameter custom_field; // global param ( for non blocking w params )
- void setup() {
- WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- delay(3000);
- Serial.println("\n Starting");
- pinMode(TRIGGER_PIN, INPUT);
- // wm.resetSettings(); // wipe settings
- if (wm_nonblocking) wm.setConfigPortalBlocking(false);
- // add a custom input field
- int customFieldLength = 40;
- // test custom html(radio)
- const char* custom_radio_str = "<br/><label for='customfieldid'>Custom Field Label</label><input type='radio' name='customfieldid' value='1' checked> One<br><input type='radio' name='customfieldid' value='2'> Two<br><input type='radio' name='customfieldid' value='3'> Three";
- new (&custom_field) WiFiManagerParameter(custom_radio_str); // custom html input
- wm.addParameter(&custom_field);
- wm.setSaveParamsCallback(saveParamCallback);
- std::vector<const char*> menu = { "wifi", "info", "param", "sep", "restart", "exit" };
- wm.setMenu(menu);
- // set dark theme
- wm.setClass("invert");
- wm.setConfigPortalTimeout(30); // auto close configportal after n seconds
- bool res;
- res = wm.autoConnect("SmartHome configurare", "password"); // password protected ap
- if (!res) {
- Serial.println("Failed to connect or hit timeout");
- // ESP.restart();
- } else {
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- }
- mqttClient.setServer(iotHubEndpoint, 8883);
- mqttClient.setCallback(callback);
- connectToAzure();
- }
- void checkButton() {
- // check for button press
- if (digitalRead(TRIGGER_PIN) == LOW) {
- // poor mans debounce/press-hold, code not ideal for production
- delay(50);
- if (digitalRead(TRIGGER_PIN) == LOW) {
- Serial.println("Button Pressed");
- // still holding button for 3000 ms, reset settings, code not ideaa for production
- delay(3000); // reset delay hold
- if (digitalRead(TRIGGER_PIN) == LOW) {
- Serial.println("Button Held");
- Serial.println("Erasing Config, restarting");
- wm.resetSettings();
- ESP.restart();
- }
- // start portal w delay
- Serial.println("Starting config portal");
- wm.setConfigPortalTimeout(120);
- if (!wm.startConfigPortal("SmartHome configurare", "password")) {
- Serial.println("failed to connect or hit timeout");
- delay(3000);
- // ESP.restart();
- } else {
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- }
- }
- }
- mqttClient.setServer(iotHubEndpoint, 8883);
- mqttClient.setCallback(callback);
- //connectToAzure();
- }
- String getParam(String name) {
- //read parameter from server, for customhmtl input
- String value;
- if (wm.server->hasArg(name)) {
- value = wm.server->arg(name);
- }
- return value;
- }
- void saveParamCallback() {
- Serial.println("[CALLBACK] saveParamCallback fired");
- Serial.println("PARAM customfieldid = " + getParam("customfieldid"));
- }
- void loop() {
- if (wm_nonblocking) wm.process(); // avoid delays() in loop when non-blocking and other long running code
- checkButton();
- // put your main code here, to run repeatedly:
- }
- void connectToAzure() {
- while (!mqttClient.connected()) {
- Serial.println("Conectare la Azure IoT Hub...");
- if (mqttClient.connect(deviceId, deviceKey, "")) {
- Serial.println("Conectat la Azure IoT Hub!");
- } else {
- Serial.println("Eroare de conectare. Se încearcă din nou în 5 secunde...");
- delay(5000);
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- // Implementați acțiunile dvs. de prelucrare a mesajelor primite
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement