loading

IOT or Internet of Things is a hot topic! According to the experts everything will be connected to the internet and all our devices and their data will soon be just an IP address away from us. So where do you start if you want to explore the world of IOT? How about a simple Temperature, Humidity, and Light sensor for your basement.

Summer is here and being in the Northeast that means HUMIDITY with a capital HUMID. Many of us have some sort of de-humidification system in our basements. My workshop is in my basement and I have a small dehumidifier that keeps it nice a dry during these months. Despite the basement being humid it is also cooler than the above ground summer temps. I decided that I wanted to know how cool and how humid it can be and thus it became the inspiration for my first IOT project.

DHT11 sensors measure temperature and relative humidity and are cheap. Perfect for a first project. The DHT11 is not what I would call "scientifically" accurate but it is good enough to monitor my basement. While I was at it, I figured I might as well add a light sensor to know if I left the lights on. Another simple and cheap solution, all I need to use is a photoresistor.

Where to put the data became the next question. I could build a web server, but I wanted this to be simple so I decided to leverage a service called Thingspeak that has an API and will let me post and review the data from my IOT monitor.

Step 1: What You Need to Complete This Project

Lets get started on building our project. We will build this on a breadboard, so no need to worry about soldering, or a designing pcb. Once we are happy with the design we can do that.

Hardware:

-Raspberry PI 2 & SD Card with Raspbian Operating System
-USB Power Supply
-USB Cable
-Breadboard & Jumper Wires
-2 x DHT 11 sensors
-2 x 10K Resistors
-2 x Photocells
-2 x 1uF Capacitors

Step 2: Prep the Raspberry PI

If you have not done so, load Raspbian on your Raspberry PI. If you don't have a Raspberry PI, you can get one at Soldering Sunday that includes NOOBS pre-loaded on the MicroSD card or you can follow our guide to loading the Operating System for your Raspberry PI.

Once your Raspberry Pi is up and running we need to setup Python to talk to the GPIO pins. The GPIO pins are our interface to the DHT11 Temp/Humidity sensor and the photocell. For a deeper look at the Raspberry Pi GPIO Pin's go to our GPIO Tutorial.

Configuring Python

Not all the libraries we need to make this project are pre-loaded on the Raspberry Pi. You will need the Adafruit GPIO Python library and the Adafruit DHT 11 library.

We will use Adafruit's guide and library for setting up Python to communicate with the Raspberry Pi GPIO pins.
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-gpio

We also need Adafruit's Python library for the DHT11 sensor, which you can find here:
https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview

Step 3: Raspberry PI - Understanding GPIO Pins

GPIO stands for General Purpose Input/Output and on the Raspberry Pi they are the physical interface between the software side of the Raspberry PI and the outside world. We will need to use the GPIO pins to connect to the DHT11 and the Photoresistor.

Different versions of the Raspberry Pi have a different amount of GPIO pins. In early versions of the Raspberry Pi there were 26 pins and the more recent versions have 40 pins. Even though more pins were added, pins 1 through 26 are the same on all versions. When you look at a reference for Raspberry Pi GPIO pins you will will find several notations for each pin. Most commonly you will find one reference for the physical name of the pin (1 to 40) and the other for the GPIO Name (GPIO1, etc). The physical name is just that, the physical ordered number of the pin. With Python we will be using the GPIO reference for our pin identification. The GPIO Name is designated from the chip set and more commonly used in advanced projects.

Referencing the wrong GPIO Pin number is very common and if you are not getting the results you expect when working with GPIO, double check the pin you are connected to and the pin you are referencing in your code.

If you would like a more in-depth review of the Raspberry Pi GPIO pins, we have a tutorial on them on our site.

Step 4: Build the Circuit

We are keeping the circuit simple and building it on a breadboard using our components and jumper wires. Before connecting anything to your Raspberry PI, disconnect the power.

Warning - you can destroy your Raspberry Pi with a short circuit from a wrong connection. Just be careful and double check everything before powering back on.

To connect from the Raspberry PI to breadboard I like to use Dupont Cables, they are jumper wires that have a female side and a male side. The female side connects right to the male header pins of he Raspberry Pi and the male side plugs right into the Breadboard.

