最近需要一个工具,需要远程读取压力,就做了一个小工具
1.硬件
选用称重传感器+HX711+ESP8266NodeMCU+电源,由于使用场景无法接电源,我直接使用充电宝当电源接入ESP8266
压力传感器接HX711
红->E+
黑->E-
白->A-
绿->A+
HX711接ESP8266NodeMcu
GND->GND
VCC->3.3V输出
DT->D1
SCK->D2
2.软件
总共3个文件
HX711.h
#ifndef __HX711__H__
#define __HX711__H__
#include <Arduino.h>
#define HX711_SCK D2
#define HX711_DT D1
extern void Init_Hx711();
extern unsigned long HX711_Read(void);
extern long Get_Weight();
extern void Get_Maopi();
#endif
HX711.CPP
#include "hx711.h"
long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;
#define GapValue 112
//****************************************************
//初始化HX711,设置SCK输出脚,DT输入脚
//****************************************************
void Init_Hx711()
{
pinMode(HX711_SCK, OUTPUT);
pinMode(HX711_DT, INPUT);
}
//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
//****************************************************
//称重
//****************************************************
long Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu = HX711_Buffer;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);
return Weight_Shiwu;
}
//****************************************************
//读取HX711
//****************************************************
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
bool Flag = 0;
digitalWrite(HX711_DT, HIGH);
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
count=0;
while(digitalRead(HX711_DT));
for(i=0;i<24;i++)
{
digitalWrite(HX711_SCK, HIGH);
delayMicroseconds(1);
count=count<<1;
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
if(digitalRead(HX711_DT))
count++;
}
digitalWrite(HX711_SCK, HIGH);
count ^= 0x800000;
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
return(count);
}
weight_sensor.ino
#include "HX711.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <stdlib.h>
#include <stdio.h>
ESP8266WebServer esp8266_server(80);
long Weight = 0;
char Weight_str[16] = {0};
//你的wifi
#define WIFI_SSID "" // WiFi账号密码,更改成自己的
#define WIFI_PASSWD "" // WiFi密码,更改成自己的
//创建WiFiClient实例
WiFiClient espClient;
//连接Wifi
void initWifi(const char *ssid, const char *password)
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi does not connect, try again ...");
delay(500);
}
Serial.println("Wifi is connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// 初始化web
void init_web()
{
esp8266_server.begin();
esp8266_server.on("/", handleRoot);
esp8266_server.onNotFound(handleNotFound);
Serial.println("HTTP esp8266_server started");
}
//处理网站根目录“/”的访问请求
void handleRoot() {
Weight = Get_Weight();
Serial.println("weight: ");
Serial.println(Weight);
ltoa(Weight,Weight_str,10);
esp8266_server.send(200, "text/plain", Weight_str);
}
// 设置处理404情况的函数'handleNotFound'
void handleNotFound(){ // 当浏览器请求的网络资源无法在服务器找到时,
esp8266_server.send(404, "text/plain", "404: Not found"); // NodeMCU将调用此函数。
}
// 初始化
void setup(){
Serial.begin(115200);
initWifi(WIFI_SSID, WIFI_PASSWD);
init_web();
Init_Hx711();
Get_Maopi();
}
void loop()
{
esp8266_server.handleClient(); // 处理http服务器访问
}
访问IP地址即可获取到压力,为了避免IP变动,可设置成固定IP