banner



How To Upload Data To Adafruit Huzza

This is the sixth part of a multi-part series on Micropython. In this post, we will upload .py files to an Adafruit Feather Huzzah ESP8266 board using Python and a Python package called ampy. At the finish of the post we will have a working WiFi weather station that will post the temperature to ThingSpeak.com

Before nosotros can use the Microython REPL (the Micropython prompt) running on the Adafruit Feather Huzzah ESP8266, Micropython needs to be installed on the ESP8266 board and PuTTY needs to be installed on the estimator to communicate with the board over a serial connexion. Run across a previous post on how to install Micropython on an ESP8266 lath and how to install PuTTY on a Windows 10 machine.

Summary of Steps:

  1. Install ampy with pip
  2. Write Python code in .py files
  3. Upload the .py files to the board with ampy
  4. Unplug and power upwards the Feather Huzzah and watch the data on ThingSpeak.com

i. Install ampy with pip

Ampy is a tool written by the expert folks at Adafruit. Ampy is used to upload files onto the ESP8266. Since I'm using a virtual environment, I need to activate the virtual environment first before installing ampy. Notation that the tool is called ampy, but we pip install ampy-adafruit.

            $ conda actuate micropython            (micropython)            $ pip install ampy-adafruit            (micropython)            $ ampy --assistance          

2. Write Python code in .py files

At present nosotros need to write the Python code in .py files that will run on the ESP8266 board. The board already contains two chief Python files: boot.py and chief.py . We tin can also add additional files to the board. boot.py is the file that runs first when the lath is powered up. Subsequently boot.py runs, then main.py runs. Nosotros can add together other .py files to the board to provide main.py some functions and classes to work with. We take two general things to practice with our Plumage board: read the temperature and post the temperature to the ThingSpeak.com. We'll apply a different .py file for each of these two general tasks.

The start module, MCP9808.py , will simplify reading temperature data off of the Adafruit MCP9808 temperature sensor breakout. We demand to write a readtemp() part that parses out the temperature data from the I2C bus and outputs the temperature as a float. The readtemp() function needs to import the auto module to use the I2C motorbus. The machine module allows us to create a new I2C object. When we instantiate the I2C object object, we need to specify the scl and sda pins continued to the sensor. scl is the I2C clock line and sda is the I2C data line. on the Adafruit Feather Huzzah scl is pin five and sda is pin iv . And so a new byte array variable needs to be created.

A byte array is needed to store the temperature data when it comes over the I2C line from the sensor to the board. And so we need to read the sensor data using the i2c.readfrom_mem_into() role. The beginning argument is the I2C bus address of the sensor. In our instance, the sensor is at I2C motorbus address 24. You can apply the line >>> i2c.scan() in the Micropython REPL to see this value. The next statement passed to the i2c.readfrom_mem_into() function is the register on the MCP9808 temperature sensor where the temperature value is stored. The temperature is stored on the MCP9808 in annals v. When we access register 5 on the MCP9808, we read in the temperature. The last argument passed into the i2c.readfrom_mem_into() role is the byte array variable that stores the temperature data. The i2c.readfrom_mem_into() function modifies the variable passed to it as a function argument, rather than producing a variable which is the part output as most functions exercise. This is why we needed to first create the byte_data variable earlier calling the i2c.readfrom_mem_into() office. Finally, we need to practice some post processing of the byte array to transform it into a temperature in degrees C. The consummate readtemp() function is below:

                        # MCP9808.py            # Functions for the  MCP9808 temperature sensor            # https://learn.adafruit.com/micropython-hardware-i2c-devices/i2c-master            def            readtemp            ():            import            auto            i2c            =            automobile            .            I2C            (            scl            =            machine            .            Pin            (            5            ),            sda            =            machine            .            Pivot            (            4            ))            byte_data            =            bytearray            (            2            )            i2c            .            readfrom_mem_into            (            24            ,            5            ,            byte_data            )            value            =            byte_data            [            0            ]            <<            8            |            byte_data            [            1            ]            temp            =            (            value            &            0xFFF            )            /            16.0            if            value            &            0x1000            :            temp            -=            256.0            return            temp          

