0

How do Adafruit IO feeds interact with Arduino pgm timing
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How do Adafruit IO feeds interact with Arduino pgm timing

by jtsliker on Tue Dec 12, 2017 3:19 pm

Can someone please tell me how the Adafruit IO feeds interact with an Arduino program timing wise? I know how to define the feed and how to write the function that handles the feed but I need to know the timing of that interaction. Does the feed act as an interrupt to the program? Is the feed handled on an immediate basis or only at the beginning or end of the program execution? It seems that sometimes one feed will "walk on top of" another feed. Any insight is appreciated. Thanks in advance.

jtsliker
 
Posts: 4
Joined: Tue Jul 25, 2017 3:07 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by adafruit_support_mike on Sat Dec 16, 2017 4:45 am

[moved to the adafruit.io forum]

adafruit_support_mike
 
Posts: 49379
Joined: Thu Feb 11, 2010 2:51 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by btreichel on Sat Dec 16, 2017 12:26 pm

Since i'm trying to learn this stuff, i got curious about your question. Seems like most people are working with python on this stuff, i'm not since i want simple (aka common arduino ide). So, actual answers aren't readily available. One would have to presume when a msg arrives, there is some version of isr. This is seems to be specfic to how the broker handles responding to new data on subscribed feeds. From what I can tell, this is not part of the MQTT spec, so again broker based implementation. One other area that could be involved is the level of service that is set for the msg. LadyAda's new IOT video gets into that.

btreichel
 
Posts: 76
Joined: Tue Jul 25, 2017 3:00 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by abachman on Thu Dec 21, 2017 5:51 pm

Hi jtsliker,


Does the feed act as an interrupt to the program? Is the feed handled on an immediate basis or only at the beginning or end of the program execution?


On a typical, single process Arduino device, IO data will only be processed when you request it. That happens when you call the `io.run();` method inside your loop function. All your subscription handler functions are called during io.run() instead of in response to an interrupt.

If I'm correctly understanding the relationship between the IO, MQTT, and board specific WiFi libraries, then you can end up with overlapping or overwritten MQTT packets if you have enough delay between calls of the io.run method. io.run calls mqtt.processPackets which does its best to process all individual MQTT packets, but is limited by the MAXBUFFERSIZE value set in the MQTT library.

Any delays or delaying calls in your code are going to affect the IO and MQTT libraries' abilities to handle incoming packets, unfortunately.

Things to try: reduce delay in your code wherever possible, increase the MAXBUFFERSIZE value in the Adafruit_MQTT_Library code on the machine you're using to program your device, decrease the number of feed subscriptions by using a single feed and sending different kinds of messages on it, decrease the rate you're sending messages to the device (which may be much harder or impossible to control), switch from multiple feed subscriptions to a single group subscription so that all your messages are handled by a single MQTT subscription object.

Hopefully there are some clues there to help you debug, I'm curious to hear what you come up with. I've read a lot of the low level MQTT + networking code at this point but I haven't had to modify much of it, so I am definitely not an expert in this arena.

Good luck!


- Adam

abachman
 
Posts: 185
Joined: Mon Feb 01, 2010 12:48 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by jtsliker on Fri Dec 29, 2017 4:06 pm

Thanks for your reply. I do have some delays in my code and it has always looked like a timing issue. In my code I set a feed and look for it to be echoed back so I know the Wi-Fi is working and communications between my ESP8266 breakout and Adafruit IO are solid. My application is a monitoring and control app for home automation using my cell phone. Now that I know that io.run() handles the feeds I am going to experiment a bit with some software traps to see exactly what is happening.

Thank you so much for your help, it has me looking in the right place and you have to hunt where the ducks are. I will let you know what I come up with.

John

jtsliker
 
Posts: 4
Joined: Tue Jul 25, 2017 3:07 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by jtsliker on Sun Dec 31, 2017 10:36 am

Hi Adam,

Here's what I found. I set up some software traps and most of the time I was not receiving the "echo" back from Adafruit IO when my program set a feed. I had to set the feed twice, and sometimes more, in order to get the "echo" back. Minimizing program delays or eliminating them had no affect. I had several "Serial.print" commands in my program as a debug tool. When I removed them, everything started working fine. Set the feed, receive the echo back and life is good.

I don't know how much "overhead" is involved in the Serial.print commands but it must be high enough to affect the way io.run() works. I have years of experience programming machine control with assembly languages and programmable logic controllers but this is my first time with a compiled language.

There are many places where timing problems can crop up: Wi-Fi, internet speeds, Adafruit IO overhead, and of course Arduino compiled code. The answer I think is to keep everything as simple as possible (that should always be done anyways) and write the code so it can recognize such problems and recover from them.

This has been a learning experience for me and your help is greatly appreciated. I hope others may see this and provide some more insight or at least get some help from it.

John

jtsliker
 
Posts: 4
Joined: Tue Jul 25, 2017 3:07 pm

Re: How do Adafruit IO feeds interact with Arduino pgm timin

by abachman on Tue Jan 02, 2018 2:54 pm

Oh wow, this is great research! Interesting findings too, I wouldn't expect Serial to have enough overhead to affect the TCP stack, but it's good to learn. Woof. Tricky bit is that may only be true for one board :/ The ESP8266, WICED, ATWINC1500, and MKR1000 all have different hardware, clock rate, memory, and Arduino core implementations, for example.

Is it possible to share more about the project code and IDE you were using for research? Like, were you just making changes to your local Adafruit_IO_Library code and building in the Ardunio app, or were you using a hardware debugger and one of the larger IDEs? Also, what board are you using for this project?

The answer I think is to keep everything as simple as possible (that should always be done anyways) and write the code so it can recognize such problems and recover from them.


Word. I've been running an office temp/humidity monitor (DHT22 + MKR1000) for more than a year and still go back occasionally to touch up the code and hardware for reliability and error handling. Moving from breadboard w/ jumpers to perf-board w/ soldered connections was the biggest win. I had a lot of transient errors and disconnections that must have been related to just loose or flaky wiring causing the whole device to just quit.

Bigger picture, everything is still improving the from both ends (hardware gets better + software gets better) and we'll never hit perfect, but work like this helps. Thanks for following up!


- adam

abachman
 
Posts: 185
Joined: Mon Feb 01, 2010 12:48 pm

Please be positive and constructive with your questions and comments.