在Jupyter笔记本电脑中使用HX711和RPiGPIO

时间:2020-01-09 12:52:39

标签: python arduino jupyter-notebook

我正在尝试通过jupyter笔记本驱动arduino板。我正在运行称重传感器,并且希望能够记录使用python收集的数据。该脚本导入的HX711似乎取决于RPi.GPIO。我通过PIP安装了库,但得到了以下

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-7103b482ccff> in <module>
     1 #!/usr/bin/python3
----> 2 from hx711 import HX711

/usr/lib/python3.8/site-packages/hx711/__init__.py in <module>
     1 # -*- coding: utf-8 -*-
     2 try:
----> 3     import RPi.GPIO as GPIO
     4 except ImportError:
     5     raise ImportError(

/usr/lib/python3.8/site-packages/RPi/GPIO/__init__.py in <module>
    21 """
    22 
---> 23 from RPi._GPIO import *
    24 
    25 VERSION = '0.7.0'

RuntimeError: This module can only be run on a Raspberry Pi!

这有道理。如何将以下内容移植到Jupyter中,以便我可以在其中包裹一些东西。 ?

/*
 Example using the SparkFun HX711 breakout board with a scale
 By: Nathan Seidle
 SparkFun Electronics
 Date: November 19th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 T
 This example code uses bogde's excellent library: https://github.com/bogde/HX711
 bogde's library is released under a GNU GENERAL PUBLIC LICENSE
 Arduino pin 2 -> HX711 CLK
 3 -> DOUT
 5V -> VCC
 GND -> GND

 Most any pin on the Arduino Uno will be compatible with DOUT/CLK.

 The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.

*/

#include "HX711.h"

#define DOUT  3
#define CLK  2

HX711 scale;

float calibration_factor =  -3800; //-3800 worked for my 440lb max scale setup

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");

  scale.begin(DOUT, CLK);
  scale.set_scale();
  scale.tare(); //Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units()*10, 2);
  Serial.print(" grams"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}

0 个答案:

没有答案