vincent wong
Published © GPL3+

Quick Start with SenseCAP K1100 and Light and IMU Sensors

Quick Start with SenseCAP K1100 and Light and IMU Sensors

BeginnerProtip3 hours129
Quick Start with SenseCAP K1100 and Light and IMU Sensors

Things used in this project

Story

Read more

Code

wio_vision_ai_blynk

C/C++
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings

#define BLYNK_TEMPLATE_ID "blynk template"
#define BLYNK_DEVICE_NAME "blynk device name"
#define BLYNK_AUTH_TOKEN "blynk token"

 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi ssid";
char pass[] = "wifi password";
 
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
 
#include <rpcWiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleWioTerminal.h>
#include "Seeed_Arduino_GroveAI.h"
#include <Wire.h>

#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis; 


GroveAI ai(Wire);
uint8_t state = 0;
 
char auth[] = BLYNK_AUTH_TOKEN;
 
BlynkTimer timer;
 
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  if (state == 1)
  {
      int light = analogRead(WIO_LIGHT);  //Get the Wio Terminal light value.
      Serial.print("Light Value: "); Serial.println(light);
      Blynk.virtualWrite(V3, light);

      float x_values, y_values, z_values;
      x_values = lis.getAccelerationX();
      y_values = lis.getAccelerationY();
      z_values = lis.getAccelerationZ();
    
      int x = x_values*100;
      int y = y_values*100;
      int z = z_values*100;
    
      Serial.print("X: "); Serial.print((float)x/100);
      Serial.print(" Y: "); Serial.print((float)y/100);
      Serial.print(" Z: "); Serial.println((float)z/100);      
  
      Blynk.virtualWrite(V0, (float)x/100);
      Blynk.virtualWrite(V1, (float)y/100);
      Blynk.virtualWrite(V2, (float)z/100);

    if (ai.invoke()) // begin invoke
    {
      while (1) // wait for invoking finished
      {
        CMD_STATE_T ret = ai.state(); 
        if (ret == CMD_STATE_IDLE)
        {
          break;
        }
        delay(20);
      }
     uint8_t len = ai.get_result_len(); // receive how many people detect
     if(len)
     {
       Serial.print("Number of people: ");
       Serial.println(len);
       object_detection_t data;       //get data
 
       for (int i = 0; i < len; i++)
       {
          Serial.println("result:detected");
          Serial.print("Detecting and calculating: ");
          Serial.println(i+1);
          ai.get_result(i, (uint8_t*)&data, sizeof(object_detection_t)); //get result
          Serial.print("confidence:");
          Serial.print(data.confidence);
          Serial.println();
          Blynk.virtualWrite(V5, data.confidence);
          Blynk.virtualWrite(V4, len);
        }
     }
     else
     {
       Serial.println("No identification");
       Blynk.virtualWrite(V4, 0);
       Blynk.virtualWrite(V5, 0);
     }
    }
    else
    {
      delay(1000);
      Serial.println("Invoke Failed.");
    }
  }
  else
  {
    state == 0;
  }
}


 
void setup()
{
  // Debug console
  Serial.begin(115200);
 
  Wire.begin();
 
  Serial.println("begin");
  if (ai.begin(ALGO_OBJECT_DETECTION, MODEL_EXT_INDEX_1)) // Object detection and pre-trained model 1
  {
    Serial.print("Version: ");
    Serial.println(ai.version());
    Serial.print("ID: ");
    Serial.println( ai.id());
    Serial.print("Algo: ");
    Serial.println( ai.algo());
    Serial.print("Model: ");
    Serial.println(ai.model());
    Serial.print("Confidence: ");
    Serial.println(ai.confidence());
    state = 1;
  }
  else
  {
    Serial.println("Algo begin failed.");
  }

   // IMU init
  lis.begin(Wire1);
  if (!lis){
    Serial.println("ERROR");
    while(1);
  }
  lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);   //Data output rate
  lis.setFullScaleRange(LIS3DHTR_RANGE_2G);        //Scale range set to 2g


  
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
 
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}
 
void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

Credits

vincent wong
81 projects • 205 followers
Contact

Comments

Please log in or sign up to comment.