Skip to content

Commit f0a4d09

Browse files
committed
init
1 parent db1ccb9 commit f0a4d09

File tree

8 files changed

+342
-0
lines changed

8 files changed

+342
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

.travis.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < https://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < https://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choose one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
# - platformio update
39+
#
40+
# script:
41+
# - platformio run
42+
43+
44+
#
45+
# Template #2: The project is intended to be used as a library with examples.
46+
#
47+
48+
# language: python
49+
# python:
50+
# - "2.7"
51+
#
52+
# sudo: false
53+
# cache:
54+
# directories:
55+
# - "~/.platformio"
56+
#
57+
# env:
58+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
59+
# - PLATFORMIO_CI_SRC=examples/file.ino
60+
# - PLATFORMIO_CI_SRC=path/to/test/directory
61+
#
62+
# install:
63+
# - pip install -U platformio
64+
# - platformio update
65+
#
66+
# script:
67+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
]
7+
}

include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

platformio.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
;PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:d1]
12+
platform = espressif8266
13+
board = d1
14+
framework = arduino
15+
16+
lib_deps =
17+
19
18+
31

src/main.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// DHT Temperature & Humidity Sensor
2+
// Unified Sensor Library Example
3+
// Written by Tony DiCola for Adafruit Industries
4+
// Released under an MIT license.
5+
6+
// REQUIRES the following Arduino libraries:
7+
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
8+
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
9+
10+
#include <Adafruit_Sensor.h>
11+
#include <DHT.h>
12+
#include <DHT_U.h>
13+
14+
#include <ESP8266WiFi.h>
15+
#include <WiFiClient.h>
16+
#include <ESP8266WebServer.h>
17+
#include <ESP8266mDNS.h>
18+
19+
#define DHTPIN 2 // Digital pin connected to the DHT sensor
20+
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
21+
// Pin 15 can work but DHT must be disconnected during program upload.
22+
// Uncomment the type of sensor in use:
23+
#define DHTTYPE DHT11 // DHT 11
24+
// #define DHTTYPE DHT22 // DHT 22 (AM2302)
25+
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
26+
// See guide for details on sensor wiring and usage:
27+
// https://learn.adafruit.com/dht/overview
28+
DHT_Unified dht(DHTPIN, DHTTYPE);
29+
uint32_t delayMS;
30+
31+
const int httpPort = 80;
32+
String deviceName = "温湿度传感器";
33+
String version = "1.0";
34+
ESP8266WebServer server(httpPort);
35+
36+
// web服务器的根目录
37+
void handleRoot() {
38+
server.send(200, "text/html", "<h1>this is index page from esp8266!</h1>");
39+
}
40+
// 设备改名的API
41+
void handleDeviceRename(){
42+
String message = "{\"code\":0,\"message\":\"success\"}";
43+
for (uint8_t i=0; i<server.args(); i++){
44+
if (server.argName(i)=="name")
45+
{
46+
deviceName = server.arg(i);
47+
}
48+
}
49+
server.send(200, "application/json", message);
50+
}
51+
52+
// 获取status
53+
void handleCurrentStatus(){
54+
String message;
55+
String temp;
56+
String humi;
57+
// Get temperature event and print its value.
58+
sensors_event_t event;
59+
dht.temperature().getEvent(&event);
60+
if (isnan(event.temperature)) {
61+
temp = String(0);
62+
}
63+
else {
64+
temp = String(event.temperature);
65+
}
66+
// Get humidity event and print its value.
67+
dht.humidity().getEvent(&event);
68+
if (isnan(event.relative_humidity)) {
69+
humi = String(0);
70+
}
71+
else {
72+
humi = String(event.relative_humidity);
73+
}
74+
message = "{\"temperature\":"+temp+
75+
",\"humidity\":"+humi+
76+
",\"code\":0,\"message\":\"success\"}";
77+
server.send(200, "application/json", message);
78+
}
79+
80+
// 设备信息
81+
void handleDeviceInfo(){
82+
String message;
83+
message = "{\n";
84+
message += "\"name\":\""+deviceName +"\",\n";
85+
message += "\"model\":\"com.iotserv.devices.dht11\",\n";
86+
message += "\"mac\":\""+WiFi.macAddress()+"\",\n";
87+
message += "\"id\":\""+String(ESP.getFlashChipId())+"\",\n";
88+
message += "\"ui-support\":[\"web\",\"native\"],\n";
89+
message += "\"ui-first\":\"native\",\n";
90+
message += "\"author\":\"Farry\",\n";
91+
message += "\"email\":\"newfarry@126.com\",\n";
92+
message += "\"home-page\":\"https://github.com/iotdevice\",\n";
93+
message += "\"firmware-respository\":\"https://github.com/iotdevice/esp8266-dht11\",\n";
94+
message += "\"firmware-version\":\""+version+"\"\n";
95+
message +="}";
96+
server.send(200, "application/json", message);
97+
}
98+
99+
// 页面或者api没有找到
100+
void handleNotFound(){
101+
String message = "File Not Found\n\n";
102+
message += "URI: ";
103+
message += server.uri();
104+
message += "\nMethod: ";
105+
message += (server.method() == HTTP_GET)?"GET":"POST";
106+
message += "\nArguments: ";
107+
message += server.args();
108+
message += "\n";
109+
for (uint8_t i=0; i<server.args(); i++){
110+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
111+
}
112+
server.send(404, "text/plain", message);
113+
}
114+
115+
void setup() {
116+
WiFi.mode(WIFI_STA);
117+
// 选取一种连接路由器的方式
118+
// WiFi.begin(ssid, password);
119+
WiFi.beginSmartConfig();
120+
121+
// Wait for connection
122+
while (WiFi.status() != WL_CONNECTED) {
123+
delay(500);
124+
// Serial.print(".");
125+
}
126+
127+
if (MDNS.begin("esp8266")) {
128+
// Serial.println("MDNS responder started");
129+
}
130+
131+
server.on("/", handleRoot);
132+
server.on("/rename", handleDeviceRename);
133+
server.on("/status", handleCurrentStatus);
134+
// about this device
135+
server.on("/info", handleDeviceInfo);
136+
// Initialize device.
137+
dht.begin();
138+
139+
server.onNotFound(handleNotFound);
140+
141+
server.begin();
142+
// Serial.println("HTTP server started");
143+
MDNS.addService("iotdevice", "tcp", httpPort);
144+
}
145+
146+
void loop() {
147+
MDNS.update();
148+
server.handleClient();
149+
}

test/README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html

0 commit comments

Comments
 (0)