''' This code can be used for arduino serial connection which will send
 *  Tempreture and Light intensity values to internet cloud via a http request.
 *  _________________________________________________
 *  |    Code By--> RITVIK DAVE                     |
 *  |    Email----> "ritvikdaveidra@gmail.com"      |
    -------------------------------------------------
        Requirement for this code
        ____________________________________________________________________________________________
        ""           * Arduino should connected to your PC via COM<> port                         ""
        ""           * Your PC must have Internet Connection                                      ""
        ""           * Your PC shold have Python 2.7 or above installed                           ""
        ""           * Change "COM19" in 4th line to your arduino connected COM                   ""
        ""           * Change "key=***" in 17th line to your Thingspeak channel's Write API Key   ""
        ____________________________________________________________________________________________
'''
import serial
import time
import urllib
arduino = serial.Serial('COM19', 9600, timeout=.1)
count=0
while True:
    data = arduino.readline()[:-1]                       #the last bit gets rid of the new-line chars
    if data:
        print data
        if count==0:
            new=[0,0]
            count=1
        else:
            new=data.split()


        temp=int(new[0])
        light=int(new[1])

        f = urllib.urlopen('https://api.thingspeak.com/update?key=NIJW2KFLALYDFNZE&field1=%s&field2=%s'%(temp,light))

        print "temp=%d & light=%d are updated"%(temp,light)

        time.sleep(3)
