0% found this document useful (0 votes)
4 views

PIA ENBEBIDOS-

The document outlines the design of an IoT system using an ESP32 microcontroller for controlling a Halloween pumpkin, detailing the web application structure, database configuration, and microcontroller code. It includes functionalities such as remote control of LEDs and servos, local interaction through button presses, and synchronization with Firebase. Additionally, it explains the use of JSON for managing device configurations and sensor data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PIA ENBEBIDOS-

The document outlines the design of an IoT system using an ESP32 microcontroller for controlling a Halloween pumpkin, detailing the web application structure, database configuration, and microcontroller code. It includes functionalities such as remote control of LEDs and servos, local interaction through button presses, and synchronization with Firebase. Additionally, it explains the use of JSON for managing device configurations and sensor data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

UNIVERSIDAD AUTÓNOMA DE NUEVO LEÓN

FACULTAD DE INGENIERÍA MECÁNICA Y ELÉCTRICA

Diseño de sistemas Embebidos

PIA:
CONTENIDO

Configuración y explicación de la base de datos

Estructura aplicación web y captura

Explicación del código de microcontrolador

Explicación del Json

Diagrama del circuito

Foto del prototipo y circuitos


Configuración y explicación de la base de datos
Estructura aplicación web y captura
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Control IoT de Calabaza de Halloween</title>
<script
src="https://www.gstatic.com/firebasejs/6.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #000000;
background: linear-gradient(to right, #434343, #000000);
color: #FFFFFF;
background-image:
url('https://p4.wallpaperbetter.com/wallpaper/885/228/61/halloween-4k-
screen-wallpaper-preview.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-blend-mode: overlay;
}

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 &middot; &copy; 2024
</footer>
</body>
</html>

3. Explicación del código de microcontrolador

En resumen, este código implementa un sistema IoT utilizando un ESP32 para:

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 BUTTON_PIN 27 // Pin GPIO donde está conectado el botón

volatile bool has_interrupted = false;

void IRAM_ATTR handleButtonPress(void);

String pathLedControl = "/ESP32IoTPIA/COLOR";


String pathLedControl1 = "/ESP32IoTPIA/Audio";

String estadoLC = "";


String estadoLC1 = "";

#define _DISABLE_TLS_

#define WIFI_SSID "S9 del guapo"


#define WIFI_PASSWORD "gonzalez2002"

//Firebase Credentials

#define FIREBASE_HOST "esp32-pia-12f82-default-rtdb.firebaseio.com"


#define FIREBASE_AUTH "AIzaSyCNFplT2ao0GNSlcRPhB9uxAJsgV91xEjI"

//Define Firebase Data objects


FirebaseData fbdo;
FirebaseData firebaseData;
FirebaseJson updateSensorValue;
FirebaseJson json;

Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB +


NEO_KHZ800);
TaskHandle_t Tarea0;
TaskHandle_t Tarea1;
TaskHandle_t Tarea2;
Servo servo1;
Servo servo2;
static const int servoPin = 13;
static const int servo2Pin = 14;
const int LED_PIN = 2;

const int LED1_PIN = 4;


//const int BUTTON1_PIN = 27;
int buttonState1 = 0;
int gpioVal1 = 0;
int gpioVal2 = 0;
int adcVal1 = 0;
int adcVal2 = 0;

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);

for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {


if(pixel < (adcVal1*8)){
NeoPixel.setPixelColor(pixel, NeoPixel.Color(R, G, B));
}else {
NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 0, 0));
}
}
NeoPixel.show();

}
}

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);
}

for(int posDegrees = 180; posDegrees >= 0; posDegrees--){


servo1.write(posDegrees);

delay(10);
}
for(int posDegrees = 0; posDegrees <= 180; posDegrees++){
servo2.write(posDegrees);

delay(10);
}

for(int posDegrees = 0; posDegrees <= 92; posDegrees++){


servo1.write(posDegrees);

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);

attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, RISING);


servo1.write(92);
}

