神刀安全网

Sensor21: Earn Bitcoin by collecting environmental data

Sensor21: Earn bitcoin by collecting environmental data

Overview

In this tutorial we are going to show you how to use bitcoin and the21 software to buy and sell data from a distributed grid of sensors. The diagram below shows how this application (which we call sensor21 ) uses micropayments to gather data from a distributed sensor network to measure barometric pressure and temperature from multiple locations:

Sensor21: Earn Bitcoin by collecting environmental data

To buy sensor readings, all you need is the21 client and some bitcoin. To sell sensor readings, however, you will need to set up physical hardware capable of collecting sensor data.

We demonstrate both steps in this tutorial, beginning by installing21 and then buying readings from the sensor21 endpoint at https://mkt.21.co/sensor21 . We will then walk through setting up your own sensor21 endpoint on a21 Bitcoin Computer or a DIY 21 Bitcoin Computer to sell your data for bitcoin. Finally, we’ll go through some troubleshooting steps to help debug your hardware.

Buy sensor data for bitcoin

After installing21, you can buy readings from the sensor21 aggregator using the 21 buy command. In this example, we pay five machines in San Francisco for their most recent barometric pressure readings. We pay 20 satoshis per request per sensor, for a total of 100 satoshis.

$ 21 buy https://mkt.21.co/sensor21 --data '{"n": 5, "sensor": "barometricpressure", "city": "San Francisco"}'  ## Output ## { ##  "sensor_type": "barometricpressure",  ##  "city": "San Francisco",  ##  "readings": [ ##    {"timestamp": "2016-05-17T19:55:52.429Z", "sensor_id": "<hash>", "reading": 101434.0},  ##    {"timestamp": "2016-05-17T19:55:52.478Z", "sensor_id": "<hash>", "reading": 100737.5}, ##    ... ##  ] ## }

As shown, the command takes in the service you want to purchase ( sensor21 ) and the POST data specifies the type of sensor you want to gather data from, as well as the number of sensors and the city you want data about. The buy command then hits an endpoint in the21 marketplace and returns a JSON array of sensor data from active Sensor21 servers.

Note that you can also use the following shortcut command, which omits the https://mkt.21.co/ prefix:

$ 21 buy sensor21 --data '{"n": 5, "sensor": "barometricpressure", "city": "San Francisco"}'

You can also see which cities have active sensors by querying the sensor21/info endpoint (for free), as shown:

$ 21 buy sensor21/info  ## Output: ## { ##   "info": [ ##     { ##        "sensors": { ##            "TEMPERATURE": {"num_active_endpoints": 1, "price_to_buy_active_endpoints": 20},  ##            "BAROMETRICPRESSURE": {"num_active_endpoints": 1, "price_to_buy_active_endpoints": 20} ##        },  ##        "city": "London" ##     },  ##     { ##        "sensors": { ##            "TEMPERATURE": {"num_active_endpoints": 3, "price_to_buy_active_endpoints": 60},  ##            "BAROMETRICPRESSURE": {"num_active_endpoints": 3, "price_to_buy_active_endpoints": 60} ##        },  ##        "city": "Chicago" ##     },  ##     { ##        "sensors": { ##            "TEMPERATURE": {"num_active_endpoints": 2, "price_to_buy_active_endpoints": 40},  ##            "BAROMETRICPRESSURE": {"num_active_endpoints": 2, "price_to_buy_active_endpoints": 40} ##        },  ##        "city": "Tokyo" ##     }, ##     ... ##   ] ## }

Sell sensor data for bitcoin

Prerequisites

To sell sensor data for bitcoin you will need the following:

You can click the links above to order each item. The materials will take a few days to ship to your location.

Set up your sensor

Once you’ve got the materials, let’s go through the process of setting up a sensor and connecting it to your Bitcoin Computer. The MPL3115A2 breakout board and jumper wires will be used to add a hardware temperature and pressure sensor to your 21 Bitcoin Computer or DIY 21 Bitcoin Computer.

Connecting the sensor to a 21 Bitcoin Computer