Now we'll build a Python file that contains a set up of WiFi functions called wifitools.py . We used this aforementioned functionally in a previous post to connect the ESP8266 to a WiFi network. In addition to the WiFi functions, we also demand a function to build the ThingSpeak.com spider web API URL. This is the URL we volition asking in club to get our temperature posted on ThingSpeak.com.

                        #wifitools.py            # Wifi connection and ThingSpeak.com postal service functions for an ESP8266 board running Micropython            #https://docs.micropython.org/en/v1.8.six/esp8266/esp8266/tutorial/network_basics.html            def            connect            (            SSID            ,            countersign            ):            import            network            sta_if            =            network            .            WLAN            (            network            .            STA_IF            )            if            not            sta_if            .            isconnected            ():            print            (            'connecting to network...'            )            sta_if            .            active            (            True            )            sta_if            .            connect            (            SSID            ,            password            )            while            not            sta_if            .            isconnected            ():            pass            impress            (            'network config:'            ,            sta_if            .            ifconfig            ())            #https://docs.micropython.org/en/v1.viii.6/esp8266/esp8266/tutorial/network_tcp.html            def            http_get            (            url            ):            import            socket            _            ,            _            ,            host            ,            path            =            url            .            split            (            '/'            ,            3            )            addr            =            socket            .            getaddrinfo            (            host            ,            80            )[            0            ][            -            1            ]            s            =            socket            .            socket            ()            s            .            connect            (            addr            )            s            .            send            (            bytes            (            'Go /            %s                          HTTP/1.0            \r\due north            Host:                        %s            \r\north\r\n            '            %            (            path            ,            host            ),            'utf8'            ))            while            True            :            data            =            due south            .            recv            (            100            )            if            data            :            print            (            str            (            data            ,            'utf8'            ),            end            =            ''            )            else            :            break            def            thingspeak_post            (            API_key            ,            information            ):            if            non            isinstance            (            data            ,            str            ):            data            =            str            (            data            )            if            non            isintance            (            API_key            ,            str            ):            API_key            =            str            (            API_key            )            base_url            =            'https://api.thingspeak.com/update'            API_key            =            '?api_key='            +            API_key            field            =            '&field1='            url            =            base_url            +            API_key            +            field            +            data            http_get            (            url            )          

At present let'due south write a script in a file chosen main.py which will use the functions in our wifitools.py and MCP9808.py files. This chief.py script will import our MCP9808 and wifitools modules and apply the wifitools.connect() part to connect the ESP8266 to a WiFi network. There is a time.slumber(v) line to let the board time to connect to the WiFi network. Next we'll run a loop for a total of 8 hours (with threescore minutes in each 60 minutes). Inside the loop, we'll read the temperature off the MCP9808 using the MCP9808.readtemp() function and post the temperature to ThingSpeak.com using the wifitools.thingspeak_post() part. To read the temperature once a minute, we need to time.sleep(60) (wait 60 seconds) between each measurement. I also have one more .py file called config.py . This file simply contains three variables: SSID, WIFI_PASSWORD and API_KEY. By using a config file, we tin keep our passwords and API keys out of version control. Similar functions and classes, we tin import variables defined in .py files for use in another script.

                        # master.py            # Adafruit Feather Huzzah ESP8266 WiFi Weather condition Station            import            wifitools            import            MCP9808            import            time            import            config            api_key            =            config            .            API_KEY            ssid            =            config            .            SSID            password            =            config            .            WIFI_PASSWORD            wifitools            .            connect            (            ssid            ,            password            )            time            .            sleep            (            five            )            for            i            in            range            (            8            *            60            ):            data            =            MCP9808            .            readtemp            ()            wifitools            .            thingspeak_post            (            api_key            ,            information            )            time            .            sleep            (            threescore            )          

3. Upload the .py files to the board with ampy

Once all the .py files are created, ensure the Adafruit Feather Huzzah ESP8266 board is connected with a USB cablevision to the computer. You will as well demand to know what series port the Feather board is connected to. We'll upload the code files to the board using ampy. Brand sure y'all are in the directory with the .py files and that y'all are working in the (micropython) virtual environment that has ampy installed in information technology.

            $ conda activate micropython            (micropython)$ ampy --port COM4 put MCP9808.py            (micropython)$ ampy --port COM4 put wifitools.py            (micropython)$ ampy --port COM4 put main.py            (micropython)$ ampy --port COM4 put config.py            (micropython)$ ampy --port COM4 ls kick.py wifitools.py MCP9808.py config.py principal.py          

four. Unplug and power up the Feather Huzzah and watch the data on ThingSpeak.com

The Feather Huzzah needs to exist restarted to run the lawmaking we just uploaded. To restart the board, unplug and then replug the board's power. Once power is restored, the board volition run through the boot.py script so commencement the main.py script. When the lath runs the principal.py script, the board will connect to the WiFi network, read the temperature from the sensor so upload the temperature to ThingSpeak.com. If we go to ThingSpeak.com, we should see the temperature plotted on our Channel's folio.

Channel Plot on ThingSpeak

Congrats! Y'all accept a working weather station that is part of the Internet of Things.

Now you lot tin read the temperature from anywhere in the world with an cyberspace connexion!

Source: https://pythonforundergradengineers.com/micropython-upload-code.html

Posted by: hamiltonsagoonger.blogspot.com

0 Response to "How To Upload Data To Adafruit Huzza"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel