Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15622

ESP32 Wake up from sleep on rising

$
0
0

I have a treadmill with a flywheel that sends "rising" interrupts. The "sensor" at the flywheel is powered off the 3.3v ping of the esp32 (power), the gnd and the 3rd wire that sees the interrupts is on pin 13. In my code, I can successfully read flywheel interrupts on this same pin to count the amount of flywheel interrupts and run on Zwift. I successfully keep the board from going to sleep, and it goes to sleep successfully when no interrupts are read for x amount of time.

My problem is the way the current "wake" is doing it's thing on the value "1" which means "high", the board wakes back up immediately after sleeping.

I'll be honest, I tried a few options to read my flywheel speed and I could just see that "rising" worked, but I don't understand the difference between rising and high. I don't know if my signal from the flywheel is seeing a constant 3v, which could explain why it wakes up, or if it passes the 3v through when the nubby on the flywheel goes by.

Can I get a similar thing as "rising" to wake the board?

Most of this code is copy/paste from projects I found and tutorials, so speak to me like I'm a newb :wink:

Open to suggestions on cleaning up the code, assuming I can understand them :slight_smile: Thanks in advance for any help!!!

//#include <Arduino.h>
#include "HT16K33.h"
#include <TimeLib.h>
#include <Time.h>
#include <WiFi.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include <math.h>

//Identify the 4 LCD's
HT16K33 speedDisplay(0x71);
HT16K33 timeDisplay(0x70);
HT16K33 paceDisplay(0x72);
HT16K33 distanceDisplay(0x74);

//WIFI
const char* ssid     = "*****";
const char* password = "*****";

//NTP Server
const char* ntpServer = "ca.pool.ntp.org";
const long  gmtOffset_sec = -5 * 3600;
const int   daylightOffset_sec = 0;

// Treadmill
uint16_t inst_speed = 0;
unsigned long currentTime;
unsigned long sleepTimer;
int sleepCounter;
int flywheel = 0; //Flywheel interrupts
int lastFlywheel = 0;
double beltDistance = 0.187; //The distance the belt will travel per flywheel interrupt in meters
double loopDistance;
float runDistance = 0.00;
float runPace = 0.00;
float kmph;
float mps;


//I don't know what this is for, came with the BLE code
byte fakePos[1] = {1};

bool _BLEClientConnected = false;

#define RSCService BLEUUID((uint16_t)0x1814)
BLECharacteristic RSCMeasurementCharacteristics(BLEUUID((uint16_t)0x2A53), BLECharacteristic::PROPERTY_NOTIFY);
BLECharacteristic sensorPositionCharacteristic(BLEUUID((uint16_t)0x2A5D), BLECharacteristic::PROPERTY_READ);

BLEDescriptor RSCDescriptor(BLEUUID((uint16_t)0x2901));
BLEDescriptor sensorPositionDescriptor(BLEUUID((uint16_t)0x2901));

class MyServerCallbacks : public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      _BLEClientConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      _BLEClientConnected = false;
    }
};

void InitBLE() {
  BLEDevice::init("Trueform Runner");
  // CBLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pRSC = pServer->createService(RSCService);

  pRSC->addCharacteristic(&RSCMeasurementCharacteristics);
  RSCDescriptor.setValue("Rate from 0 to 200");
  RSCMeasurementCharacteristics.addDescriptor(&RSCDescriptor);
  RSCMeasurementCharacteristics.addDescriptor(new BLE2902());

  pRSC->addCharacteristic(&sensorPositionCharacteristic);
  pServer->getAdvertising()->addServiceUUID(RSCService);
  pRSC->start();
  pServer->getAdvertising()->start();
}

void flywheelInterrupt() {
  flywheel++;
}

void setup() {
  Serial.begin(115200);

  //Attach flywheel interrupts
  pinMode(13, INPUT);
  attachInterrupt(digitalPinToInterrupt(13),flywheelInterrupt,RISING);
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 1); // This is what I found online for waking the board, which I assume is being attached to pin 13

  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");

  //Setup Treadmill Stats and Displays
  Wire.begin();
  Wire.setClock(100000);

  paceDisplay.begin();
  speedDisplay.begin();
  distanceDisplay.begin();
  timeDisplay.begin();

  paceDisplay.displayOn();
  paceDisplay.displayColon(1);
  speedDisplay.displayOn();
  speedDisplay.displayColon(0);
  distanceDisplay.displayOn();
  distanceDisplay.displayColon(0);
  timeDisplay.displayOn();
  timeDisplay.setDigits(4);
  timeDisplay.displayColon(1);

  InitBLE();
}

void loop(){
  //Save flywheel count
  int loopFlywheel = flywheel - lastFlywheel;
  lastFlywheel = flywheel; 
  Serial.println("lastFlywheel");
  Serial.println(lastFlywheel);

  if (loopFlywheel == 0 && sleepCounter == 0) {
    sleepCounter = 1;
    sleepTimer = currentTime;
  }

  else if (loopFlywheel > 0) {
    sleepCounter = 0;
    sleepTimer = currentTime;
  }

  //if ((currentTime - sleepTimer) > 600000) {
  if ((currentTime - sleepTimer) > 45000) {
    Serial.println("go to sleep");
    esp_deep_sleep_start();
  }

Serial.println("sleeptimer");
Serial.println(currentTime-sleepTimer);
  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);  //Don't let time drift
  unsigned long previousTime = currentTime; //So we can see how much time passed since last loop
    
  // From here on, we only use the standard C timing functions.
  // time() returns the current time as a single number of type time_t,
  // this is the number of seconds elapsed since a reference "epoch".
  time_t now = time(nullptr);
  currentTime = millis();
  float elapsedTime = currentTime - previousTime;
  float elapsedSeconds = elapsedTime/1000;
 
  Serial.println("flywheel");
  Serial.println(flywheel);
  
  loopDistance = loopFlywheel*beltDistance;
  Serial.println("loopDistance");
  Serial.println(loopDistance);
  mps = loopDistance/elapsedSeconds;
  Serial.println("mps");
  Serial.println(mps);
  kmph=mps*3.6;
  Serial.println("kmph");
  Serial.println(kmph);
  double runSpeed = kmph;
  inst_speed = (256/3.6)*kmph; //Zwift Speed
  runPace = elapsedTime/loopDistance; //Calculate run pace in km
  runDistance+= loopDistance/1000;
  uint8_t paceday, pacehrs, pacemin, pacesec;
  convertTime (runPace, paceday, pacehrs, pacemin, pacesec); //Convert pace to minutes
   
  paceDisplay.displayTime(pacemin, pacesec, true, false);
  speedDisplay.displayFloat(runSpeed, 1);
  distanceDisplay.displayFloat(runDistance, 2);
  timeDisplay.displayTime(minute(), second(), true, false); //do not display leading zero.

  //Create the bytearray to send to Zwift via BLE
  byte charArray[10] = {
      3,
      (unsigned byte)inst_speed, (unsigned byte)(inst_speed >> 8)

  };

  RSCMeasurementCharacteristics.setValue(charArray,10);
  RSCMeasurementCharacteristics.notify();
  sensorPositionCharacteristic.setValue(fakePos, 1);

   
  delay(500);
}

void convertTime(unsigned long timeInSeconds, uint8_t &days, uint8_t &hours, uint8_t &minutes, uint8_t &seconds) {
  seconds = timeInSeconds % 60;
  timeInSeconds /= 60;
  minutes = timeInSeconds % 60;
  timeInSeconds /= 60;
  hours = timeInSeconds % 24;
  days = timeInSeconds / 24;

}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15622

Latest Images

Trending Articles



Latest Images