Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,30 @@ platform = [email protected]
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 = [email protected]

# 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 = [email protected]
framework = arduino
board = esp32dev
#board = esp32thing_plus
upload_port = /dev/ttyUSB0
#monitor_speed = 115200
#upload_speed = 115200
#upload_flags = -b 115200
52 changes: 48 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@
#include "mavesp8266_httpd.h"
#include "mavesp8266_component.h"

#ifndef ARDUINO_ESP32_DEV
#include <ESP8266mDNS.h>
#else
/* ESP32 */
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
#include <esp_event.h>
#include <esp_event_loop.h>
#include <esp_wifi.h>
#include <esp_wifi_types.h>
#include <esp_err.h>
#include <mdns.h>
#endif

#define GPIO02 2

Expand Down Expand Up @@ -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(".");
Expand All @@ -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);
}
Expand All @@ -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
}

//---------------------------------------------------------------------------------
Expand All @@ -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());

Expand All @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions src/mavesp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/mavesp8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@
#ifndef MAVESP8266_H
#define MAVESP8266_H



#ifdef ARDUINO_ESP32_DEV
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <WiFiClient.h>
#include <WiFiUdp.h>

#undef F
#include <ardupilotmega/mavlink.h>

extern "C" {
#ifndef ARDUINO_ESP32_DEV
extern "C" {
// Espressif SDK
#include "user_interface.h"
}
#endif

class MavESP8266Parameters;
class MavESP8266Component;
Expand Down
4 changes: 4 additions & 0 deletions src/mavesp8266_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 29 additions & 2 deletions src/mavesp8266_gcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
#include "mavesp8266_gcs.h"
#include "mavesp8266_parameters.h"
#include "mavesp8266_component.h"
#ifdef ARDUINO_ESP32_DEV
#include <esp_wifi.h>
#include <tcpip_adapter.h>
#endif

WiFiUDP _udp;

//---------------------------------------------------------------------------------
MavESP8266GCS::MavESP8266GCS()
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/mavesp8266_gcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 33 additions & 3 deletions src/mavesp8266_httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@
*
* @author Gus Grubba <[email protected]>
*/

#ifdef ARDUINO_ESP32_DEV
#include <WebServer.h>
#include <Update.h>
#include <esp_spi_flash.h>
#else
#include <ESP8266WebServer.h>

#endif
#include "mavesp8266.h"
#include "mavesp8266_httpd.h"
#include "mavesp8266_parameters.h"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -332,7 +346,11 @@ static void handle_getStatus()
message += "</td></tr></table>";
message += "<p>System Status</p><table>\n";
message += "<tr><td width=\"240\">Flash Size</td><td>";
#ifdef ARDUINO_ESP32_DEV
message += spi_flash_get_chip_size();
#else
message += ESP.getFlashChipRealSize();
#endif
message += "</td></tr>\n";
message += "<tr><td width=\"240\">Flash Available</td><td>";
message += flash;
Expand Down Expand Up @@ -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(),
Expand Down
Loading