If you have bought a21 Bitcoin Computer, use the female to female jumper wires to connect the breakout board to the 21 Bitcoin Computer’s expansion header. See the pin associations and connection diagram below.

  • VCC_3V3 -> Vin
  • GND -> GND
  • SDA -> GPIO6
  • SCL -> GPIO13

Sensor21: Earn Bitcoin by collecting environmental data

Connecting the sensor to a DIY 21 Bitcoin Computer If you have

If you have instead set up a DIY 21 Bitcoin Computer on a Raspberry Pi , we will use the female to female jumper wires to connect the MPL3115A2 breakout board to the Raspberry Pi expansion header. See the pin associations and connection diagram below.

  • VCC_3V3 -> 3V3 (Pin 01)
  • GND -> GND (Pin 06)
  • SDA -> SDA (Pin 03)
  • SCL -> SCL (Pin 05)

Sensor21: Earn Bitcoin by collecting environmental data

Setting up the protective enclosure

After you have connected the MPL3115A2 board to your Bitcoin Computer or DIY Bitcoin Computer, you can put the entire thing inside a protective enclosure as shown:

Sensor21: Earn Bitcoin by collecting environmental data

As noted above, you can print this enclosure at Shapeways or use this STL file to 3D print your own.

Step 1: Set up your sensor21 endpoint

Next, let’s set up the sensor21 endpoint.

As a first step, if you are using a DIY 21 Bitcoin Computer, you must have the I2C kernel module enabled. Follow the directions in the code below to enable it:

# Enable I2C Kernel Module for DIY 21 Bitcoin Computer Users sudo raspi-config  # Navigate to Advanced Options # Navigate to I2C # Enable I2C & Force I2C Module loading on startup # Choose to reboot your machine now

Next, we are going to clone the sensor21 repository and run the install script. Do this by running the following commands:

cd ~/  # Install git to clone the Sensor21 code sudo apt-get install git  # git clone the code git clone https://github.com/21dotco/sensor21  # run the setup script cd sensor21 source setup.sh

The setup script will install required software packages: unzip, PIGPIO, SQLite3, python3-pip, python-periphery, and flask. Then it runs db.py, which creates a database and initializes it. Finally, it sets up a cron job (in cron.txt ) to query the sensor every minute. The measured data is inserted into a database table called Barometer .

Here’s the output from a successful install on a DIY 21 Bitcoin Computer.

Sensor21: Earn Bitcoin by collecting environmental data

Step 2: Start selling your sensor21 readings

At this point you have the hardware sensor set up and the software to read and store recordings. We can now start the sensor21 server and publish our endpoint, so that others can buy sensor readings from us in aggregate. Start the server by running the following command:

cd ~/sensor21 python3 sensor21-server.py -d

Now publish your endpoint to the marketplace. Substitute your own name and email in the publish command. For more information about publishing new endpoints in the marketplace, please read the 21 marketplace documentation .

21 publish submit manifest.yaml -p 'name="Joe Smith" email="joe@example.com" price="5" host="AUTO" port="5002"'

You can verify that your Sensor21 endpoint is part of the aggregator pool by testing the /verify endpoint:

# grab your ZeroTier IP address and save it to shell variable ZT_IP ZT_IP=$(ifconfig | grep -A2 zt | grep inet | sed 's|[^0-9. ]||g' | sed 's|[^ /t]*||' | awk 'NR==1{print $1}')  # verify your endpoint is part of the pool curl https://mkt.21.co/sensor21/verify?zt_ip=$ZT_IP

Note that if you shut down your 21 Bitcoin Computer or turn off the flask server, the server will no longer be accessible. To re-start the server, navigate to the sensor21 directory, and re-run python3 sensor21-server.py -d in the terminal.

How the sensor works

The sensor21 example uses temperature and pressure sensors from the MPL3115A2 board. However, there are a number of other sensor types you might want to use to generate machine-buyable data. While there are many similarities in approach, each sensor manufacturer approaches communication, data capture, and data transmission differently. We will describe how sensor21 works so that you can apply the same concepts to another device.

System administration (administration.sh)

The bash script administration.sh ( view it here ) helps manage the processes related to sensor21 . Run administration.sh with the function you want to execute as the argument.

