c - 如何使用开关在 esp32 中从 Wifi 接入点模式切换到 STA
问题描述
我正在做一个自动驾驶汽车项目,我需要手动控制以及自动功能,因此手动控制是通过 wifi 使用“手势控制”完成的,对于自动控制,我想通过 HTTP 请求发送位置数据,但是然后我必须切换到 STA 模式,所以我想使用连接到地的 Toggle 开关和一个引脚作为输入并将其放入“if 语句”中,但所有初始设置都在 void setup() 中,所以我不知道如何继续我已经提供了我正在使用的主要代码的 AP 部分
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <TinyGPS++.h> // Tiny GPS Plus Library
#define CHANNEL 4
uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
struct __attribute__((packed)) DataStruct {
//char text[32];
int x;
int y;
unsigned long time;
};
DataStruct myData;
//**********************************************
// GPS Locations
unsigned long Distance_To_Home; // variable for storing the distance to destination
int ac =0; // GPS array counter
int wpCount = 0; // GPS waypoint counter
double Home_LATarray[50]; // variable for storing the destination Latitude - Only Programmed for 5 waypoint
double Home_LONarray[50]; // variable for storing the destination Longitude - up to 50 waypoints
int increment = 0;
#define autopilot 13
void gesturecontroll();
void getGPS();
void getCompass();
void setWaypoint();
void move();
int blueToothVal;
void setup()
{ Serial.begin(9600); // Serial 0 is for communication with the computer
S2.begin(9600); // Serial 2 is for GPS communication at 9600 baud - DO NOT MODIFY - Ublox Neo 6m
Serial.println("ESPNow/Basic/Slave Example");
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP);
// configure device AP mode
// This is the mac address of the Slave in AP Mode
esp_wifi_set_mac(ESP_IF_WIFI_STA, &mac[0]);
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
// Init ESPNow with a fallback logic
if (esp_now_init()!=0) {
Serial.println("*** ESP_Now init failed");
while(true) {};
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.
esp_now_register_recv_cb(OnDataRecv);
Serial.print("Aheloiioi");
// Extras////////////////////////////////////////////////////////////////////////////////////
pinMode(autopilot, INPUT);
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
memcpy(&myData, data, sizeof(myData));
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Recv from: "); Serial.println(macStr);
Serial.print("Last Packet Recv Data: ");
Serial.println(myData.x);
Serial.println(myData.y);
Serial.println("");
Serial.println();
//move();
Serial.println();
}
//********************************************************************************************************
// Main Loop
void loop()
{ if (autopilot == HIGH)
{
// going for manual control, BUT WHAT SHOULD I PUT HERE?
}
else
{
getGPS(); // Update the GPS location
getCompass(); // Update the CompaSerial Heading
Ping(); // Use at your own discretion, this is not fully tested
}
}
解决方案
推荐阅读
- react-native - ios 10 中的 Linking.openURL(jumpUrl) 无法打开网址:tel xxx
- sql - 如何为已解码的列添加条件
- react-native - 有没有一种方法可以使用 PHP 为每个特定用户显示 MySQL 表值,以便在反应本机应用程序中查看他们的个人资料?
- flutter - 如何使用计数器方法增加颤动中淡入淡出动画的时间?
- c++ - vector.reserve(10000000) 的缺点是因为我想保留原始内存位置?
- sql - sql 两个where子句从一个表变成两个新行
- sql - 之前和之后的 SQL 子字符串
- angular - 在Angular 10中使用ng测试进行测试时如何克服业力中的NullInjector错误
- python - 如何在熊猫数据框中输入列表作为列值?
- php - PayPal Smart Button 服务器端实现 - 捕获交易的问题 - PHP