For this circuit we need to use the 3.3v out from the Raspberry Pi Pin 1 (do not use the 5v on Pin 2) and we need Ground (GND) of course. Connect these from the Pi to the Breadboard.

The DHT 11 has 4 Pins. Pin 1 is VCC, Pins 2 is Data, Pin 3 is NOT USED, Pin 4 is Ground.

  • Connect DHT 11 Pin 1 to 3.3v
  • Connect DHT 11 Pin 2 to Raspberry PI Pin 16/GPIO 23 and connect a 4.7 or 10k resistor from DHT 11 Pin 2 to DHT Pin 1
  • Connect DHT 11 Pin 4 to Ground

The photo resistor has 2 pins

  • Connect one pin to 3.3.v
  • Connect the Other Pin to Raspberry Pi Pin 18/GPIO 24
  • Connect a 1uF Capacitor to the same pin that the photo resistor is connected to on GPIO24. The Ground (White Stripe) side of the capacitor should go to Ground.

Check your work against the attached Fritzing Diagram and the photos.

Step 5: Setup Thingspeak for Our IOT Data

Our Python script is going to read data from the DHT11 sensor and the photoresistor and then publish the values of that data to our channel on Thingspeak. First we need to set it up.

Go to Thingspeak.com and create a free acount or login to you existing account. Click on "My Channels" and then click on New Channel. Name your new channel and name the fields. The order of the fields is important later when we post data. They can be in any order, but when you post the data the data you need to remember position.

You can decide if you want the channel to be public or not as well as publish information about it's location. This is all up to you and will not affect our code. You will also need the Write API key for the channel as it will be required to post data to the channel.

Step 6: Mini-intro to Python

Python has been around for many years and is has made a roaring comeback as the goto language for developers of all skill levels. It is an easy and clean language for the beginner and a powerful option for the advanced developer. It was a perfect choice for the Raspberry Pi. On the Raspberry Pi you will notice that there is a Python 2 and a Python 3 option. We will be working in Pyhton 3.

Why Python 3?

Good question. To answer you I will quote from the Python Wiki: "Python 2.x is legacy, Python 3.x is the present and future of the language"

Where can I learn more about Python?

Start at the source, python.org or try your hand at Python onlie at Code Academy. For a tutorials on Python on the Raspberry PI go here.

Launch IDLE on the Raspberry Pi

Most programming languages have a Integrated Development Environment, or IDE, and Python is no different. For Python it is called IDLE which also stands for Integrated DeveLopment Environment. IDLE is essentially a souped up text editor that understands you are coding in Python and will highlight your syntax and provide shortcuts to help you write your code. It will also help validate that your syntax is correct.

To Launch IDLE on the Raspberry Pi go to MENU -> Programming -> Python 3

To start writing our script go to FILE -> NEW and a new, untitled window will appear. This is where we will actually write our code. Save and name the file before we begin. Go to FILE ->Save and the file myfirstIOT.py

Take note of the directory you saved it in. It is probably /home/pi

Don't forget to add the ".py" at the end of your file, IDLE will not add it automatically.

Step 7: Create the Python Script

Download the attached Python Script. You can copy paste to your IDLE session or download it to your Raspberry PI and save it in the /pi folder.

Before running it be sure to edit your API key from your ThingSpeak channel - edit this line:

#Setup our API and delay
myAPI = "***Insert Your API CODE HERE***"


Step 8: Start Your IOT Engine!

We are ready to turn on our IOT engine - all we need to do is run our script.

Open LXTerminal. At the default command prompt you should be in the /pi directory already. You can type LS to see the file listings and the myfirstIOT.py should be there. We need SUDO (administrative) rights for script to access the GPIO Pins, so run the script as follows:

sudo python myfirstIOT.py

You should see the base url printed with your API key and the next line will be data from the DHT 11.

Each line printed locally will display:

Temp (c) - Temp (f) - Humidity - Light ( 0=off / 1=on)

Then go check you Channel on Thingspeak, you should have data.

SUCCESS! You Did it!
You are now part of the Internet of Things revolution.

