Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <ESP8266WebServer.h>
  2. #include <Ticker.h>
  3.  
  4. #define HTTP_WEB_PORT 80
  5. #define WIFI_RETRY_DELAY 500
  6. #define MAX_WIFI_INIT_RETRY 50
  7.  
  8.  
  9. const char* wifi_ssid = "SMHWN";
  10. const char* wifi_passwd = "mNyh7XS2j8Gm3KwnXsdJ79jxq";
  11.  
  12. String stato = "FERMA";
  13. unsigned long attesa = 10000L;
  14. unsigned long posizione = 0;
  15.  
  16. Ticker tapparella_manager;
  17.  
  18. IPAddress local_IP(192,168,10,10);
  19. IPAddress gateway(192,168,10,10);
  20. IPAddress subnet(255,255,255,0);
  21.  
  22. ESP8266WebServer http_server(HTTP_WEB_PORT);
  23.  
  24. void setup(void) {
  25. int retries = 0;
  26. Serial.begin(115200);
  27. tapparella_manager.attach(0.5, gestisci_tapparella);
  28.  
  29.  
  30. Serial.println("Configurazione RoboCar WiFi AP..........");
  31. WiFi.mode(WIFI_STA);
  32. WiFi.begin(wifi_ssid, wifi_passwd);
  33.  
  34. while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) {
  35. retries++;
  36. delay(WIFI_RETRY_DELAY);
  37. Serial.print("#");
  38. }
  39.  
  40. Serial.print("RoboCar WiFi AP IP Address = ");
  41. Serial.println(WiFi.localIP());
  42. delay(3000);
  43.  
  44. config_server_routing();
  45.  
  46. http_server.begin();
  47. Serial.println("RoboCar HTTP Server Started");
  48. }
  49.  
  50. void config_server_routing() {
  51. http_server.on("/", HTTP_GET, []() { http_server.send(200, "text/html", "Benvenuto sul ESP8266 RoboCar Web Server"); });
  52. http_server.on("/tapparella", HTTP_GET, muovi_tapparella);
  53. }
  54.  
  55. void gestisci_tapparella() {
  56. Serial.println("gestisci tapparella");
  57. if (stato == "ALZA") {
  58. posizione += 500;
  59. Serial.println(posizione);
  60. }
  61. else if (stato == "ABBASSA") {
  62. posizione -= 500;
  63. Serial.println(posizione);
  64. }
  65. else if (stato == "FERMA")
  66. Serial.println(posizione);
  67. }
  68.  
  69. void muovi_tapparella() {
  70. String comando = String(http_server.arg(0));
  71. Serial.print("Ricevuto comando = ");
  72. Serial.println(comando);
  73.  
  74. http_server.send(200, "text/html", "tapparella - comando ricevuto");
  75.  
  76. if (comando == "ALZA")
  77. alza_tapparella();
  78. else if (comando == "ABBASSA")
  79. abbassa_tapparella();
  80. else if (comando == "FERMA")
  81. ferma_tapparella();
  82. }
  83.  
  84.  
  85. void alza_tapparella() {
  86. Serial.println("entro in alza_tapparella");
  87. stato = "ALZA";
  88. Serial.println(stato);
  89. Serial.println("esco da alza_tapparella");
  90. }
  91.  
  92. void abbassa_tapparella() {
  93. Serial.println("entro in abbassa_tapparella");
  94. stato = "ABBASSA";
  95. Serial.println(stato);
  96. Serial.println("esco da abbassa_tapparella");
  97. }
  98.  
  99. void ferma_tapparella() {
  100. Serial.println("entro in ferma_tapparella");
  101. stato = "FERMA";
  102. Serial.println(stato);
  103. Serial.println("esco da ferma_tapparella");
  104. }
  105.  
  106.  
  107. void loop(void) {
  108. http_server.handleClient();
  109. delay(1);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement