Advertisement
Alx09

arduino

Jun 15th, 2023
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiManager.h>
  4. #include <PubSubClient.h>
  5.  
  6. // Setările pentru Azure IoT Hub
  7. const char* iotHubEndpoint = "licentaDrulaAlex.azure-devices.net";
  8. const char* deviceId = "1";
  9. const char* deviceKey = "HostName=licentaDrulaAlex.azure-devices.net;DeviceId=1;SharedAccessKey=5itKG9VL/duaqiELaaF8JWbqtHVnaQtjPtEiX4mfCBM=";
  10. WiFiClientSecure wifiClient;
  11. PubSubClient mqttClient(wifiClient);
  12.  
  13. #define TRIGGER_PIN 0
  14.  
  15.  
  16.  
  17.  
  18. bool wm_nonblocking = false;  // change to true to use non blocking
  19.  
  20. WiFiManager wm;                     // global wm instance
  21. WiFiManagerParameter custom_field;  // global param ( for non blocking w params )
  22.  
  23. void setup() {
  24.   WiFi.mode(WIFI_STA);  // explicitly set mode, esp defaults to STA+AP
  25.   Serial.begin(115200);
  26.   Serial.setDebugOutput(true);
  27.   delay(3000);
  28.   Serial.println("\n Starting");
  29.  
  30.   pinMode(TRIGGER_PIN, INPUT);
  31.  
  32.   // wm.resetSettings(); // wipe settings
  33.  
  34.   if (wm_nonblocking) wm.setConfigPortalBlocking(false);
  35.  
  36.   // add a custom input field
  37.   int customFieldLength = 40;
  38.  
  39.  
  40.   // test custom html(radio)
  41.   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";
  42.   new (&custom_field) WiFiManagerParameter(custom_radio_str);  // custom html input
  43.  
  44.   wm.addParameter(&custom_field);
  45.   wm.setSaveParamsCallback(saveParamCallback);
  46.  
  47.   std::vector<const char*> menu = { "wifi", "info", "param", "sep", "restart", "exit" };
  48.   wm.setMenu(menu);
  49.  
  50.   // set dark theme
  51.   wm.setClass("invert");
  52.  
  53.   wm.setConfigPortalTimeout(30);  // auto close configportal after n seconds
  54.  
  55.  
  56.   bool res;
  57.  
  58.   res = wm.autoConnect("SmartHome configurare", "password");  // password protected ap
  59.  
  60.   if (!res) {
  61.     Serial.println("Failed to connect or hit timeout");
  62.     // ESP.restart();
  63.   } else {
  64.     //if you get here you have connected to the WiFi
  65.    Serial.println("connected...yeey :)");
  66.   }
  67.    
  68.     mqttClient.setServer(iotHubEndpoint, 8883);
  69.     mqttClient.setCallback(callback);
  70.  
  71.     connectToAzure();
  72. }
  73.  
  74. void checkButton() {
  75.   // check for button press
  76.   if (digitalRead(TRIGGER_PIN) == LOW) {
  77.     // poor mans debounce/press-hold, code not ideal for production
  78.     delay(50);
  79.     if (digitalRead(TRIGGER_PIN) == LOW) {
  80.       Serial.println("Button Pressed");
  81.       // still holding button for 3000 ms, reset settings, code not ideaa for production
  82.       delay(3000);  // reset delay hold
  83.       if (digitalRead(TRIGGER_PIN) == LOW) {
  84.         Serial.println("Button Held");
  85.         Serial.println("Erasing Config, restarting");
  86.         wm.resetSettings();
  87.         ESP.restart();
  88.       }
  89.  
  90.       // start portal w delay
  91.       Serial.println("Starting config portal");
  92.       wm.setConfigPortalTimeout(120);
  93.  
  94.       if (!wm.startConfigPortal("SmartHome configurare", "password")) {
  95.         Serial.println("failed to connect or hit timeout");
  96.         delay(3000);
  97.         // ESP.restart();
  98.       } else {
  99.         //if you get here you have connected to the WiFi
  100.         Serial.println("connected...yeey :)");
  101.       }
  102.     }
  103.   }
  104.   mqttClient.setServer(iotHubEndpoint, 8883);
  105.   mqttClient.setCallback(callback);
  106.  
  107.   //connectToAzure();
  108. }
  109.  
  110.  
  111. String getParam(String name) {
  112.   //read parameter from server, for customhmtl input
  113.   String value;
  114.   if (wm.server->hasArg(name)) {
  115.     value = wm.server->arg(name);
  116.   }
  117.   return value;
  118. }
  119.  
  120. void saveParamCallback() {
  121.   Serial.println("[CALLBACK] saveParamCallback fired");
  122.   Serial.println("PARAM customfieldid = " + getParam("customfieldid"));
  123. }
  124.  
  125. void loop() {
  126.   if (wm_nonblocking) wm.process();  // avoid delays() in loop when non-blocking and other long running code
  127.   checkButton();
  128.   // put your main code here, to run repeatedly:
  129.  
  130. }
  131.  
  132. void connectToAzure() {
  133.   while (!mqttClient.connected()) {
  134.     Serial.println("Conectare la Azure IoT Hub...");
  135.     if (mqttClient.connect(deviceId, deviceKey, "")) {
  136.       Serial.println("Conectat la Azure IoT Hub!");
  137.     } else {
  138.       Serial.println("Eroare de conectare. Se încearcă din nou în 5 secunde...");
  139.       delay(5000);
  140.     }
  141.   }
  142. }
  143. void callback(char* topic, byte* payload, unsigned int length) {
  144.   // Implementați acțiunile dvs. de prelucrare a mesajelor primite
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement