Rithvik PodduturiHemalatha Nayudu
Published © MPL-2.0

UltraSonic Value to Thingspeak

Uploading Ultrasonic value to Thingspeak Cloud.

BeginnerShowcase (no instructions)9,712
UltraSonic Value to Thingspeak

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Ultrasonic Sensor With Raspberry PI

UltraSonic connections

Code

Ultrasonic Sensor Values to thingspeak

Python
Sudo python yourfilename.py XXXXXXXXX (thingspeak Write API Key)

Run it in terminal
import time
import RPi.GPIO as GPIO
import sys
import urllib2

#from LED_display import display_number
def getSensorData():
    GPIO.setmode(GPIO.BOARD)

    trig = 38  # sends the signal
    echo = 40  # listens for the signal

    GPIO.setwarnings(False)
    GPIO.setup(echo, GPIO.IN)
    GPIO.setup(trig, GPIO.OUT)



        

    GPIO.output(trig, True)
    time.sleep(0.00001)
    GPIO.output(trig, False)

    while GPIO.input(echo) == 0: pass

    start = time.time()  # reached when echo listens

    while GPIO.input(echo) == 1:  pass

    end = time.time() # reached when signal arrived

    distance = ((end - start) * 34300) / 2

    return (int (distance))
def main():
    if len(sys.argv) < 2:
        print('Usage: python tstest.py PRIVATE_KEY')
        exit(0)
    print 'starting...'
    
    baseURL = 'https://api.thingspeak.com/update?api_key=%s' % sys.argv[1]
       
    while True:
        try:

            distance = getSensorData()
            f = urllib2.urlopen(baseURL + "&field1=%s" % int(distance/5))
            print f.read()
            f.close()
            sleep(15)
        except:
            print 'exiting.'
            break

         

       
            
# call main
if __name__ == '__main__':
    main()            

Credits

Rithvik Podduturi

Rithvik Podduturi

3 projects • 30 followers
Entrepreneur & Community builder. Love to Travel around the world. Loves Food and Technology ❤
Hemalatha Nayudu

Hemalatha Nayudu

1 project • 16 followers
Student->Topper->GoldMedalist-AppDeveloper-GuinnessWorldRecorder->R&DEngineer-HadoopDeveloper->CTO

Comments