forked from Developers-RCCS/BTUI-Sessions-22
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba615e3
commit 0cd2edc
Showing
9 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// C++ code | ||
// | ||
void setup() | ||
{ | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
} | ||
|
||
void loop() | ||
{ | ||
digitalWrite(LED_BUILTIN, HIGH); | ||
delay(200); // Wait for 1000 millisecond(s) | ||
digitalWrite(LED_BUILTIN, LOW); | ||
delay(200); // Wait for 1000 millisecond(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
RoboticsAndIoT/hello_proximity_source-main/.vscode/extensions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
// See http://go.microsoft.com/fwlink/?LinkId=827846 | ||
// for the documentation about the extensions.json format | ||
"recommendations": [ | ||
"platformio.platformio-ide" | ||
], | ||
"unwantedRecommendations": [ | ||
"ms-vscode.cpptools-extension-pack" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
; 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:esp32dev] | ||
platform = espressif32 | ||
board = esp32dev | ||
framework = arduino | ||
lib_deps = mobizt/Firebase ESP32 Client@^4.0.0 |
228 changes: 228 additions & 0 deletions
228
RoboticsAndIoT/hello_proximity_source-main/src/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
#include <Arduino.h> | ||
#include <WiFi.h> | ||
#include <FirebaseESP32.h> | ||
|
||
//Provide the token generation process info. | ||
#include "addons/TokenHelper.h" | ||
//Provide the RTDB payload printing info and other helper functions. | ||
#include "addons/RTDBHelper.h" | ||
|
||
// Device ID | ||
#define DEVICE_UID "1X" | ||
|
||
// Your WiFi credentials | ||
#define WIFI_SSID "wifi_id" // Enter your wifi ID | ||
#define WIFI_PASSWORD "password" // Enter your wifi password | ||
|
||
// Your Firebase Project Web API Key | ||
#define API_KEY "API_key" | ||
// Your Firebase Realtime database URL | ||
#define DATABASE_URL "databse_url" | ||
|
||
// Firebase Realtime Database Object | ||
FirebaseData fbdo; | ||
// Firebase Authentication Object | ||
FirebaseAuth auth; | ||
// Firebase configuration Object | ||
FirebaseConfig config; | ||
// Device Location config | ||
String device_location = "Farm"; | ||
// Firebase database path | ||
String databasePath = ""; | ||
// Firebase Unique Identifier | ||
String fuid = ""; | ||
// Stores the elapsed time from device start up | ||
unsigned long elapsedMillis = 0; | ||
// The frequency of sensor updates to firebase, set to 10seconds | ||
unsigned long update_interval = 10000; | ||
// Dummy counter to test initial firebase updates | ||
int count = 0; | ||
// Store device authentication status | ||
bool isAuthenticated = false; | ||
|
||
// Variables to hold sensor readings | ||
float temperature = 0; | ||
float humidity = 0; | ||
float daytime = 0; | ||
// JSON object to hold updated sensor values to be sent to be firebase | ||
FirebaseJson temperature_json; | ||
FirebaseJson humidity_json; | ||
FirebaseJson daytime_json; | ||
// connect ir sensor to arduino pin 2 | ||
int IRSensor = 2; | ||
|
||
|
||
void Wifi_Init() { | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
Serial.print("Connecting to Wi-Fi"); | ||
while (WiFi.status() != WL_CONNECTED){ | ||
Serial.print("."); | ||
delay(300); | ||
} | ||
Serial.println(); | ||
Serial.print("Connected with IP: "); | ||
Serial.println(WiFi.localIP()); | ||
Serial.println(); | ||
} | ||
|
||
void firebase_init() { | ||
// configure firebase API Key | ||
config.api_key = API_KEY; | ||
// configure firebase realtime database url | ||
config.database_url = DATABASE_URL; | ||
// Enable WiFi reconnection | ||
Firebase.reconnectWiFi(true); | ||
Serial.println("------------------------------------"); | ||
Serial.println("Sign up new user..."); | ||
// Sign in to firebase Anonymously | ||
if (Firebase.signUp(&config, &auth, "", "")) | ||
{ | ||
Serial.println("Success"); | ||
isAuthenticated = true; | ||
// Set the database path where updates will be loaded for this device | ||
databasePath = "/" + device_location; | ||
fuid = auth.token.uid.c_str(); | ||
} | ||
else | ||
{ | ||
Serial.printf("Failed, %s\n", config.signer.signupError.message.c_str()); | ||
isAuthenticated = false; | ||
} | ||
// Assign the callback function for the long running token generation task, see addons/TokenHelper.h | ||
config.token_status_callback = tokenStatusCallback; | ||
// Initialise the firebase library | ||
Firebase.begin(&config, &auth); | ||
} | ||
|
||
void updateSensorReadings(){ | ||
Serial.println("------------------------------------"); | ||
Serial.println("Reading Sensor data ..."); | ||
temperature = 99; | ||
humidity = 80; | ||
float daytime = digitalRead (IRSensor); | ||
|
||
// Check if any reads failed and exit early (to try again). | ||
if (isnan(temperature) || isnan(humidity)) { | ||
Serial.println(F("Failed to read from DHT sensor!")); | ||
return; | ||
} | ||
Serial.printf("Temperature reading: %.2f \n", temperature); | ||
Serial.printf("Humidity reading: %.2f \n", humidity); | ||
Serial.printf("Daytime reading: %.2f \n", daytime); | ||
temperature_json.set("value", temperature); | ||
humidity_json.set("value", humidity); | ||
daytime_json.set("value", daytime); | ||
} | ||
|
||
void uploadSensorData() { | ||
if (millis() - elapsedMillis > update_interval && isAuthenticated && Firebase.ready()) | ||
{ | ||
elapsedMillis = millis(); | ||
updateSensorReadings(); | ||
String temperature_node = databasePath + "/temperature"; | ||
String humidity_node = databasePath + "/humidity"; | ||
String daytime_node = databasePath + "/daytime"; | ||
|
||
if (Firebase.setJSON(fbdo, temperature_node.c_str(), temperature_json)) | ||
{ | ||
Serial.println("PASSED"); | ||
Serial.println("PATH: " + fbdo.dataPath()); | ||
Serial.println("TYPE: " + fbdo.dataType()); | ||
Serial.println("ETag: " + fbdo.ETag()); | ||
Serial.print("VALUE: "); | ||
printResult(fbdo); //see addons/RTDBHelper.h | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
else | ||
{ | ||
Serial.println("FAILED"); | ||
Serial.println("REASON: " + fbdo.errorReason()); | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
if (Firebase.setJSON(fbdo, humidity_node.c_str(), humidity_json)) | ||
{ | ||
Serial.println("PASSED"); | ||
Serial.println("PATH: " + fbdo.dataPath()); | ||
Serial.println("TYPE: " + fbdo.dataType()); | ||
Serial.println("ETag: " + fbdo.ETag()); | ||
Serial.print("VALUE: "); | ||
printResult(fbdo); //see addons/RTDBHelper.h | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
else | ||
{ | ||
Serial.println("FAILED"); | ||
Serial.println("REASON: " + fbdo.errorReason()); | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
if (Firebase.setJSON(fbdo, daytime_node.c_str(), daytime_json)) | ||
{ | ||
Serial.println("PASSED"); | ||
Serial.println("PATH: " + fbdo.dataPath()); | ||
Serial.println("TYPE: " + fbdo.dataType()); | ||
Serial.println("ETag: " + fbdo.ETag()); | ||
Serial.print("VALUE: "); | ||
printResult(fbdo); //see addons/RTDBHelper.h | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
else | ||
{ | ||
Serial.println("FAILED"); | ||
Serial.println("REASON: " + fbdo.errorReason()); | ||
Serial.println("------------------------------------"); | ||
Serial.println(); | ||
} | ||
|
||
} | ||
} | ||
|
||
void setup() { | ||
// Initialise serial communication for local diagnostics | ||
Serial.begin(115200); | ||
// Initialise Connection with location WiFi | ||
Wifi_Init(); | ||
// Initialise firebase configuration and signup anonymously | ||
firebase_init(); | ||
// Initialise IR Sensor | ||
pinMode (IRSensor, INPUT); // sensor pin INPUT | ||
|
||
// Initialise temperature and humidity json data | ||
temperature_json.add("deviceuid", DEVICE_UID); | ||
temperature_json.add("name", "DHT11-Temp"); | ||
temperature_json.add("type", "Temperature"); | ||
temperature_json.add("location", device_location); | ||
temperature_json.add("value", temperature); | ||
// Print out initial temperature values | ||
String jsonStr; | ||
temperature_json.toString(jsonStr, true); | ||
Serial.println(jsonStr); | ||
|
||
humidity_json.add("deviceuid", DEVICE_UID); | ||
humidity_json.add("name", "DHT11-Hum"); | ||
humidity_json.add("type", "Humidity"); | ||
humidity_json.add("location", device_location); | ||
humidity_json.add("value", humidity); | ||
// Print out initial humidity values | ||
String jsonStr2; | ||
humidity_json.toString(jsonStr2, true); | ||
Serial.println(jsonStr2); | ||
|
||
daytime_json.add("deviceuid", DEVICE_UID); | ||
daytime_json.add("name", "DHT11-Hum"); | ||
daytime_json.add("type", "Humidity"); | ||
daytime_json.add("location", device_location); | ||
daytime_json.add("value", daytime); | ||
// Print out initial humidity values | ||
String jsonStr3; | ||
daytime_json.toString(jsonStr3, true); | ||
Serial.println(jsonStr3); | ||
} | ||
|
||
void loop() { | ||
uploadSensorData(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
This directory is intended for PlatformIO Test Runner and project tests. | ||
|
||
Unit Testing is a software testing method by which individual units of | ||
source code, sets of one or more MCU program modules together with associated | ||
control data, usage procedures, and operating procedures, are tested to | ||
determine whether they are fit for use. Unit testing finds problems early | ||
in the development cycle. | ||
|
||
More information about PlatformIO Unit Testing: | ||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html |
Oops, something went wrong.