[disregard] Advice on device communication and turning on and off

To give some immediate context on what I’m doing, see this circuit diagram below, I’m using the right side.

The whole point is to preserve power. I realize the ESP side could just use a super-cap or a small LDO/not use a giant step down regulator when it will be constantly on.

Anyway the Seeeduino is turned on/off by the ESP by a digital high output which enables the ground to flow on the parent MOSFET. This generally works but I’m having problems syncing the two devices, so the Seeeduino will get into this “brown out” state I call it. Where it’s not powered up correctly, the green/amber lights are both on together(should just be green).

I have tried a few ways to sync them, the seemingly more reliable way is to communicate between the two (Seeeduino says “I’m booted” to ESP)… but the data between the two is not reliable… I’ll send for example “sb” and I’ll get back “sbx” or “sby” on the ESP side.

I’ve attached two images of my code that I tried(some methods omitted but not needed). The 9999 to string I know is dumb but it was out of desperation, originally I was sending something like “seeeduino booted” but seemed to be too many characters to reliably transmit/receive in one piece/correctly so I went with just “sb” and then tried a “number”.

Anyway thanks for any thoughts, this project did become overblown it was not my intent to do this originally but it’s good practice for trying to make something that lasts at least a month/use all the pins on the board imo. It generally works but the serial com is what is messing it up, possibly the on/off/on/off booting of the Seeeduino.

No communication attempt just wait for serial data to be available

ESP-code

void loop()
{
  digitalWrite(LED, HIGH); // turn Seeeduino on
  if (Serial.available() > 0)

  {
    String serialMsg = Serial.readString();

    if (serialMsg.length() > 0)
    {
      digitalWrite(LED, LOW); // turn Seeeduino off have data
      delay(5000); // wait for Seeeduino to completely power off
      txStrByWiFi(serialMsg); // this works fine
      clearSerialBuffer();
      sleepDelay = 30000; // long sleep
    }
  }

  delay(sleepDelay); // this is initially 1000
}

Seeeduino code

void setup()
{
  Serial1.begin(115200);
  analogReadResolution(10);
  pinMode(0, OUTPUT);
}

void loop()
{
  delay(5000); // wait for ESP to be ready (first turned on, both will share same power switch)
  digitalWrite(0, HIGH); // turns shared parallel analog sensor power on
  delay(5000); // wait for power to build up, this is excessive
  int aVal = analogRead(A1);
  digitalWrite(0, LOW); // sensors off
  Serial1.write(aVal); // data to ESP
  delay(10000); // long delay to wait till it dies/not loop again
}

Communication attempt

ESP-code

void loop()
{
  digitalWrite(LED, HIGH); // turn Seeeduino circuit on

  if (Serial.available() > 0)
  {
    String serialMsg = Serial.readString();

    if (serialMsg.length() > 0)
    {
      // https://forum.arduino.cc//index.php?topic=543780.0
      if (serialMsg.equals("9999")) // not a possible value
      {
        txStrByWiFi("match");
        seeeduinoBooted = true;
        clearSerialBuffer();
      } else {
        digitalWrite(LED, LOW);
        delay(2000); // wait to completely die
        seeeduinoBooted = false;
        txStrByWiFi(serialMsg);
        clearSerialBuffer();
        sleepDelay = 10000; // long sleep
      }
    }
  }

  delay(sleepDelay);
  // ESP.deepSleep(sleepDelay);
}

Seeeduino-code

void setup() {
  // Serial.begin(115200); // to monitor // also sends
  Serial1.begin(115200); // to ESP // also works
  analogReadResolution(10);
  pinMode(0, OUTPUT);
}

void loop() {
  Serial1.write(9999); // seeeduino booted avoiding long strings
  delay(2000); // still using unguaranteed delays
  digitalWrite(0, HIGH);
  delay(5000);
  int aVal = analogRead(A1);
  digitalWrite(0, LOW);
  // Serial.println("done measuring"); // comment out if monitor not connected
  Serial1.write(aVal);
  delay(10000); // this shouldn't turn on again for a while
}

Edit: can right-click open new tab to see the image in full size

Those black background pictures doesn’t work, as You already realised. Read the first topic named “How to use this Forum”, #7, how to post code. Code tags, the symbol up to the left in this window is the magic.

Your question was raised not long ago. It looks like high side switching power off. Any chance You could find that thread? It’s not easy to do, but doable as I remember.

Yeah I did consider the code embed thing I can try it, just seems like a lot of code. This one wasn't too bad but I would have 2 sets.

I don't think the power system is the problem, the code is the problem. It can't reliably communicate/know the state of the other device. I'm wondering if I'm not aware of something.

Anyway I'll work on posting the code as embedded text.

