I am trying to upload a code to the NodeMCU to Ask FirebaseDatabase for the Sensor State i.e whether Sensor is 1 or 0 which I will change according to my need. Then read the data from the sensor and update on Firebase.
Currently I am Using 2 Sensors i.e MPU6050 and MAX30100. My code is something like this:
#include <MAX30100_PulseOximeter.h>
#include<FirebaseESP8266.h>
#include<ESP8266WiFi.h>
#include<Wire.h>
/////////////////////Define Pins Here///////////////
const int MPU6050pin=D0;
const int MAX30100pin=D3;
////////////////////////////////////////////////////
#define FIREBASE_HOST "//my host" // database URL
#define FIREBASE_AUTH "//my auth" // secret key
#define WIFI_SSID "//my ssd" //Wifi Name
#define WIFI_PASSWORD "//my wifi password" //Wifi Password
#define REPORTING_PERIOD_MS 1000
/////////////////////////////////////////////////////
/////////////////////Define Sensors Here//////////////
const int MPU_addr=0x68; // I2C address of the MPU-6050
//////////////////////////////////////////////////////
////////////////////Define Variables Here/////////////
float AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
FirebaseData firebaseData;
int MPU6050state,MAX30100state;
boolean MPU6050flag,MAX30100flag;
PulseOximeter pox;
uint32_t tsLastReport = 0;
int MAX30100firstTime=0;
///////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD); //Connecting to Wifi Network
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
//////////////////Set Up Pins Here///////////////
pinMode(MPU6050pin,OUTPUT);
pinMode(MAX30100pin,OUTPUT);
/////////////////////////////////////////////////
/////////////////Initialize all Sensors to OFF///////
digitalWrite(MPU6050pin,LOW);
digitalWrite(MAX30100pin,LOW);
////////////////////////////////////////////////////
}
void onBeatDetected()
{
Serial.println("Beat!");
}
void UploadMPUToFirebase(){
Firebase.setFloat(firebaseData,"/Readings/MPU6050/RoomTemperature",(Tmp/340.00)+36.53);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Acceleration/0",AcX/16384);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Acceleration/1",AcY/16384);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Acceleration/2",AcZ/16384);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Gyro/0",GyX/131);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Gyro/1",GyY/131);
Firebase.setFloat(firebaseData,"/Readings/MPU6050/Gyro/2",GyZ/131);
}
void UploadMAXtoFirebase(){
}
void MPU6050Data(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void MAX30100DataSetup(){
Serial.print("Initializing pulse oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
} else {
Serial.println("SUCCESS");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// Register a callback for the beat detection
pox.setOnBeatDetectedCallback(onBeatDetected);
MAX30100firstTime=1;
MAX30100DataLoop();
}
void MAX30100DataLoop(){
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
tsLastReport = millis();
}
}
void TurnSensorsOnOff(){
if(MPU6050flag){
digitalWrite(MPU6050pin,HIGH);
MPU6050Data();
UploadMPUToFirebase();
}else{
digitalWrite(MPU6050pin,LOW);
}
if(MAX30100flag){
digitalWrite(MAX30100pin,HIGH);
if(MAX30100firstTime==0){
MAX30100DataSetup();
}else{
MAX30100DataLoop();
}
UploadMAXtoFirebase();
}else{
digitalWrite(MAX30100pin,LOW);
MAX30100firstTime=0;
}
}
void loop() {
if(Firebase.get(firebaseData, "/SensorState/switchGesture/switchGestureState"))
{
if(firebaseData.dataType() == "int"){
MPU6050state=firebaseData.intData();
Serial.println("MPU6050 = ");
Serial.print(MPU6050state);
}else{
Serial.println("MPU state is Not Retrieved");
MPU6050state=0;
}
if(MPU6050state==0){
MPU6050flag=false;
}else{
MPU6050flag=true;
}
}
if(Firebase.get(firebaseData, "/SensorState/switchOxygenHeartBeat/switchOxygenHeartBeatState"))
{
if(firebaseData.dataType() == "int"){
MAX30100state=firebaseData.intData();
Serial.println("MAX30100 = ");
Serial.print(MAX30100state);
}else{
Serial.println("MAX state is Not Retrieved");
MAX30100state=0;
}
if(MAX30100state==0){
MAX30100flag=false;
}else{
MAX30100flag=true;
}
}
/////////////////////////////////Add otherSensorStateCheakingHere
TurnSensorsOnOff();
}
When I try to upload this code to NodeMCU it compiles successfully but gives this error while uploading
Executable segment sizes:
IROM : 451380 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 28276 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1344 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1936 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 27184 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 482936 bytes (46%) of program storage space. Maximum is 1044464 bytes.
Global variables use 30464 bytes (37%) of dynamic memory, leaving 51456 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.8
Serial port COM8
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 2c:f4:32:20:7d:8e
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 487088 bytes to 354774...
Writing at 0x00000000... (4 %)
Writing at 0x00004000... (9 %)
Writing at 0x00008000... (13 %)
Writing at 0x0000c000... (18 %)
Writing at 0x00010000... (22 %)
Writing at 0x00014000... (27 %)
Writing at 0x00018000... (31 %)Traceback (most recent call last):
File "C:\Users\Prashang\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1/tools/upload.py", line 65, in <module>
esptool.main(cmdline)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 2938, in main
operation_func(esp, args)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 2374, in write_flash
esp.flash_defl_block(block, seq, timeout=DEFAULT_TIMEOUT * ratio * 2)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 104, in inner
return func(*args, **kwargs)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 672, in flash_defl_block
self.ESP_FLASH_DEFL_DATA, struct.pack('<IIII', len(data), seq, 0, 0) + data, self.checksum(data), timeout=timeout)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 369, in check_command
val, data = self.command(op, data, chk, timeout=timeout)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 347, in command
p = self.read()
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 292, in read
return next(self._slip_reader)
File "C:/Users/Prashang/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 2045, in slip_reader
raise FatalError("Timed out waiting for packet %s" % waiting_for)
esptool.FatalError: Timed out waiting for packet header
esptool.FatalError: Timed out waiting for packet header
Although I found some similar issues so I tried some things to solve this problem:
Node of the above solutions worked.
Adding: The basic examples provided with libraries is uploading and working perfectly that means there is no problem with hardware.
User contributions licensed under CC BY-SA 3.0