/* 11/12/2021 * NodeMCU configurato come Access Point e Web Server per gestione client Slider per lampada LED esterna Gropparello * Variazione della tensione di alimentazione della lampada impostata tramite potenziometro digitale MCP4132 collegato * ad un dc/dc converter XL6009 (commenti e collegamenti vedi sketch */ #include // Include the Wi-Fi library #include #include #include #define MCP_PIN 15 // Chip Select MCP4132: GPIO15 (NodeMCU D8) int potVal = B00000001; // valore iniziale Rmin = 80R const char *ssid = "****************"; // The name of the Wi-Fi network that will be created const char *password = "****************"; // The password required to connect to it, leave blank for an open network const int output = 4; String sliderValue = "0"; const char* PARAM_INPUT = "value"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); const char index_html[] PROGMEM = R"rawliteral( Outdoor Garage Lamp

Outdoor Garage Lamp

%SLIDERVALUE%

)rawliteral"; // Replaces placeholder with button section in your web page String processor(const String& var){ //Serial.println(var); if (var == "SLIDERVALUE"){ return sliderValue; } return String(); } void setup() { Serial.begin(115200); delay(10); Serial.println('\n'); pinMode(MCP_PIN, OUTPUT); digitalWrite(MCP_PIN,HIGH); analogWrite(output, sliderValue.toInt()); SPI.begin(); SPI.setFrequency(1000000); //fmax 4132 = 10MHz, 1MHz enough digitalWrite(MCP_PIN, LOW); SPI.transfer(B00000000); // Comando Write SPI.transfer(potVal); // digitalWrite(MCP_PIN, HIGH); Serial.print ("potVal: "); Serial.println (potVal); delay(50); WiFi.softAP(ssid, password); // Start the access point Serial.print("Access Point \""); Serial.print(ssid); Serial.println("\" started"); Serial.print("IP address:\t"); Serial.println(WiFi.softAPIP()); // Send the IP address of the ESP8266 to the computer // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Send a GET request to /slider?value= server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputMessage; // GET input1 value on /slider?value= if (request->hasParam(PARAM_INPUT)) { inputMessage = request->getParam(PARAM_INPUT)->value(); sliderValue = inputMessage; // analogWrite(output, sliderValue.toInt()); potVal = (sliderValue.toInt()); digitalWrite(MCP_PIN, LOW); SPI.transfer(B00000000); // Comando Write SPI.transfer(potVal); // digitalWrite(MCP_PIN, HIGH); Serial.print ("potVal: "); Serial.println (potVal); } else { inputMessage = "No message sent"; } Serial.println(inputMessage); request->send(200, "text/plain", "OK"); }); // Start server server.begin(); } void loop() { }