diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 83fb7578..afaf3102 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -158,6 +158,11 @@ #include "hasp_ethernet.h" #endif +#if HASP_USE_WIFI > 0 && defined(STM32F4xx) +#include "WiFiSpi.h" +static WiFiSpiClass WiFi; +#endif + #ifndef FPSTR #define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) #endif diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index 10ff8bfe..e928c8fc 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -51,6 +51,12 @@ char httpPassword[32] = ""; EthernetWebServer webServer(80); #endif +#if defined(STM32F4xx) && HASP_USE_WIFI>0 +#include +// #include +EthernetWebServer webServer(80); +#endif + #if defined(ARDUINO_ARCH_ESP8266) #include ESP8266WebServer webServer(80); @@ -479,7 +485,18 @@ void webHandleInfo() } else { httpMessage += F("Very Bad)"); } - +#if defined(STM32F4xx) + byte mac[6]; + WiFi.macAddress(mac); + char macAddress[16]; + sprintf_P(macAddress, PSTR("%02x%02x%02x"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + httpMessage += F("
IP Address: "); + httpMessage += String(WiFi.localIP()); + httpMessage += F("
Gateway: "); + httpMessage += String(WiFi.gatewayIP()); + httpMessage += F("
MAC Address: "); + httpMessage += String(macAddress); +#else httpMessage += F("
IP Address: "); httpMessage += String(WiFi.localIP().toString()); httpMessage += F("
Gateway: "); @@ -489,6 +506,7 @@ void webHandleInfo() httpMessage += F("
MAC Address: "); httpMessage += String(WiFi.macAddress()); #endif +#endif /* Mqtt Stats */ #if HASP_USE_MQTT > 0 @@ -901,7 +919,7 @@ void webHandleConfig() } // Reboot after saving wifi config in AP mode -#if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() != WIFI_STA) { httpHandleReboot(); } @@ -1105,7 +1123,7 @@ void webHandleWifiConfig() } httpMessage += F("'>

"); -#if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() == WIFI_STA) { httpMessage += PSTR("

