请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于STM32的工厂监测

[复制链接]
木木&点点 发布时间:2019-3-25 18:21
本帖最后由 点点&木木 于 2019-4-12 11:28 编辑 . p* z! O/ S9 D, H* U# \

# P+ u2 B. g) J+ x, W( G
介绍
我们是Polytech Sorbonne的3名学生,专门研究EI2I(电子和计算机科学)
我们项目的目标是创建一个由多个传感器组成的模块,以帮助来自其他专业(AGRAL:农业食品行业)的学生进行种植园的后续工作。
微控制器在屏幕上实时显示传感器发送的温度(空气和土壤),湿度(空气和土壤),光度等信息。同时,它可通过LPWAN网络Sigfox将数据发送到在线服务器Ubidots。
& g7 I& s% t7 P9 W2 H$ S4 d
1.jpg
( V/ R' ^+ @9 b
硬件组件
DHT22温度传感器             ×     1      
STMicroelectronics STM32 MCU Nucleo-32板   ×     1      
TSL2561 - Lux传感器   ×    1      
TCS34725 - RGB传感器 ×    1      
SSD1306 OLED - 屏幕  ×     1      
SigFox   ×     1      
DFRobot土壤水分  × 1      
模温探头           ×         1      
Li-Pro Rider Pro   ×      1      
Accu Li-Ion 3,7 V 1050 mAh   ×   1      
太阳能板     ×  1
; ^* }  r" T- n# d7 O6 m
1)使用传感器
·  1. DHT22 - 空气传感器的温度和湿度
2.jpg
DHT 22
3 A" h8 n* Q4 r6 r
DHT22温度和湿度传感器通过串行端口与微控制器通信。传感器经过校准,不需要使用任何其他组件。
电源:3.3至6 Vdc
测量范围:
- 温度:-40至+ 80°C
- 湿度:0至100%RH
精确:
- 温度:±0.5°C
- 湿度:±2%RH
- P. ~" B4 O$ y
为了使用这个传感器,我们使用了Mbed中的预定义功能
·  DHT22.h
/*5 g' C9 B0 M: `1 T- f
*  DHT Library for  Digital-output Humidity and Temperature sensors3 Y" l+ @" O: c- F2 O
*" _4 C, G" F: a9 B
*  Works with DHT11, DHT21, DHT221 X7 M- v$ N+ N& {) v0 i" s
*             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)) ]2 P3 f4 s4 Q3 A9 D& T; Y
*             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
, C1 e6 [$ @( u* g. q7 s
*             AM2302   ,  temperature-humidity sensor
& L& k8 d/ Y( q. n
*             RHT01,RHT02, RHT03    ,  Humidity and Temperature Sensor         (Sparkfun)/ ~4 W( _* {' I1 D
** X8 V6 D: c# M3 J% G8 Q* @
*  Copyright (C) Wim De Roeve6 y# ?  S" p/ z3 [
*                based on DHT22 sensor library by HO WING KIT
+ v! G# t( v$ A( s
*                Arduino DHT11 library
2 G9 N1 W  G3 F/ k
*
9 h+ f* m. S& i" t1 V3 O5 l9 h
* Permission is hereby granted, free of charge, to any person obtaining a copy& M! h1 h! {- G0 W, \: j/ E( m
* of this software and associated documnetation files (the "Software"), to deal2 k2 c* E+ o# @$ ?4 e* ~
* in the Software without restriction, including without limitation the rights; N. m$ |% k( \9 {" Y! n1 M
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell% k8 N5 \1 x- S
* copies of the Software, and to permit persons to  whom the Software is
+ d  G! e( f0 H! X+ u- l% ?3 z
* furished to do so, subject to the following conditions:
3 {9 r5 w( M. [
*
* h' y  v5 q8 m: P% X
* The above copyright notice and this permission notice shall be included in, f8 y2 f& l9 F  w
* all copies or substantial portions of the Software.
% g" W# @& o8 X' P( h6 M
*
1 C1 j. t$ R: c2 c' T  L' t0 a
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6 x! h5 ^' Q' M' p8 ]' x9 _
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,3 t" a" o( e: j# \! D2 D/ l6 F
* FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE/ m6 o. Y' v1 Q4 c+ ^# p
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
. q7 @% \$ U9 _1 k  U
* LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,8 D$ R( h  Q" `
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  m  G8 b3 N2 A$ t+ w: d8 e  H
* THE SOFTWARE.
: t/ z9 v" w/ L1 @
*/4 E+ e9 V' H' n; S& D, p" ]
#ifndef MBED_DHT_H& a* u/ J9 a5 B. O
#define MBED_DHT_H
- m$ f& ]- I$ U( B' K- L
#include "mbed.h"
2 B4 r4 H/ I7 h! `2 P
typedef enum eType eType;8 A' N8 `- J1 {' r4 ?6 k! `
enum eType {, d4 f4 o' C3 i, U+ @( _
   DHT11     = 11,$ C* F4 }7 H8 `5 P/ t- }7 x2 [
   SEN11301P = 11,
& g4 S( o9 O# ^3 y1 y$ N" O
   RHT01     = 11,& F) _# F2 Y' I; C
   DHT22     = 22,
  R7 u) a) u6 D/ q) p
   AM2302    = 22,0 s( e( t1 C  B3 t
   SEN51035P = 22,8 Y: l: B0 c  @$ O! V3 X
   RHT02     = 22,
5 S6 X6 h. t: X8 T0 M
   RHT03     = 22  ^, f8 ^# U' a% V7 m; i* W! D
};  ^+ c0 k  R8 e% R
typedef enum eError eError;
( c# v' B# N. E( b% F$ A
enum eError {6 B. y/ y- d, Y0 r+ O5 z
   ERROR_NONE = 0,5 n" F# i6 n2 L/ q
   BUS_BUSY,
7 g9 Q% W7 M& R' a! E4 p
   ERROR_NOT_PRESENT,$ U* W$ f' C# R7 F3 s3 P% E
   ERROR_ACK_TOO_LONG,
# F- i4 Z1 U4 n/ v9 p
   ERROR_SYNC_TIMEOUT,
/ F% |9 ]1 A- Y4 _6 {% B
   ERROR_DATA_TIMEOUT,
7 H& ]; N7 f) b$ z! q
   ERROR_CHECKSUM,, r0 @/ p) j7 M  q& m" w% N
   ERROR_NO_PATIENCE; M$ n3 w  k0 U) t
};1 H! E  \/ P# @; v
typedef enum eScale eScale;' D6 L1 g7 d  h5 ?* z1 A" E
enum eScale {
& v* ~( L6 ~# h6 T, Q
   CELCIUS = 0,
9 _7 b  u' H; k; v
   FARENHEIT,- p  A7 R* G) I9 E& {* k! M: M0 q
   KELVIN
) ~" d. v+ B# A9 C, A, e0 r
};
9 G3 _- }6 I2 F- b
class DHT
9 v/ l  H8 s1 G* W" b" a% `$ S5 X
{
; s" P; }2 w" I* X, s
public:! C0 P0 [; R* t0 d7 A
   DHT(PinName pin, eType DHTtype);
# i' K# \1 l# b9 H1 M" v/ D" [
   ~DHT();
8 c( t6 F- q3 W! S: X, a
   eError readData(void);. [  K$ r7 j# m" k8 t4 `
   float ReadHumidity(void);
5 A4 y" P& o% y) ?6 q5 \
: N% b( N7 P* B# W# z
   float ReadTemperature(eScale const Scale);6 ^7 T! e; `( @5 t0 ?
   float CalcdewPoint(float const celsius, float const humidity);- m  c, M$ y$ {+ i5 e
   float CalcdewPointFast(float const celsius, float const humidity);3 ]- [8 z( O% i, _4 n
private:4 H  E) o! _% m3 k: F* F2 \
   time_t  _lastReadTime;' M) I% D- n8 }  s7 e, s
   float _lastTemperature;% Q5 @" @0 p) A  r2 I6 u& o
   float _lastHumidity;
8 e/ I0 @) T  O; ~/ T
   PinName _pin;! {3 O$ v& @0 g3 w6 X/ ^
   bool _firsttime;4 B( ~) x3 J# c/ U( ?
   eType _DHTtype;
  Z; }) l7 S( X' m; P) x6 \
   uint8_t DHT_data[5];7 R# ?6 ]  J, `* G4 N; {/ {$ ^6 x
   float CalcTemperature();  d; q& S6 j; s0 `
   float CalcHumidity();
4 d2 \% B1 ^3 f1 h1 t
   float ConvertCelciustoFarenheit(float const);
! o" H% B; Y0 z/ G
   float ConvertCelciustoKelvin(float const);- E8 Y  G% P/ S8 C
   eError stall(DigitalInOut &io, int const level, int const max_time);$ Q4 d) r/ }( V6 e% C
};
# d! x& y3 P! R3 e0 X) u
#endif
DHT22.cpp
#include "DHT22.h"# T# \& }3 `" B, j6 P  d
DHT22:HT22(PinName pin) {" F  h8 D  d4 d6 N* g
   _data_pin = pin;3 ?# F# K  v* }& D
}+ _8 p7 u: [" E6 ?" p: ~1 a
int DHT22::getTemperature() {, x. v; C! i% x: _6 `- i2 y( i
   return _temperature;9 o' h: w9 Y5 c9 I$ t; o
}
7 V  E. @7 T& ?; W' S
int DHT22::getHumidity() {
5 k% N/ r% b* s' s* U
   return _humidity;
  i1 Y2 ~  }% i, \
}# ^9 ^2 B: `7 i# G/ w1 e* V! \0 m
bool DHT22::sample() {9 {; c7 X  w1 c2 V7 `
   DigitalInOut DHT22(_data_pin);+ [$ \; S2 O, j9 _; H
   int dht22_dat [5];% B! M  d8 w/ e! a
   DHT22.output();) }: B3 G) o" Q* Z/ q( e
   DHT22.write(0);: X8 T. e; N0 T1 ^: V$ {
   wait_ms(18);
2 e& ~1 _7 X/ m! w( w
   DHT22.write(1);
. c& s; U& h" x/ D& a
   DHT22.input();2 Y7 f( H( C: e4 m
   wait_us(40);
& H' ~# P4 M" L5 i! B
   wait_us(80);" b) Q* b( [/ @8 X  I. h" W# A! w4 G
   int i,j,result=0;4 t/ c/ d5 y9 p5 a, \+ ~$ C% V
   for (i=0; i<5; i++) {
2 k* A9 n' I& P, w! D
       result=0;9 G$ i5 i* ]6 C9 u' h$ E. n
       for (j=0; j<8; j++) {
3 n" z  \' d9 W2 t. Q
           while (DHT22);. y: t+ }+ V; [6 t+ u5 X
           while (!DHT22);* [7 ~, [, H( o: C% p
           wait_us(50);
' L7 q& F( e! W1 i6 A$ z3 x
           int p;8 J: ?/ M" [5 R/ V( ~1 G( _0 m
           p=DHT22;" E- f" m; @+ w6 Q  u
           p=p <<(7-j);
+ I- o' J+ R& X+ ~& ]
           result=result|p;
5 a4 b: _) T' k/ s: t
       }+ w: G* u" E4 O, p
       dht22_dat = result;
# x2 ]5 i- c+ I( w1 [- z+ [
   }
" B5 r" ?. H) G
   int dht22_check_sum;2 G5 E, \8 g0 E% W
   dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];/ {3 H& t% T7 \5 ~" E8 \  A/ i
   dht22_check_sum= dht22_check_sum%256;/ V1 S0 M1 d1 K) C- b
   if (dht22_check_sum==dht22_dat[4]) {
6 e$ R& i2 @5 t6 Y
       _humidity=dht22_dat[0]*256+dht22_dat[1];9 u/ Y) n4 b# d) `7 ^( n
       _temperature=dht22_dat[2]*256+dht22_dat[3];7 B$ C; T" I' ]1 O3 ^* D0 H: o
       return true;
8 Y8 u% s( c/ e# d
   }
9 |" O: S! l( O, f( Z; W' N  J
   return false;0 J  J: x, v+ m  D) |
}
首先,您需要定义传感器所连接的引脚。
   //DHT22 (Temperature and humidity of the air)   #define pinDHT22 D9 //PA_8   #define modeleDHT AM2302
+ V) v8 j5 B+ }( n/ T
现在您可以使用传感器的功能。
#ifdef MesureDHT22void mesuresDHT22(){  CaptDHT.readData();  TemperatureAir = CaptDHT.ReadTemperature(CELCIUS);  HumiditeAir = CaptDHT.ReadHumidity();}#endif
2.亮度传感器
3.jpg
TSL2561
5 V* @' d) K& D- v  \
基于TSL2561的亮度传感器,可测量0.1至40000 Lux的亮度。它与微控制器通信。
电源:2.7至3.6 VDC
TSL2561.h
/*
/ a6 c8 k4 P% ^1 |& X. f3 y8 q
* mbed library program
& U% x1 }" g" P8 @/ U* B& l6 V; }+ h
*  Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output)& K8 P; C+ e% `6 K9 R: T) L  K0 l
*  TSL2561 by Texas Advanced Optoelectronic Solutions Inc.
" M; [* R% y! |' ~* v; l
*6 @9 W+ A" s2 i/ y- ?( J
3 j, R( N4 v( o+ h" w4 o1 _3 r( h
#ifndef TSL2561_H
& \; U# c% |0 h: \# F0 g) C$ _5 U
#define TSL2561_H
3 s- P( d/ c: _( P+ t% i- R( G
#include "mbed.h"" f$ A3 {! C6 U( a$ B
// Luminosity sensor, TSL2561. G: X1 a9 Z# O! k' Y
// Address b7=0,b6=1,b5=1,b4=1,b3=0,b2=0,b1=1, b0=R/W
& t9 |; V+ Q8 t* z* [
#define TSL2561_ADDRESS_GND         (0x29 << 1)/ u; Y8 s3 R# w2 A2 b& K: E5 U' j
#define TSL2561_ADDRESS_FLOAT       (0x39 << 1)+ ^  ?- k, @7 e! y, o# c# |1 A
#define TSL2561_ADDRESS_VDD         (0x49 << 1)
4 z7 b( X% @3 M) I6 W" ^4 {% `
////////////// Registers //////////////////////////////////6 M4 f7 E! w3 e) x  U) e6 _: E, [- S
// Register definition( J9 H# e  G0 |4 P: X" X4 }
#define TSL2561_CONTROL             0x00
# H& d, e* w! d8 ~
#define TSL2561_TIMING              0x01) a) E" f/ h! M/ I) U8 a
#define TSL2561_THRESHLOWLOW        0x02
& w9 o  B7 [1 F0 d! r
#define TSL2561_THRESHHIGHLOW       0x04- ]6 D" S# b2 N" b9 v. O
#define TSL2561_INTERRUPT           0x06
/ m! O2 z6 P8 b; L3 x% `, M
#define TSL2561_CRC                 0x08$ ?- V1 G: a' ^! D; ~0 U
#define TSL2561_ID                  0x0A# h/ Z/ ~1 R! _8 C) E4 g9 A
#define TSL2561_DATA0LOW            0x0C
& S" v0 L% I: ]6 L' O3 ]# I6 ?
#define TSL2561_DATA0HIGH           0x0D5 e5 z. [* s5 `6 g3 G* E
#define TSL2561_DATA1LOW            0x0E
/ ]0 k6 v# ]1 f. ?* e
#define TSL2561_DATA1HIGH           0x0F
& }, I. T5 k3 h0 Z0 F, f5 f
////////////// TIMING PARAMETER ///////////////////////////
5 N6 r4 ~9 Q- l  q( Q) P
#define TIMING_GAIN_1               (0UL << 4)
# D9 Y/ I% Y( y
#define TIMING_GAIN_16              (1UL << 4)0 x8 S! T+ V0 l, M( f( Z; \
#define TIMING_TIME_13R7            (0x0)" H5 \- v0 _5 n( u0 |3 y* J2 g  ]6 C
#define TIMING_TIME_101             (0x1). v4 J& X7 {- s, l
#define TIMING_TIME_402             (0x2)' a$ n: E3 `" t# i+ ^# S5 l! f
#define TIMING_TIME_MANU            (0x3)3 F  w* E) ~4 `8 f- v5 \8 I& G
#define TIMING_DEFAULT              (TIMING_GAIN_1 + TIMING_TIME_402)
) Y9 ~5 \# v8 W2 Q2 V
////////////// ID /////////////////////////////////////////* t7 z7 y% W# q
#define I_AM_TSL2561                0x50
0 r' o/ r) i2 i$ E( K
#define REG_NO_MASK                 0x0F
" F0 a  ?+ i7 h* g8 E
////////////// COMMAND ////////////////////////////////////
: B2 ~. q$ M( P9 o4 ]- }9 h* |" J

2 S! g$ o( x( B  @$ b+ l
#define CMD_CMDMODE                 (1UL << 7)
8 r! Z8 I( {9 M
#define CMD_CLEAR                   (1UL << 6)
/ k: h; @# G9 t/ C- c
#define CMD_WORD                    (1UL << 5)2 g$ u$ m' j3 w/ E
#define CMD_BLOCK                   (1UL << 4)3 a; _; F0 N) b# e3 A
#define CMD_SINGLE                  (CMD_CMDMODE)% o$ j( J0 |* ~2 y" h" A- l% J
#define CMD_MULTI                   (CMD_CMDMODE + CMD_WORD)
- p) p" ^5 ]! _! G6 C
/** Interface for Luminosity sensor, TSL2561
6 V/ ?* D( a' V2 k, }5 A
* @code
$ `) X+ b3 |2 ]1 L3 n+ z
* #include "mbed.h", c! k5 c" t8 e) ~
* #include "TSL2561.h"
" j5 ?0 v* A6 b6 F& K3 X( G" ^
*
' C: H7 n0 O9 R% k( \! D  t
* // I2C Communication0 W1 ]# [  f. M9 z" }% y
*  TSL2561      lum(dp5,dp27);    // TSL2561 SDA, SCL
: o- J: g* }* J' k9 G. m+ S2 [, m
* // If you connected I2C line not only this device but also other devices,& A+ }* S2 w3 c# d$ @7 L$ }# Q
* //     you need to declare following method.
! v# n! J% H& A! r* N7 s) m2 O

+ i  P2 Y: p6 B4 B
*  I2C          i2c(dp5,dp27);    // SDA, SCL7 O6 U/ D3 Y5 c0 O3 F
*  TSL2561      lum(i2c);         // TSL2561 SDA, SCL (Data available every 400mSec)
7 P/ K* R$ d, b+ y9 ^
*
! ?" j2 i# n7 c. }
* int main() {;- F! T$ p9 h6 G9 N
*   while(true){4 h7 L; S: {4 _" S9 x- h( w
*      printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux());7 V" T4 H# ]# B! W+ g' x
*      wait(1.0);
1 b" B2 s5 z# v/ u
*   }# s4 F5 Q* f6 I8 g, S2 a
* }
. n. N# p2 x. Q+ p) f
* @endcode
+ i. P% e2 k; z  L9 w; ]0 J4 p$ ~
*/
/ y) w8 [- E9 D
class TSL2561. U3 C. x2 J- Y( A
{
( w. k: a# }# |
public:
. S6 [, R0 D0 b9 X/ D
   /** Configure data pin
( p( U% J$ A3 c4 g, C: `% f
     * @param data SDA and SCL pins
5 B6 {6 j5 i9 w6 U2 o* x
   TSL2561(PinName p_sda, PinName p_scl);+ R! y3 g6 s4 A% C, u3 a: a2 G- X
   TSL2561(PinName p_sda, PinName p_scl, uint8_t addr);/ g( R4 t0 j# ?' J  u2 m, P) A$ Y' v
   /** Configure data pin (with other devices on I2C line)
( j" Y4 W. ~& C' E- G$ w8 Z
     * @param I2C previous definition
" E, u+ [. K+ P3 c
     */
& l( N  c, r& P  Z, U* H6 V
   TSL2561(I2C& p_i2c);
4 e3 l  V9 p' e. o
   TSL2561(I2C& p_i2c, uint8_t addr);5 Y: y( O/ d- C, O& C- |5 y
   /** Get approximates the human eye response
$ T0 [  n  C* Z- s5 `2 M0 R
     *  in the commonly used Illuminance unit of Lux
3 J) q) E8 u4 R. y8 V, c
     * @param none
9 K1 p4 `  w, I- P8 h* ^2 N
     * @return Lux8 {4 ^8 w. r9 W& H4 p& A
     */9 I' |! p; g" b
   float lux(void);
. }9 L0 G9 B" x3 s
   /** Set timing register
+ a2 u; b% C- S7 b
     * @param timing parameter& d! U, B  O( ?, I# X6 }
     * @return timing read data; O. g. I  U. i) q  [1 l# z3 u
     */
" ~$ G+ }, o: c+ V: p0 ^& p
   uint8_t set_timing_reg(uint8_t parameter);
3 m  c" d5 n9 ?, U" E. R
   /** Read timing register
% O" \  J! ^+ F# n! B6 R
     * @param timing parameter
  Y3 y; j% [9 \0 j
     * @return timing read data. m/ _" |9 b3 n6 L* a0 T* I. f$ Z
     */3 O1 m( h, p* s; c% Y5 Y
   uint8_t read_timing_reg(void);
. ]- U9 B- L2 d$ [7 `
   /** Set I2C clock frequency
; V; i" C, w( ^; O! {- n; K; ~
     * @param freq.9 O8 K+ F! U$ l0 N- A
     * @return none
+ m! {8 W6 _7 Y8 l+ u
     */% Q. p2 z. Z+ ~
   void frequency(int hz);" C' f7 K# q' i
   /** check Device ID number
6 q' |( o. q8 `8 R) H$ e; ~  I) j5 ?3 n
     * @param none
( Z) C0 X% `2 K% v
     * @return TSL2561 = 1, others  02 _( t* D- q/ `
     */& b: U2 r6 h, v8 S8 F) z3 N9 C
   uint8_t who_am_i(void);& Z- Y$ n1 q+ i
   /** Read ID and Revision Number
; p6 A5 f! R3 }; C' c7 W
     * @param none" T( ?4 P, x( Z1 n$ d) u
     * @return ID + REVNO
# i/ Q: R3 T3 ~) I* L+ v2 M
     */
9 _5 K7 L( D7 b% Q9 @
   uint16_t read_ID(void);
! g( u0 e% b  e$ W* T& B
   /** Power Up/Down
% E# b. [/ U; q0 |# t
     * @param none
' L5 T* e, B- m' r. y- H
     * @return none  m4 r5 o% L" K! _( i6 k4 @9 s
     */
! G& l7 \9 O7 k* w' y: C1 t; t7 D6 L
   void power_up(void);
1 w' L8 k" M1 V8 f! t
   void power_down(void);) u5 D6 O4 g: H
protected:3 i* H" n, p0 G/ D5 E  x
   I2C *_i2c_p;
4 e! F$ [! y& }6 N- n
   I2C &_i2c;
" m, c: G0 s' L/ K: f7 t  u
   void init(void);
: {1 }9 C% O" ~0 K9 B" X
private:9 a0 I# X/ r2 t3 k
   bool TSL2561Initialised;
& f% t1 H6 r9 t* M
   uint8_t  TSL2561_addr;+ z. T, g9 y9 T1 g
   uint8_t  dt[4];
0 s. W9 b8 S( q! e0 C
   uint32_t ch0;1 t: V! `+ }6 w' A, ]+ t6 p
   uint32_t ch1;. ?3 W. c9 k) y1 @) m
   int8_t   gain;$ X# v5 l$ @  o0 s( s
   uint8_t  id_number;( A1 b/ X9 A" V" k% w% F
   double   integ_time;
) g" f% ?, S3 a9 R5 A
};" ~( ^. a6 R. O, i1 |/ j- E
#endif      // TSL2561_H
( J" R: J3 ]  ~, q, U0 i' X& g9 T; N
TSL2561.cpp
/*1 ]7 I- }# O/ G! M
* mbed library program: r& K# i! t6 P- b0 I* x
*  Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output)& W2 `: }( d$ |5 K$ a& C& Y$ f# s+ z
*  TSL2561 by Texas Advanced Optoelectronic Solutions Inc.( y+ O! i  F3 \
*2 U0 O& _3 R; V& k

3 a; g$ l! R$ k9 h* j% T7 X
*      Created: Feburary   21st, 2015& n4 s$ q( ]; N! I
*      Revised: August     23rd, 20170 N" L8 u$ t4 c( c9 l: t
*/  Q& X! Q3 T* e
#include "TSL2561.h"
& g, r+ }# \- T' l6 F
TSL2561::TSL2561 (PinName p_sda, PinName p_scl); X$ `' U' m7 ~& D$ q: F
: _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
3 U" ?( r; R( ?5 d
{   TSL2561_addr = TSL2561_ADDRESS_GND;   init();}  ^+ |- n4 }# o3 Z$ P! |
TSL2561::TSL2561 (PinName p_sda, PinName p_scl, uint8_t addr)# e3 a! H" \5 c' w* j
: _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
7 p' M9 A. ^* i
{1 A! H4 h1 F0 @4 z$ `8 W4 N
   TSL2561_addr = addr;  A6 o# [: d; s$ K
   init();! }9 X* {7 N6 @/ {/ v: M# K
}
* o! S0 |3 h3 g( T
TSL2561::TSL2561 (I2C& p_i2c)' |/ k, h7 k- z3 O( E
: _i2c(p_i2c)1 v) O% W$ N! h; [
{9 T& s$ U% m8 x. g, ?2 Z  ^
   TSL2561_addr = TSL2561_ADDRESS_GND;
/ o# ~7 Q, N, t# z
   init();
8 e- l/ R1 u% ~$ o2 Z
}
" P7 S6 g  J% ~% V5 T
TSL2561::TSL2561 (I2C& p_i2c, uint8_t addr)) a8 f" y& b% a7 N3 N7 I. O
: _i2c(p_i2c)
" d. G, i7 k  O
{
3 H$ p2 K3 _9 |) {& D
   TSL2561_addr = addr;0 Z2 B4 m( q) s7 x7 {
   init();8 q3 a& [9 A; _! O% B
}
  z9 n6 L; ], Z7 B0 ^$ I
/////////////// Read Lux from sensor //////////////////////
6 C& d1 @! h# G0 W7 i# e# v% Q
/*# ]$ [" K1 C0 D3 q* U5 ^. U+ U9 c/ J
For 0    < CH1/CH0 < 0.50 Lux = 0.0304  x CH0-0.062  x CH0 x ((CH1/CH0)1.4)+ }) R, X! V) V0 N# s+ R: t
For 0.50 < CH1/CH0 < 0.61 Lux = 0.0224  x CH0-0.031  x CH1
5 |" t  a" s  o% t# l/ Y, f
For 0.61 < CH1/CH0 < 0.80 Lux = 0.0128  x CH0-0.0153 x CH1
/ e1 ?' n, n( C' Z$ r9 @( C" o
For 0.80 < CH1/CH0 < 1.30 Lux = 0.00146 x CH0-0.00112x CH1
5 S. U  j! K; Q& o9 }
For        CH1/CH0 > 1.30 Lux = 0/ [9 m+ }8 L+ e
*/( H. A0 M6 @# J. v
float TSL2561::lux()( D; Q, j5 K. F
{
% {, Q2 }; S' o' s- F; e1 S
   double lux0, lux1;
. O' n% E6 R0 U9 ^* e+ f# z% v% x
   double ratio;
* V  V& h* ^1 ^  O- P+ s  m8 @
   double dlux;
- l! U7 X0 r2 z! ~8 z
   dt[0] = CMD_MULTI + TSL2561_DATA0LOW;
! x+ R; j5 {: b1 g
   _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);1 x( L# |) {  b# o$ \( J! w2 t, g
   _i2c.read(TSL2561_addr, (char *)dt, 2, false);! x* |/ }& d+ r$ x7 C3 [" W
   ch0 = dt[1] << 8 | dt[0];
7 k. F: E! r. z3 h; q3 x( K$ j5 u
   dt[0] = CMD_MULTI + TSL2561_DATA1LOW;7 J5 ^! y+ P- V' i) |/ [
   _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);% s/ N9 ]) D0 a3 U. n9 E: x
   _i2c.read(TSL2561_addr, (char *)dt, 2, false);
, J3 d% j( H  ]. ?2 @7 L0 l
   ch1 = dt[1] << 8 | dt[0];( I# c" L, [2 G, {
   if (ch0 == 0xFFFF) {  S! M' k8 l6 }  F; T; w
       return 2500.0;
7 e) g/ G% B) ]4 E( ?
   }
8 v- k; j% [  G" y+ b0 p& k
   lux0 = (double)ch0;$ P: }) B- @1 j) {* k6 S
   lux1 = (double)ch1;
+ P! u% X7 r. W+ g) n, e& M
   ratio = lux1 / lux0;
! v! a0 Q' K, w* s1 }4 @+ S4 V( p
   read_timing_reg();
5 m" D: J6 P0 W$ D+ \& h
   lux0 *= (402.0/integ_time);
* q4 ?# \% h- q" |1 ^) m& y5 j& l) |
   lux1 *= (402.0/integ_time);6 y3 W( y/ N! @* C
   lux0 /= gain;' V4 ^5 I! `0 v% B
   lux1 /= gain;( r  J& r- M: `+ b
   if (ratio <= 0.5) {
: [6 o+ Z* [: G! b+ z$ C
       dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4);- s  Z( I. g2 c3 u& ]
   } else if (ratio <= 0.61) {
( M6 C, m/ n5 d1 |( Y! R# o& G4 K
       dlux = 0.02240 * lux0 - 0.03100 * lux1;" o. C$ C# m) j+ N: U
   } else if (ratio <= 0.80) {5 O# X8 m. L. r! M  u) u
       dlux = 0.01280 * lux0 - 0.01530 * lux1;( p& p; F$ T& o* d. E. h* X# x* v
   } else if (ratio <= 1.30) {
; }# P8 v" `8 ~% `
       dlux = 0.00146 * lux0 - 0.00112 * lux1;
3 F/ j# n& `7 [8 ^# u2 N" x
   } else {; h! h2 K0 I, B8 y! P
       dlux = 0;
9 I8 T. `! X7 H3 ^3 n
   }
' W, L! t2 Z! B1 B3 S* `
   return (float)dlux;6 m5 l0 N& W- `7 J
}
# r7 c* |3 }* L, F( q4 z$ C
/////////////// Initialize ////////////////////////////////1 i5 G6 C8 Z& H
void TSL2561::init()$ o. [4 E$ w9 v
{
+ R* H$ g8 j. y! R, T5 O$ f. c
   _i2c.frequency(100000);
+ z6 i, y& f0 @' X
   power_up();' p% N5 w9 c- s9 q2 h
   set_timing_reg(TIMING_DEFAULT);
. o8 G. M' s* l# O. d9 l
}2 C* B2 o+ l: {, p, D/ H+ N) I
/////////////// Timing Register ///////////////////////////! D' z; g9 z  B2 H+ C/ e5 Z& I
uint8_t TSL2561::set_timing_reg(uint8_t parameter)
9 n) H# B! n6 r
{$ M: }% K: Y$ ]8 A. w! C, Z& b- {
   dt[0] = CMD_SINGLE + TSL2561_TIMING;
, ?6 S4 T" _0 I, p  ~! f
   dt[1] = parameter;/ S" n3 N% _6 O8 U
   _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);1 U# R& e3 |1 V. ]  R
   dt[0] = CMD_SINGLE + TSL2561_TIMING;1 c) H1 @6 Z6 F
   _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
: W; y/ i* ^0 T4 u0 I' \. ]
   _i2c.read(TSL2561_addr, (char *)dt, 1, false);" O  i7 w  t1 `
   return dt[0];
- o' n! g% M& Y8 W( [' k
}0 Z$ Q3 \5 u# r( `! M" {
uint8_t TSL2561::read_timing_reg(void)5 E( e- V$ D! M8 R$ N, F
{5 R6 O/ v1 j$ [" w9 H
   uint8_t i;
% C- j, p: M& s8 k
   dt[0] = CMD_SINGLE + TSL2561_TIMING;: E' P  s) o# w, R; f+ s3 d
   _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);7 w6 t, t1 h; q/ ~8 D
   _i2c.read(TSL2561_addr, (char *)dt, 1, false);+ {- m0 w+ L4 [: h+ c
   if (dt[0] & TIMING_GAIN_16){" m: e- }  f0 n
       gain = 16;
- V/ ?4 F' y6 w8 }- L% [/ j9 ?
   } else {  r, c! [5 n. _7 E+ O( C
       gain = 1;  }( k8 i. E& S5 p# U
   }" ^% y6 S: V1 N3 Z/ y+ n- R. {
   i = dt[0] & 0x3;
! A% {; d- J: R
   switch (i) {
- S& f( O: P( m. X) D  K9 H
       case 0:6 B) K1 f. K1 y, m# n6 d! D
           integ_time = 13.7;. [: L# X0 G% b1 r) X$ K: S& w
           break;
! w6 x* }$ M- Y7 s9 k' Q. }
       case 1:
  W# }) h8 q5 c/ O
           integ_time = 101.0;
3 O, u& ]5 D' x  r+ j" a
           break;
1 L' _1 [! Y1 w7 [+ i( }3 T7 J
       case 2:
# L/ V" Y) I9 Y  a2 X1 U* [8 l+ q
           integ_time = 402.0;
' F+ u3 Y- l3 a
           break;
$ M. J0 q- J% ]' o; s5 k
       default:- J8 `8 i6 j- Z
           integ_time = 0;
5 f  I; `/ V5 f' m2 {
           break;   }   return dt[0];}( E$ J8 N4 J% G9 M2 k/ Q0 |
/////////////// ID ////////////////////////////////////////+ ?1 N1 {! K/ G# L; A
uint16_t TSL2561::read_ID()* I1 m% ~3 _( I% h/ d
{; e/ p$ H3 @& b7 Y) Y6 K
   dt[0] = CMD_SINGLE + TSL2561_ID;% ]: _# j: x! _4 ]7 {. l8 ]
   _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
/ \( U  [! Y, D! N  x
   _i2c.read(TSL2561_addr, (char *)dt, 2, false);! _4 t6 \: G. [- w3 s
   id_number = dt[0] << 8 | dt[1];( {' h$ y5 `- ~
   return id_number;
# V, Q  K+ W0 q0 Q1 {. W
}
" s, o; r+ I9 F% g( D- B/ c
uint8_t TSL2561::who_am_i()9 Z$ \* z, P* Y, ~& H
{
5 Z1 O+ n% i& l) V6 D/ n5 P
   read_ID();
- q( M1 t2 P4 b2 z* W
   if ((id_number >> 4) == I_AM_TSL2561) {. q* ]% I3 t# [0 |6 N4 p' A
       return 1;
# B; S( c9 S# {$ l
   } else {, R8 v* H- J2 p4 G* B: x
       return 0;
1 g6 b* s. t0 r8 k2 L- r
   }# l5 j5 D8 b' E' P+ e4 H2 n& L
}
2 I8 g% S: ]4 |9 a: d5 V0 Q
/////////////// Power ON/OFF //////////////////////////////$ j3 s7 C+ v7 y- C" l0 G9 k
void TSL2561::power_up()! s8 W1 @) F' {5 |
{
- A( R1 _1 `/ n) `
   dt[0] = CMD_SINGLE + TSL2561_CONTROL;
6 R" [6 g8 p+ \% m6 x1 V
   dt[1] = 3;
5 d8 _( \+ D  I- L4 H+ X8 T* Z/ Y3 N
   _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);
* Q1 |  p$ e( e" L- h' N
}
3 T. G" V7 y9 s2 L5 Y6 _; \
void TSL2561::power_down()# \- L& x& F* z0 g* S
{' a4 C" X; c- x$ r& _) Z6 ?& v: p1 r
   dt[0] = CMD_SINGLE + TSL2561_CONTROL;- ^1 q9 ~3 |% k- b5 K3 ?0 P/ D
   dt[1] = 0;
+ Y* N+ A" l* j/ P: {
   _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);! [3 V( k# @' z; b" e
}" w) s1 x( A" {
/////////////// I2C Freq. /////////////////////////////////2 ~( i: t4 A7 r1 i
void TSL2561::frequency(int hz)
. O) {! Q, ]. {& s; k
{& w! [/ g. q! y0 d9 g
   _i2c.frequency(hz);9 ~5 E( ?+ V9 d$ H3 u2 o# X  `
}
3 T/ v0 \9 r* o. w( M# a
对于此传感器,您需要选择用于营养和I2C的引脚(SCA,SDL)

4 W$ V" R3 H1 @: c
   //I2C   #define pinSDA D0   #define pinSCL D1#ifdef MesureIntensiteLumineusevoid MesureLux(){   //int i;   Lux = 0;   for(int i = 0; i < NombreDeMesures; i++){       Lux += COEF_TSL*Lumiere.lux();       wait_ms(DelaiEntreMesures);   }   Lux /= NombreDeMesures;}#endif
3. RGB传感器
4.jpg
TCS34725
/ k4 o# A( ^- G; f( c$ ], a
该模块用于检测光源或对象的颜色。它基于TCS34725传感器,通过I2C端口进行通信。传感器用于检测RGB颜色和白色。
电源:3.3至5 Vdc
I2C协议
#if defined(MesureIntensiteLumineuse) || defined(MesureRGB)I2C i2c(pinSDA, pinSCL);#endif#ifdef MesureRGBvoid mesureRGB()
( v3 ^7 ~8 ^. A8 `{  CaptRGB.getRawData(&Red, &Green, &Blue, &Clear); & \  U0 x: N# N: k2 o( {- V
uint32_t Sum = Clear;  RedValue = Red; RedValue /= Sum; 1 n, E) ?+ K* @7 g& j8 s
GreenValue = Green; GreenValue /= Sum;  
: y! E6 \$ g3 c3 s/ \$ @  V1 hBlueValue = Blue; BlueValue /= Sum;  
) E6 L6 `3 z5 \RedValue *= 256; GreenValue *= 256; BlueValue *= 256;}#endif
4.地面温度
5.jpg
温度探针
温度探头是一种精确的防水温度传感器,工作温度范围为-40℃至105℃。
与其他温度传感器不同,它可以精确地报告温度,并且不需要任何其他组件。
   //DS1820 (Temperature of the ground)   #define pinDS1820 D12#ifdef MesureTemperatureSolvoid mesureTemperatureSonde(){  TemperatureSonde.convertTemperature(true, DS1820::all_devices);  TemperatureSol = TemperatureSonde.temperature();}#endif
5.地面湿度
6.jpg
传感器土壤水分
我们的土壤湿度传感器通过电容式传感测量土壤水分,而不像其他传感器那样测量电阻传感
该模块包括一个板载稳压器,工作电压范围为3.3~5.5V。
   //CSMS (Humidite du sol)   #define pinCSMS A0
/ h& w2 j6 m, g" j$ H  p// Mesure de l'humidité du sol#ifdef MesureHumiditeSolvoid mesureHumiditeCSMS()
6 O! q, Y( [( O" z1 ^2 V, R{ . l2 J/ l  \  a/ t- s& Z
  float a = -0.005312; . V8 j" E/ q1 l$ r& Y
  float b = 0.92265;  2 Z  ^+ c5 C7 K7 j* _
  float SommeMesures = 0;  
$ z& B2 x/ e2 {; w* `) m: O  for(int i = 0; i < NombreDeMesures; i++)
# B3 A: n! ~& e7 E: M# c, H; l   {     
6 k9 M' W! O3 }5 r0 F      HumiditeSol = CSMS.read();
+ ~4 o3 r% [+ d' J( {: F      HumiditeSol = (1/a)*(HumiditeSol-b); ( @, H3 _& i6 l7 e: s4 \
      if (HumiditeSol < 0)  
% b2 S+ O2 H$ S# v7 A' ]) X3 T        HumiditeSol = 0;   
1 D* {" D; f7 i2 d      if (HumiditeSol > 100)   
6 H, w2 V% F, q/ t* Q2 D       HumiditeSol = 100;   
3 U0 O, `' z7 Z( S      SommeMesures += HumiditeSol;  
7 {% |( ~  M% w4 S& a* `      wait_ms(DelaiEntreMesures);  
! f5 _; {3 ?  r, h" C   }   
+ K5 f. x. l6 D7 i* A  SommeMesures /= NombreDeMesures; & t6 C- z. A# g- Y* A6 I8 s: S+ ]
  HumiditeSol = SommeMesures;
! A1 u; v0 U/ f5 n5 Y8 N}; }+ p3 h  x" l$ u" p' v" z
#endif
, Q* Z- {. D0 w9 ~7 h' d1 o% Q

/ V9 K* S# K$ o# J" ~
6. STM32 Nucleo-32 L432KC
7.jpg
STM32 Nucleo-32 L432KC
所有传感器都连接到此卡,如下所示。
8.jpg
1)守则
为了开发这个的项目,我们使用了Mbed编译器,这是一个互联网上的软件,允许我们在任何计算机上工作,非常有用,在家里就可以完成项目。
9.jpg
Mbed编译器

! Z9 C' `8 R2 P5 x- e- ^
我们开发了一个非常结构化的代码,以便能够非常轻松地进行调试。使用了一些#define和#ifdef,因此您可以通过仅在头文件上注释一行来轻松关闭传感器,该头文件包含许多全局变量,例如引脚名称,其中连接传感器以快速更改它。
Mbed的优势在于每个传感器都有许多的库。
程序将记录每个传感器的值,然后将它们显示在LCD屏幕上,并使用Sigfox发送这些记录以便稍后显示。
Mapping.h
#ifndef __MAPPING_H__
! C! d4 h$ O$ B2 L. m, P% \   #define __MAPPING_H__
8 z, t1 O0 a) _: [0 _2 @   // Commenter pour désactiver certaines mesures ou fonctions
/ \/ R3 d! L9 M1 j) m( w   #define MesureDHT22
* k6 {4 K! i' P: V1 y   #define MesureIntensiteLumineuse  //I2C
2 S, d7 g! o3 b( C8 U) }" Q   #define MesureRGB                 //I2C: h: r" P" n6 z6 w- Z! }: X9 \, E
   #define MesureTemperatureSol
: h, B- s* f7 \% @. G4 C$ O   #define MesureHumiditeSol
6 h8 V' f, c7 x0 a# ^   #define AffichageOLED
2 P& J( r  c/ K- d( C& U   #define Debug
# P# {9 K3 m4 D   //#define SigfoxActif% }* s* n  P4 Y  o
   //#define DeepSleepEnable
. o" O% B6 _$ ~; Z  e   //Moyenne de mesures+ j" A2 g! k% z' i$ P" ~* u
   #define NombreDeMesures 100 e7 B' `+ \( Y% [" U& Z
   #define DelaiEntreMesures 25 // exprimé en ms; M/ \1 s8 v9 u* w( q& D; J, s
   //Luxmètre
' ^/ k1 s" e4 X" W3 f" D) `   #define COEF_TSL 10% I1 ]. J" v' e  g! m( L
   //CSMS (Humidite du sol)( @7 `3 W) D8 R0 B' \8 \
   #define pinCSMS A04 [; t( O1 J+ V7 k- ^
   //DS1820 (Temperature du sol)
2 {$ ^: l/ m/ d7 E" y   #define pinDS1820 D12
- c8 S4 l) }$ z7 H   //DHT22 (Temperature et humidite de l'air)7 k. Y- V/ b. t  H5 h- w
   #define pinDHT22 D9 //PA_8& W6 C, F9 S, h3 `
   #define modeleDHT AM2302
  k2 d' ?7 x' a% X: n$ g   //I2C1 [$ f/ f9 D$ `% @. s9 i
   #define pinSDA D0
& ~* ]! Z/ i6 g) g   #define pinSCL D1
0 w8 Y, S' T& n* _0 G   //Sigfox/ n  [. E* q3 x8 T
   #define SigfoxTX D5
* ]9 _! n3 v- _. b- `0 H   #define SigfoxRX D4- b" ~6 x% o" L; k; T/ y  O8 B2 {
   /*#define SigfoxTX A7
8 x: g1 o% Z4 X* X   #define SigfoxRX A2*/
% a  p7 i+ h" I5 z8 N* b   #define DelaiEnvoi 30 //Délai entre 2 envois en secondes
! O8 e' u# W5 ]$ }( Y$ @   //Deep Sleep7 v/ l/ b" M% x3 Y
   #define DureeDeepSleep 3000 //Duree en secondes
8 D( n! b/ K( K( q   //Ecran OLED
& h5 Z' V* \5 r& ^   #define pinCS D6
0 h0 z' s" }3 y" s; T   #define pinRS D3. c/ p# t' W! N" K: h  }
   #define pinDC A5
/ S& }/ v( K( R$ s3 t5 H5 S% Q  I. j   #define pinCLK A4' c- |' Z+ Y. f. ~7 M
   #define pinDATA D2& c. m  Y2 _  b# {9 I' ]
#endif5 B% A  ~! U% R) y% f! s
Main.cpp
( l' e! P7 u0 e3 Y$ D& J#include "mbed.h"
4 _7 I! L5 Z/ s#include "mapping.h"
) y+ q5 w: c9 O( y9 M* [#include "ssd1306.h"2 |. e" a! Q! v) k) e1 K
#include "standard_font.h"" G8 o' F) W  F
#include "bold_font.h"
# |1 t8 O/ S+ ~) a7 @6 c#include "DHT.h"
4 p. l5 r# A, Z1 g% ~% v#include "TSL2561.h"
6 h+ M/ F- l5 x; t. I3 I- {#include "Adafruit_TCS34725.h"
( |& Q+ ?5 ?8 s#include "DS1820.h"
' g1 k( ]3 l& S* b7 v- q8 J#include "WakeUp.h"  " S# @2 H/ p0 [  F- k
/************************************
6 v% \( q3 h5 [8 }  {! \8 Y*     Declaration des objets
! B4 d3 u& a3 P3 F- g' u***********************************/
7 e/ [5 S8 {- b$ W#ifdef SigfoxActif+ T" f. I1 _$ N. {- i9 p% A1 e
Serial Sigfox(SigfoxTX, SigfoxRX);' W& @# B) O9 @* X2 y5 S
#endif
6 {  \: X1 Z' y6 f#ifdef AffichageOLED  K  w) {/ `! i- Q
SSD1306 oled(pinCS, pinRS, pinDC, pinCLK, pinDATA);% x* H. ]0 ?% {1 M, d' ^% p
#endif* e/ n- a6 L& k  j+ q
#if defined(MesureIntensiteLumineuse) || defined(MesureRGB). i/ Q, w+ j% n+ F* i4 S, y
I2C i2c(pinSDA, pinSCL);
$ Z) W, B" [' H7 z#endif
+ b% \9 F  f- v% o#ifdef MesureIntensiteLumineuse3 f6 v" W$ }4 d0 V; ]  q7 e6 }
TSL2561 Lumiere(i2c,TSL2561_ADDRESS_FLOAT);: m) z1 i6 F, h8 Q. w4 e
#endif
% ]: J* ~4 a" Y: k* Z( K1 \#ifdef MesureRGB
1 Q3 u$ \: B; [- TAdafruit_TCS34725 CaptRGB(&i2c, TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);1 O/ e: Q9 D- t# y7 [. @" U
#endif
# m! v7 |& g) H6 _! B0 k#ifdef MesureDHT22
" @4 a2 \* J  T* RDHT CaptDHT(pinDHT22, modeleDHT);$ A% ]8 L; w) m
#endif
9 [. r- Z# r3 g( i* [" I6 j#ifdef MesureTemperatureSol
% x4 G& B1 \" g7 U5 c) D7 ~DS1820 TemperatureSonde(pinDS1820);' F$ E' `+ S, ]+ t
#endif" f. k/ |9 O; ~3 e0 A  X$ g! @
#ifdef MesureHumiditeSol
- L9 c6 l* f7 mAnalogIn CSMS(pinCSMS);; \. r4 C2 R8 j3 K6 f  \! P3 r
#endif
0 j/ @% E3 O/ I  L& ~4 TDigitalOut myled(LED1);9 ^+ c1 E+ S/ E7 z
#ifdef Debug
- m3 k6 u+ s! b* S; h: u* _& JSerial pc(USBTX,USBRX);
/ |) p5 f9 b7 [  L3 Q#endif+ j9 Z! b( T8 R! N/ b! F
/*
8 e) m8 @2 C4 H4 \#ifdef DeepSleepEnable/ R, E; F0 J2 T) ]0 N7 q0 d
   WakeUp ModeVeille;
. a2 o( T# ?$ `* P% n   ModeVeille.calibrate();
! p9 ]# \) \8 @/ r7 Z3 ?#endif
& w- a$ F3 {, v" w) e# L: t/ O) |4 \*/
7 b2 k# R, |5 ]( P/************************************8 E* ^) Y3 L" Y; a9 Z/ G
* Declaration des variables globales+ g9 {  X: y2 g9 ^3 O" X( s$ T
************************************/& N$ h' F. n. z: T
int TemperatureAir, HumiditeAir;
2 B. p7 g" F" ^0 F' I) i2 k2 nint RGB;# Z3 t  ?. y/ b6 K5 W$ |9 @" A9 y
float Lux;1 ^$ q1 {. l- d3 I* T: I
float TemperatureSol;9 r6 O. ^9 D( w# e) b
float HumiditeSol;; Z9 l9 K" q1 o5 _6 ~4 N; j3 o
float RedValue, GreenValue, BlueValue;7 _/ E  o. [3 O' G
uint16_t Clear, Red, Green, Blue;
0 P* ~  p: P1 t' O4 v' y9 b/************************************
& d9 n( @) J  X5 h*     Fonctions de mesures ou affichage
, q; F% N" N; |: ]8 a***********************************/
, Z/ h6 x) a, e/ u& P9 P5 _// Fonction permettant l'affichage des mesures sur l'ecran/ y2 ~' X7 R/ D/ g+ w3 w) ~' ^
#ifdef AffichageOLED) X/ q; L  ^6 R6 n
void affichageOLED(){7 g( i1 G% J/ k. ^2 w, s% S
  oled.clear();
9 x! K" ^5 l+ G# L- {7 v' J  oled.set_font(bold_font, 8);
5 F; J1 h9 z! v$ g: i6 ~$ B* `4 [  oled.printf("rojet AGRAL\r\n");5 N8 H- @4 y- y% O8 K( f
  oled.printf("\r\n");3 s$ y0 M# @9 Q. r9 S
  oled.set_font(standard_font, 6);
$ M# p9 Q5 f& h" B1 e4 g; ]* ~#ifdef MesureDHT229 n# c4 c9 R1 w- E& \, R9 J: Y/ E
  oled.printf("Tempe Air : %d\r\n", TemperatureAir);* T0 e9 ^8 r0 |4 \9 h0 L3 r* r) g
  oled.printf("Humidite : %d %%\r\n", HumiditeAir);: C9 @# X  ^' L2 r& A4 r2 D* _( I
#endif( ^7 Q( A- Z/ x; o6 j
#ifdef MesureIntensiteLumineuse
/ ^, E7 F6 E4 S0 M  oled.printf("Lux : %.0f lux\r\n", Lux);9 A; G- c6 t7 L0 Q# U
#endif
7 Y& [4 X0 k* Y5 a/ F6 o#ifdef MesureRGB
, x( M! |/ L% V& Y  oled.printf("RGB : %d %d %d\r\n", int(RedValue),int(GreenValue),int(BlueValue));
1 m% r# {6 i5 X/ x$ p8 Q1 B#endif
& _  x- d9 _  Z2 r3 e2 n#ifdef MesureTemperatureSol- s% Y. v8 x4 Y" s
  oled.printf("Temp Sol : %3.0f\r\n", TemperatureSol);
- N, R* ?1 z8 W3 x4 }$ @#endif
6 I+ v' m/ g8 ~#ifdef MesureHumiditeSol
" z! [/ s. R: P  oled.printf("Hum Sol : %.0f %%\r\n", HumiditeSol);
, D5 {# q! q- [, i$ s: \4 o! W#endif0 g/ s+ y' h6 `( S3 I
  oled.update();
0 g+ k9 a5 Z2 G" i* F+ V* X7 {  W  }0 P  b! _4 Q" N( U
#endif3 X8 l$ M. o# a3 C4 S
#ifdef Debug& A0 C' ?2 |) ]6 R+ V' P  @# C; p
void debugPrintf(void){! ^2 R8 P* s7 ?. l7 @3 X( E
   #ifdef MesureDHT22; S- M2 i5 G: `; v) T
   pc.printf("Temperature : %d\r\n", TemperatureAir);
0 B& F+ U' u& ~2 f, b   pc.printf("Humidite : %d %%\r\n", HumiditeAir);* o1 I8 j% v# o2 Y; C4 @0 N/ I
   #endif
0 u& G% ~7 [6 `" ]4 w( X* W   #ifdef MesureTemperatureSol
4 G$ a4 w; u6 j, Y' s' B7 y4 R0 w) L   pc.printf("Temp Sol : %3.0f\r\n", TemperatureSol);
8 A- H2 I( s& H, w   #endif
, f* ], b0 ]/ j) f( d2 C   #ifdef MesureHumiditeSol
8 \; E1 Q, D/ X6 I7 e$ K4 ?; U   pc.printf("Hum Sol : %.0f %%\r\n", HumiditeSol);
1 Z9 A. ^5 V' E   #endif' M( z- r. }9 M1 ]5 L/ q
   #ifdef MesureIntensiteLumineuse2 y! }+ u+ b1 h1 K3 h% {
   pc.printf("Lux : %.0f lux\r\n", Lux);. b) U3 W/ x6 \0 v+ _; x2 p
   #endif
- [7 H  Z8 K& [# Q! Y   #ifdef MesureRGB
1 ]* K9 I8 U6 _+ L' M! D4 [9 p   pc.printf("RGB : %d %d %d\r\n", int(RedValue),int(GreenValue),int(BlueValue));) L& n! @( m3 V+ `, l3 W
   #endif
' t5 Y2 b# c, q7 ?   #ifdef SigfoxActif
* p! X( O6 B  b; l( ]) R- e   pc.printf("\r\n");
* X2 \* m8 p3 G. r$ M4 q   pc.printf("AT$SF=%02x%02x%04x%02x%02x%02x%02x%02x\n\r", (int)TemperatureAir, (int)HumiditeAir, (int)Lux, (int)TemperatureSol, (int)HumiditeSol, (int)RedValue, (int)GreenValue, (int)BlueValue);! z% [4 b$ t  t% X) u8 \
   pc.printf("\r\n");
* R8 e- l# y) p4 L& }; j   #endif& r1 C" B& O8 x1 T+ o) z
   pc.printf("---------------------------------------------------\r\n");
5 b& G% I% K# I( {}
3 ?4 o: p9 |) A' r) \5 H" l#endif+ J1 ~9 g9 `+ p' J
// Mesure de la température et de l'humidité de l'air# m. i5 L# i: n
#ifdef MesureDHT225 V: k: d0 a6 S
void mesuresDHT22(){
" c: y2 z2 ]5 a2 G# g  CaptDHT.readData();
+ o. A: b5 b0 A& v0 x! p  e& H  TemperatureAir = CaptDHT.ReadTemperature(CELCIUS);* Y* B2 i1 n1 `& H8 L
  HumiditeAir = CaptDHT.ReadHumidity();
2 D6 v2 M8 y4 i# d2 K0 x}
( v1 d) R. m6 g3 U#endif
# w3 i% K+ o; E2 y& q// Mesure du spectre de la lumière; N% O* m/ Q0 Y# J: t- D
#ifdef MesureRGB6 z+ M  A8 s3 b$ x4 b
void mesureRGB(){4 m9 w" u) @2 j
  CaptRGB.getRawData(&Red, &Green, &Blue, &Clear);
6 X1 e) D) I% q- L7 g  uint32_t Sum = Clear;$ B7 P# M0 i) ^+ F" E
  RedValue = Red; RedValue /= Sum;
9 q- x( k, B2 f6 R  GreenValue = Green; GreenValue /= Sum;
* j7 z  W6 R+ q  BlueValue = Blue; BlueValue /= Sum;6 y% Y. o/ K/ _3 p1 W3 ~2 Q
  RedValue *= 256; GreenValue *= 256; BlueValue *= 256;4 c' F% n  L5 r, q8 ~9 z
}0 S! M# I6 o; ~: R! G) _% k
#endif
: @6 Y. A$ Q# R: f, |% Y3 ~// Mesure de l'intensité lumineuse en Lux! v, }! B7 t) x- f* G3 p$ l
#ifdef MesureIntensiteLumineuse' [9 G# _4 R2 T# e( d* Y1 i9 `/ f" T
void MesureLux(){) C/ p0 ]( u" J% J  ?
   //int i;; f; m4 Z$ j" }" o+ I! K
   Lux = 0;
9 m8 y2 `" A9 }/ A   for(int i = 0; i < NombreDeMesures; i++){6 K! q$ `3 x( l, W
       Lux += COEF_TSL*Lumiere.lux();8 O( S  M" y3 X' |
       wait_ms(DelaiEntreMesures);
9 _% D( v% j" ]1 }. l   }
/ o  G1 y( g0 ?/ d: J0 D   Lux /= NombreDeMesures;1 B$ x7 D" w' O8 R5 `, \0 b! i
}
) ]$ L5 R, {2 F+ b0 k/ w( Q2 y#endif
) t5 F) T$ D! K3 V- E// Mesure de la temperature du sol
: h7 ?: g! }5 q! R( v#ifdef MesureTemperatureSol
, ?; [% Q; k/ u4 h( \" bvoid mesureTemperatureSonde(){" U/ u9 |' W9 ?8 E  y- X6 ~9 L1 O
  TemperatureSonde.convertTemperature(true, DS1820::all_devices);# U  t; P" b; U" M$ s5 \- f  c) f
  TemperatureSol = TemperatureSonde.temperature();% ]' k! j$ I6 [! l
}
4 A- f- ?" k, ~  n' U#endif
. `1 K6 s# m' K8 l" N// Mesure de l'humidité du sol
& c: b. [+ z% v6 h3 _#ifdef MesureHumiditeSol
1 S; D7 m9 M( |, Yvoid mesureHumiditeCSMS(){6 A1 u% }8 \; j' J
  float a = -0.005312;3 L8 r7 \2 w; D, m+ v, u& ?
  float b = 0.92265;; R; u0 u2 V2 |. l( `9 n
  float SommeMesures = 0;
8 ?/ b9 g0 N  Y$ y1 g) b5 ~3 R; s  for(int i = 0; i < NombreDeMesures; i++){, v. K7 A! Q8 Q* l+ P3 `" X
      HumiditeSol = CSMS.read();( E1 i  _* F/ y$ ~; f: f
      HumiditeSol = (1/a)*(HumiditeSol-b);4 |5 P2 |$ I- O4 G( P: S: Q# _
      if (HumiditeSol < 0)4 J' k1 E5 ^; b" @( J
          HumiditeSol = 0;' l$ Z. Z& N5 {% l0 a& s! _* V" ]" _
      if (HumiditeSol > 100)
* n6 ~/ f- G9 v* F2 K          HumiditeSol = 100;0 a* ]' y+ a) Q, c$ l  `8 A' d  U
       SommeMesures += HumiditeSol;
6 i& `5 L& @) w0 ?8 v* Z* `       wait_ms(DelaiEntreMesures);5 [9 ~: s3 v2 F3 d5 l; o3 h
   }2 e$ J$ X% x+ P6 V8 v6 @. G# D
   SommeMesures /= NombreDeMesures;+ ?6 u1 v) n2 Z/ }
   HumiditeSol = SommeMesures;
& T$ k/ O) }! k" i}8 d! ~& |2 M6 U  i6 L1 g
#endif6 i! @$ J' y7 _, @
void ClignotementLED(int duree){7 J6 p+ J' U+ v: q+ J& w
   myled = !myled;
! b" u+ p7 h( G2 W% I   wait_ms(duree);
8 X' t+ i6 j' Z  d# D, f5 }6 r" Z   myled = !myled;8 X& w$ h/ [1 f  N
   wait_ms(duree);* e  X  k% C, `$ M
   myled = !myled;% k& ~' H/ h8 _$ f
   wait_ms(duree);
* r3 Z/ k" W6 H, ]( F0 B# A   myled = !myled;
6 N& @* I6 g1 O6 H9 n- z3 H+ b   wait_ms(duree);
" d  S8 @, P: P7 G7 T7 R; L) g   }  b/ \# Z/ d" B& m8 [9 A/ A
#ifdef AffichageOLED/ O2 F/ h/ E8 S( N4 _, T
void eteindreEcran(){
- X7 E9 O7 ^8 x* F! |: B   oled.clear();
2 B5 u) _' o& z  W5 V}) A+ j5 f, y& b! @( d/ W
#endif
+ X7 X' i; N) \- v/***********************************
- ?+ R, s# d: Q4 l*           Fonction main. e% z7 c6 N9 P: F, w
************************************/
- c) V: l7 G: c  O! R% }$ m! aint main()$ j/ ]( e4 |3 i& m4 |7 s" b" i8 v
{8 N, s. u5 y+ [/ v  i0 q& x
  // Initialisation de l'écran
1 B! M. o, K3 F( e& ^% @9 b#ifdef AffichageOLED
) L2 g7 d- w. e1 F1 @  oled.initialise();
, K- N4 _* t7 j% A4 ]' f, s  oled.set_contrast(255);% m2 D* g; z; {# D1 e0 `
  oled.clear();
- f; Q0 S; x' o' p# X2 D: g#endif
- G6 P1 B+ r7 W6 o( x! G5 A% w" \  while (1)" x/ A' K9 j+ i  O4 S  S( l9 ~. p1 I
  {
- C0 F# c1 t& R  S* ?   #ifdef MesureDHT22
% o( N5 c; ^: ~       mesuresDHT22();
1 h6 v5 R: g0 Q% L   #endif
( C0 k9 S& w1 n   #ifdef MesureIntensiteLumineuse
5 C# {2 I) x# t# s  ^( G) v       MesureLux();
% ~! w, _8 t, ^3 y  @/ X   #endif7 Y+ C2 ~! g' W7 e9 D, C3 G3 X$ }
   #ifdef MesureRGB3 F) Q9 Z  g2 v4 U
       mesureRGB();3 W& m, ?, F" t- n' G# g! K
   #endif' s: r. k3 m3 S* r
   #ifdef MesureTemperatureSol% r# m/ ~- Q1 w1 r
       mesureTemperatureSonde();' ?: c% R' \8 F; I8 [8 B
   #endif
$ R- r, v" }+ I   #ifdef MesureHumiditeSol
: R& I4 E' W2 g       mesureHumiditeCSMS();
! p! K* ?- o. v) ?7 e   #endif8 G6 n( T# N0 P- ]9 t$ x
   #ifdef AffichageOLED
) o$ |% `1 N( K8 J7 q7 O/ z  G       affichageOLED();
- N. M3 g/ H  k1 P0 g4 w8 v   #endif
5 B+ o* e6 o8 h, u/ i- J& h   #ifdef SigfoxActif; F( N; \/ M3 A" ~9 u5 s+ n5 `
       Sigfox.printf("AT$SF=");9 d% E& r" j  a0 L. O8 _
       //Sigfox.printf("15290107141f675e47");0 T- h. f# I- x/ W1 p
       //Sigfox.printf("65");
2 v# s6 j! E3 X- c       //Sigfox.printf("%02x%02x%04x%02x%02x%02x%02x%02x", (int)TemperatureAir, (int)HumiditeAir, (int)Lux, (int)TemperatureSol, (int)HumiditeSol, (int)RedValue, (int)GreenValue, (int)BlueValue);
) D8 X" y( B" g) T' X9 _& u& b       Sigfox.printf("%02x%02x%04x%02x", (int)TemperatureAir, (int)HumiditeAir, (int)Lux, (int)HumiditeSol);0 \- L# N. P0 r0 ^( p
       Sigfox.printf("\n\r");2 i$ Q- W# i- v( _+ p* t
       ClignotementLED(40); //On indique quand un message est envoyé par Sigfox
4 L6 O; x; @$ m) x; ^* o( a0 I   #endif; W8 ?  F5 v& T! Z
   #ifdef Debug
* y# c( P( T; v9 o       debugPrintf();
+ E2 B- P. Z* a+ M   #endif
3 k4 i( X( U  o% G   #ifdef SigfoxActif5 N$ W/ s1 b1 l- [$ Y6 l5 M
       wait(DelaiEnvoi);
( W$ I9 K" D" w+ X   #endif 0 w: q+ P, |& r2 L
   #ifdef DeepSleepEnable
6 s; r! _# I! d& ]$ i3 \       wait(2);' G- ^3 M/ @: v
       eteindreEcran();
$ o, G6 ]6 [2 b7 c, N       ClignotementLED(40);
& S$ v# l) e9 L- C; x+ f- p$ B       //ModeVeille.set(DureeDeepSleep);' f4 z: @3 S+ }- ]* e
       WakeUp::set_ms(30000);# g" d% m) B% I& y6 {6 A
       deepsleep();
4 P/ S$ Z: v7 W, ^$ V+ y   #endif    # N! \4 U3 ?. d$ e
   //NVIC_SystemReset();
6 [1 @6 Y8 T8 A8 J7 M8 }" n3 w  Y  }% R  b; N) e  |; O& ~% E$ K4 W2 n
}5 w8 V, [7 j8 r$ P8 z3 R

6 k4 w8 L3 y9 J0 c- ?0 p, L
2Ubidots
Ubidots是一种物联网平台,通过易于使用的界面,可以开发和部署互联网连接的应用程序和解决方案。借助REST API,Cloud Plateform能够接收和处理HTTP请求。
变量
变量对应于封装到JSON字符串中的数据。变量必须与传感器的数据具有相同的名称,以便很好地分析数据。因此,我们创建了与传感器数据一样多的变量。
10.jpg
Ubidots的变量
3)图形界面
为了开发我们的项目,我们使用Ubidots教育云数据库,该数据库收集来自Sigfox的所有传感器数据。对Json脚本的所有值都会被自动检测到,然后我们只需要放置一些小部件并告诉哪些数据与之相关。
11.jpg
我们在Ubidots项目的仪表板
4)印刷板
最后,我们选择了EASYEDA来创建我们的PCB.EASYEDA也是一个在线共享工具,每个成员都可以编辑项目以及任何使其成为私有或公共的计算机。完成shematic后,软件将生成一个PCB,您必须找到将传感器引脚连接到电路板上引脚的正确方法。
12.jpg
我们项目的电路板示意图
13.jpg
原理图
  E* I8 g5 s+ q. M, O
14.png
PCB
15.png

% z. c  v) ~! u+ W( _& I" e8 J! F$ O* J, k1 w$ D! C
收藏 1 评论0 发布时间:2019-3-25 18:21

举报

0个回答

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版