void loop() {

/*int button1State = digitalRead(BUTTON1_PIN);

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);
}

void IRAM_ATTR handleButtonPress(void)


{
has_interrupted = true;

4. Explicacón del JSON

Este código es útil en aplicaciones IoT para:

1. Almacenar configuraciones del dispositivo.


2. Sincronizar datos entre el dispositivo (ESP32) y la base de datos Firebase.
3. Gestionar información estructurada como estados de sensores, configuraciones de usuario, y
más, en formato JSON.
FirebaseJsonBase::FirebaseJsonBase()
{
MB_JSON_InitHooks(&MB_JSON_hooks);
}

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;
}

bool FirebaseJsonBase::setRaw(const char *raw)


{
mClear();

if (raw)
{
size_t i = 0;
while (i < strlen(raw) && raw[i] == ' ')
{
i++;
}

if (raw[i] == '{' || raw[i] == '[')


{
this->root_type = (raw[i] == '{') ? Root_Type_JSON : Root_Type_JSONArray;
root = parse(raw);
}
else
{
this->root_type = Root_Type_Raw;
root = MB_JSON_CreateRaw(raw);
}
}

return root != NULL;


}

MB_JSON *FirebaseJsonBase::parse(const char *raw)


{
const char *s = NULL;
MB_JSON *e = MB_JSON_ParseWithOpts(raw, &s, 1);
errorPos = (s - raw != (int)strlen(raw)) ? s - raw : -1;
return e;
}

void FirebaseJsonBase::prepareRoot()
{
if (root == NULL)
{
if (root_type == Root_Type_JSONArray)
root = MB_JSON_CreateArray();
else
root = MB_JSON_CreateObject();
}
}

void FirebaseJsonBase::searchElements(MB_VECTOR<MB_String> &keys, MB_JSON


*parent, struct search_result_t &r)
{
MB_JSON *e = parent;
for (size_t i = 0; i < keys.size(); i++)
{
r.status = key_status_not_existed;
e = getElement(parent, keys[i].c_str(), r);
r.stopIndex = i;
if (r.status != key_status_existed)
{
if (i == 0)
r.parent = parent;
break;
}
r.parent = parent;
r.foundIndex = i;
parent = e;
}
}
MB_JSON *FirebaseJsonBase::getElement(MB_JSON *parent, const char *key, struct
search_result_t &r)
{
MB_JSON *e = NULL;
bool isArrKey = isArrayKey(key);
int index = isArrKey ? getArrIndex(key) : -1;
if ((isArray(parent) && !isArrKey) || (isObject(parent) && isArrKey))
r.status = key_status_mistype;
else if (isArray(parent) && isArrKey)
{
e = MB_JSON_GetArrayItem(parent, index);
if (e == NULL)
r.status = key_status_out_of_range;
}
else if (isObject(parent) && !isArrKey)
{
e = MB_JSON_GetObjectItemCaseSensitive(parent, key);
if (e == NULL)
r.status = key_status_not_existed;
}

if (e == NULL)
return parent;

r.status = key_status_existed;
return e;
}

void FirebaseJsonBase::mAdd(MB_VECTOR<MB_String> keys, MB_JSON **parent, int


beginIndex, MB_JSON *value)
{
MB_JSON *m_parent = *parent;

for (size_t i = beginIndex; i < keys.size(); i++)


{
bool isArrKey = isArrayKey(keys[i].c_str());
int index = isArrKey ? getArrIndex(keys[i].c_str()) : -1;
MB_JSON *e = (i < keys.size() - 1) ? (isArrayKey(keys[i + 1].c_str()) ?
MB_JSON_CreateArray() : MB_JSON_CreateObject()) : value;

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;
}
}
}
}

void FirebaseJsonBase::makeList(const char *str, MB_VECTOR<MB_String> &keys,


char delim)
{
clearList(keys);

int current = 0, previous = 0;


current = strpos(str, delim, 0);
MB_String s;
while (current != -1)
{
s.clear();
substr(s, str, previous, current - previous);
trim(s);
if (s.length() > 0)
keys.push_back(s);

previous = current + 1;
current = strpos(str, delim, previous);
}

s.clear();

if (previous > 0 && current == -1)


substr(s, str, previous, strlen(str) - previous);
else
s = str;

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
}

bool FirebaseJsonBase::isArray(MB_JSON *e)


{
return MB_JSON_IsArray(e);
}

bool FirebaseJsonBase::isObject(MB_JSON *e)


{
return MB_JSON_IsObject(e);
}
MB_JSON *FirebaseJsonBase::addArray(MB_JSON *parent, MB_JSON *e, size_t size)
{
for (size_t i = 0; i < size - 1; i++)
MB_JSON_AddItemToArray(parent, MB_JSON_CreateNull());
MB_JSON_AddItemToArray(parent, e);
return e;
}

void FirebaseJsonBase::appendArray(MB_VECTOR<MB_String> &keys, struct


search_result_t &r, MB_JSON *parent, MB_JSON *value)
{
MB_JSON *item = NULL;

int index = getArrIndex(keys[r.stopIndex].c_str());

if (r.foundIndex > -1)


{
if (isArray(parent))
parent = MB_JSON_GetArrayItem(parent,
getArrIndex(keys[r.foundIndex].c_str()));
else
parent = MB_JSON_GetObjectItemCaseSensitive(parent,
keys[r.foundIndex].c_str());
}

if (isArray(parent))
{
int arrSize = MB_JSON_GetArraySize(parent);

if (r.stopIndex < (int)keys.size() - 1)


{
item = isArrayKey(keys[r.stopIndex + 1].c_str()) ? MB_JSON_CreateArray() :
MB_JSON_CreateObject();
mAdd(keys, &item, r.stopIndex + 1, value);
}
else
item = value;

for (int i = arrSize; i < index; i++)


MB_JSON_AddItemToArray(parent, MB_JSON_CreateNull());

MB_JSON_AddItemToArray(parent, item);
}
else
MB_JSON_Delete(value);
}

void FirebaseJsonBase::replaceItem(MB_VECTOR<MB_String> &keys, struct


search_result_t &r, MB_JSON *parent, MB_JSON *value)
{
if (r.foundIndex == -1)
{
if (r.status == key_status_not_existed)
mAdd(keys, &parent, 0, value);
else if (r.status == key_status_mistype)
{
MB_JSON *m_parent = MB_JSON_CreateObject();
mAdd(keys, &m_parent, 0, value);
*parent = *m_parent;
}
else
MB_JSON_Delete(value);
}
else
{
if (r.status == key_status_not_existed && !
isArrayKey(keys[r.stopIndex].c_str()))
{
MB_JSON *curItem = isArray(parent) ? MB_JSON_GetArrayItem(parent,
getArrIndex(keys[r.foundIndex].c_str())) : MB_JSON_GetObjectItem(parent,
keys[r.foundIndex].c_str());
if (isObject(curItem))
{
mAdd(keys, &curItem, r.foundIndex + 1, value);
return;
}
}
MB_JSON *item = NULL;

if ((r.status == key_status_mistype ? r.stopIndex : r.foundIndex) <


(int)keys.size() - 1)
{
item = isArrayKey(keys[r.stopIndex].c_str()) ? MB_JSON_CreateArray() :
MB_JSON_CreateObject();
mAdd(keys, &item, r.stopIndex, value);
}
else
item = value;

replace(keys, r, parent, item);


}
}

void FirebaseJsonBase::replace(MB_VECTOR<MB_String> &keys, struct


search_result_t &r, MB_JSON *parent, MB_JSON *item)
{
if (isArray(parent))
MB_JSON_ReplaceItemInArray(parent, getArrIndex(keys[r.foundIndex].c_str()),
item);
else
MB_JSON_ReplaceItemInObject(parent, keys[r.foundIndex].c_str(), item);
}

size_t FirebaseJsonBase::mIteratorBegin(MB_JSON *parent)


{
mIteratorEnd();
char *p = MB_JSON_PrintUnformatted(parent);
if (p == NULL)
return 0;

buf = p;
MB_JSON_free(p);
iterator_data.buf_size = buf.length();
int index = -1;
mIterate(parent, index);
return iterator_data.result.size();
}

size_t FirebaseJsonBase::mIteratorBegin(MB_JSON *parent,


MB_VECTOR<MB_String> *keys)
{
mIteratorEnd();

if (keys == NULL)
return 0;
int index = -1;
mIterate(parent, index);

return iterator_data.result.size();
}

void FirebaseJsonBase::mIteratorEnd(bool clearBuf)


{
if (clearBuf)
buf.clear();
iterator_data.path.clear();
iterator_data.buf_size = 0;
iterator_data.buf_offset = 0;
iterator_data.result.clear();
iterator_data.depth = -1;
iterator_data._depth = 0;
if (iterator_data.parentArr != NULL)
MB_JSON_Delete(iterator_data.parentArr);
iterator_data.parentArr = NULL;
}

void FirebaseJsonBase::mIterate(MB_JSON *parent, int &arrIndex)


{
if (!parent)
return;

bool isAr = isArray(parent);

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++;
}
}
}

void FirebaseJsonBase::mCollectIterator(MB_JSON *e, int type, int &arrIndex)


{
struct iterator_result_t result;

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);
}

int FirebaseJsonBase::mIteratorGet(size_t index, int &type, String &key, String


&value)
{
key.remove(0, key.length());
value.remove(0, value.length());
int depth = -1;

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);
}
}

char *m_val = (char *)newP(iterator_data.result[index].len2 + 1);


if (m_val)
{
memset(m_val, 0, iterator_data.result[index].len2 + 1);
int ofs = iterator_data.result[index].ofs1 + iterator_data.result[index].len1 +
iterator_data.result[index].ofs2;
int len = iterator_data.result[index].len2;

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;
}

struct FirebaseJsonBase::fb_js_iterator_value_t FirebaseJsonBase::mValueAt(size_t


index)
{
struct fb_js_iterator_value_t value;
int depth = mIteratorGet(index, value.type, value.key, value.value);
value.depth = depth;
return value;
}

void FirebaseJsonBase::toBuf(fb_json_serialize_mode mode)


{
if (root != NULL)
{
char *out = mode == fb_json_serialize_mode_pretty ? MB_JSON_Print(root) :
MB_JSON_PrintUnformatted(root);
if (out)
{
buf = out;
MB_JSON_free(out);
}
}
}

bool FirebaseJsonBase::mReadClient(Client *client)


{
// blocking read
buf.clear();
if (readClient(client, buf))
{
if (root != NULL)
MB_JSON_Delete(root);
root = parse(buf.c_str());
buf.clear();
return root != NULL;
}
return false;
}

bool FirebaseJsonBase::mReadStream(Stream *s, int timeoutMS)


{
// non-blocking read
if (readStream(s, serData, buf, true, timeoutMS))
{
if (root != NULL)
MB_JSON_Delete(root);
root = parse(buf.c_str());
buf.clear();
return root != NULL;
}
return false;
}

#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

const char *FirebaseJsonBase::mRaw()


{
toBuf(fb_json_serialize_mode_plain);
return buf.c_str();
}

bool FirebaseJsonBase::mRemove(const char *path)


{
bool ret = false;
prepareRoot();
MB_VECTOR<MB_String> keys = MB_VECTOR<MB_String>();
makeList(path, keys, '/');

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;
}

void FirebaseJsonBase::mGetPath(MB_String &path, MB_VECTOR<MB_String>


paths, int begin, int end)
{
if (end < 0 || end >= (int)paths.size())
end = paths.size() - 1;
if (begin < 0 || begin > end)
begin = 0;

for (int i = begin; i <= end; i++)


{
if (i > 0)
path += (const char *)MBSTRING_FLASH_MCR("/");
path += paths[i].c_str();
}
}

size_t FirebaseJsonBase::mGetSerializedBufferLength(bool prettify)


{
if (!root)
return 0;
return MB_JSON_SerializedBufferLength(root, prettify);
}

void FirebaseJsonBase::mSetFloatDigits(uint8_t digits)


{
floatDigits = digits;
}

void FirebaseJsonBase::mSetDoubleDigits(uint8_t digits)


{
doubleDigits = digits;
}

int FirebaseJsonBase::mResponseCode()
{
return httpCode;
}

bool FirebaseJsonBase::mGet(MB_JSON *parent, FirebaseJsonData *result, const


char *path, bool prettify)
{
bool ret = false;
prepareRoot();
MB_VECTOR<MB_String> keys = MB_VECTOR<MB_String>();
makeList(path, keys, '/');

if (keys.size() > 0)
{
if (isArrayKey(keys[0].c_str()) && root_type == Root_Type_JSON)
{
clearList(keys);
return false;
}
}

MB_JSON *_parent = parent;


struct search_result_t r;
searchElements(keys, parent, r);
_parent = r.parent;

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;
}

void FirebaseJsonBase::mSetResInt(FirebaseJsonData *data, const char *value)


{
if (strlen(value) > 0)
{
char *pEnd;
#if !defined(__AVR__)
value[0] == '-' ? data->iVal.int64 = strtoll(value, &pEnd, 10) : data-
>iVal.uint64 = strtoull(value, &pEnd, 10);
#else
value[0] == '-' ? data->iVal.int64 = strtol(value, &pEnd, 10) : data-
>iVal.uint64 = strtoull_alt(value);
#endif
}
else
data->iVal = {0};

data->intValue = data->iVal.int32;
data->boolValue = data->iVal.int32 > 0;
}

void FirebaseJsonBase::mSetResFloat(FirebaseJsonData *data, const char *value)


{
if (strlen(value) > 0)
{
char *pEnd;
data->fVal.setd(strtod(value, &pEnd));
}
else
data->fVal.setd(0);
data->doubleValue = data->fVal.d;
data->floatValue = data->fVal.f;
}

void FirebaseJsonBase::mSetElementType(FirebaseJsonData *result)


{
char *buf = (char *)newP(32);
if (result->type_num == MB_JSON_Invalid)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("undefined"));
result->typeNum = JSON_UNDEFINED;
}
else if (result->type_num == MB_JSON_Object)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("object"));
result->typeNum = JSON_OBJECT;
}
else if (result->type_num == MB_JSON_Array)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("array"));
result->typeNum = JSON_ARRAY;
}
else if (result->type_num == MB_JSON_String)
{
if (result->stringValue.c_str()[0] == '"')
result->stringValue.remove(0, 1);
if (result->stringValue.c_str()[result->stringValue.length() - 1] == '"')
result->stringValue.remove(result->stringValue.length() - 1, 1);

strcpy(buf, (const char *)MBSTRING_FLASH_MCR("string"));


result->typeNum = JSON_STRING;

// try casting the string to numbers


if (result->stringValue.length() <= 32)
{
mSetResInt(result, result->stringValue.c_str());
mSetResFloat(result, result->stringValue.c_str());
}
}
else if (result->type_num == MB_JSON_NULL)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("null"));
result->typeNum = JSON_NULL;
}
else if (result->type_num == MB_JSON_False || result->type_num ==
MB_JSON_True)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("boolean"));
bool t = strcmp(result->stringValue.c_str(), (const char
*)MBSTRING_FLASH_MCR("true")) == 0;
result->typeNum = JSON_BOOL;

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());

if (strpos(result->stringValue.c_str(), (const char *)MBSTRING_FLASH_MCR("."),


0) > -1)
{
double d = atof(result->stringValue.c_str());
if (d > 0x7fffffff)
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("double"));
result->typeNum = JSON_DOUBLE;
}
else
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("float"));
result->typeNum = JSON_FLOAT;
}
}
else
{
strcpy(buf, (const char *)MBSTRING_FLASH_MCR("int"));
result->typeNum = JSON_INT;
}
}

result->type = buf;
delP(&buf);
}

void FirebaseJsonBase::mSet(const char *path, MB_JSON *value)


{
prepareRoot();
MB_VECTOR<MB_String> keys = MB_VECTOR<MB_String>();
makeList(path, keys, '/');

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;
}
}

MB_JSON *parent = root;


struct search_result_t r;
searchElements(keys, parent, r);
parent = r.parent;

if (value == NULL)
value = MB_JSON_CreateNull();

if (r.status == key_status_mistype || r.status == key_status_not_existed)


replaceItem(keys, r, parent, value);
else if (r.status == key_status_out_of_range)
appendArray(keys, r, parent, value);
else if (r.status == key_status_existed)
replace(keys, r, parent, value);
else
MB_JSON_Delete(value);

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::operator=(FirebaseJson other)


{
if (isObject(other.root))
mCopy(other);
return *this;
}

FirebaseJson::FirebaseJson(FirebaseJson &other)
{
if (isObject(other.root))
mCopy(other);
}

FirebaseJson::~FirebaseJson()
{
clear();
}

FirebaseJson &FirebaseJson::nAdd(const char *key, MB_JSON *value)


{
prepareRoot();
MB_VECTOR<MB_String> keys = MB_VECTOR<MB_String>();
// makeList(key, keys, '/');
MB_String ky = key;
keys.push_back(ky);

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::operator=(FirebaseJsonArray other)


{
if (isArray(other.root))
mCopy(other);
return *this;
}

FirebaseJsonArray::FirebaseJsonArray(FirebaseJsonArray &other)
{
if (isArray(other.root))
mCopy(other);
}

FirebaseJsonArray &FirebaseJsonArray::nAdd(MB_JSON *value)


{
if (root_type != Root_Type_JSONArray)
mClear();

root_type = Root_Type_JSONArray;

prepareRoot();

if (value == NULL)
value = MB_JSON_CreateNull();

MB_JSON_AddItemToArray(root, value);

return *this;
}

bool FirebaseJsonArray::mGetIdx(FirebaseJsonData *result, int index, bool prettify)


{
bool ret = false;
prepareRoot();

result->clear();

MB_JSON *data = NULL;


if (isArray(root))
data = MB_JSON_GetArrayItem(root, index);

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;
}

bool FirebaseJsonArray::mSetIdx(int index, MB_JSON *value)


{
if (root_type != Root_Type_JSONArray)
mClear();
root_type = Root_Type_JSONArray;

prepareRoot();

int size = MB_JSON_GetArraySize(root);


if (index < size)
return MB_JSON_ReplaceItemInArray(root, index, value);
else
{
while (size < index)
{
MB_JSON_AddItemToArray(root, MB_JSON_CreateNull());
size++;
}
MB_JSON_AddItemToArray(root, value);
}
return true;
}

bool FirebaseJsonArray::mRemoveIdx(int index)


{
int size = MB_JSON_GetArraySize(root);
if (index < size)
{
MB_JSON_DeleteItemFromArray(root, index);
return size != MB_JSON_GetArraySize(root);
}
return false;
}

FirebaseJsonArray &FirebaseJsonArray::clear()
{
mClear();
return *this;
}

FirebaseJsonArray &FirebaseJsonArray::add(FirebaseJson &value)


{
MB_JSON *e = MB_JSON_Duplicate(value.root, true);
nAdd(e);
return *this;
}

FirebaseJsonArray &FirebaseJsonArray::add(FirebaseJsonArray &value)


{
MB_JSON *e = MB_JSON_Duplicate(value.root, true);
nAdd(e);
return *this;
}
FirebaseJsonData::FirebaseJsonData()
{
}

FirebaseJsonData::~FirebaseJsonData()
{
clear();
}

bool FirebaseJsonData::getArray(FirebaseJsonArray &jsonArray)


{
if (typeNum != FirebaseJson::JSON_ARRAY || !success || stringValue.length() ==
0)
return false;
return getArray(stringValue.c_str(), jsonArray);
}

bool FirebaseJsonData::mGetArray(const char *source, FirebaseJsonArray


&jsonArray)
{

if (jsonArray.root != NULL)
MB_JSON_Delete(jsonArray.root);

jsonArray.root = jsonArray.parse(source);

return jsonArray.root != NULL;


}

bool FirebaseJsonData::getJSON(FirebaseJson &json)


{
if (typeNum != FirebaseJson::JSON_OBJECT || !success || stringValue.length() ==
0)
return false;
return getJSON(stringValue.c_str(), json);
}

bool FirebaseJsonData::mGetJSON(const char *source, FirebaseJson &json)


{
if (json.root != NULL)
MB_JSON_Delete(json.root);

json.root = json.parse(source);

return json.root != NULL;


}

size_t FirebaseJsonData::getReservedLen(size_t len)


{
int blen = len + 1;

int newlen = (blen / 4) * 4;

if (newlen < blen)


newlen += 4;

return (size_t)newlen;
}

void FirebaseJsonData::delP(void *ptr)


{
void **p = (void **)ptr;
if (*p)
{
free(*p);
*p = 0;
}
}

void *FirebaseJsonData::newP(size_t len)


{
void *p;
size_t newLen = getReservedLen(len);
#if defined(BOARD_HAS_PSRAM) && defined(MB_STRING_USE_PSRAM)
if (ESP.getPsramSize() > 0)
p = (void *)ps_malloc(newLen);
else
p = (void *)malloc(newLen);
if (!p)
return NULL;

#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

5.Diagrama del circuito


6. Foto del prototipo y circuitos

You might also like