Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Capteur autonome Penn Avel
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pronto 3
Capteur autonome Penn Avel
Commits
08058633
Commit
08058633
authored
2 months ago
by
CARNEIRO--GILLET Alexandre
Browse files
Options
Downloads
Patches
Plain Diff
ajout de code trouvé sur sens com
parent
8318fff7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
GW-custom/DHT.h
+75
-0
75 additions, 0 deletions
GW-custom/DHT.h
GW-custom/dht22-wifi.ino
+176
-0
176 additions, 0 deletions
GW-custom/dht22-wifi.ino
with
251 additions
and
0 deletions
GW-custom/DHT.h
0 → 100644
+
75
−
0
View file @
08058633
/* DHT library
MIT license
written by Adafruit Industries
*/
#ifndef DHT_H
#define DHT_H
#if ARDUINO >= 100
#include
"Arduino.h"
#else
#include
"WProgram.h"
#endif
// Uncomment to enable printing out nice debug messages.
//#define DHT_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef DHT_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
// Define types of sensors.
#define DHT11 11
#define DHT22 22
#define DHT21 21
#define AM2301 21
class
DHT
{
public:
DHT
(
uint8_t
pin
,
uint8_t
type
,
uint8_t
count
=
6
);
void
begin
(
void
);
float
readTemperature
(
bool
S
=
false
,
bool
force
=
false
);
float
convertCtoF
(
float
);
float
convertFtoC
(
float
);
float
computeHeatIndex
(
float
temperature
,
float
percentHumidity
,
bool
isFahrenheit
=
true
);
float
readHumidity
(
bool
force
=
false
);
boolean
read
(
bool
force
=
false
);
private:
uint8_t
data
[
5
];
uint8_t
_pin
,
_type
;
#ifdef __AVR
// Use direct GPIO access on an 8-bit AVR so keep track of the port and bitmask
// for the digital pin connected to the DHT. Other platforms will use digitalRead.
uint8_t
_bit
,
_port
;
#endif
uint32_t
_lastreadtime
,
_maxcycles
;
bool
_lastresult
;
uint32_t
expectPulse
(
bool
level
);
};
class
InterruptLock
{
public:
InterruptLock
()
{
noInterrupts
();
}
~
InterruptLock
()
{
interrupts
();
}
};
#endif
This diff is collapsed.
Click to expand it.
GW-custom/dht22-wifi.ino
0 → 100644
+
176
−
0
View file @
08058633
/************************************************************/
/* DHT22 -> luftdaten.info api */
/************************************************************/
// increment on change
#define SOFTWARE_VERSION "MFA-2015-002-DHT22"
/**********************************************/
/* DHT declaration
/**********************************************/
#include
"DHT.h"
#define DHTPIN 4 // D2
#define DHTTYPE DHT22
DHT
dht
(
DHTPIN
,
DHTTYPE
);
/**********************************************/
/* WiFi declarations
/**********************************************/
#include
<ESP8266WiFi.h>
const
char
*
ssid
=
"Freifunk"
;
const
char
*
password
=
""
;
// const char* host = "192.168.1.12";
const
char
*
host
=
"api.luftdaten.info"
;
// const int httpPort = 8000;
const
int
httpPort
=
80
;
int
value
=
0
;
unsigned
long
starttime
;
unsigned
long
sampletime_ms
=
30000
;
/**********************************************/
/* WiFi connecting script
/**********************************************/
void
connectWifi
()
{
WiFi
.
begin
(
ssid
,
password
);
// Start WiFI
Serial
.
print
(
"Connecting "
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
println
(
"WiFi connected"
);
Serial
.
print
(
"IP address: "
);
Serial
.
println
(
WiFi
.
localIP
());
}
/**********************************************/
/* send data to rest api
/**********************************************/
void
sendData
(
const
String
&
data
,
int
pin
=-
1
)
{
// delay(60000);
++
value
;
Serial
.
print
(
"connecting to "
);
Serial
.
println
(
host
);
// Use WiFiClient class to create TCP connections
WiFiClient
client
;
if
(
!
client
.
connect
(
host
,
httpPort
))
{
Serial
.
println
(
"connection failed"
);
return
;
}
// create an URI for the request
String
url
=
"/v1/push-sensor-data/"
;
Serial
.
print
(
"Requesting URL: "
);
Serial
.
println
(
url
);
Serial
.
println
(
data
);
String
pinstr
=
String
(
'-'
);
if
(
pin
>
0
){
pinstr
=
String
(
pin
);}
// send request to the server
client
.
print
(
String
(
"POST "
)
+
url
+
" HTTP/1.1
\r\n
"
+
"Host: "
+
host
+
"
\r\n
"
+
"Pin: "
+
pinstr
+
"
\r\n
"
+
"Content-Type: application/json
\r\n
"
+
"Sensor: esp8266-"
);
client
.
println
(
ESP
.
getChipId
());
client
.
print
(
"Content-Length: "
);
client
.
println
(
data
.
length
(),
DEC
);
client
.
println
(
"Connection: close
\r\n
"
);
client
.
println
(
data
);
delay
(
1
);
// Read reply from server and print them
while
(
client
.
available
()){
char
c
=
client
.
read
();
Serial
.
print
(
c
);
}
Serial
.
println
();
Serial
.
println
(
"closing connection"
);
}
String
Float2String
(
float
value
)
{
// Convert a float to String with two decimals.
char
temp
[
15
];
String
s
;
dtostrf
(
value
,
13
,
2
,
temp
);
s
=
String
(
temp
);
s
.
trim
();
return
s
;
}
// DHT22 Sensor
void
sensorDHT
(){
String
data
;
float
h
=
dht
.
readHumidity
();
//Read Humidity
float
t
=
dht
.
readTemperature
();
//Read Temperature
// Check if valid number if non NaN (not a number) will be send.
if
(
isnan
(
t
)
||
isnan
(
h
))
{
Serial
.
println
(
"DHT22 couldn’t be read"
);
}
else
{
Serial
.
print
(
"Humidity : "
);
Serial
.
print
(
h
);
Serial
.
print
(
" %
\n
"
);
Serial
.
print
(
"Temperature : "
);
Serial
.
print
(
t
);
Serial
.
println
(
" C"
);
// json for push to api: h t
data
=
"{
\"
software_version
\"
:
\"
"
;
data
+=
SOFTWARE_VERSION
;
data
+=
"
\"
,"
;
data
+=
"
\"
sensordatavalues
\"
:[{"
;
data
+=
"
\"
value_type
\"
:
\"
temperature
\"
,
\"
value
\"
:
\"
"
;
data
+=
Float2String
(
t
);
data
+=
"
\"
},{"
;
data
+=
"
\"
value_type
\"
:
\"
humidity
\"
,
\"
value
\"
:
\"
"
;
data
+=
Float2String
(
h
);
data
+=
"
\"
}]}"
;
sendData
(
data
,
DHTPIN
);
}
}
/**********************************************/
/* The Setup
/**********************************************/
void
setup
()
{
Serial
.
begin
(
9600
);
//Output to Serial at 9600 baud
pinMode
(
DHTPIN
,
INPUT
);
delay
(
10
);
starttime
=
millis
();
// store the start time
dht
.
begin
();
// Start DHT
delay
(
1000
);
connectWifi
();
// Start ConnecWifi
Serial
.
print
(
"
\n
"
);
Serial
.
println
(
"ChipId: "
);
Serial
.
println
(
ESP
.
getChipId
());
}
/**********************************************/
/* And action
/**********************************************/
void
loop
()
{
// Checking if it is time to sample
if
((
millis
()
-
starttime
)
>
sampletime_ms
)
{
starttime
=
millis
();
// store the start time
sensorDHT
();
// getting temperature and humidity
Serial
.
println
(
"------------------------------"
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment