The Pico In MicroPython: HTTP Client |
Written by Harry Fairhead & Mike James | ||||
Monday, 02 January 2023 | ||||
Page 3 of 3
A Temperature Sensor ClientThe standard approach to implementing a sensor device that makes its readings available to other devices is to implement a web server or a custom protocol that allows other devices to connect. A simpler solution is to implement an HTTP client and allow the sensor device to send data to a server which other devices can then connect to as required. As we have already seen in Chapter 15, it is very easy to use the DS18B20 to collect data about ambient temperature. All we have to do is take a reading every so often, convert the floating-point value to a byte object and send it to the server using a PUT: from machine import Pin, Timer import network import rp2 import onewire import ds18x20 from time import sleep_ms import urequests wifi = setup("country", "ssid", "key") url = "http://192.168.253.45:8080" ow = onewire.OneWire(Pin(22)) presence = ow.reset() if presence: print("Device present") else: print("No device") DS = ds18x20.DS18X20(ow) roms = DS.scan() while True: DS.convert_temp() temp = DS.read_temp(roms[0]) buf = str(temp).encode("utf-8") try: r = urequests.put(url, data=buf) r.close() except: print("Server Not Online") sleep_ms(500) The setup function given earlier which makes the WiFi connection has been omitted. The server simply has to respond to the PUT request and convert the bytes to a string and then a float: from http.server import HTTPServer, As before, it is simple enough to convert the server to HTTPS. The overriding of the log_message method is to suppress the regular printing of status messages. The do_PUT handler method simply prints the temperature. It could, of course, do much more. You can appreciate that this architecture works well if you can allocate a simple device to act as a server. If the sensor supplies more data than a single measurement then using a JSON encoding makes things easier. For example, if you want to send a timestamp along with the temperature you could change the while loop to read: while True: DS.convert_temp() temp = DS.read_temp(roms[0]) jsonData={"temp":temp,"time":time()} try: r = urequests.put(url,json=jsonData) r.close() except: print("Server Not Online") sleep_ms(500) You can see that the only difference is that now we make up a dictionary with key/value pairs corresponding to the data we want to send to the server. The only other difference is that we now set json=jsonData rather than setting it to data and the urequests module converts the dictionary into a JSON string. To process the JSON string the server’s do_PUT method now has to use the json module to convert the received JSON string back into a dictionary object: def do_PUT(self): content_length = If you run the client and the server with these changes you will see something like: {'time': 1609459985, 'temp': 14.4375} {'time': 1609459986, 'temp': 14.5} {'time': 1609459987, 'temp': 14.4375} You can unpack the data from the dictionary and use it as you would any other data. In chapter but not in this extract
Summary
Programming the Raspberry Pi Pico/W In MicroPython Second EditionBy Harry Fairhead & Mike JamesBuy from Amazon. Contents
Also of interest: <ASIN:B0BR8LWYMZ> <ASIN:B0BL1HS3QD> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info
|
||||
Last Updated ( Monday, 02 January 2023 ) |