Help with communication would be much appreciated.

Home Assistant automation projects, questions, etc. go here.
Post Reply
cnselpaso
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Thu Jul 01, 2021 5:05 pm

I am new to this site and have already received some help which I much appreciate!
This is a separate question from the first on ino files.
My greenhouse is finished and already putting plants in it. However I haven't finished the automation and could use some help to finish up.
I built the same box as ledgardener and it is working but communication between the esp32 and the arduino mega is where I am stuck.

Here is a brief description of what I have done so far....
I setup Node Red and established MQTT Broker which is communicating properly. (Quite a learning curve for me to get these functional). :)
My esp32 is connecting to my Wifi and appears to be connecting to my MQTT broker even though I don't see the "connected" line in my serial monitor on esp32 sketch. The serial monitor doesn't go beyond the line "connecting to mqtt_server."
When I go into HA and view logs I see that it connected to my esp32 chip. Let me know if I missed something here since I figured the mqtt server would show "connected" and then possibly would load other items afterwards???
I did purchase the White labs Shield, EC, and pH probes. That part works because I first used the sketch from White Labs and was able to calibrate probes and get readings.
Afterward I uploaded the "control_box_mega" ino file to the Arduino mega and the esp32 ino file uploaded successfully.
That is where I have camped out for the last 2 weeks. Can't seem to get the readings from the probes or 8 channel relay to the esp32. If I go to Node Red and ask to see data from my topic feedback/atlas_ph I get "NAN" (not a number) for reply.
I did check and found that my 2 serial lines were not connected to the right channels on the esp32, so I changed and verified the mega TX3 and RX3 were connected to RX2 and TX2 on the esp32 After change, still no communication.
Meanwhile I started my greenhouse and am doing manual dosing and reading from my bluelab pH and EC probes for temporary solution.

Any assistance would be much appreciated...
gmarbles
LED-Curious
LED-Curious
Reactions:
Posts: 5
Joined: Wed Jul 21, 2021 7:48 pm

Not much help coming from me, but i'm in a similar situation. I can see messages going from HA to esp32 but the story after that is unclear. The mega doesn't see the serial data coming in.

One thing i looked at was ensuring the current for both the esp32 and the mega were 7v. I saw something that the pinup/down would have issues if they were not on the same voltage (but i have no idea cuz that isnt working for me anyway)

I have the HA Pi setup running HA
I have the esp32 which is connected via wifi to the HA(pi) via mqqt.
You should be able to see the Mosquito broker log in the supervisor / mosquito broker section / Log.

Mine looks like
Mosquitto broker
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] mosquitto.sh: executing...
[12:13:23] INFO: Setting up user mqttuser
[12:13:24] INFO: SSL is not enabled
[cont-init.d] mosquitto.sh: exited 0.
[cont-init.d] nginx.sh: executing...
[cont-init.d] nginx.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[12:13:24] INFO: Starting NGINX for authentication handling...
[12:13:25] INFO: Starting mosquitto MQTT broker...
1628615605: mosquitto version 1.6.12 starting
1628615605: |-- *** auth-plug: startup
[12:13:26] INFO: Successfully send discovery information to Home Assistant.
[12:13:26] INFO: Successfully send service information to the Supervisor.


I have confirmed that mosquito is working from the HA to the ESP 32.
Confirmed by connecting the esp32 to your computer and view the serial monitor output.
It should enable you to see the messages sent to the esp from HA.

If you get the serial connection between the esp and the arduino working, let me know how!

Finally, i have a NODE MCU version of the esp32. I'm wondering if the tx/rx (2) is working... or if i need a different board.
Last edited by gmarbles on Tue Aug 10, 2021 6:42 pm, edited 1 time in total.
cnselpaso
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Thu Jul 01, 2021 5:05 pm

Thanks for the feedback gmarbles!
I am just glad to connect with someone that is also going in this direction.
I appreciate the insight you show. Let me look over the info you sent and will get back to you when I have time to devote which should be by tomorrow.
gmarbles
LED-Curious
LED-Curious
Reactions:
Posts: 5
Joined: Wed Jul 21, 2021 7:48 pm

Updated my post with a little more information.
gmarbles
LED-Curious
LED-Curious
Reactions:
Posts: 5
Joined: Wed Jul 21, 2021 7:48 pm

