Looking for Somebody to Help Me with some Arduino/C Questions/Issues

Home Assistant automation projects, questions, etc. go here.
Post Reply
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Hi guys,

I'd like to nail down an issue I'm having with my code for my ESP8266 and Mega so I can post the improved version and share what I have with more confidence. I'm new to writing actual code, and what I have mostly works, but there are some weirdnesses and I'm sure my code is riddled with issues/inefficiencies. I'm hoping there's an experienced programmer in here who can help a noob out and maybe provide some suggestions/best practices for me to improve it.

The main problem I have is that I've had to implement a restart for my ESP on a daily basis because it becomes unresponsive after a day or two - I presume I have some sort of memory leak or something.
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!
justindmyers
LED-Curious
LED-Curious
Reactions:
Posts: 3
Joined: Wed Jul 08, 2020 4:42 pm

So I'm pretty new to the Arduino world myself, but I have been programming for quite a while (although mostly on the webdev side of things).

I'm looking for a setup pretty similar to yours anyways so I'd be happy to take a look at your code to see if I could help out at all. You could always make a private repo on github to share your in-progress code (if you'd rather it not be public yet) with others and I'd be more than willing to help you get set up on a workflow for others contributing/improving your code.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

That'd be wonderful! I'll get the repo set up and go back through and try to better-comment my code so y'all have a clue what the hell I'm trying to do and we'll go from there. Thanks!
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!
Autodev
LED-Curious
LED-Curious
Reactions:
Posts: 17
Joined: Fri Jun 05, 2020 6:08 am

I would like to see.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Autodev wrote:
Sun Jul 12, 2020 6:10 am
I would like to see.
Alright, cool. My goal is to get the code commented and have it up on github this weekend so we can start fixing it.
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!
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Alright, @justindmyers I've got the private repo up with all my HA files as well as my Arduino code. Looks like I just need to invite people now... so, anyone who's interested PM me your git username or email address, I guess?
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!
justindmyers
LED-Curious
LED-Curious
Reactions:
Posts: 3
Joined: Wed Jul 08, 2020 4:42 pm

Sent you my username.

To invite just go to the repo Settings > Manage Access
Autodev
LED-Curious
LED-Curious
Reactions:
Posts: 17
Joined: Fri Jun 05, 2020 6:08 am

I created an issue on the repo with my thoughts. Hoping to get a closer look this weekend.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Thank you for digging into this @Autodev. I read through your notes about logging in the ESP and the MQTT callback publish thing. I’ve been going over the example you linked and am trying to grasp what it is he’s doing in there and how it applies to what I’m doing.

I am trying to solidify my understanding of pointers and I think I’m close but still not 100%. I am not doing any memory allocation or freeing in my code either and I see that happening in there as well and that seems to be a whole other thing to learn about.
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!
Autodev
LED-Curious
LED-Curious
Reactions:
Posts: 17
Joined: Fri Jun 05, 2020 6:08 am

Yeah, after looking at your code again; I don't think what you're running in to is a consequence of what he's describing there (or at least my interpretation of it). Seems like payload is shared so if you call publish inside the callback; payload will be overwritten. Seems like for all the cases you handle, you read from payload, then call publish for something else, then never touch payload again, so you should be good in your scenario. I think the best practice here might be to extract everything you need to out of payload asap and then never touch it again as any calls to publish sneaking up on it would change the value of payload if anything tried to read it afterwards. It's a good thing to know overall.


You might be seeing a similar effect in void recvWithStartEndMarkers() as you set recvInProgress and ndx as static and then are having to reset them at the end in line 588, 589; not sure if this is by design or your reasoning here.

For anyone curious or who might understand better than me, here's what I referenced: https://github.com/knolleary/pubsubclie ... llback.ino

Disclosure: This is my first time really looking at any kind of C as most of my experience has been in JavaScript, Swift, C#; C is way "closer to the metal" and "holds your hands" a lot less, so if anyone has any corrections, please chime in.
Post Reply