And... Please post a wiring diagram, especially of the power parts but also of the connections between the powered down device and its surrounding.

Powering down one circuit might lead to ghost currents, currents flowing through the logic lines with unpredictable results.

I did update the post to have the code, I just think giant walls of code is daunting/syntax highlighting helps to read it faster. Although the scroll/window truncation of the code is nice.

Powering down one circuit might lead to ghost currents, currents flowing through the logic lines with unpredictable results.

Yeah I was concerned of something like that happening. It seems to work fine until they get out of sync and then the Seeeduino stops working... I have to power it off, start it over. It seems to have just gotten progressively worse the more I try to do any kind of state management between the two as opposed to just waiting for the ESP to receive serial data from the Seeeduino.

It will take me a bit to make a more legit wiring diagram than the power one above but I can work on that. I did just get the Fritzing software.

Ehh. Don't use Fritzings.... Pen and paper mostly do a better job. There are easy systems to use, only I can't point them out.

You're aware of "ghost currents" I think. I'm not fully aware of the total situation but more members than You want to put the controller into sleep mode and also "cut off" power to other devices.
That raises the question of how to wake them up, initiate those powered down peripherals.
I never practised that in any design but, as I told, this was recently the subject in a topic.
Hopefully important helpers from that "story" will pick up this thread.

Yeah I can definitely draw something faster than figuring out Fritzing.

Right now the ESP-01 will always be on unfortunately. I mean it is in deep sleep mode(I did that mod where you solder two pins). I expect that to constantly draw power(the 96% efficiency converter) I don't have an exact number but estimating it to be 5-10mA.

Anyway so the ESP-01 will wake the Seeeduino circuit which has its own electronic switch (IRL MOSFET) to turn on the sensors that are normally off with its own battery system.

I'll work on that circuit, thanks.

Will watch the progress of Your topic...

Well I added it, I was out of crayons so I used MS Paint this time.

I don't get it but I see You posted code.
No complain but please post new material in Your latest reply. That saves us a lot of scrolling. Also, always post the entire code. Quite often the reason for trouble comes from early declarations.
Bed tine here, hours ago...
Check Your reply some 8 hours later.

Oh is it sorted recent to old? I posted it in the very first message but okay I'll update my previous response.

I'll just link to the github pages for the code because I think it's too much code to look at with the code embed.

Anyway thanks for your time.

communication method full code.
ESP
Seeeduino

no communication just serial
ESP
Seeeduino

Take a look at the entire topic and You will see what happened.
Try to use code tags and if there's an error message the option is to include a. ino file. Less helpers will be able to read it but that's the option.

ynoloopy:
Well I added it, I was out of crayons so I used MS Paint this time.

Forget software, get out pen(cil) and paper and draw it in the real world, then post a picture of your circuit.
In the above circuit you are low side switching the Seeduino and the analog sensors, switching the gnd,, not the way to do it as you will possibly still have power leaking via the input/output link you have.
You need to HIGH side switch, that is switch the positive not the negative side of the seedunino and sensors OFF and ON.
Keep ALL gnds common and complete.
What is your project?
What is its application?
Thanks.. Tom.. :slight_smile:

Thanks for understanding my circuit/having a suggestion haha I know it's a soup of crap from my part.

...you will possibly still have power leaking via the input/output link you have.

I think that is the key/I've observed that. When I disconnect the Seeeduino's power source I still see the Seeeduino's LED on sometimes.

It's a moisture sensor ::slight_smile: but an overkill one (up to 8-9 sensors measured). Initially I made a basic thing that used the ESP-01 on its own but I could not step down the 3V analog output to the 1V max of the ESP-01's ADC(hardware mod required). The moisture sensor would stop working correctly. So I went for this way. I just wanted to use every single ADC pin on the Seeeduino and this thing is battery powered/want it to last at least a month. And of course, internet of s--- it will output data about the moisture and battery voltage of the Seeeduino(not shown here but would pass through a Vdivider).

About the high trigger thing, I'm going to have to look into that because it took me a bit to understand how to use a MOSFET. It seemed backwards to me how it works(using ground draining). It does work, I mean when it's all plugged in, I can watch the voltage go up/down as expected to power things on/off.

I just thought that the code is the problem because my string matching from serial communication is not working/matching. I keep getting garbage eg. I send sb from Seeeduino and ESP-01 gets sby.

