Advertisement
pleasedontcode

Programma Luca

May 1st, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.59 KB | Source Code | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     Pleasedontcode gives you this package free of charge and you
  7.     can use it for your personal projects. We will not be held liable for
  8.     any loss or damage of any kind. For all terms and conditions,
  9.     please visit pleasedontcode.com/termsandconditions
  10.  
  11.     - Project: ProgrammaLuca
  12.     - Source Code created on: 2023-05-01 18:19:20
  13.     - Source Code generated by: Alessandro
  14.  
  15. ********* Pleasedontcode.com **********/
  16.  
  17.  
  18. /********* User code review feedback **********
  19. #### Feedback 1 ####
  20. - generete code
  21. #### Feedback 2 ####
  22. - usa "WiFiManager" per connetterti al wifi
  23. ********* User code review feedback **********/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include "Arduino.h"
  27. #include "UniversalTelegramBot.h"
  28. #include "HTTPClient.h"
  29. #include "WiFi.h"
  30. #include "WiFiClientSecure.h"
  31. #include "ESPmDNS.h"
  32. #include "WiFiUdp.h"
  33. #include "WiFiManager.h"
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. #define Button_PIN_D5   5
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. #define Led_PIN_D25 25
  40.  
  41. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool    Button_PIN_D5_rawData       = LOW; // Digital Input
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool    Led_PIN_D25_rawData     = 0;
  48.  
  49. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float   Button_PIN_D5_phyData       = 0.0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float   Led_PIN_D25_phyData     = 0.0;
  56.  
  57. /***** DEFINITION OF VARIABLES FOR MESSAGE SENDING *****/
  58. #define serverName  "..."  //indirizzo server portalevf
  59. #define Token_server "..." //token per invio selettiva su bot
  60. #define Distaccamento "test"
  61. #define MSG "Attenzione selettiva RADIO ricevuta per Distaccamento, recarsi in caserma!"
  62.  
  63. unsigned long lastSentTime = 0; // Used to store the time of the last sent message
  64.  
  65. WiFiManager wifiManager;
  66.  
  67. void setup()
  68. {
  69.     // put your setup code here, to run once:
  70.     pinMode(Button_PIN_D5,  INPUT_PULLUP);
  71.     pinMode(Led_PIN_D25,     OUTPUT);
  72.  
  73.     // Connect to WiFi using WiFiManager
  74.     wifiManager.autoConnect("AutoConnectAP");
  75. }
  76.  
  77. void loop()
  78. {
  79.     // put your main code here, to run repeatedly:
  80.     updateInputs(); // Refresh input data
  81.     updateOutputs(); // Refresh output data
  82.     pulsantePremuto(); // Check if the button has been pressed for more than 500ms
  83. }
  84.  
  85. void updateInputs()
  86. {
  87.     Button_PIN_D5_rawData   = digitalRead(Button_PIN_D5);
  88. }
  89.  
  90. void updateOutputs()
  91. {
  92.     digitalWrite(Led_PIN_D25, Led_PIN_D25_rawData);
  93. }
  94.  
  95. void InviaMessaggio()
  96. {
  97.     /***** REQUIREMENT 1 *****/
  98.     /* Quando la funzione è richiamata invia il messaggio */
  99.     /* definito MSG (è una define) usando la libreria */
  100.     /* UniversalTelegramBot */
  101.  
  102.     /***** REQUIREMENT 2 *****/
  103.     /* MSG = "Attenzione selettiva RADIO ricevuta */
  104.     /* per " Distaccamento ", recarsi in */
  105.     /* caserma!" */
  106.  
  107.     /***** REQUIREMENT 3 *****/
  108.     /* usa le seguenti liberie: "UniversalTelegramBot.h", */
  109.     /* "HTTPClient.h", "WiFi.h", "WiFiClientSecure.h", */
  110.     /* "ESPmDNS.h", "WiFiUdp.h", "WiFiManager.h" */
  111.  
  112.     /***** REQUIREMENT 4 *****/
  113.     /* invia su portale web se il messaggio è stato */
  114.     /* inviato utilizzando il seguente codice "String url */
  115.     /* = String(serverName) + */
  116.     /* String(Distaccamento)+".portalevf.it"+ */
  117.     /* String(Token_server);  HTTPClient http; */
  118.     /* http.begin(url); int httpResponseCode = */
  119.     /* http.POST(Distaccamento);" */
  120.  
  121.     /***** REQUIREMENT 5 *****/
  122.     /* Definisci #define serverName  "..."  //indirizzo */
  123.     /* server portalevf #define Token_server "..." */
  124.     /* //token per invio selettiva su bot #define */
  125.     /* Distaccamento "test" */
  126.  
  127.     // Check if at least 10 seconds have passed since the last sent message
  128.     if (millis() - lastSentTime >= 10000) {
  129.         // Send the message using the UniversalTelegramBot library
  130.         UniversalTelegramBot bot(Token_server);
  131.         bot.sendMessage(Distaccamento, MSG);
  132.        
  133.         // Send the message to the web portal
  134.         String url = String(serverName) + String(Distaccamento) + ".portalevf.it" + String(Token_server);
  135.         HTTPClient http;
  136.         http.begin(url);
  137.         int httpResponseCode = http.POST(Distaccamento);
  138.         http.end();
  139.        
  140.         // Update the time of the last sent message
  141.         lastSentTime = millis();
  142.     }
  143. }
  144.  
  145. void pulsantePremuto()
  146. {
  147.     /***** REQUIREMENT 1 *****/
  148.     /* usa input Button e definiscilo come INPUT_PULLDOWN */
  149.  
  150.     /***** REQUIREMENT 2 *****/
  151.     /* quando il pulsante Button è stato premuto per */
  152.     /* almeno 500 millisecondi allora richiama */
  153.     /* InviaMessaggio. */
  154.  
  155.     /***** REQUIREMENT 3 *****/
  156.     /* Non richiamare InviaMessaggio se non sono passato */
  157.     /* almeno 10 secondi dall'ultimo invio. */
  158.  
  159.     /* ### Use (if needed) the following other functions for this function: */
  160.     /* ### - InviaMessaggio */
  161.  
  162.     static unsigned long buttonPressStartTime = 0; // Used to store the time when the button was pressed
  163.  
  164.     if (Button_PIN_D5_rawData == LOW) {
  165.         // Button is pressed, start counting time
  166.         if (buttonPressStartTime == 0) {
  167.             buttonPressStartTime = millis();
  168.         }
  169.         // Button is still pressed, check if at least 500ms have passed
  170.         else if (millis() - buttonPressStartTime >= 500) {
  171.             // At least 500ms have passed, call the InviaMessaggio function
  172.             InviaMessaggio();
  173.             // Reset the button press start time
  174.             buttonPressStartTime = 0;
  175.         }
  176.     }
  177.     else {
  178.         // Button is not pressed, reset the button press start time
  179.         buttonPressStartTime = 0;
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement