Python Forum
Raspberry Pi Python Rest Post
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspberry Pi Python Rest Post
#1
I have two questions, i want to call a URL passing in three parameters SensorName Temperature Humidity the sensor is working so the temp and humi parameters are correct so how to i correctly call the usrl passing the parameters thanks Madaxe
 #!/usr/bin/env python import urllib3 import subprocess import os import pigpio import DHT22 import urllib2 import urllib host = subprocess.check_output('hostname', shell=True) http = urllib3.PoolManager() from time import sleep os.chdir('/home/pi/PIGPIO') pi = pigpio.pi() s = DHT22.sensor(pi, 4) try: while 1: s.trigger() temp = s.temperature() humi = s.humidity() print 'Temp: {0:0.1f}*C Humidity: {1:0.1f}%'.format(temp, humi) r = request.post('http://192.168.0.1/WebTempHumidity/WebServiceTempHumidity.asmx/WriteTempHumidity?SensorName=Marcs&Temperature=56&Humidity=78') sleep(3) except KeyboardInterrupt: pass 

i solved the first part of adding the values to the url, the code runs but the url does not post, but in the output if i open the url then it works

#!/usr/bin/env python
import urllib3
import subprocess
import os
import pigpio
import DHT22
import requests

host = subprocess.check_output('hostname', shell=True)
http = urllib3.PoolManager()
from time import sleep
os.chdir('/home/pi/PIGPIO')
pi = pigpio.pi()
s = DHT22.sensor(pi, 4)

try:
    while 1:
	s.trigger()
	temp = s.temperature() 
	humi = s.humidity()
        print 'Temp: {0:0.1f}*C  Humidity: {1:0.1f}%'.format(temp, humi)    
		RBS_URL ='http://192.168.178.28/WebTempHumidity/WebServiceTempHumidity.asmx/WriteTempHumidity?SensorName=Marcs&Temperature=' + str(temp) + '&Humidity=' + str(humi) + ''
		print(RBS_URL)
		r = requests.post(RBS_URL)
		sleep(3)
except KeyboardInterrupt:
    pass

Here is the terminal output from the code above i printed out the response also
the strange thing is if i paste the url into a browser on the pi then i get data in the table

Temp: 24.1*C  Humidity: 59.6%
http://192.168.178.28/WebTempHumidity/WebServiceTempHumidity.asmx/WriteTempHumidity?SensorName=Marcs&Temperature=24.1&Humidity=59.6
Request format is invalid: .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,180 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Coding for Python and Raspberry pi beast 3 39,502 Sep-21-2021, 09:56 PM
Last Post: beast
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,938 Mar-21-2021, 05:17 PM
Last Post: yome
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,401 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Calling Oracle REST SQL from Python johnjacob 2 1,993 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  Uploading files to NAS from Raspberry PI using python zazas321 0 2,424 Sep-07-2020, 09:02 AM
Last Post: zazas321
  Port my python program to Raspberry pi seamlessly Hassibayub 1 1,898 Jun-29-2020, 12:58 PM
Last Post: snippsat
  Parse a REST API call using Python GKT 1 1,874 May-07-2020, 04:15 AM
Last Post: buran
  How to get valid error message while invoking REST API through Python gollupraveen 0 2,049 Oct-04-2019, 07:15 PM
Last Post: gollupraveen
  A question about python in Raspberry Psycopast 3 2,309 Aug-14-2019, 06:36 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020