edit: I've already wired the circuit up, sorry for this nightmare inducing picture. I've had it work for hours straight before but it'll get into that bad state and I'm trying to avoid that/guarantee it won't happen since this will be a "sealed unit" in a "garden" and won't touch it for a month(whenever batteries need to be charged, it has bms's per battery too).

I did look into high-side switches... sucks it says you need a P-type MOSFET or NPN transistor... I have the opposite ones ahh... may need to buy more parts.

Hi,

I could not step down the 3V analog output to the 1V max of the ESP-01's ADC(hardware mod required).

Did you try a simple potential divider?
https://learn.sparkfun.com/tutorials/voltage-dividers/all

Thanks.. Tom.. :slight_smile:

Did you try a simple potential divider?

Yeah I didn't write that well. I could step down the voltage. I'm saying the capacitive moisture sensor stopped working when I did that. Normally if you take a measurement it outputs 2.6V or something like that, and you dip it in a cup of water, it drops to 1.2V or something(there is an obvious difference). After I piped the analog output through a voltage divider it wouldn't work the same(proportionally). There would be a hardly noticeable difference between open air and water measurement. I tried various resistor sizes too(I used a voltage divider calculator).

Also seems like part of my problem is using Serial1.print vs. Serial1.write (print is correct/outputs right values that are received by ESP/transmitted to remote server).

Anyway I think I have enough to move forward from here.

Hi,
What values of resistors did you use?
Can you post a link to the sensors please?

Thanks.. Tom... :slight_smile:

This is the sensor. At the analog out, I used a divider to read it into the ADC of the ESP-01 which has a 1V max limit.
Well I've tried a few resistors, I started "low" as in 2Kohm to 1Kohm, then even tried 2Mohm, 1Mohm seems like it's a 2:1 thing but I was using this calculator. To drop 3V to 1V.

Idk... "it could be an XY problem" as someone would say. This is the "circuit diagram" for the single sensor based off the ESP-01 by itself. At this time I was not aware of the logic-level MOSFET thing. But with a multimeter I could confirm that I was getting the expected result i.e. ESP goes digital high, analog sensor starts to work/output the expected value.

I think the serial vs. print thing I lowkey mentioned before is a contributor. If I use print it probably will honor my state/string matching and then the ESP will wait for the Seeeduino to be booted and what not/not get out of sync. I will update some things. I did the print/serial change recently, I was using print before and generally that was working, though I was hitting the "out of sync" problem after a couple hours/decent amount of time. Not reliable enough to leave alone for days... sadly from a linear extrapolation it seems like the ESP will only last up to two weeks with the constantly on step down regulator... sucks but can do better (WW84 meme) with the coin-cell supercap approach, no regulator.

Still, at the end of the day when I look back at the years of my life(this project) it was productive. Learned a lot, never used a MOSFET before, found out about LDOs, swapping TX to GPIO1, etc...

Well... I changed my stuff back, the main difference is I'm using Serial.print() now vs. Serial.write() and I got rid of the state management, so this is using "unguaranteed time delays". I had it working like this before but it would get into that bad state I've mentioned.

Here's a video of it working. Here's a screenshot of the server logs of the analog data being received from the ESP, the server added the timestamps. Here it is not working.

Ha... just my luck I filmed it going into a bad state. What do I do to fix it? I unplug the Seeeduino's ground and... it's still on. Stealing power somewhere. The Amber light is now flashing like every second vs. rapidly. Plugging Seeeduino ground back in, starts working again.

ESP loop code

void loop()
{
  digitalWrite(LED, HIGH); // turn Seeeduino circuit on

  if (Serial.available() > 0)
  {
    String serialMsg = Serial.readString();
    if (serialMsg.length() > 0)
    {
      digitalWrite(LED, LOW);
      delay(2000); // wait to completely die
      txStrByWiFi(serialMsg);
      clearSerialBuffer();
      sleepDelay = 10000; // long sleep
    }
  }

  delay(sleepDelay);
  // ESP.deepSleep(sleepDelay);
}

Seeeduino loop code

void loop() {
  digitalWrite(0, HIGH);
  delay(5000);
  int aVal = analogRead(A1);
  digitalWrite(0, LOW);
  // Serial.println("done measuring"); // comment out if monitor not connected
  Serial1.print(aVal);
  delay(10000); // this shouldn't turn on again for a while
}

So... I'm pretty sure this means I have to use state vs. estimated time delays. Problem is... the serial strings join together, despite me calling the buffer clear function.

But yeah... I'm going in circles. Thanks to everyone for checking it out, time to slash and burn, begin anew. The high-side switch was good though, gotta plan for that in the future.

One thing I would like is a physical switch or something that easily updates the device/port when I have to swap/flash different devices eg. the ESP vs. Seeeduino.

update:

I'm going to buy components to do the high-side switching and then I'll also opt for the ideal case where the ESP runs off a super cap(again more parts) vs. having its own battery, possibly source an LDO or something.

Hi,
Water Level Sensor schematic.