Commit 2612c038 authored by Daniil Kukushkin's avatar Daniil Kukushkin

Release for geek picnic, added wifi channel setting

parent 599360a9
......@@ -101,6 +101,7 @@ void stopRetransmitTimer()
void MainController::initializeSystem()
{
bool res;
int ret = NANOPIX_STATUS_OK;
IPAddress localIP;
......@@ -108,9 +109,9 @@ void MainController::initializeSystem()
isSomeConnectedBefore = false;
noConnectSeconds = 0;
Serial.begin(UART_BAUDRATE);
Serial.begin(DEBUG_BAUDRATE);
delay(500);
wifiChannel = selectOptimalWifiChannel();
srand(ESP.getCycleCount());
......@@ -121,9 +122,13 @@ void MainController::initializeSystem()
WiFi.softAPConfig(udpSender.ip, udpSender.gateway, udpSender.subnet);
settingsManager.initAll(&udpSender);
settingsManager.loadSSID_Pass();
wifiChannel = settingsManager.getWifiChannel();
if(wifiChannel == 0)
wifiChannel = selectOptimalWifiChannel();
WiFi.softAP(settingsManager.SSID.c_str(), settingsManager.PASS.c_str(), wifiChannel);
res = WiFi.softAP(settingsManager.SSID.c_str(), settingsManager.PASS.c_str(), wifiChannel);
Serial.printf("SoftAP %s:%s on channel %d", settingsManager.SSID.c_str(), settingsManager.PASS.c_str(), wifiChannel);
Serial.println(res ? " READY" : " START FAILURE");
//WiFi.softAP("quadcopter", "testpass");
//WiFi.setOutputPower(10);
......@@ -168,6 +173,9 @@ void MainController::initializeSystem()
out:
if(ret != NANOPIX_STATUS_OK)
Serial.printf("Initialization failure, code: %d", ret);
delay(50);
Serial.end();
Serial.begin(UART_BAUDRATE);
}
......@@ -177,14 +185,12 @@ void udpOnPacketHandler(AsyncUDPPacket packet) {
uint8_t* packetData = packet.data();
int dataLength = packet.length();
if (dataLength > 0)
{
mainControllerPtr->isSomeConnectedBefore = true;
}
for(int i = 0; i < dataLength; i++) {
uint8_t res = mavlink_parse_char(MAV_RX_CHAN, packetData[i], &(mainControllerPtr->rxMessage), &(mainControllerPtr->mavStatus) );
if(res) {
mainControllerPtr->isSomeConnectedBefore = true;
uint8_t packID = mainControllerPtr->rxMessage.msgid;
uint8_t packetSeq = mainControllerPtr->rxMessage.seq;
......@@ -194,12 +200,12 @@ void udpOnPacketHandler(AsyncUDPPacket packet) {
uint8_t dif = packetSeq - prevPacketSeq;
dropCount += dif - 1;
#if DEBUG
Serial.print(packID);
/*Serial.print(packID);
Serial.print(" | ");
Serial.print(mainControllerPtr->rxMessage.seq);
Serial.print(" | ");
Serial.write(packetData, dataLength);
Serial.println();
Serial.println();*/
#endif
prevPacketSeq = packetSeq;
......@@ -272,7 +278,9 @@ void udpOnPacketHandler(AsyncUDPPacket packet) {
if(dataLength)
{
#if not DEBUG
Serial.write(packetData, dataLength);
#endif
IPAddress addr = packet.remoteIP();
if(!mainControllerPtr->udpSender.hasController(addr))
mainControllerPtr->udpSender.registerController(addr);
......@@ -582,13 +590,13 @@ void MainController::mainServiceRoutine()
sendLogMsg(dropLog, true);
#if DEBUG
Serial.print("----------------- UDP PACKETS RECEIVED: ");
/*Serial.print("----------------- UDP PACKETS RECEIVED: ");
Serial.print(mavPacketsReceived);
Serial.print(" | DROPED: ");
Serial.print(dropCount);
Serial.print(" | DROP RATE: ");
Serial.print(dropRate);
Serial.println("%");
Serial.println("%");*/
#endif
dropCount = 0;
mavPacketsReceived = 0;
......@@ -672,7 +680,7 @@ void WiFiEvent(WiFiEvent_t event) {
#endif
break;
case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
mainControllerPtr->isSomeConnectedBefore = true;
//mainControllerPtr->isSomeConnectedBefore = true;
#if DEBUG
Serial.println("[AP] new client");
printClientsStatus();
......
......@@ -40,16 +40,18 @@
#include "settings_manager.h"
#include "Color.h"
#include "UdpPacketSender.h"
#include "nanopix_config.h"
extern "C" {
#include<user_interface.h>
}
namespace NanopixCore {
#define DEBUG 0
#define DEBUG_BAUDRATE 74880
#if DEBUG
#define UART_BAUDRATE 230400
#define UART_BAUDRATE DEBUG_BAUDRATE
#else
#define UART_BAUDRATE 460800
#endif
......
......@@ -30,6 +30,8 @@ SUCH DAMAGE.
#ifndef NANOPIX_CONFIG
#define NANOPIX_CONFIG
#define DEBUG 0
#define NANOPIX_STATUS_OK 0
#define NANOPIX_STATUS_NULL_ARGUMENT 1
#define NANOPIX_STATUS_ALREADY_EXIST 2
......@@ -47,4 +49,4 @@ SUCH DAMAGE.
goto out;\
}}
#endif
\ No newline at end of file
#endif
......@@ -36,17 +36,28 @@
#include <EEPROM.h>
#include "mavlink\common\mavlink.h"
#include "UdpPacketSender.h"
#include "nanopix_config.h"
namespace NanopixCore {
#define ESP_SOFTWARE_VERSION 1.4
#define ESP_SOFTWARE_VERSION 1.6
#define ESP_SOFTWARE_VERSION_ID "esp_soft_vers"
#define ESP_WIFI_CHANNEL_ID "esp_wifi_chan"
class SettingsManager {
static const byte START_TOKEN_BYTE1 = 0xF5;
static const byte START_TOKEN_BYTE2 = 0x7E;
#define DEFAULT_PASSWORD "12345678"
#define AUTH_DATA_ADDR (2)
#define MAX_SSID_BYTES (30)
#define MAX_PASS_BYTES (30)
#define WIFI_CHANNEL_ADDR (AUTH_DATA_ADDR + MAX_SSID_BYTES + MAX_PASS_BYTES)
#define WIFI_CHANNEL_BYTES (1)
#define DEFAULT_PASSWORD "12345678"
WiFiUDP espUDP;
unsigned int localESP_UdpPort = 5610;
......@@ -55,12 +66,13 @@ class SettingsManager {
mavlink_statustext_t statusTextMsg;
uint8_t buffer[256];
UdpPacketSender * udpSender;
uint8_t wifiChannel;
public:
String SSID, PASS;
SettingsManager() {
}
void initAll(UdpPacketSender * udpSender)
......@@ -70,9 +82,17 @@ class SettingsManager {
//EEPROM.write(0,0);
//EEPROM.commit();
espUDP.begin(localESP_UdpPort);
randomSeed(ESP.getCycleCount());
if(isEepromInitialized())
{
loadSSID_Pass(SSID, PASS);
loadWifiChannel();
} else {
initEepromTokens();
resetSSID_PassToDefault();
wifiChannel = 0; // if zero - then search optimal
}
}
void parceMavPackets()
......@@ -92,10 +112,16 @@ class SettingsManager {
msgText = String(statusTextMsg.text);
if(msgText == String("ESP:ssid_pass_request"))
{
sendSSID_PASS();
sendWifiChannel();
}
if(msgText.substring(0, 12) == String("ESP:setAuth:"))
{
#if DEBUG
Serial.printf("ESP:setAuth command received\n");
#endif
int tokenPos;
SSID = substringToToken(msgText, 12, ',', tokenPos);
PASS = substringToToken(msgText, tokenPos+1, ';', tokenPos);
......@@ -103,17 +129,42 @@ class SettingsManager {
sendSSID_PASS();
saveSSID_Pass(SSID, PASS);
}
if(msgText.substring(0, 12) == String("ESP:setChan:"))
{
#if DEBUG
Serial.printf("ESP:setChan command received\n");
#endif
int newChan = 0;
newChan = atoi(msgText.substring(12, 14).c_str());
if(newChan >= 0 && newChan <= 12)
{
char buf[64];
sprintf(buf, "WiFi channel set to %d", newChan);
sendString(buf);
wifiChannel = newChan;
saveWifiChannel();
}
}
if(msgText.substring(0, 10) == String("ESP:reboot"))
{
#if DEBUG
Serial.printf("Reboot command received\n");
#endif
ESP.restart();
}
if(msgText.substring(0, 17) == String("ESP:setRandomSSID"))
{
#if DEBUG
Serial.printf("ESP:setRandomSSID command received\n");
#endif
resetSSID_PassToDefault();
sendSSID_PASS();
wifiChannel = 0;
saveWifiChannel();
}
//sendString(msgText.c_str());
#if DEBUG
sendString(msgText.c_str());
#endif
break;
case MAVLINK_MSG_ID_PARAM_REQUEST_READ:
......@@ -133,9 +184,9 @@ class SettingsManager {
bool sendParam(char* strId)
{
int paramsCount = 2;
if(strcmp(strId, ESP_SOFTWARE_VERSION_ID) == 0)
{
int paramsCount = 1;
int paramIndex = 0;
mavlink_message_t txMsg;
mavlink_msg_param_value_pack(0, 0, &txMsg, ESP_SOFTWARE_VERSION_ID, ESP_SOFTWARE_VERSION, MAVLINK_TYPE_FLOAT, paramsCount, paramIndex);
......@@ -144,6 +195,16 @@ class SettingsManager {
udpSender->sendToConnected(buffer, txSize, SenderSourceType::OwnMessage);
return true;
}
if(strcmp(strId, ESP_WIFI_CHANNEL_ID) == 0)
{
int paramIndex = 1;
mavlink_message_t txMsg;
mavlink_msg_param_value_pack(0, 0, &txMsg, ESP_WIFI_CHANNEL_ID, wifiChannel, MAVLINK_TYPE_FLOAT, paramsCount, paramIndex);
int txSize = mavlink_msg_to_send_buffer(buffer, &txMsg);
udpSender->sendToConnected(buffer, txSize, SenderSourceType::OwnMessage);
return true;
}
return false;
}
......@@ -152,6 +213,9 @@ class SettingsManager {
generateRandomSSID(SSID);
PASS = DEFAULT_PASSWORD;
saveSSID_Pass(SSID, PASS);
#if DEBUG
Serial.printf("SSID and pass random generated\n");
#endif
}
bool isDefaultPasswordNow()
......@@ -164,6 +228,13 @@ class SettingsManager {
sendString( ("ESP.auth:"+SSID + ":" + PASS).c_str());
}
void sendWifiChannel()
{
char buf[64];
sprintf(buf, "ESP.wifiChan:%d", wifiChannel);
sendString(buf);
}
String substringToToken(String src, int start, char token, int& tokenPos)
{
char buf[50];
......@@ -190,33 +261,59 @@ class SettingsManager {
{
sendString(str.c_str());
}
void loadSSID_Pass()
uint8_t getWifiChannel()
{
return wifiChannel;
}
void loadWifiChannel()
{
wifiChannel = EEPROM.read(WIFI_CHANNEL_ADDR);
if(wifiChannel > 12 || wifiChannel < 0)
wifiChannel = 0;
}
void saveWifiChannel()
{
EEPROM.write(WIFI_CHANNEL_ADDR, wifiChannel);
EEPROM.commit();
}
bool isEepromInitialized()
{
byte checkByte1 = EEPROM.read(0);
byte checkByte2 = EEPROM.read(1);
if(checkByte1 == START_TOKEN_BYTE1 && checkByte2 == START_TOKEN_BYTE2)
{
readSSID_Pass(SSID, PASS);
} else {
EEPROM.write(0, START_TOKEN_BYTE1);
EEPROM.write(1, START_TOKEN_BYTE2);
EEPROM.commit();
resetSSID_PassToDefault();
}
bool res = ((checkByte1 == START_TOKEN_BYTE1) && (checkByte2 == START_TOKEN_BYTE2));
#if DEBUG
Serial.printf("EEPROM wrong tokens: %d, %d | target: %d, %d\n", checkByte1, checkByte2, START_TOKEN_BYTE1, START_TOKEN_BYTE2);
#endif
return res;
}
void initEepromTokens()
{
#if DEBUG
Serial.printf("init EEPROM tokens\n");
#endif
EEPROM.write(0, START_TOKEN_BYTE1);
EEPROM.write(1, START_TOKEN_BYTE2);
EEPROM.commit();
}
void generateRandomSSID(String& ssid)
{
int randNumber = random(100000);
String num = String(randNumber);
ssid = "Nanopix" + num;
#if DEBUG
Serial.printf("Random SSID generated: %s\n", ssid.c_str());
#endif
}
void saveSSID_Pass(String ssid, String pass)
{
int i = 2;
int i = AUTH_DATA_ADDR;
for(int k = 0; ssid[k] != 0; k++)
EEPROM.write(i++, ssid[k]);
EEPROM.write(i++, 0);
......@@ -226,11 +323,11 @@ class SettingsManager {
EEPROM.commit();
}
void readSSID_Pass(String& ssid, String& pass)
void loadSSID_Pass(String& ssid, String& pass)
{
char ssidBuf[30], passBuf[30];
char ssidBuf[MAX_SSID_BYTES], passBuf[MAX_PASS_BYTES];
byte readByte = 1;
int addr = 2;
int addr = AUTH_DATA_ADDR;
int k = 0;
do {
ssidBuf[k] = readByte = EEPROM.read(addr);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment