基于ESP8266 HX711 克级单位称重式压力传感器(接入阿里云物联网平台)—— 看到有点意思,所以我也照着做了一个
Posted 单片机菜鸟哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于ESP8266 HX711 克级单位称重式压力传感器(接入阿里云物联网平台)—— 看到有点意思,所以我也照着做了一个相关的知识,希望对你有一定的参考价值。
手痒做一个
1、参考文档
用到材料:
- ESP8266NodeMcu
- HX711
- 数码管(TM1637)
2、完整软件代码
2.1 HX711,包括h文件和cpp文件
- 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
//****************************************************
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);
2.2 TM1637,包括h文件和cpp文件
- TM1637.h 文件
/*
* TM1637.h
* A library for the 4 digit display
*
* Copyright (c) 2012 seeed technology inc.
* Website : www.seeed.cc
* Author : Frankie.Chu
* Create Time: 9 April,2012
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TM1637_h
#define TM1637_h
#include <inttypes.h>
#include <Arduino.h>
//************definitions for TM1637*********************
#define ADDR_AUTO 0x40
#define ADDR_FIXED 0x44
#define STARTADDR 0xc0
/**** definitions for the clock point of the digit tube *******/
#define POINT_ON 1
#define POINT_OFF 0
/**************definitions for brightness***********************/
#define BRIGHT_DARKEST 0
#define BRIGHT_TYPICAL 2
#define BRIGHTEST 7
class TM1637
public:
uint8_t Cmd_SetData;
uint8_t Cmd_SetAddr;
uint8_t Cmd_DispCtrl;
boolean _PointFlag; //_PointFlag=1:the clock point on
TM1637(uint8_t, uint8_t);
void init(void); //To clear the display
int writeByte(int8_t wr_data);//write 8bit data to tm1637
void start(void);//send start bits
void stop(void); //send stop bits
void display(int8_t DispData[]);
void display(uint8_t BitAddr,int8_t DispData);
void clearDisplay(void);
void set(uint8_t = BRIGHT_TYPICAL,uint8_t = 0x40,uint8_t = 0xc0);//To take effect the next time it displays.
void point(boolean PointFlag);//whether to light the clock point ":".To take effect the next time it displays.
void coding(int8_t DispData[]);
int8_t coding(int8_t DispData);
void bitDelay(void);
private:
uint8_t Clkpin;
uint8_t Datapin;
;
#endif
- TM1637.cpp文件
/*
* TM1637.cpp
* A library for the 4 digit display
*
* Copyright (c) 2012 seeed technology inc.
* Website : www.seeed.cc
* Author : Frankie.Chu
* Create Time: 9 April,2012
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "TM1637.h"
#include <Arduino.h>
static int8_t TubeTab[] = 0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71;//0~9,A,b,C,d,E,F
TM1637::TM1637(uint8_t Clk, uint8_t Data)
Clkpin = Clk;
Datapin = Data;
pinMode(Clkpin,OUTPUT);
pinMode(Datapin,OUTPUT);
void TM1637::init(void)
clearDisplay();
int TM1637::writeByte(int8_t wr_data)
uint8_t i,count1;
for(i=0;i<8;i++) //sent 8bit data
digitalWrite(Clkpin,LOW);
if(wr_data & 0x01)digitalWrite(Datapin,HIGH);//LSB first
else digitalWrite(Datapin,LOW);
wr_data >>= 1;
digitalWrite(Clkpin,HIGH);
digitalWrite(Clkpin,LOW); //wait for the ACK
digitalWrite(Datapin,HIGH);
digitalWrite(Clkpin,HIGH);
pinMode(Datapin,INPUT);
bitDelay();
uint8_t ack = digitalRead(Datapin);
if (ack == 0)
pinMode(Datapin,OUTPUT);
digitalWrite(Datapin,LOW);
bitDelay();
pinMode(Datapin,OUTPUT);
bitDelay();
return ack;
//send start signal to TM1637
void TM1637::start(void)
digitalWrite(Clkpin,HIGH);//send start signal to TM1637
digitalWrite(Datapin,HIGH);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,LOW);
//End of transmission
void TM1637::stop(void)
digitalWrite(Clkpin,LOW);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,HIGH);
digitalWrite(Datapin,HIGH);
//display function.Write to full-screen.
void TM1637::display(int8_t DispData[])
int8_t SegData[4];
uint8_t i;
for(i = 0;i < 4;i ++)
SegData[i] = DispData[i];
coding(SegData);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_AUTO);//
stop(); //
start(); //
writeByte(Cmd_SetAddr);//
for(i=0;i < 4;i ++)
writeByte(SegData[i]); //
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
//******************************************
void TM1637::display(uint8_t BitAddr,int8_t DispData)
int8_t SegData;
SegData = coding(DispData);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_FIXED);//
stop(); //
start(); //
writeByte(BitAddr|0xc0);//
writeByte(SegData);//
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
void TM1637::clearDisplay(void)
display(0x00,0x7f);
display(0x01,0x7f);
display(0x02,0x7f);
display(0x03,0x7f);
//To take effect the next time it displays.
void TM1637::set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr)
Cmd_SetData = SetData;
Cmd_SetAddr = SetAddr;
Cmd_DispCtrl = 0x88 + brightness;//Set the brightness and it takes effect the next time it displays.
//Whether to light the clock point ":".
//To take effect the next time it displays.
void TM1637::point(boolean PointFlag)
_PointFlag = PointFlag;
void TM1637::coding(int8_t DispData[])
uint8_t PointData;
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
for(uint8_t i = 0;i < 4;i ++)
if(DispData[i] == 0x7f)DispData[i] = 0x00;
else DispData[i] = TubeTab[DispData[i]] + PointData;
int8_t TM1637::coding(int8_t DispData)
uint8_t PointData;
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
if(DispData == 0x7f) DispData = 0x00 + PointData;//The bit digital tube off
else DispData = TubeTab[DispData] + PointData;
return DispData;
void TM1637::bitDelay(void)
delayMicroseconds(50);
2.3 aliyun_mqtt,包括h文件和cpp文件
- aliyun_mqtt.h
/*
Aliyun_mqtt.h - Library for connect to Aliyun MQTT server with authentication by
product key, device name and device secret.
https://www.alibabacloud.com/help/product/30520.htm
*/
#ifndef _ALIYUN_MATT_H
#define _ALIYUN_MATT_H
#include <Arduino.h>
#include "PubSubClient.h"
/**
* Connect to Alibaba Cloud MQTT server. In connection process, it will try several times for
* possible network failure. For authentication issue, it will return false at once.
*
* @param mqttClient: Caller provide a valid PubSubClient object (initialized with network client).
* @param productKey: Product Key, get from Alibaba Cloud Link Platform.
* @param deviceName: Device Name, get from Alibaba Cloud Link Platform.
* @param deviceSecret: Device Secret, get from Alibaba Cloud Link Platform.
*
* @param region: Optional region, use "cn-shanghai" as default. It can be "us-west-1",
* "ap-southeast-1" etc. Refer to Alibaba Cloud Link Platform.
*
*
* @return true if connect succeed, otherwise false.
*/
extern "C" bool connectAliyunMQTT(
PubSubClient &mqttClient,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region = "cn-shanghai");
/**
* Two new added APIs are designed for devices with limited resource like Arduino UNO.
* Since it is hard to calculate HMAC256 on such devices, the calculation can be done externally.
*
* These two APIs should be used together with external HMAC256 calculation tools, e.g.
* http://tool.oschina.net/encrypt?type=2
* They can be used together to replace connectAliyunMQTT on resource-limited devices.
*/
/**
* This API should be called in setup() phase to init all MQTT parameters. Since HMAC256
* calculation is executed extenrally, a fixed timestamp string should be provided, such
* as "23668" etc. The same timestamp string is also used to calculate HMAC256 result.
*
* Other params are similar to them in connectAliyunMQTT.
*/
extern "C" void mqttPrepare(
const char *timestamp,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region = "cn-shanghai");
/**
* Use tools here to calculate HMAC256: http://tool.oschina.net/encrypt?type=2
* The calculated result should be defined as constants and passed when call this function.
*/
extern "C" bool connectAliyunMQTTWithPassword(PubSubClient &mqttClient, const char *password);
#endif
- aliyun_mqtt.cpp
/*
Aliyun_mqtt.h - Library for connect to Aliyun MQTT server.
*/
#include "aliyun_mqtt.h"
#include "SHA256.h"
#define MQTT_PORT 1883
#define SHA256HMAC_SIZE 32
// Verify tool: http://tool.oschina.net/encrypt?type=2
static String hmac256(const String &signcontent, const String &ds)
byte hashCode[SHA256HMAC_SIZE];
SHA256 sha256;
const char *key = ds.c_str();
size_t keySize = ds.length();
sha256.resetHMAC(key, keySize);
sha256.update((const byte *)signcontent.c_str(), signcontent.length());
sha256.finalizeHMAC(key, keySize, hashCode, sizeof(hashCode));
String sign = "";
for (byte i = 0; i < SHA256HMAC_SIZE; ++i)
sign += "0123456789ABCDEF"[hashCode[i] >> 4];
sign += "0123456789ABCDEF"[hashCode[i] & 0xf];
return sign;
static String mqttBroker;
static String mqttClientID;
static String mqttUserName;
static String mqttPassword;
// call this function once
void mqttPrepare(const char *timestamp,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region)
mqttBroker = productKey;
mqttBroker += ".iot-as-mqt以上是关于基于ESP8266 HX711 克级单位称重式压力传感器(接入阿里云物联网平台)—— 看到有点意思,所以我也照着做了一个的主要内容,如果未能解决你的问题,请参考以下文章
基于单片机HX711电子秤称重控制设计(仿真,原理图,程序)