"); @@ -1114,7 +1132,11 @@ void webHandleWifiConfig() webSendPage(httpGetNodename(), httpMessage.length(), false); webServer.sendContent(httpMessage); +#if defined(STM32F4xx) + httpMessage = ""; +#else httpMessage.clear(); +#endif webSendFooter(); } #endif @@ -1572,8 +1594,14 @@ void webStart() webServer.begin(); webServerStarted = true; #if HASP_USE_WIFI > 0 +#if defined(STM32F4xx) + IPAddress ip; + ip = WiFi.localIP(); + Log.notice(F("HTTP: Server started @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); +#else Log.notice(F("HTTP: Server started @ http://%s"), (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); +#endif #else IPAddress ip; ip = Ethernet.localIP(); @@ -1594,10 +1622,12 @@ void httpSetup() // httpSetConfig(settings); #if HASP_USE_WIFI > 0 +#if !defined(STM32F4xx) if(WiFi.getMode() != WIFI_STA) { Log.notice(F("HTTP: Wifi access point")); webServer.on(F("/"), webHandleWifiConfig); } else { +#endif #endif webServer.on(F("/page/"), []() { @@ -1679,7 +1709,9 @@ void httpSetup() webServer.on(F("/reboot"), httpHandleReboot); webServer.onNotFound(httpHandleNotFound); #if HASP_USE_WIFI > 0 +#if !defined(STM32F4xx) } +#endif #endif // Shared pages @@ -1704,7 +1736,7 @@ void httpReconnect() if(webServerStarted) { webStop(); } else -#if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) #endif { diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index 1f7506f0..5844c796 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -18,7 +18,10 @@ WiFiClient mqttNetworkClient; #include WiFiClient mqttNetworkClient; #else - +#if defined(STM32F4xx) && HASP_USE_WIFI>0 +// #include +WiFiSpiClient mqttNetworkClient; +#else #if defined(W5500_MOSI) && defined(W5500_MISO) && defined(W5500_SCLK) #define W5500_LAN #include @@ -28,6 +31,7 @@ WiFiClient mqttNetworkClient; EthernetClient mqttNetworkClient; #endif +#endif #include "hasp_hal.h" #include "hasp_tft.h" @@ -190,9 +194,18 @@ void mqtt_send_statusupdate() snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"), haspGetVersion().c_str(), long(millis() / 1000)); strcat(buffer, data); -#if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI>0 +#if defined(STM32F4xx) + IPAddress ip; + ip = WiFi.localIP(); + char espIp[16]; + memset(espIp, 0 ,sizeof(espIp)); + snprintf_P(buffer, sizeof(buffer), PSTR("\"ssid\":\"%s\",\"rssi\":%i,\"ip\":\"%d.%d.%d.%d\","), WiFi.SSID(), + WiFi.RSSI(), ip[0], ip[1], ip[2], ip[3]); +#else snprintf_P(buffer, sizeof(buffer), PSTR("\"ssid\":\"%s\",\"rssi\":%i,\"ip\":\"%s\","), WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.localIP().toString().c_str()); +#endif strcat(data, buffer); #endif snprintf_P(buffer, sizeof(buffer), PSTR("\"heapFree\":%u,\"heapFrag\":%u,\"espCore\":\"%s\","), diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index 950db31d..00df9802 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -19,6 +19,12 @@ static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler; +#elif defined(STM32F4xx) +// #include +// #include "WiFiSpi.h" +// extern WiFiSpiClass WiFi; +SPIClass spi2(ESPSPI_MOSI, ESPSPI_MISO, ESPSPI_SCLK); // SPI port where esp is connected + #endif //#include "DNSserver.h" @@ -43,7 +49,13 @@ uint8_t wifiReconnectCounter = 0; void wifiConnected(IPAddress ipaddress) { +#if defined(STM32F4xx) + IPAddress ip; + ip = WiFi.localIP(); + Log.notice(F("WIFI: Received IP address %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); +#else Log.notice(F("WIFI: Received IP address %s"), ipaddress.toString().c_str()); +#endif Log.verbose(F("WIFI: Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no")); haspProgressVal(255); @@ -125,7 +137,9 @@ bool wifiShowAP(char * ssid, char * pass) WiFi.macAddress(mac); sprintf_P(ssid, PSTR("HASP-%02x%02x%02x"), mac[3], mac[4], mac[5]); sprintf_P(pass, PSTR("haspadmin")); - +#if defined(STM32F4xx) + Log.warning(F("WIFI: We should setup Temporary Access Point %s password: %s"), ssid, pass); +#else WiFi.softAP(ssid, pass); /* Setup the DNS server redirecting all the domains to the apIP */ @@ -135,12 +149,47 @@ bool wifiShowAP(char * ssid, char * pass) Log.warning(F("WIFI: Temporary Access Point %s password: %s"), ssid, pass); Log.warning(F("WIFI: AP IP address : %s"), WiFi.softAPIP().toString().c_str()); // httpReconnect();} - +#endif return true; } void wifiSetup() { +#if defined(STM32F4xx) + // Temp ESP reset function + pinMode(ESPSPI_RST, OUTPUT); + digitalWrite(ESPSPI_RST, 0); + delay(150); + digitalWrite(ESPSPI_RST, 1); + delay(150); + // + + // Initialize the WifiSpi library + WiFiSpi.init(ESPSPI_CS, 8000000, &spi2); + + // check for the presence of the shield: + if (WiFiSpi.status() == WL_NO_SHIELD) { + Log.notice(F("WIFI: WiFi shield not present")); + // don't continue: + while (true); + } + + if (!WiFiSpi.checkProtocolVersion()) { + Log.notice(F("WIFI: Protocol version mismatch. Please upgrade the firmware")); + // don't continue: + while (true); + } + + // attempt to connect to Wifi network + int status = WL_IDLE_STATUS; // the Wifi radio's status + + // while (status != WL_CONNECTED) { + Log.notice(F("WIFI: Connecting to : %s"), wifiSsid); + // Connect to WPA/WPA2 network + status = WiFi.begin(wifiSsid, wifiPassword); + // } + +#else if(wifiShowAP()) { WiFi.mode(WIFI_AP_STA); } else { @@ -159,13 +208,18 @@ void wifiSetup() WiFi.begin(wifiSsid, wifiPassword); Log.notice(F("WIFI: Connecting to : %s"), wifiSsid); } +#endif } bool wifiEvery5Seconds() { +#if defined(STM32F4xx) + if(WiFi.status() == WL_CONNECTED) { +#else if(WiFi.getMode() != WIFI_STA) { return false; } else if(WiFi.status() == WL_CONNECTED) { +#endif return true; } else { wifiReconnectCounter++; @@ -224,14 +278,27 @@ bool wifiTestConnection() { uint8_t attempt = 0; WiFi.begin(wifiSsid, wifiPassword); +#if defined(STM32F4xx) + IPAddress ip; + ip = WiFi.localIP(); + char espIp[16]; + memset(espIp, 0 ,sizeof(espIp)); + snprintf_P(espIp, sizeof(espIp), PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); + while(attempt < 10 && (WiFi.status() != WL_CONNECTED || String(espIp) == F("0.0.0.0"))) { +#else while(attempt < 10 && (WiFi.status() != WL_CONNECTED || WiFi.localIP().toString() == F("0.0.0.0"))) { +#endif attempt++; Log.verbose(F("WIFI: Trying to connect to %s... %u"), wifiSsid, attempt); delay(1000); } +#if defined(STM32F4xx) + Log.verbose(F("WIFI: Received IP addres %s"), espIp); + if((WiFi.status() == WL_CONNECTED && String(espIp) != F("0.0.0.0"))) return true; +#else Log.verbose(F("WIFI: Received IP addres %s"), WiFi.localIP().toString().c_str()); if((WiFi.status() == WL_CONNECTED && WiFi.localIP().toString() != F("0.0.0.0"))) return true; - +#endif WiFi.disconnect(); return false; } @@ -240,7 +307,9 @@ void wifiStop() { wifiReconnectCounter = 0; // Prevent endless loop in wifiDisconnected WiFi.disconnect(); +#if !defined(STM32F4xx) WiFi.mode(WIFI_OFF); +#endif Log.warning(F("WIFI: Stopped")); } diff --git a/user_setups/stm32f4xx/stm32f407-black_ili9341_parallel.ini b/user_setups/stm32f4xx/stm32f407-black_ili9341_parallel.ini new file mode 100644 index 00000000..3519c4ef --- /dev/null +++ b/user_setups/stm32f4xx/stm32f407-black_ili9341_parallel.ini @@ -0,0 +1,60 @@ +;***************************************************; +; Generic ESP32 build with ; +; - ESP32dev board ; +; - ili9488 TFT ; +; - GT911 touch controller ; +;***************************************************; + +[env:stm32f4xx-parallel] +platform = ststm32 +board = diymore_f407vgt +board_build.mcu = stm32f407vet6 +upload_protocol = dfu +monitor_port = COM7 ; To change the port, use platform_override.ini +build_flags = + ${env.build_flags} + ${flags.stm32_flags} + -I include/stm32f4 +; -- TFT_eSPI build options ------------------------ + -D ILI9341_DRIVER=1 + -D TFT_WIDTH=240 + -D TFT_HEIGHT=320 + -D TFT_PARALLEL_8_BIT=1 + -D TFT_BCKL=PB1 ;None, configurable via web UI (e.g. 2 for D4) + -D TFT_CS=PB12 ; Chip select control pin + -D TFT_DC=PD13 ; =RS; Data Command control pin - must use a pin in the range 0-31 + -D TFT_RST=-1 ; Reset pin + -D TFT_WR=PD5 ; Write strobe control pin - must use a pin in the range 0-31 + -D TFT_RD=PD4 + -D TFT_D0=PD14 ; Must use pins in the range 0-31 for the data bus + -D TFT_D1=PD15 ; so a single register write sets/clears all bits + -D TFT_D2=PD0 + -D TFT_D3=PD1 + -D TFT_D4=PE7 + -D TFT_D5=PE8 + -D TFT_D6=PE9 + -D TFT_D7=PE10 + ; -D TFT_SPI2 + ; -D TFT_SCLK=PB13 + ; -D TFT_MISO=PB14 + ; -D TFT_MOSI=PB15 + ; -D TOUCH_CS=PB12 + ; -D TOUCH_IRQ=PC5 ; use 34-39 as these are input only pins + ; -D TOUCH_RST=-1 ; not used, connected to 3.3V + -D HASP_OUTPUT_PIN=PA6 ; User LED D2 on MCUDev Black board + -D HASP_INPUT_PIN=PE4 ; User Button K0 on MCUDev Black board + -D HASP_USE_HTTP=0 + -D HASP_USE_MQTT=0 + +; -- Debugging options ----------------------------- +; -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG + +; -- Library options ------------------------------- +lib_deps = + ${env.lib_deps} + Ticker@^3.1.5 + +lib_ignore = + lv_lib_zifont + +src_filter = +<*> -<.git/> -<.svn/> - - - - - + diff --git a/user_setups/stm32f4xx/stm32f407-black_ili9341_wifi.ini b/user_setups/stm32f4xx/stm32f407-black_ili9341_wifi.ini new file mode 100644 index 00000000..f4161a51 --- /dev/null +++ b/user_setups/stm32f4xx/stm32f407-black_ili9341_wifi.ini @@ -0,0 +1,70 @@ +;***************************************************; +; STM32F4 build with ; +; - DIY_more board ; +; - ili9341 TFT ; +; - xpt2046 touch controller ; +;***************************************************; + +[env:black_f407vg_wifi] +platform = ststm32 +board = diymore_f407vgt +board_build.mcu = stm32f407vgt6 +upload_protocol = dfu +monitor_port = COM7 ; To change the port, use platform_override.ini +build_flags = + ${env.build_flags} + ${flags.stm32_flags} + -I include/stm32f4 +; -- TFT_eSPI build options ------------------------ + ${lcd.lolin24} + ;-D TFT_MISO=PB4 ;Default + ;-D TFT_MOSI=PB5 ;Default + ;-D TFT_SCLK=PB3 ;Default + -D USE_DMA_TO_TFT=1 + -D TFT_CS=PE13 ;D8 + -D TFT_DC=PE14 ;D3 + -D TFT_BCKL=PA15 ;None, configurable via web UI (e.g. 2 for D4) + -D TOUCH_CS=PA6 ;NC + -D TFT_RST=PD13 ;D4 + -D HASP_OUTPUT_PIN=PE0 ; User LED D2 on DevEBox board + -D HASP_INPUT_PIN=PD15 ; User Button K1 on DevEBox board + -D STM32_SERIAL1 ; Set this option to use Serial1 as default sersial port, leave out if using Serial2 + ;-D HAL_ETH_MODULE_ENABLED=1 ; enable ethernet support + ;-D LAN8742A_PHY_ADDRESS=0x01U ; set LAN8720 PHY address + -D HASP_USE_WIFI=1 + -D HASP_USE_MQTT=1 + -D HASP_USE_HTTP=1 + -D HASP_USE_MDNS=0 + -D HASP_USE_SYSLOG=0 + -D HASP_USE_TELNET=0 + -D HASP_USE_SPIFFS=0 + -D HASP_USE_EEPROM=1 + -D HASP_USE_GPIO=0 + -D HASP_USE_TASMOTA_SLAVE=0 + -D HASP_USE_ETHERNET=0 + -D ESPSPI_MOSI=PB15 ;SPI2 MOSI + -D ESPSPI_MISO=PB14 ;SPI2 MISO + -D ESPSPI_SCLK=PB13 ;SPI2 SCLK + -D ESPSPI_CS=PB6 ;SPI2 CS + -D ESPSPI_RST=PD1 ;SPI2 CS + +lib_deps = + ${env.lib_deps} + Ticker@^3.1.5 + ;Ethernet + ; STM32duino LwIP@^2.1.2 + ; STM32duino STM32Ethernet@^1.0.5 + ; https://github.com/stm32duino/LwIP.git + ; https://github.com/netwizeBE/Ethernet3.git + ; https://github.com/khoih-prog/EthernetWebServer_STM32 + https://github.com/arovak/EthernetWebServer_STM32.git + https://github.com/JiriBilek/WiFiSpi.git + +lib_ignore = + GxTFT + XPT2046_Touchscreen + lv_lib_zifont + +src_filter = +<*> -<.git/> -<.svn/> - - - - - + + +;***************************************************