Well i have some news, but not great news.
Seems this shit is hard to debug.

I took the esp32 i had and uploaded this code.

Code: Select all

//esp32

#include <HardwareSerial.h>

HardwareSerial MySerial(1);

void setup() {

    Serial.begin(9600);
    MySerial.begin(9600, SERIAL_8N1, 16, 17);

    Serial.println("Setup");
}

void loop() {
    while (MySerial.available() > 0) {
        uint8_t byteFromSerial = MySerial.read();
        // Do something
         static byte ndx = 0;
          char commandChar = byteFromSerial;
        Serial.print("reading...");
        Serial.println(commandChar);
    }

    
    //Write something like that
    Serial.println("not avail");
    delay(1000);    
}

Code: Select all

//mega
void setup() {

  // initialize both serial ports:

  Serial.begin(115200);

  Serial3.begin(9600);
}

void loop() {

  // read from port 1, send to port 0:  
  delay(1000);
  if (Serial3.available()) 
  {
    int inByte = Serial3.read();
    Serial3.println("mega");
    Serial.println("mega");
  }
 else
  {
     //Serial.write("no data");
  }
  
}
I plugged in the tx/rx rx/tx and both connected to my computer via usb.
I then watched the consoles for both and saw that "mega" was being sent from the mega to the esp32 via serial.
I haven't tested the other way around yet.

I did see that the voltage may be an issue related to this youtube video.


It says that the output is 3.3 so the mega needs to run on 3.3v? I'm out of town for a week, but if you get anything going let me know

Thanks,

Grant
cnselpaso
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Thu Jul 01, 2021 5:05 pm

Hi Grant,

Appreciate your work on this. I too am out of town and will be back on Monday so i will also pick up where you are at and check voltage I am running on my Mega... Glad to see you could see the serial communication at least going one direction.

Thanks,

Dana
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

I initially used a level shifter in between the mega and esp32 to bring the voltage down, but it gave me more problems so I scrapped it. My system has been running with a direct connection between the 2 ever since.

If you subscribe to the MQTT topic feedback/debug, do you see any debug info? This should publish anything the ESP32 receives from the Mega.

The TX3 on your Mega connects to the RX2 on the ESP and the RX3 on your Mega connects to the TX2 on your ESP, correct?
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
gmarbles
LED-Curious
LED-Curious
Reactions:
Posts: 5
Joined: Wed Jul 21, 2021 7:48 pm

Well i successfully round tripped communication between the esp32 and the MEGA.
So i guess i have the pins correctly setup.

I'm now going to try to flash the mega and esp32 with the ino provided. How will i be able to tell if the mega is getting something from the esp32 that got a mqqt message from Home assistant?

Frustrating for sure. Can't wait until i pass this last major hurdle.

I'm getting closer.
I now have both of the devices communicating when plugged into the computer.
I then tried to unplug the esp32 from the computer and power via the box power and it seemed to no longer work.
This could easily be due to power issues or ground issues.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Anything that the Mega sends to the ESP32 will be published to the MQTT topic feedback/debug, so try subscribing to that topic with a program like MQTT Box in windows and see what comes back. You should see information like water level, etc. You can try turning on one of the 8 relays from home assistant and see if the relays fire or if you get feedback on that MQTT topic. If I recall, I'm not printing any debug info over the first serial connection. If you wanted to see it, you could add a Serial.println() in the processSerialData() function to print on serial so you can read it with your computer USB connection like this:

Code: Select all

void processSerialData()
{
  if (newData == true)
  {
    client.publish("feedback/debug", receivedChars);
    Serial.println(receivedChars);
    //the rest of the function below omitted
  }
    
If you want me to go over your code, you can PM me and we can take a look. I just helped another guy figure out a few issues with comms the other day.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
cnselpaso
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Thu Jul 01, 2021 5:05 pm

Hi Gmarbles,

Sorry I have been out of town but got back and was able to figure out the problem.
Turns out that when I connected the serial lines between the Mega and ESP chips I must have tapped into power because after correcting the problem I still wasn't getting any data flowing even using the feedback/debug.
However after replacing both chips and hooking back the serial lines correctly, I am getting a ton of info coming in. Also I began to see my ph and ec probes were sending data ... YEAAAA :lol: !
I saw your feedback and glad we have LEDGARDENER to help us out. Thanks to both of you!
Post Reply