Introduction: Basic Thingspeak Introduction Using Python
What this is about
This is a small introduction into the service of Thingspeak. It will be so simple that you can easiely understand how it all works and therefor adapt it in your own projects too.
Recently my dad told me about these wireless Pooltermostats that he uses and which are always breaking. He switched to a better model where he can just change the transmitter, but still those are around 30 bucks each. It sounded like a waste of money to me and i decided to build him a better one. After some research concerning the topic of IoT I found Thingspeak which i found to be great for storing and showing all the data. I started searching for some tutorials or infos including the most basic code there is, to not just copy a project but really understand the matter. Since i did not found a lot of easy beginner tutorial i decided to do my own.
Without further ado, lets jump right into it by creating a channel...
Step 1: Create a Channel
The first thing you of course got to do is to go to Thingspeak.com and create an account. I will not go into this since it is completely self explaining.
The second thing you want to do, is to go to the Channels tab in the Mainview and then go to My Channels.
Then press the create new channel button and you will get to the third part of creating a channel. The Setup!
For the most basic project just fill it out like i did or any way you fancy. Just remember, the number of fields you now create define the number of different graphs that you can view afterwords. But if you forget something it is fine to, you can always add more Fields later.
Save the Channel and we are almost done here.
Step 2: Get You Key
Now click on the my channel button again and you get to a list of all your channels. There chose the one you want to use and click on the API Key button. Now you need to note down your Write API Key sine we will need this one in the next and final step.
Step 3: Code
We did all the setup and now just need to understand the code for using thingspeak.
I created the most basic program you could imagine and now will go through everything in more or less detail, so you understand how this works.
First we need to import some stuff. The only thing needed for the basic code is just the time and the urllib2 import. The time just gives you the chance of creating a delay and not flooding your channel. The urllib2 import is important since we use this to open the link that updated the data.
Next is just some variable declaration that is needed for the mathematic formulation i want to express. Do not get caught in there. The baseURL variable actually is important. You need to put your API_key here instead of mine (CX98XANY3K3AGUAO). If you do not do so, I will get your data and not you ;)
As you can see the last word of the baseURL is field1 this tells Thingspeak that the data you add later will be put into field1. If you have more than just one field you will need to change this and create a second baseURL ending with ...&field2=
Next i start a loop and print out the value to the screen that will later be updated.
Now comes the important part we define variable called f which is the sum of the baseURL and the value a that we want to upload. If you want to upload some different variable just replace the a with the corresponding name.
Now we open the link by f.read() and directly close it again by f.close().
Afterwords i created a 5 second delay.
The part after this is just the calculation for the number coming next. The numbers that are uploaded a the numbers of the Fibonacci Series .. I just choose those so that the graph will not be linear.
This is all. I hope that this will help you, and you are now able to understand and use thingspeak in your own projects. Of course there is way more you can do with thingspeak, but this is just a really basic tutorial to get you going.
Thanks for reading.