Bulk update using Raspberry Pi | Raspberry Pi | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Bulk update using Raspberry Pi
No permission to create posts
July 5, 2019
12:16 pm
Avatar
mac

Gold
Members
Forum Posts: 21
Member Since:
August 10, 2017
sp_UserOfflineSmall Offline

This code is on the ThingSpeak web site at
https://au.mathworks.com/help/thingspeak/continuously-collect-data-and-bulk-update-a-thingspeak-channel-using-a-raspberry-pi-board.html#d117e3791

5) Define the function httpRequest to send data to ThingSpeak and to print the response code from the server. A response code 202 indicates that the server has accepted the request and will process it.

def httpRequest():
'''Function to send the POST request to
ThingSpeak channel for bulk update.'''

global messageBuffer
data = json.dumps({'write_api_key':writeAPIkey,'updates':messageBuffer}) # Format the json data buffer
req = ul.Request(url = url)
requestHeaders = {"User-Agent":"mw.doc.bulk-update (Raspberry Pi)","Content-Type":"application/json","Content-Length":str(len(data))}
for key,val in requestHeaders.iteritems(): # Set the headers
req.add_header(key,val)
req.add_data(data) # Add the data to the request
# Make the request to ThingSpeak
try:
response = ul.urlopen(req) # Make the request
print response.getcode() # A 202 indicates that the server has accepted the request
except ul.HTTPError as e:
print e.code # Print the error code
messageBuffer = [] # Reinitialize the message buffer
global lastConnectionTime
lastConnectionTime = time.time() # Update the connection time

pi@rpb:~ $ python3 test4.py
Traceback (most recent call last):
File "test4.py", line 107, in
updatesJson()
File "test4.py", line 100, in updatesJson
httpRequest()
File "test4.py", line 48, in httpRequest
req = ul.Request(url = url)
AttributeError: module 'urllib3' has no attribute 'Request'
pi@rpb:~ $

I have used urllib3 as urllib2 is not compatible with Python3,
but using urllib3 errors with the ThingSpeak example code as well Python 2.7 will have no support.
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date.
A future version of pip will drop support for Python 2.7.

I need to use the module busio which is not compatible with python 2.7.

What code fix is needed for urllib3 to allow this bulk update to Thingspeak?

July 10, 2019
10:32 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 781
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Thank you for sharing the upcoming deprecation. Ill look into this and post here if I get a solution.

July 13, 2019
10:45 am
Avatar
mac

Gold
Members
Forum Posts: 21
Member Since:
August 10, 2017
sp_UserOfflineSmall Offline

I have now been able to use urllib3 successfully but the code needs improvement
Note also that print cmd in python 3.x requires () eg print(e.code)

#import urllib as ul
import urllib3
.
.
.
def httpRequest():
#Function to send the POST request to
#ThingSpeak channel for bulk update.
http=urllib3.PoolManager()
global messageBuffer
data = {'write_api_key':WriteAPIKey,'updates': messageBuffer} # Format the json data buffer
encoded_data = json.dumps(data).encode('utf-8')
r = http.request('POST', url, body=encoded_data, headers={'Content-Type': 'application/json'})

#additional code is required to get the response from the server

messageBuffer = [] # Reinitialize the message buffer
global lastConnectionTime
lastConnectionTime = time.time() # Update the connection time

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

dogarty, dlwiener, bulksms, bmchoice4u, irajames, YesItsMe

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457