diff --git a/README.md b/README.md index 92b2b788..1b05d458 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,25 @@ platformio run When you run ```platformio run``` for the first time, it will download the toolchains and all necessary libraries automatically. -### Useful commands: +### Useful commands: ESP32-WROOM + +* ```platformio run -e espwroom32 -t upload``` + +### Useful commands: ESP12e * ```platformio run``` - process/build all targets * ```platformio run -e esp12e``` - process/build just the ESP12e target (the NodeMcu v2, Adafruit HUZZAH, etc.) * ```platformio run -e esp12e -t upload``` - build and upload firmware to embedded board * ```platformio run -t clean``` - clean project (remove compiled files) +### Useful commands: ESP-01 +* ```platformio run``` - process/build all targets +* ```platformio run -e esp01_1m``` - process/build just the ESP01 target with 1MB [Amazon](https://www.amazon.com/gp/product/B01EA3UJJ4) +* ```platformio run -e esp01_1m -t upload``` - build and upload firmware to embedded board +* ```platformio run -t clean``` - clean project (remove compiled files) + + + The resulting image(s) can be found in the directory ```.pioenvs``` created during the build process. ### MavLink Submodule @@ -44,6 +56,9 @@ User level (as well as wiring) instructions can be found [here for px4](https:// * Resetting to Defaults: In case you change the parameters and get locked out of the module, all the parameters can be reset by bringing the GPIO02 pin low (Connect GPIO02 pin to GND pin). +Get the ESP-01 adapter board here on [Amazon](https://www.amazon.com/gp/product/B07Q17XJ36/); commonly called an "ESP01 programmer", this one has a little rocker switch on the side (UART side for serial TTL debugging by AT commands, PROG for firmware programming) and a yellow pin header which allows you plugin the ESP01 module without any wires. It defaults to 115200 and enumerates under Ubuntu as a ch341-uart converter. + + ### MavLink Protocol The MavESP8266 handles its own set of parameters and commands. Look at the [PARAMETERS](PARAMETERS.md) page for more information. diff --git a/platformio.ini b/platformio.ini index a4a4e87a..f7be4ea4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,15 +31,30 @@ platform = espressif8266@1.8.0 framework = arduino build_flags = !echo "-DPIO_SRC_REV="$(git rev-parse HEAD) "-DPIO_BUILD_DATE="$(date +%%Y-%%m-%%d) "-DPIO_BUILD_TIME="$(date +%%H:%%M:%%S) ${version_env.version_flags} extra_scripts = pre:platformio_prebuild.py +#platform_packages = tool-esptoolpy@1.20100.0 # Platform specific settings [env:esp12e] board = esp12e build_flags = ${env.build_flags} -Wl,-Tesp8266.flash.4m.ld +upload_port = /dev/ttyUSB1 [env:esp01_1m] board = esp01_1m +upload_port = /dev/ttyUSB0 [env:esp01] board = esp01 +upload_port = /dev/ttyUSB1 + +[env:espwroom32] +platform = espressif32 +#platform_packages = tool-esptoolpy@1.20100.0 +framework = arduino +board = esp32dev +#board = esp32thing_plus +upload_port = /dev/ttyUSB0 +#monitor_speed = 115200 +#upload_speed = 115200 +#upload_flags = -b 115200 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c2a82fbb..bb27706b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,7 +42,20 @@ #include "mavesp8266_httpd.h" #include "mavesp8266_component.h" +#ifndef ARDUINO_ESP32_DEV #include +#else +/* ESP32 */ +typedef uint8_t uint8; +typedef uint16_t uint16; +typedef uint32_t uint32; +#include +#include +#include +#include +#include +#include +#endif #define GPIO02 2 @@ -109,7 +122,11 @@ void wait_for_client() { #ifdef ENABLE_DEBUG int wcount = 0; #endif +#ifdef ARDUINO_ESP32_DEV + uint8 client_count = WiFi.softAPgetStationNum(); +#else uint8 client_count = wifi_softap_get_station_num(); +#endif while (!client_count) { #ifdef ENABLE_DEBUG Serial1.print("."); @@ -119,7 +136,11 @@ void wait_for_client() { } #endif delay(1000); +#ifdef ARDUINO_ESP32_DEV + client_count = WiFi.softAPgetStationNum(); +#else client_count = wifi_softap_get_station_num(); +#endif } DEBUG_LOG("Got %d client(s)\n", client_count); } @@ -129,7 +150,11 @@ void wait_for_client() { void reset_interrupt(){ Parameters.resetToDefaults(); Parameters.saveAllToEeprom(); +#ifdef ARDUINO_ESP32_DEV + ESP.restart(); +#else ESP.reset(); +#endif } //--------------------------------------------------------------------------------- @@ -155,7 +180,11 @@ void setup() { if(Parameters.getWifiMode() == WIFI_MODE_STA){ //-- Connect to an existing network +#ifdef ARDUINO_ESP32_DEV + WiFi.mode(WIFI_MODE_STA); +#else WiFi.mode(WIFI_STA); +#endif WiFi.config(Parameters.getWifiStaIP(), Parameters.getWifiStaGateway(), Parameters.getWifiStaSubnet(), 0U, 0U); WiFi.begin(Parameters.getWifiStaSsid(), Parameters.getWifiStaPassword()); @@ -178,20 +207,35 @@ void setup() { if(Parameters.getWifiMode() == WIFI_MODE_AP){ //-- Start AP +#ifdef ARDUINO_ESP32_DEV + WiFi.mode(WIFI_MODE_AP); /* Default to WPA2 */ +#else WiFi.mode(WIFI_AP); - WiFi.encryptionType(AUTH_WPA2_PSK); + WiFi.encryptionType(AUTH_WPA2_PSK); +#endif WiFi.softAP(Parameters.getWifiSsid(), Parameters.getWifiPassword(), Parameters.getWifiChannel()); localIP = WiFi.softAPIP(); wait_for_client(); } - - //-- Boost power to Max + //-- Boost power to Max +#ifdef ARDUINO_ESP32_DEV + { + int8_t power; + esp_wifi_get_max_tx_power(&power); + esp_wifi_set_max_tx_power(power); + } +#else WiFi.setOutputPower(20.5); - //-- MDNS +#endif char mdsnName[256]; sprintf(mdsnName, "MavEsp8266-%d",localIP[3]); +#ifdef ARDUINO_ESP32_DEV + mdns_init(); + mdns_service_add(NULL, "http", "tcp", 80, NULL, 0); +#else MDNS.begin(mdsnName); MDNS.addService("http", "tcp", 80); +#endif //-- Initialize Comm Links DEBUG_LOG("Start WiFi Bridge\n"); DEBUG_LOG("Local IP: %s\n", localIP.toString().c_str()); diff --git a/src/mavesp8266.cpp b/src/mavesp8266.cpp index 51651750..f4711d7e 100644 --- a/src/mavesp8266.cpp +++ b/src/mavesp8266.cpp @@ -105,7 +105,13 @@ MavESP8266Log::log(const char *format, ...) { va_list arg; va_start(arg, format); char temp[1024]; +#ifdef ARDUINO_ESP32_DEV + size_t len = vsnprintf(temp, 1024, format, arg); +#else size_t len = ets_vsnprintf(temp, 1024, format, arg); +#endif + + #ifdef ENABLE_DEBUG Serial1.print(temp); #endif diff --git a/src/mavesp8266.h b/src/mavesp8266.h index b29fe8ce..0508747a 100644 --- a/src/mavesp8266.h +++ b/src/mavesp8266.h @@ -38,17 +38,26 @@ #ifndef MAVESP8266_H #define MAVESP8266_H + + +#ifdef ARDUINO_ESP32_DEV +#include +#else #include +#endif + #include #include #undef F #include - extern "C" { +#ifndef ARDUINO_ESP32_DEV +extern "C" { // Espressif SDK #include "user_interface.h" } +#endif class MavESP8266Parameters; class MavESP8266Component; diff --git a/src/mavesp8266_component.cpp b/src/mavesp8266_component.cpp index 77c6001d..83cbeb59 100644 --- a/src/mavesp8266_component.cpp +++ b/src/mavesp8266_component.cpp @@ -317,5 +317,9 @@ MavESP8266Component::_wifiReboot(MavESP8266Bridge* sender) { _sendStatusMessage(sender, MAV_SEVERITY_NOTICE, "Rebooting WiFi Bridge."); delay(50); +#ifdef ARDUINO_ESP32_DEV + ESP.restart(); +#else ESP.reset(); +#endif } diff --git a/src/mavesp8266_gcs.cpp b/src/mavesp8266_gcs.cpp index 14d9194e..db49738e 100644 --- a/src/mavesp8266_gcs.cpp +++ b/src/mavesp8266_gcs.cpp @@ -39,6 +39,12 @@ #include "mavesp8266_gcs.h" #include "mavesp8266_parameters.h" #include "mavesp8266_component.h" +#ifdef ARDUINO_ESP32_DEV +#include +#include +#endif + +WiFiUDP _udp; //--------------------------------------------------------------------------------- MavESP8266GCS::MavESP8266GCS() @@ -87,7 +93,11 @@ MavESP8266GCS::readMessage() bool MavESP8266GCS::_readMessage() { +#ifdef ARDUINO_ESP32_DEV + char msgReceived = false; +#else bool msgReceived = false; +#endif int udp_count = _udp.parsePacket(); if(udp_count > 0) { @@ -114,7 +124,11 @@ MavESP8266GCS::_readMessage() if(_message.msgid == MAVLINK_MSG_ID_HEARTBEAT) { //-- We no longer need DHCP if(getWorld()->getParameters()->getWifiMode() == WIFI_MODE_AP) { +#ifdef ARDUINO_ESP32_DEV + tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); +#else wifi_softap_dhcps_stop(); +#endif } _heard_from = true; _system_id = _message.sysid; @@ -155,7 +169,11 @@ MavESP8266GCS::_readMessage() if(_heard_from && (millis() - _last_heartbeat) > HEARTBEAT_TIMEOUT) { //-- Restart DHCP and start broadcasting again if(getWorld()->getParameters()->getWifiMode() == WIFI_MODE_AP) { - wifi_softap_dhcps_start(); +#ifdef ARDUINO_ESP32_DEV + tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP); +#else + wifi_softap_dhcps_start(); +#endif } _heard_from = false; _ip[3] = 255; @@ -248,10 +266,19 @@ MavESP8266GCS::_sendRadioStatus() uint8_t rssi = 0; uint8_t lostVehicleMessages = 100; uint8_t lostGcsMessages = 100; - +#ifdef ARDUINO_ESP32_DEV + { + wifi_mode_t mode; + esp_wifi_get_mode(&mode); + if(mode == WIFI_MODE_STA) { + rssi = WiFi.RSSI(); + } + } +#else if(wifi_get_opmode() == STATION_MODE) { rssi = (uint8_t)wifi_station_get_rssi(); } +#endif if (st->packets_received > 0) { lostVehicleMessages = (st->packets_lost * 100) / st->packets_received; diff --git a/src/mavesp8266_gcs.h b/src/mavesp8266_gcs.h index 564277d2..a5058353 100644 --- a/src/mavesp8266_gcs.h +++ b/src/mavesp8266_gcs.h @@ -59,7 +59,6 @@ class MavESP8266GCS : public MavESP8266Bridge { void _checkUdpErrors (mavlink_message_t* msg); private: - WiFiUDP _udp; IPAddress _ip; uint16_t _udp_port; mavlink_message_t _message; diff --git a/src/mavesp8266_httpd.cpp b/src/mavesp8266_httpd.cpp index efcb286f..341d3f4a 100644 --- a/src/mavesp8266_httpd.cpp +++ b/src/mavesp8266_httpd.cpp @@ -34,9 +34,13 @@ * * @author Gus Grubba */ - +#ifdef ARDUINO_ESP32_DEV +#include +#include +#include +#else #include - +#endif #include "mavesp8266.h" #include "mavesp8266_httpd.h" #include "mavesp8266_parameters.h" @@ -80,7 +84,13 @@ const char* kFlashMaps[7] = { static uint32_t flash = 0; static char paramCRC[12] = {""}; +#ifdef ARDUINO_ESP32_DEV +WebServer webServer(80); +extern WiFiUDP _udp; +#else ESP8266WebServer webServer(80); +#endif + MavESP8266Update* updateCB = NULL; bool started = false; @@ -133,7 +143,11 @@ void handle_upload_status() { #ifdef DEBUG_SERIAL DEBUG_SERIAL.setDebugOutput(true); #endif +#ifdef ARDUINO_ESP32_DEV + _udp.stop(); +#else WiFiUDP::stopAll(); +#endif #ifdef DEBUG_SERIAL DEBUG_SERIAL.printf("Update: %s\n", upload.filename.c_str()); #endif @@ -332,7 +346,11 @@ static void handle_getStatus() message += ""; message += "