In the age of Google and mass collaboration, it must be said that nothing we are doing here is original or has not been done before. We are just putting together components of software and hardware to solve the challenge we have. With that said, it means that if I can do it, you can do it too. What will you do next? Take this and expand on it.

Hello <br>Actually i m new to raspberry pi pls is anybody can help to add Adafruit library to my pi
<p>Thanks for the detailed article . I also made it . I have hosted the code of it on github https://github.com/rahulbagal/iotexample </p><p>My detailed log on my website <a href="http://www.24x7code.com/web/internet-of-things-a-simple-demonstration-how-build-an-iot-device/" rel="nofollow">http://www.24x7code.com/web/internet-of-things-a-s...</a> </p>
<p>Here is the correct code!</p><p>&quot;&quot;&quot;</p><p>My First Internet of Things</p><p>Temperature/Humidity Light monitor using Raspberry Pi, DHT11, and photosensor </p><p>Data is displayed at thingspeak.com</p><p>2015/06/15</p><p>SolderingSunday.com</p><p>Based on project by Mahesh Venkitachalam at electronut.in</p><p>&quot;&quot;&quot;</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import RPi.GPIO as GPIO</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p>DEBUG = 1</p><p># Setup the pins we are connect to</p><p>RCpin = 24</p><p>DHTpin = 23</p><p>#Setup our API and delay</p><p>myAPI = &quot;***Insert Your API CODE HERE***&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>GPIO.setmode(GPIO.BCM)</p><p>GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p>def RCtime(RCpin):</p><p> LT = 0</p><p> if (GPIO.input(RCpin) == True):</p><p> LT += 1</p><p> return (str(LT))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> LT = RCtime(RCpin)</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW)+</p><p> &quot;&amp;field4=%s&quot; % (LT))</p><p> print f.read()</p><p> print TW + &quot; &quot; + TWF+ &quot; &quot; + RHW + &quot; &quot; + LT</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>if __name__ == '__main__':</p><p> main()</p>
<p>Statements in this code are perfect. But if you copy paste the code here, You will definitely have indentation problem. Thus, i recommend to compare this code with the MyfirstIOT.py's code and complete the code.</p>
I have made this project but i have not shot any pictures. Python program provided in the instructable has a lot of unwanted stuff in it. All the green colored (between &quot;&quot;&quot;&quot; &quot;&quot;&quot;&quot;) is comment in python and one should delete those first. The remaining code is incomplete then. One should compare the code with the one given by AlainB18 and complete the end. Statement's in AlainB18's code are correct but lack python's syntax.
<p>sudo python myfirstIOT.py<br> File &quot;myfirstIOT.py&quot;, line 68<br> &quot;&quot;&quot;<br> ^<br>SyntaxError: invalid syntax</p>
<p>Here is how to get the project to work:</p><p>Step 1</p><p>Replace my write API key Y5HGYOS51NFJ2I5O with your write api key in 3 lines (85,161 and 217).</p><p>Step 2</p><p>Save the file using the name dht11_thingspeak.py Iin the /home/pi folder</p><p>Step 3</p><p>You run the python script by entering this command in the command prompt. Replace YOURWRITEAPIKEY in the command with your write API key:</p><pre>sudo python dht11_thingspeak.py YOURWRITEAPIKEY </pre><p>Here is the corrected python code:</p><p>#My First Internet of Things</p><p>#Temperature/Humidity Light monitor using Raspberry Pi, DHT11, and photosensor </p><p>#Data is displayed at thingspeak.com</p><p>#2015/06/15</p><p>#SolderingSunday.com</p><p>#Based on project by Mahesh Venkitachalam at electronut.in</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import RPi.GPIO as GPIO</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p>DEBUG = 1</p><p># Setup the pins we are connect to</p><p>RCpin = 24</p><p>DHTpin = 23</p><p>#Setup our API and delay</p><p>myAPI = &quot;Y5HGYOS51NFJ2I5O&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>GPIO.setmode(GPIO.BCM)</p><p>GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p>def RCtime(RCpin):</p><p> LT = 0</p><p> if (GPIO.input(RCpin) == True):</p><p> LT += 1</p><p> return (str(LT))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL </p><p>#My First Internet of Things</p><p>#Temperature/Humidity Light monitor using Raspberry Pi, DHT11, and photosensor </p><p>#Data is displayed at thingspeak.com</p><p>#2015/06/15</p><p>#SolderingSunday.com</p><p>#Based on project by Mahesh Venkitachalam at electronut.in</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import RPi.GPIO as GPIO</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p>DEBUG = 1</p><p># Setup the pins we are connect to</p><p>RCpin = 24</p><p>DHTpin = 23</p><p>#Setup our API and delay</p><p>myAPI = &quot;Y5HGYOS51NFJ2I5O&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>GPIO.setmode(GPIO.BCM)</p><p>GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p>def RCtime(RCpin):</p><p> LT = 0</p><p> if (GPIO.input(RCpin) == True):</p><p> LT += 1</p><p> return (str(LT))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> LT = RCtime(RCpin)</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW)+</p><p> &quot;&amp;field4=%s&quot; % (LT))</p><p> print f.read()</p><p> print TW + &quot; &quot; + TWF+ &quot; &quot; + RHW + &quot; &quot; + LT</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>#My First Internet of Things</p><p>#Temperature/Humidity Light monitor using Raspberry Pi, DHT11, and photosensor </p><p>#Data is displayed at thingspeak.com</p><p>#2015/06/15</p><p>#SolderingSunday.com</p><p>#Based on project by Mahesh Venkitachalam at electronut.in</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import RPi.GPIO as GPIO</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p>DEBUG = 1</p><p># Setup the pins we are connect to</p><p>RCpin = 24</p><p>DHTpin = 23</p><p>#Setup our API and delay</p><p>myAPI = &quot;Y5HGYOS51NFJ2I5O&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>GPIO.setmode(GPIO.BCM)</p><p>GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p>def RCtime(RCpin):</p><p> LT = 0</p><p> if (GPIO.input(RCpin) == True):</p><p> LT += 1</p><p> return (str(LT))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL </p><p>#My First Internet of Things</p><p>#Temperature/Humidity Light monitor using Raspberry Pi, DHT11, and photosensor </p><p>#Data is displayed at thingspeak.com</p><p>#2015/06/15</p><p>#SolderingSunday.com</p><p>#Based on project by Mahesh Venkitachalam at electronut.in</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import RPi.GPIO as GPIO</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p>DEBUG = 1</p><p># Setup the pins we are connect to</p><p>RCpin = 24</p><p>DHTpin = 23</p><p>#Setup our API and delay</p><p>myAPI = &quot;Y5HGYOS51NFJ2I5O&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>GPIO.setmode(GPIO.BCM)</p><p>GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p>def RCtime(RCpin):</p><p> LT = 0</p><p> if (GPIO.input(RCpin) == True):</p><p> LT += 1</p><p> return (str(LT))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> LT = RCtime(RCpin)</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW)+</p><p> &quot;&amp;field4=%s&quot; % (LT))</p><p> print f.read()</p><p> print TW + &quot; &quot; + TWF+ &quot; &quot; + RHW + &quot; &quot; + LT</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>if __name__ == '__main__':</p><p> main()</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> LT = RCtime(RCpin)</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW)+</p><p> &quot;&amp;field4=%s&quot; % (LT))</p><p> print f.read()</p><p> print TW + &quot; &quot; + TWF+ &quot; &quot; + RHW + &quot; &quot; + LT</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>if __name__ == '__main__':</p><p> main()</p><p>if __name__ == '__main__':</p><p> main()</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> LT = RCtime(RCpin)</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW)+</p><p> &quot;&amp;field4=%s&quot; % (LT))</p><p> print f.read()</p><p> print TW + &quot; &quot; + TWF+ &quot; &quot; + RHW + &quot; &quot; + LT</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>if __name__ == '__main__':</p><p> main()</p>
<p>Finally..made my first IOT project!!</p><p>Learned that Indenting is extremely important in Python.</p>
<p>I am in Bangaluru City, weather.com on phone is showing 23 deg celsius, but the pi placed near TV backside has DHT11 censor attached to out is showing temperature 30 deg celsius, this is 7 deg of difference, It works as I tested by keeping ice brick and temperature fell immediately , </p><p>so I am doubtful is it accurate temperature of the room or I have not connected the 10K OHM resister which might have made the difference.</p><p>see it in action : https://thingspeak.com/channels/166617</p>
<p>I made this one and it indeed works using the code provided by AlainB18.</p><p>This was my first project.</p><p>Thanks a lot.</p>
<p>Can someone make a correct version of this myfirstIOT.py file please</p>
<p>Can someone make a correct version of this myfirstIOT.py file please</p>
<p>Nice project - But could someone explain why the capacitor is used?</p>
<p>Could someone post the actual correct python code for both sensors (as intended by the author). Thank you very much. </p>
<p>3 copies of the same code, you commented out one.. you're a boof</p>
<p>Very good project. There are some confusion with your code (maybe someone damage it, good take a look). I did mine and worked fine. The tip of Thigspeak was great! Thanks a lot. </p><p>Bellow a video with my experiment, including a Twitter alarm.</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/hJmctXPE96o" width="500"></iframe></p>
<p>Does it works if someone has a DHT11 Sensor, that is not manufactured by Adafruit, <br>Since we don't have Adafruit products in India :(<br>Kindly Help </p>
yes, should have no problem. I tried with 3 different sensors, 2 from China and one from Amazon. All worked fine.<br>
Very nice indeed. I am glad my project was helpful. You did a great job.
Really nice. I am glad my project helped you.
<p>Does it works if someone has a DHT11 Sensor, that is not manufactured by Adafruit, <br>Since we don't have Adafruit products in India :(<br>Kindly Help</p>
<p>Hi there,</p><p>i got quite a few problems after running the script. The mistakes with the &quot;&quot; i fixed myself, but still there is the problem with the url.</p><p>Can someone please post a well working version of the script.</p><p>The project does look so nice - i would luv to try!</p><p>Thanks</p>
<p>Check my post up there</p>
<p>Hi, this seemed to be working well until I tried to run the python. I don't really know much about python so i'm not sure how to fix this. I downloaded the file, added my API and every time I try to run the script it gives me syntax errors. </p><p>Here is an example of the errors i'm getting: </p><p>pi@raspberrypi ~ $ ls</p><p>Desktop Downloads myfirstIOT.py python_games tempRead.py thermometer.py</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 67</p><p> &quot;&quot;&quot;</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 124</p><p> My First Internet of Things</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 190</p><p> &quot;&quot;&quot;</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 261</p><p> = '<a href="https://api.thingspeak.com/update?api_key=%s" rel="nofollow">https://api.thingspeak.com/update?api_key=%s</a>' % myAPI</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 67</p><p> &quot;&quot;&quot;</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 67</p><p> &quot;&quot;&quot;</p><p> ^</p><p>SyntaxError: invalid syntax</p><p>pi@raspberrypi ~ $ sudo python myfirstIOT.py</p><p> File &quot;myfirstIOT.py&quot;, line 272</p><p> = '<a href="https://api.thingspeak.com/update?api_key=%s" rel="nofollow">https://api.thingspeak.com/update?api_key=%s</a>' % myAPI</p><p> ^</p><p>SyntaxError: invalid syntax</p>
<p>Check my post way up for the python fix</p>
&quot;&quot;:I fixed and it works. First you need to check on 272 that the correct is baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI <br> <br>And for &quot;&quot;&quot;&quot;&quot; its just a the position of one of the comments block that is wrong. You can just remove anything inside this &quot;&quot;&quot; or check it one by one.
<p>I'm not seeing what correction you made at line 272?</p>
same with me. quite strange
<p>Here's the fixed python below. The downloadable python seems to have some serious copy-paste issues. :(</p><p>I also cleaned away the photo sensor bits as I don't have it, but you can use the original downloaded version. My script uses GPIO4, but you can change that easily. Also change the thinkspeak channel key.</p><p>Thanks for the original poster a great project.</p><p>---- code below ----</p><p>&quot;&quot;&quot;</p><p>My First Internet of Things</p><p>Temperature/Humidity Light monitor using Raspberry Pi, DHT11 </p><p>Data is displayed at thingspeak.com</p><p>2015/06/15</p><p>SolderingSunday.com</p><p>Based on project by Mahesh Venkitachalam at electronut.in</p><p>&quot;&quot;&quot;</p><p># Import all the libraries we need to run</p><p>import sys</p><p>import os</p><p>from time import sleep</p><p>import Adafruit_DHT</p><p>import urllib2</p><p># Setup the pin we connect to</p><p>DHTpin = 4</p><p>#Setup our API and delay</p><p>myAPI = &quot;insert_here_your_thingspeak_channel_key&quot;</p><p>myDelay = 15 #how many seconds between posting data</p><p>def getSensorData():</p><p> RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)</p><p> #Convert from Celius to Farenheit</p><p> TWF = 9/5*TW+32</p><p> # return dict</p><p> return (str(RHW), str(TW),str(TWF))</p><p># main() function</p><p>def main():</p><p> print 'starting...'</p><p> baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI</p><p> print baseURL</p><p> while True:</p><p> try:</p><p> RHW, TW, TWF = getSensorData()</p><p> f = urllib2.urlopen(baseURL + </p><p> &quot;&amp;field1=%s&amp;field2=%s&amp;field3=%s&quot; % (TW, TWF, RHW))</p><p> print f.read()</p><p> print &quot;tempC &quot; + TW + &quot;, tempF &quot; + TWF+ &quot;, humidity &quot; + RHW</p><p> f.close()</p><p> sleep(int(myDelay))</p><p> except:</p><p> print 'exiting.'</p><p> break</p><p># call main</p><p>if __name__ == &quot;__main__&quot;:</p><p> main()</p>
I love this project, was great fun to make. I could not get it to work, toen I noticed I forgot to connect the 3.3V line. Connected it and works great.
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi. I downloaded the attached myfirstIOT.py file, changed the API code as instructed. However, I kept having syntax error. Please advice me on this thanks!<br></p><p>Best Regards,</p><p>Priscilla</p>
<p>Hi I wanted to set up using visual studio 2015 where my raspberry pi2 has windows 10 IoT os. What steps should i use then?</p>
<p>does myfirstIOT.py script need to be run as a cron job or just run once? </p>
<p>Just run once. It will stay resident. </p>
<p>excellent thanks, do you have any suggestion on running the script on boot and pushing the console output in to the background?</p>
<p>Very good Project. I just add the Photocell. Worked fine. Including I sent Tweets as &quot;alarm&quot; when light was ON or Temperature reached a pre-stablished level. </p><p>Thanks a lot.</p>
Nice work!
<p>I used the DHT22 sensor and did not add the photo resistor (yet). Thingspeak has a very nice UI and so easy to use. I am now onto trying to do this same project via a Flash web app.</p>
<p>Hi, Project is working well upto temperature and humidity. But photocell values are staying 0 all the time despite Light intensity varying...</p>
<p>Could use Wia (https://www.wia.io) with this too. </p>
<p>how do i know what kind of resistor to use</p>
<p>Nice project well explained :) <br><br>Quote: &quot;..for Python it is called IDLE which also stands for <strong>I</strong>ntegrated <strong>D</strong>eve<strong>L</strong>opment <strong>E</strong>nvironment.&quot;<br>Fun fact for those who don't already know - Python is named after &quot;Monty Python&quot; - and in the tradition to sneaking in python sketch references - as well as what you say above, IDLE is also reference to &quot;Eric Idle&quot; - one of the members.</p>
<p>Many thanks for this instructable! I had great fun making it. </p>
<p>Nicely done !!! This will be the first thing that I will make when my Raspberry Pi arrives. :)</p>
<p>Very good project! I used a DHT22 instead of DHT11.</p>

About This Instructable

133,750views

339favorites

License:

Bio: Soldering Sunday was started by a couple of Makers. We are dedicated to providing a supportive environment for Makers of all skill sets from the ... More »
More by SolderingSunday:Build Your First IOT with a Raspberry Pi, DHT11 sensor, and Thingspeak. EZ LED Glasses Circuit Stickers Holiday Centerpiece 
Add instructable to: