Skip to content

Commit

Permalink
Code added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio committed Nov 30, 2020
1 parent e733995 commit 3247ecf
Show file tree
Hide file tree
Showing 41 changed files with 16,767 additions and 1 deletion.
41 changes: 40 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
firebase-debug.*.log*

# Firebase cache
.firebase/
.firebaserc

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
Expand Down Expand Up @@ -56,4 +69,30 @@ typings/

# dotenv environment variables file
.env

secrets.h
config.js


# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
/.vscode
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
5 changes: 5 additions & 0 deletions HW/dht11/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
7 changes: 7 additions & 0 deletions HW/dht11/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
39 changes: 39 additions & 0 deletions HW/dht11/include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
46 changes: 46 additions & 0 deletions HW/dht11/lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
25 changes: 25 additions & 0 deletions HW/dht11/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
upload_speed = 921600
monitor_speed = 115200

lib_deps =
# RECOMMENDED
# Accept new functionality in a backwards compatible manner and patches
adafruit/DHT sensor library @ ^1.4.0
arduino-libraries/NTPClient @ ^3.1.0
bblanchon/ArduinoJson
; FirebaseArduino=https://github.com/FirebaseExtended/firebase-arduino/archive/v0.3.zip
FirebaseArduino
203 changes: 203 additions & 0 deletions HW/dht11/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#include <NTPClient.h>
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>


#include "secrets.h"

const String SENSOR_ID = "hermes001";

WiFiUDP ntpUDP; // UDP client
NTPClient timeClient(ntpUDP); // NTP client

// #define DONT_HAVE_SENSORS

// Feather ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
#define DHTPIN D3 // Digital pin connected to the DHT sensor

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

#define BAUDRATE 115200

struct readings {
bool gas;
float hum; // Humidity in Percent ( %)
float temp; // Temperature in Celsius (°C)
} readings;


// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

// will store last time data was sent to Firebase
unsigned long previousMillis = 0;
const long INTERVAL = 30000;
long unsigned int timestamp = 0;

// Defs.
void connectToWiFi(char const *ssid, char const *password);
void readSensors(struct readings *r);
void displaySensors(struct readings r);
void sendDataToFirebase(struct readings r);

void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(BAUDRATE);
connectToWiFi(SSID, PASSWORD);

timeClient.begin(); // init NTP
timeClient.setTimeOffset(0); // 0= GMT, 3600 = GMT+1

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
dht.begin();

}

void loop() {
// check to see if it's time to send data to Firebase; that is, if the difference
// between the current time and last time we sent data is bigger than
// the interval at which we want to send data.
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= INTERVAL) {
// save the last time we sent data to Fireabase
previousMillis = currentMillis;
readSensors(&readings);
displaySensors(readings);
sendDataToFirebase(readings);
}
}

void connectToWiFi(char const *ssid, char const *password) {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.disconnect();

//start connecting to WiFi
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// timestamp
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// get Epoch time
Serial.print("> NTP Time:");
Serial.println(timeClient.getFormattedTime());
timestamp = timeClient.getEpochTime();

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void readSensors(struct readings *r) {
#ifdef DONT_HAVE_SENSORS
readings.gas = !readings.gas;
readings.temp = random(0, 80);
readings.hum = random(0, 80);
#else
// Read Gas status
r->gas = true;
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
r->hum = dht.readHumidity();
// Read temperature as Celsius (the default)
r->temp = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(r->hum) || isnan(r->temp)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
#endif
}

void displaySensors(struct readings r) {
if (r.gas == true) {
Serial.println("[INFO] Gas DETECTED!!!");
}
Serial.print("[INFO] Gas status: ");
Serial.println(r.gas);
Serial.print("[INFO] Humidity: ");
Serial.print(r.hum);
Serial.println("%");
Serial.print("[INFO] Temperature: ");
Serial.print(r.temp);
Serial.print("°C ");
Serial.println();

}

void sendDataToFirebase(struct readings r) {
timeClient.update();
timestamp = timeClient.getEpochTime();

const size_t capacity = JSON_OBJECT_SIZE(4);
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& object1 = jsonBuffer.createObject();

object1["timestamp"] = timestamp;
object1["temp"] = r.temp;
object1["hum"] = r.hum;
object1["gas"] = r.gas;

Serial.print("Data:");
object1.printTo(Serial);
Serial.println();

String requestStatusID = Firebase.push(SENSOR_ID, object1);
if (Firebase.failed()) {
Serial.println("[ERROR] pushing data failed:");
Serial.println(Firebase.error());
return;
}

// String humValueID = Firebase.pushFloat(SENSOR_ID + String("/json"), r.temp);
// if (Firebase.failed()) {
// Serial.print("[ERROR] pushing /json failed:");
// Serial.println(Firebase.error());
// return;
// }
// String tempValueID = Firebase.pushFloat(SENSOR_ID + String("/dht11/temperature"), r.temp);
// if (Firebase.failed()) {
// Serial.print("[ERROR] pushing /dht11/temperature failed:");
// Serial.println(Firebase.error());
// return;
// }
}
Loading

0 comments on commit 3247ecf

Please sign in to comment.