System Status

\n"; message += "\n"; message += "
Flash Size"; +#ifdef ARDUINO_ESP32_DEV + message += spi_flash_get_chip_size(); +#else message += ESP.getFlashChipRealSize(); +#endif message += "
Flash Available"; message += flash; @@ -373,18 +391,30 @@ void handle_getJSysInfo() if(!paramCRC[0]) { snprintf(paramCRC, sizeof(paramCRC), "%08X", getWorld()->getParameters()->paramHashCheck()); } +#if ARDUINO_ESP32_DEV + uint32_t fid = 0; /* TODO */ +#else uint32_t fid = spi_flash_get_id(); +#endif char message[512]; snprintf(message, 512, "{ " - "\"size\": \"%s\", " +#if ARDUINO_ESP32_DEV + "\"size\": \"%d\", " +#else + "\"size\": \"%s\", " +#endif "\"id\": \"0x%02lX 0x%04lX\", " "\"flashfree\": \"%u\", " "\"heapfree\": \"%u\", " "\"logsize\": \"%u\", " "\"paramcrc\": \"%s\"" " }", +#if ARDUINO_ESP32_DEV + ESP.getFlashChipSize(), +#else kFlashMaps[system_get_flash_size_map()], +#endif (long unsigned int)(fid & 0xff), (long unsigned int)((fid & 0xff00) | ((fid >> 16) & 0xff)), flash, ESP.getFreeHeap(), diff --git a/src/mavesp8266_parameters.h b/src/mavesp8266_parameters.h index e6135527..d2fa2147 100644 --- a/src/mavesp8266_parameters.h +++ b/src/mavesp8266_parameters.h @@ -38,8 +38,12 @@ #ifndef MAVESP8266_PARAMETERS_H #define MAVESP8266_PARAMETERS_H +#ifdef ARDUINO_ESP32_DEV +#include +#else #define WIFI_MODE_AP 0 #define WIFI_MODE_STA 1 +#endif //-- Constants #define DEFAULT_WIFI_MODE WIFI_MODE_AP diff --git a/src/mavesp8266_vehicle.cpp b/src/mavesp8266_vehicle.cpp index a50e8fb4..c9af5427 100644 --- a/src/mavesp8266_vehicle.cpp +++ b/src/mavesp8266_vehicle.cpp @@ -174,7 +174,7 @@ MavESP8266Vehicle::getStatus() bool MavESP8266Vehicle::_readMessage() { - bool msgReceived = false; + uint8_t msgReceived = false; while(Serial.available()) { int result = Serial.read();