Usama Ahmad Saleem
Published

Boxed Solution for smart cradle system

With the increasing necessity of smart cradles we have devised a solution to make the pre existing cradles and carts iot based smart cradle

IntermediateWork in progress24 hours770
Boxed Solution for smart cradle system

Things used in this project

Story

Read more

Schematics

Integration of esp8266 and MAX9814

Code

Code for esp8266 and MAX9814

Arduino
#include <ESP8266WiFi.h>
#include <Grandeur.h>
#define MICROPHONE_PIN A0
#define AUDIO_BUFFER_MAX 4096

int counttt,cnt,ii=0;
int audioStartIdx = 0, audioEndIdx = 0;
unsigned char audioBuffer[AUDIO_BUFFER_MAX];
unsigned char txBuffer[AUDIO_BUFFER_MAX] ;

// version without timers
unsigned long lastRead = micros();

String apiKey = "grandeurl67j2h69002e0jy0dwuz1vho";
String deviceID = "devicel67j2i38002o0jy09xhv0n75";
String token = "45422785aa8b1ef2ac73310b912b2588ddafc6cd1ae642d0722328d38dc1c8c9";

/* WiFi credentials */
String ssid = "Usama";
String password = "12345678";
unsigned int BC;

void setup() {
    Serial.begin(115200);
     /* Connect to WiFi */
    connectWiFi();
    /* Initializes the global object "grandeur" with your configurations. */
    project = grandeur.init(apiKey, token);

    /* Get reference to device */
    device = project.device(deviceID);

    pinMode(MICROPHONE_PIN, INPUT);
    
    // 1/8000th of a second is 125 microseconds
    counttt=0;
    cnt=0;
    lastRead = micros();
}

void loop() {
    //listen for 100ms, taking a sample every 125us,
    //and then send that chunk over the network.
    listenAndSend(100);
    
}

void listenAndSend(int delay) {
    unsigned long startedListening = millis();
    
    while ((millis() - startedListening) < delay) {
        unsigned long time = micros();
        
        if (lastRead > time) {
            // time wrapped?
            //lets just skip a beat for now, whatever.
            lastRead = time;
        }
        
        //125 microseconds is 1/8000th of a second
        if ((time - lastRead) > 125) {
            lastRead = time;
            readMic();
        }
    }
    sendAudio();
}

 
// Callback for Timer 1
void readMic(void) {
    unsigned char value = analogRead(MICROPHONE_PIN);
    if (audioEndIdx >= AUDIO_BUFFER_MAX) {
        audioEndIdx = 0;
    }
    audioBuffer[audioEndIdx++] = value;
}

void copyAudio(unsigned char *bufferPtr) {
    //if end is after start, read from start->end
    //if end is before start, then we wrapped, read from start->max, 0->end
    
    int endSnapshotIdx = audioEndIdx;
    bool wrapped = endSnapshotIdx < audioStartIdx;
    int endIdx = (wrapped) ? AUDIO_BUFFER_MAX : endSnapshotIdx;
    int c = 0;
    
    for(int i=audioStartIdx;i<endIdx;i++) {
        // do a thing
        bufferPtr[c++] = audioBuffer[i];
    }
    
    if (wrapped) {
        //we have extra
        for(int i=0;i<endSnapshotIdx;i++) {
            // do more of a thing.
            bufferPtr[c++] = audioBuffer[i];
        }
    }
    
    //and we're done.
    audioStartIdx = audioEndIdx;
    
    if (c < AUDIO_BUFFER_MAX) {
        bufferPtr[c] = -1;
    }
}

// Callback for Timer 1
void sendAudio(void) {
    copyAudio(txBuffer);
    while( txBuffer[ii] < 4096 ) {
         Serial.print(txBuffer[ii++]);
         Serial.print(',');
         if(txBuffer[ii++]<100)
         {
          cnt++;         
         }
         if(cnt>6)
         {
          device.data().set("BC",1);
          }
    }
        Serial.println("DONE");
  cnt=0;  
}

void connectWiFi() {
    /* Set mode to station */
    WiFi.mode(WIFI_STA);

    /* Connect using the ssid and password */
    WiFi.begin(ssid, password);

    /* Block till the WiFi is connected */
    while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
    }

    /* Print when connected */
    Serial.println("");
    Serial.println("WiFi connected");
  Serial.println(ssid);
    /* And IP address */
    Serial.println(WiFi.localIP());
}

Credits

Usama Ahmad Saleem

Usama Ahmad Saleem

1 project • 3 followers

Comments