0

IO Code lessons/tutorials regarding sending data to IO
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Fri Jun 21, 2019 6:39 pm

Are there any instructional tutorials relating specifically to IO code. I've followed some of samples relating to a RasPi weather station and see as part of the project code lines that are io.send and such like that sends my weather data to IO but I'm looking for something that focuses on sending data to IO in general rather than just snippets here and there in different project build code samples.

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by franklin97355 on Fri Jun 21, 2019 7:34 pm


franklin97355
 
Posts: 20773
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Fri Jun 21, 2019 7:50 pm

I have not seen that. Thank you. Right from the jump I've already installed the library. I kind of had an epiphany in regards to how to go through one of these projects once I stumbled upon the MU editor and started messing with that. Prior to was just running random code samples and was not able to put it all together in my head as a cohesive structure.
Thanks

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by brubell on Tue Jun 25, 2019 10:04 am

If you're using the Adafruit IO Python client library, there's code samples for performing Adafruit IO interacting using the HTTP API (https://github.com/adafruit/Adafruit_IO ... amples/api, https://github.com/adafruit/Adafruit_IO ... les/basics) and MQTT API (https://github.com/adafruit/Adafruit_IO ... mples/mqtt).

If you're using Adafruit IO CircuitPython, check out the library's examples folder: https://github.com/adafruit/Adafruit_Ci ... r/examples

These examples are less specific than the Adafruit IO projects on learn and might be what you're looking for.

brubell
 
Posts: 431
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 11:42 am

I'll take a look at those.

I do have another IO question though. I'm piggybacking off of a weather dashboard project and I noticed the way the code runs is roughly like below but cut down for brevity.
Does code need to alternate like that or can you have all the print functions in a block and all of the io.send in a block. I am guessing you wrote the project Adafruit IO for Feather or Raspberry Pi.

print BME
io.send BME

print VEML
io.send VEML

vs.

print BME
print VEML

io.send BME
io.send VEML

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by brubell on Tue Jun 25, 2019 12:15 pm

Which board are you using?

You can do either, just make sure to call the io.send() or print() methods until the BME/VEML values have been obtained first.

brubell
 
Posts: 431
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 1:23 pm

Right now I have the following code running in terminal window.

while True:
print("Reading sensor values")
print()
# Read VEML6075
print("UV Index: %0.1f UV" % veml.uv_index)

#Read BME280 data
temp_data = bme280.temperature
# convert temperature (C->F)
temp_data = int(temp_data) * 1.8 + 32
print('Temperature: %0.1f F' % temp_data)
print("Humidity: %0.1f %%" % bme280.humidity)
print("Pressure: %0.1f hPa" % bme280.pressure)
print("Altitude: %0.2f meters" % bme280.altitude)
b = 17.62
c = 243.12
gamma = (b * bme280.temperature /(c + bme280.temperature)) + math.log(bme280.humidity / 100.0)
dewpoint = int((c * gamma) / (b - gamma))
print("Dewpoint: %0.1f %%" % dewpoint)
print("eCO2 = %d ppm \t TVOC = %d ppb" % (sgp30.eCO2, sgp30.TVOC))

I have your code already in place where you either read or create the feeds. It is above this code snippet.

Am wondering if I can just below the print eCO2 line just insert all the io.send lines and everything should be fine or is it going to crash.

As far as the sensors I subbed a VEML6075 for the VEML6070. BME280 and SGP30 are same. Also have realtime clock DS1307.

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 1:34 pm

Maybe I sort of misunderstood you but got lucky in installing the SGP last and putting its code in last..

You are saying that I need to print and io.send the SGP30 last?
If that is the case, how come?

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 1:52 pm

RasPi 3 B+ board

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by brubell on Tue Jun 25, 2019 1:54 pm

Am wondering if I can just below the print eCO2 line just insert all the io.send lines and everything should be fine or is it going to crash.


It's not going to crash, but it could cause a throttle depending on which Adafruit IO account type you have and how often you are sending data.

The print statements were added to the example to prevent sending too much data to Adafruit IO at once, which causes IO throttle your account (30 data points per minute on IO Free, 60 data points per minute on IO Plus).

btw - please don't double-post in threads, you may can your post if you need to add more information.

brubell
 
Posts: 431
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 1:59 pm

I have IO Plus acct

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by abachman on Tue Jun 25, 2019 2:29 pm

hi wsquared58!


I just wanted to emphasize brent's point about timeouts / slowing down data publishing.

Any time you're running a program that sends data in a "while True:" loop, you must include some code to make sure data can't be sent too quickly. With IO+, your rate limit is 1 data point every second. If you're sending three different sensor values, that means your loop should sleep for at least three seconds either after publishing or just before publishing. "Publishing", in your case, is the "io.send" command. If you're writing python, you can use the "time.sleep" method to pause your program. Please don't rely on print statements alone to slow the program.

Using python as an example, leaving out all the sensor setup and IO connection code, that would look something like this:
Code: Select all | TOGGLE FULL SIZE
import time

while True:
    temperature = readTemperature()
    print("temperature: {}".format(temperature))
    io.send_data("temperature", temperature)
    time.sleep(5)


As a rule of thumb, if knowing more data won't help you make better decisions, then store less data. In this case, if the weather around your device isn't changing more than once every five minutes, then only take readings and store data once every five minutes: time.sleep(300)

And a total aside, but Python is a very space sensitive language, so please make sure to use the \[code\] button in the post editor when you're posting code samples. Otherwise the spaces get lost and it's tricky to interpret code samples.


- adam b.

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

Re: IO Code lessons/tutorials regarding sending data to IO

by wsquared58 on Tue Jun 25, 2019 4:24 pm

Thank you very much for the explanation. I'm a total noobie at this and the "why" of doing things is invaluable. This stuff is hard if you have never done it before.
I look at the code examples for various sensors and the code may be identical with the exception of a timer and there is no explanation of why the difference.
In the case of the BME sensor there is a colon on two of the readings in the print code and an equal sign in the third. Is that their for a specific reason or is it a typo? After changing the = sign to a : turns out it was just a typo (I guess) since I changed it and it was just a text change.
I am not a programmer but worked in a highly automated powerplant that had a computer engineers and I know from talking with them that if you gave 5 of those folks a problem and you wanted a particular outcome there might be 3 diffferent solutions that yielded same result.

This station hardware is ultimately going to a friends fishing cottage in a hurricane prone area that he doesn't live in full time and the 5 minutes you reference is ample except for one device I haven't tangled with yet which is an anemometer and gusts and how to manage that.

I'm still trying to decide the best way to measure any potential storm surge due to the cottages proximity to the ocean.

wsquared58
 
Posts: 59
Joined: Sun Jun 09, 2019 9:13 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by brubell on Wed Jun 26, 2019 1:42 pm

In the case of the BME sensor there is a colon on two of the readings in the print code and an equal sign in the third. Is that their for a specific reason or is it a typo? After changing the = sign to a : turns out it was just a typo (I guess) since I changed it and it was just a text change.

Different examples may have been written by different people who may prefer different formatting. You can use any character within a python `print()` statement as long as it's surrounded by quotation marks, such as:

Code: Select all | TOGGLE FULL SIZE
print("dog: poodle")
which would print to your terminal dog: poodle
or
Code: Select all | TOGGLE FULL SIZE
print("dog=poodle")
which would print dog=poodle
This station hardware is ultimately going to a friends fishing cottage in a hurricane prone area that he doesn't live in full time and the 5 minutes you reference is ample except for one device I haven't tangled with yet which is an anemometer and gusts and how to manage that.

Neat and good luck!

brubell
 
Posts: 431
Joined: Fri Jul 17, 2015 10:33 pm

Re: IO Code lessons/tutorials regarding sending data to IO

by abachman on Wed Jun 26, 2019 1:51 pm

I look at the code examples for various sensors and the code may be identical with the exception of a timer and there is no explanation of why the difference.


Yeah, there are decisions the original author makes at a few different levels. Some concern using the sensor, some concern connecting with IO, and some concern the usage of this script within the context of a whole network of other devices. It's hard to tell from scanning which is which. It's not advice, but I can say that experience reading code is just about the only way to get a better feel for what's going on.

In the case of the BME sensor there is a colon on two of the readings in the print code and an equal sign in the third. Is that their for a specific reason or is it a typo? After changing the = sign to a : turns out it was just a typo (I guess) since I changed it and it was just a text change.


Totally right that it's just a text change. Everything inside the parenthesis of the `print()` lines is purely for display purposes, which here--like in most code--means it's just for humans to read. You can put anything you want in there. Probably even emoji.

if you gave 5 of those folks a problem and you wanted a particular outcome there might be 3 diffferent solutions that yielded same result.


Ha! truth. Give them enough time and you'll get 4 more on top of that :D

one device I haven't tangled with yet which is an anemometer and gusts and how to manage that.


Ooooo, interesting. Yeah, if you want to record spiky / intermittent data to IO, the best way is to set a timer, check it on each loop tracking the max windspeed over a period of time and then publishing intermittently. In pseudocode, that would look something like this:
Code: Select all | TOGGLE FULL SIZE
import time

# constant value for tracking how often (in seconds) data should be published.
# All caps isn't significant to Python, it just lets me know that this is a
# value that shouldn't change.
PUBLISH_INTERVAL = 60

# record the time stamp of the last time data was published. set to 0 now since
# no data has ever been published
last_published = 0

# set initial max value to 0 to indicate no reading
max_wind = 0

while True:
  # get a sensor reading and compare it to the current interval's max value
  current_wind = measureWindSpeed()
  max_wind = max(max_wind, current_wind)

  # get the current time in seconds - https://learn.adafruit.com/arduino-to-circuitpython/time
  now = time.monotonic()

  # if enough time has passed, publish and reset sensor readings
  if now - last_published > PUBLISH_INTERVAL:
      publishData()
      max_wind = 0

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

Please be positive and constructive with your questions and comments.