I have a device that transmits data to a phone or computer either via wifi or usb. Accordingly, different operating modes are enabled. How do I determine during setup and loop whether the power is coming from the poverbank or from the phone/computer?
Power source can not be determined.
if (Serial)
indicates a USB serial connection to a computer.
Thanks, but how can I determine what is connected to the phone?
The if (Serial)
command will mean "computer/Serial USB" or "not computer/Serial USB."
I do not know how you are using the phone... but it would be something similar.
What you are saying is the WiFi and USB are the same. If not you need to read one or the other to get the data. Which one shows it has the data tells where it is coming from.
during setup, I set the course of the entire program. In terms of beauty, it should work like this: if the device is connected to the poverbank, then the device transmits data via wifi, if connected to a computer or phone, then transmits data via usb. And I have difficulties with the fact that I cannot determine where the device is connected.
if(Serial){}
- It is also true for poverbank
now I determine this if I press the button before inserting it into the powerbank. But this is a bad option for further work.
if (Power->IsBatteryPwr)
{
//Creating an instance of a class for working with a Wi-Fi connection
SerialInterface = new TWiFiTCPSerial();
//Setting the connection target
//SerialInterface->setNetworkConfig("Keenetic-7163", "rk5B4Gj2");
//SerialInterface->saveNetworkConfig();
//Clearing the connection target
//SerialInterface->clearNetworkConfig();
//Trying to establish a connection
bool IsWiFiStarted = SerialInterface->begin(0);
if (!IsWiFiStarted)
{
//Shutting down the ESP32 with an error display
BlinkErrorAndShutdown(10);
delay(1000);
ESP.restart();
}
}
else
{
SerialInterface = new TUSBSerial(); //USB-OTG
/* Opening the serial port for data transfer */
SerialInterface->begin(12000000);
//Serial Port Settings
#if DEBUG
SerialInterface->setDebugOutput(true);
#endif
//setting the timeout for the serial (so that reading commands does not stop the loop)
SerialInterface->setTimeout(1);
}
You will probably have to write some handshaking code to make that determination.
You say pushing a button when connecting it will determine what it is but not later, how does that work?
This code in Setup. Further logic comes from the interface, and the interface is defined in Setup
Post an annotated schematic showing how you wired this. Show all connections, power, ground, and power sources. Post all of the code, this snippet shown does not help.
What value is returned for you from if (Serial)
?
A common practice is to use while (!Serial)
to only proceed when a Serial device is active (USB is connected). You should be able to use while (!Serial)
to start the "phone" routine as opposed to the "computer" routine.
Returns "true" if I connect to a power bank (logical), but I need to somehow understand that the device is connected to a power bank, and not to a phone/computer
What does if(Serial)
return when connected to a computer?
also "true"
Strange that a powerbank has a serial port. And what isnreturned when you disconnect the USB cable?
if we disconnect from the usb cable, the device turns off, it is powered by the phone/computer or power bank
The basic premise is --
If (there is Serial terminal available then it's USB-powered)
else
it is powerbanked.
The Serial facility isn't always available super-immediately, so you have to give that time - polling that, after which either
it's there or
you give up waiting → inferring that since Serial is unavailable it must be connected to a powerbank.
The "classic" Arduino (avr-based Uno R3 / Nano) have a separate controller talking USB. The atmega328 does not notice the difference and has a dummy bool operator for Serial, always responding true
, no matter if the power comes via real USB, a 5V wallwart with USB A jack or from a 7-12V Vin power supply.
Only Arduinos with a controller directly handling USB are able to distinguish if there's really a Serial connection.
Can't you organize that in a way that "phone or computer" request data, and your arduino reacts on such a request, wherever it comes from?
that's the specificity extant.