PIA ENBEBIDOS-
PIA ENBEBIDOS-
PIA:
CONTENIDO
h1 {
font-family: 'Creepster', cursive;
color: #FF7F50;
text-shadow: 2px 2px #000000;
}
.container {
display: flex;
flex-wrap: wrap;
width: 90%;
max-width: 1200px;
margin: 0 auto;
}
.box, .box2 {
flex: 1 1 30%;
min-width: 300px;
height: 200px;
margin: 10px;
background: rgba(255, 69, 0, 0.7);
color: #ffffff;
justify-content: center;
border-radius: 10%;
padding: 15px;
position: relative;
font-size: 1.2em;
box-shadow: 10px 5px 15px black;
border: 2px solid #FF4500;
}
.box2 {
background: rgba(75, 0, 130, 0.7);
border: 2px solid #800080;
}
.inner-box, .inner-box2 {
padding: 10px;
border-radius: 8px;
height: 20%;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 1.2em;
text-shadow: 1px 1px #000000;
}
button {
width: 120px;
height: 50px;
font-size: 16px;
border: none;
border-radius: 8px;
cursor: pointer;
color: white;
background: #FF6347;
transition: transform 0.3s, background-color 0.3s;
}
button:active {
transform: scale(0.95);
background-color: #FF4500;
}
#pushButton {
background: #FF6347;
}
#toggleBtn {
background: #800080;
}
footer {
font-family: 'Creepster', cursive;
color: #FF7F50;
text-shadow: 2px 2px #000000;
}
</style>
</head>
<body>
<h1>Control IoT de Calabaza de Halloween</h1>
<div class="container">
<div class="box">
<div class="inner-box">
<h3>Reproducir Audio</h3>
</div>
<div class="inner-box2">
<button id="pushButton">Apagado</button>
</div>
</div>
<div class="box2">
<div class="inner-box">
<h3>Color RGB</h3>
</div>
<div class="inner-box2">
<button id="toggleBtn">Apagado</button>
</div>
</div>
</div>
<script src="js/IoTPIA.js"></script>
<footer class="pt-5 my-5 text-muted border-top">
Created by Elian Gonzalez and Nestor Cervantes · © 2024
</footer>
</body>
</html>
1. Control remoto: Permite controlar LEDs, servos y otras funcionalidades desde Firebase
en tiempo real.
2. Interacción local: Detecta eventos como presionar un botón físico para ejecutar acciones
específicas, como mover servomotores o encender LEDs.
3. Efectos visuales: Controla una tira de LEDs NeoPixel con transiciones de colores y
patrones dinámicos.
4. Sincronización con Firebase: Lee y actualiza datos en la base de datos para sincronizar
los dispositivos con comandos remotos.
5. Multitarea: Usa FreeRTOS para ejecutar múltiples procesos simultáneamente (efectos
de LEDs, lectura de sensores y control de hardware).
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#define PIN_NEO_PIXEL 12 // The ESP32 pin GPIO16 connected to NeoPixel
#define NUM_PIXELS 64 // The number of LEDs (pixels) on NeoPixel LED strip
#define _DISABLE_TLS_
//Firebase Credentials
int R = 0;
int G = 0;
int B = 0;
void loop0(void*parameter){
while(1){
for(R=0;R<255; R++)delay(10);
for(G=0;G<255; G++)delay(10);
for(R=255;R>0; R--)delay(10);
for(B=0;B<255; B++)delay(10);
for(G=255;G>0; G--)delay(10);
for(R=0;R<255; R++)delay(10);
for(B=255;B>0; B--)delay(10);
for(R=255;R>0; R--)delay(10);
}
}
void loop1(void*parameter){
while(1){
adcVal1 = map(analogRead(34), 0, 1500, 0, 8);
}
}
void loop2(void*parameter){
while(1){
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == 1 || buttonState1 == 1) {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
for(int posDegrees = 92; posDegrees <= 180; posDegrees++){
servo1.write(posDegrees);
delay(10);
}
for(int posDegrees = 92; posDegrees >= 0; posDegrees--){
servo2.write(posDegrees);
delay(10);
}
delay(10);
}
for(int posDegrees = 0; posDegrees <= 180; posDegrees++){
servo2.write(posDegrees);
delay(10);
}
delay(10);
}
for(int posDegrees = 180; posDegrees >= 92; posDegrees--){
servo2.write(posDegrees);
delay(10);
}
//has_interrupted = false;
}/*else{
gpioVal1 = 0;
}
updateSensorValue.add("MOV",gpioVal1);
updateSensorValue.add("ESP32IoTApp",json);
Firebase.RTDB.updateNode(&fbdo, "/ESP32IoTPIA", &updateSensorValue);
*/
}
}
void setup() {
Serial.begin(9600);
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();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
xTaskCreatePinnedToCore(loop0,"Tarea_0",1000,NULL,1,&Tarea0,0);
xTaskCreatePinnedToCore(loop1,"Tarea_1",2000,NULL,1,&Tarea1,1);
xTaskCreatePinnedToCore(loop2,"Tarea_2",1000,NULL,1,&Tarea2,1);
NeoPixel.begin();
servo1.attach(servoPin);
servo2.attach(servo2Pin);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
digitalWrite(LED_PIN, LOW);
pinMode(LED1_PIN, OUTPUT);
//pinMode(BUTTON1_PIN, INPUT_PULLDOWN);
digitalWrite(LED1_PIN, LOW);
void loop() {
if (button1State == HIGH) {
digitalWrite(LED1_PIN, HIGH);
}else{
digitalWrite(LED1_PIN, LOW);
}*/
Firebase.get(firebaseData, pathLedControl);
estadoLC = firebaseData.stringData();
Serial.print("Led Control:");
Serial.println(estadoLC);
if (estadoLC.equals("Apagado"))
{
digitalWrite(LED1_PIN, LOW);
Serial.println(estadoLC);
}else if (estadoLC.equals("Encendido"))
{
digitalWrite(LED1_PIN, HIGH);
Serial.println(estadoLC);
}
Firebase.get(firebaseData, pathLedControl1);
estadoLC1 = firebaseData.stringData();
Serial.print("Led Control:");
Serial.println(estadoLC1);
if (estadoLC1.equals("Apagado"))
{
buttonState1 = 0;
//digitalWrite(LED_PIN, LOW);
Serial.println(estadoLC1);
}else if (estadoLC1.equals("Encendido"))
{
buttonState1 = 1;
//digitalWrite(LED_PIN, HIGH);
Serial.println(estadoLC1);
}
delay(700);
}
FirebaseJsonBase::~FirebaseJsonBase()
{
mClear();
}
FirebaseJsonBase &FirebaseJsonBase::mClear()
{
mIteratorEnd();
if (root != NULL)
MB_JSON_Delete(root);
root = NULL;
buf.clear();
errorPos = -1;
return *this;
}
void FirebaseJsonBase::mCopy(FirebaseJsonBase &other)
{
mClear();
this->root = MB_JSON_Duplicate(other.root, true);
this->doubleDigits = other.doubleDigits;
this->floatDigits = other.floatDigits;
this->httpCode = other.httpCode;
this->serData = other.serData;
this->root_type = other.root_type;
this->iterator_data = other.iterator_data;
this->buf = other.buf;
}
if (raw)
{
size_t i = 0;
while (i < strlen(raw) && raw[i] == ' ')
{
i++;
}
void FirebaseJsonBase::prepareRoot()
{
if (root == NULL)
{
if (root_type == Root_Type_JSONArray)
root = MB_JSON_CreateArray();
else
root = MB_JSON_CreateObject();
}
}
if (e == NULL)
return parent;
r.status = key_status_existed;
return e;
}
if (isArray(m_parent))
{
if (isArrKey)
m_parent = addArray(m_parent, e, index + 1);
else
MB_JSON_AddItemToArray(m_parent, e);
}
else
{
if (isArrKey)
{
if ((int)i == beginIndex)
{
m_parent = MB_JSON_CreateArray();
MB_JSON_Delete(*parent);
*parent = m_parent;
}
m_parent = addArray(m_parent, e, index + 1);
}
else
{
MB_JSON_AddItemToObject(m_parent, keys[i].c_str(), e);
m_parent = e;
}
}
}
}
previous = current + 1;
current = strpos(str, delim, previous);
}
s.clear();
trim(s);
if (s.length() > 0)
keys.push_back(s);
s.clear();
}
void FirebaseJsonBase::clearList(MB_VECTOR<MB_String> &keys)
{
size_t len = keys.size();
for (size_t i = 0; i < len; i++)
keys[i].clear();
for (int i = len - 1; i >= 0; i--)
keys.erase(keys.begin() + i);
keys.clear();
#if defined(MB_USE_STD_VECTOR)
MB_VECTOR<MB_String>().swap(keys);
#endif
}
if (isArray(parent))
{
int arrSize = MB_JSON_GetArraySize(parent);
MB_JSON_AddItemToArray(parent, item);
}
else
MB_JSON_Delete(value);
}
buf = p;
MB_JSON_free(p);
iterator_data.buf_size = buf.length();
int index = -1;
mIterate(parent, index);
return iterator_data.result.size();
}
if (keys == NULL)
return 0;
int index = -1;
mIterate(parent, index);
return iterator_data.result.size();
}
if (isAr)
arrIndex = 0;
MB_JSON *e = parent->child;
if (e)
{
iterator_data.depth++;
while (e)
{
if (isArray(e) || isObject(e))
mCollectIterator(e, e->string ? JSON_OBJECT : JSON_ARRAY, arrIndex);
if (isArray(e))
{
MB_JSON *item = e->child;
int _arrIndex = 0;
if (e->child)
{
iterator_data.depth++;
while (item)
{
if (isArray(item) || isObject(item))
mIterate(item, _arrIndex);
else
mCollectIterator(item, item->string ? JSON_OBJECT : JSON_ARRAY,
_arrIndex);
item = item->next;
_arrIndex++;
}
}
}
else if (isObject(e))
mIterate(e, arrIndex);
else
mCollectIterator(e, e->string ? JSON_OBJECT : JSON_ARRAY, arrIndex);
e = e->next;
if (isAr)
arrIndex++;
}
}
}
if (e->string)
{
size_t pos = buf.find((const char *)e->string, iterator_data.buf_offset);
if (pos != MB_String::npos)
{
result.ofs1 = pos;
result.len1 = strlen(e->string);
iterator_data.buf_offset = (e->type != MB_JSON_Object && e->type !=
MB_JSON_Array) ? pos + result.len1 : pos;
}
}
char *p = MB_JSON_PrintUnformatted(e);
if (p)
{
int i = iterator_data.buf_offset;
size_t pos = buf.find(p, i);
if (pos != MB_String::npos)
{
result.ofs2 = pos - result.ofs1 - result.len1;
result.len2 = strlen(p);
MB_JSON_free(p);
iterator_data.buf_offset = (e->type != MB_JSON_Object && e->type !=
MB_JSON_Array) ? pos + result.len2 : pos;
}
}
result.type = type;
result.depth = iterator_data.depth;
iterator_data.result.push_back(result);
}
if (buf.length() == iterator_data.buf_size)
{
if (index > iterator_data.result.size() - 1)
return depth;
if (iterator_data.result[index].len1 > 0)
{
char *m_key = (char *)newP(iterator_data.result[index].len1 + 1);
if (m_key)
{
memset(m_key, 0, iterator_data.result[index].len1 + 1);
strncpy(m_key, &buf[iterator_data.result[index].ofs1],
iterator_data.result[index].len1);
key = m_key;
delP(&m_key);
}
}
if (iterator_data.result[index].type == JSON_STRING)
{
if (buf[ofs] == '"')
ofs++;
if (buf[ofs + len - 1] == '"')
len--;
}
strncpy(m_val, &buf[ofs], len);
value = m_val;
delP(&m_val);
}
type = iterator_data.result[index].type;
depth = iterator_data.result[index].depth;
}
return depth;
}
#if defined(ESP32_SD_FAT_INCLUDED)
bool FirebaseJsonBase::mReadSdFat(SD_FAT_FILE &file, int timeoutMS)
{
// non-blocking read
if (readSdFatFile(file, serData, buf, true, timeoutMS))
{
if (root != NULL)
MB_JSON_Delete(root);
root = parse(buf.c_str());
buf.clear();
return root != NULL;
}
return false;
}
#endif
if (keys.size() > 0)
{
if (isArrayKey(keys[0].c_str()) && root_type == Root_Type_JSON)
{
clearList(keys);
return false;
}
}
MB_JSON *parent = root;
struct search_result_t r;
searchElements(keys, parent, r);
parent = r.parent;
if (r.status == key_status_existed)
{
ret = true;
if (isArray(parent))
MB_JSON_DeleteItemFromArray(parent,
getArrIndex(keys[r.stopIndex].c_str()));
else
{
MB_JSON_DeleteItemFromObjectCaseSensitive(parent,
keys[r.stopIndex].c_str());
if (parent->child == NULL && r.stopIndex > 0)
{
MB_String path;
mGetPath(path, keys, 0, r.stopIndex - 1);
mRemove(path.c_str());
}
}
}
clearList(keys);
return ret;
}
int FirebaseJsonBase::mResponseCode()
{
return httpCode;
}
if (keys.size() > 0)
{
if (isArrayKey(keys[0].c_str()) && root_type == Root_Type_JSON)
{
clearList(keys);
return false;
}
}
if (r.status == key_status_existed)
{
MB_JSON *data = NULL;
if (isArray(_parent))
data = MB_JSON_GetArrayItem(_parent,
getArrIndex(keys[r.stopIndex].c_str()));
else
data = MB_JSON_GetObjectItemCaseSensitive(_parent,
keys[r.stopIndex].c_str());
if (data != NULL)
{
if (result != NULL)
{
result->clear();
char *p = prettify ? MB_JSON_Print(data) :
MB_JSON_PrintUnformatted(data);
result->stringValue = p;
MB_JSON_free(p);
result->type_num = data->type;
result->success = true;
mSetElementType(result);
}
ret = true;
}
}
clearList(keys);
return ret;
}
data->intValue = data->iVal.int32;
data->boolValue = data->iVal.int32 > 0;
}
result->iVal = {t};
result->fVal.setd(t);
result->boolValue = t;
result->intValue = t;
result->floatValue = t;
result->doubleValue = t;
}
else if (result->type_num == MB_JSON_Number || result->type_num ==
MB_JSON_Raw)
{
mSetResInt(result, result->stringValue.c_str());
mSetResFloat(result, result->stringValue.c_str());
result->type = buf;
delP(&buf);
}
if (keys.size() > 0)
{
if ((isArrayKey(keys[0].c_str()) && root_type == Root_Type_JSON) || (!
isArrayKey(keys[0].c_str()) && root_type == Root_Type_JSONArray))
{
MB_JSON_Delete(value);
clearList(keys);
return;
}
}
if (value == NULL)
value = MB_JSON_CreateNull();
clearList(keys);
}
#if defined(__AVR__)
unsigned long long FirebaseJsonBase::strtoull_alt(const char *s)
{
unsigned long long sum = 0;
while (*s)
{
sum = sum * 10 + (*s++ - '0');
}
return sum;
}
#endif
FirebaseJson::FirebaseJson(FirebaseJson &other)
{
if (isObject(other.root))
mCopy(other);
}
FirebaseJson::~FirebaseJson()
{
clear();
}
if (value == NULL)
value = MB_JSON_CreateNull();
if (keys.size() > 0)
{
if (!isArrayKey(keys[0].c_str()) || root_type == Root_Type_JSONArray)
mAdd(keys, &root, 0, value);
}
clearList(keys);
return *this;
}
FirebaseJson &FirebaseJson::clear()
{
mClear();
return *this;
}
FirebaseJsonArray::~FirebaseJsonArray()
{
mClear();
};
FirebaseJsonArray::FirebaseJsonArray(FirebaseJsonArray &other)
{
if (isArray(other.root))
mCopy(other);
}
root_type = Root_Type_JSONArray;
prepareRoot();
if (value == NULL)
value = MB_JSON_CreateNull();
MB_JSON_AddItemToArray(root, value);
return *this;
}
result->clear();
if (data != NULL)
{
char *p = prettify ? MB_JSON_Print(data) : MB_JSON_PrintUnformatted(data);
result->stringValue = p;
MB_JSON_free(p);
result->type_num = data->type;
result->success = true;
mSetElementType(result);
ret = true;
}
return ret;
}
prepareRoot();
FirebaseJsonArray &FirebaseJsonArray::clear()
{
mClear();
return *this;
}
FirebaseJsonData::~FirebaseJsonData()
{
clear();
}
if (jsonArray.root != NULL)
MB_JSON_Delete(jsonArray.root);
jsonArray.root = jsonArray.parse(source);
json.root = json.parse(source);
return (size_t)newlen;
}
#else
#if defined(ESP8266_USE_EXTERNAL_HEAP)
ESP.setExternalHeap();
#endif
p = (void *)malloc(newLen);
bool nn = p ? true : false;
#if defined(ESP8266_USE_EXTERNAL_HEAP)
ESP.resetHeap();
#endif
if (!nn)
return NULL;
#endif
memset(p, 0, newLen);
return p;
}
void FirebaseJsonData::clear()
{
stringValue.remove(0, stringValue.length());
iVal = {0};
fVal.setd(0);
intValue = 0;
floatValue = 0;
doubleValue = 0;
boolValue = false;
type.remove(0, type.length());
typeNum = 0;
success = false;
}
#endif