cd ~/sensor21 source administration.sh <function>  ## function list # Note: pigpiod is only used for software I2C (used on 21 Bitcoin Computer) #  # start_pigpiod, stop_pigpiod, restart_pigpiod # start_server, stop_server, restart_server # start_cron_job, stop_cron_job  # Example source administration.sh stop_server

As an example, before you can use python to directly access the sensor board, you will need to stop the cron job that is making sensor measurements. The administration.sh script contains multiple functions to help with managing this sensor21 setup.

source administration.sh stop_cron_job

When you are done, you can reinstate the cron job as follows:

source administration.sh start_cron_job

Software and hardware I2C (mpl3115a2.py, python-periphery, pigpio)

The file mpl3115a2.py ( view it here ) implements hardware and software I2C and contains a driver to enable, configure, and measure data from the barometric pressure sensor. I2C can be implemented either by using metal-level features of a processor, or by using GPIO pins and internal timers to replicate the metal-level implementation in software. For DIY Bitcoin Computer users, we use python-periphery to access hardware I2C through python. For 21 Bitcoin Computer users, we use software I2C to set up 2 GPIO pins as I2C Clock and Data lines. Software I2C is also referred to as ‘bit banging’ .

Python-periphery , a python module for the Raspberry Pi, enables hardware I2C access in userspace Linux. Learn more about python-periphery here .

PIGPIO , a C library for the Raspberry Pi, gives us a python interface to access software I2C. It provides access to both hardware and software communication protocols (Serial, SPI, and I2C). Learn more about PIGPIO here and here .

You can programmatically access the sensor using commands like the following:

import mpl3115a2 pressure = mpl3115a2.PressureAPI() print(pressure.get_pressure()) print(pressure.get_temperature()) pressure.close()

Datastore (sqldb.py)

The file sqldb.py ( view it here ) is a helper script which provides python functions for interacting with a SQLite database. This script helps set up the database tables, append a new sensor reading to the next row, and read data from the tables.

Because the I2C bus is not equipped to handle multiple users attempting simultaneous access, it is important to separate the low-level I2C process from server requests with a database and cron job. If each server request instead resulted in an I2C operation, there would be issues when multiple users made overlapping calls. By storing the readings in a database, we eliminate this problem, and we allow a client to request one or more readings in a single request.

cron.py contains the python script needed to update the database, and cron.txt is a cron file used to run the cron.py script every minute.

Web service (sensor21-server.py)

The script sensor21-server.py ( view it here ) starts a flask server that provides a sensor21 endpoint. When queried, the server requests a payment and then provides the requested sensor readings. It also implements a server_status route which checks to see whether a sensor reading has been written to the database within the last five minutes.

Troubleshooting

If you’re relatively new to hardware hacking, it can be a little confusing at first to learn how to debug issues. If you can’t read data from the sensor board, there are a few things to try.

If you are getting I2C related errors on a DIY 21 Bitcoin Computer, ensure that I2C is enabled on your Raspberry Pi:

sudo raspi-config  # Navigate to Advanced Options # Navigate to I2C # Enable I2C & Force I2C Module loading on startup # Choose to reboot your machine now

If you are getting PIGPIO related errors on a 21 Bitcoin Computer, restart the pigpio daemon:

cd ~/sensor21 source administration.sh restart_pigpiod

And if you are getting error codes or do not see sensor output during the setup script, try these steps in the following order:

  • Make sure that the pins are connected according to the connection diagram you used during setup.
  • Power cycle the MPL3115A2 by removing the Vcc jumper wire and then re-connect.
  • Perform a system reboot: sudo reboot now .
  • Then perform a MPL3115A2 software reset as shown:
// Software Reset import mpl3115a2 as mpl p = mpl.PressureAPI() p.mpl3115a2.software_reset() p.close()

If you encounter other issues, please join our public Slack group at slack.21.co and we will try to help!

Next Steps

In this tutorial you learned how to buy and sell bitcoin-incentivized distributed data sets through sensor21 . We hope this tutorial will inspire and help guide you to develop your own bitcoin-payable hardware applications. Come discuss your hardware ideas with other developers in our#iot channel on Slack!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Sensor21: Earn Bitcoin by collecting environmental data

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址