Hackster is hosting Impact Spotlights highlighting smart energy storage. Start streaming on Thursday!Stream Impact Spotlights on Thursday!
vinay y.n
Published © GPL3+

Ameba pro 2 SOS: Alerts, Maps & Camera

Ameba Pro 2 SOS snaps pic & shares location (Telegram & Maps) for fast help!

IntermediateFull instructions provided16 hours257
Ameba pro 2 SOS: Alerts, Maps & Camera

Things used in this project

Hardware components

Realtek AMB82-Mini
×1
GNSS L89
×1
Rechargeable Battery, 3.7 V
Rechargeable Battery, 3.7 V
×1
Buzzer
Buzzer
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Flash Memory Card, MicroSDHC Card
Flash Memory Card, MicroSDHC Card
×1

Software apps and online services

Google Maps
Google Maps
Arduino IDE
Arduino IDE
Telegram

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Sch

Code

CODE

Arduino
Requires ArduinoJson library version 6.5.12.
#include <TinyGPS++.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include "VideoStream.h"
#include "AmebaFatFS.h"
int button = 17;
int button_status ;
int buzzer = 20;
char ssid[] = "SSID";    // your network SSID (name)
char pass[] = "Password";      // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;          // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;    // Indicater of Wifi status
unsigned long startMillis = 0;        // will store last time LED was updated
const long interval = 200;           // interval at which to blink (milliseconds)
#define BOTtoken "Your Telegram Bot Token" // your Bot Token (Get from Botfather)
#define CHAT_ID "Bot chat ID"
#define CHANNEL 2
#define FILENAME "image.jpg"
VideoSetting config(VIDEO_FHD, CAM_FPS, VIDEO_JPEG, 1);
uint32_t img_addr = 0;
uint32_t img_len = 0;
AmebaFatFS fs;
WiFiSSLClient wifiClient;
UniversalTelegramBot bot(BOTtoken, wifiClient);
const unsigned long BOT_MTBS = 100; // mean time between scan messages
unsigned long bot_lasttime;     // last time messages' scan has been done
float lat, lon;
int i;
File myFile;
bool isMoreDataAvailable();
byte getNextByte();
const int GPSBaud = 9600;
String chat_id = "Bot chat ID";
String file_name = FILENAME;
char commandbuffer[100] = {0}; // Initialize with zeros to prevent garbage data
int z = 0; // Index for command buffer
TinyGPSPlus gps;

bool isMoreDataAvailable()
{
  return myFile.available();
}

byte getNextByte()
{
  return myFile.read();
}

void _delay()
{
  startMillis = millis();
    while((millis() - startMillis) <= interval) {
     }
}
void handleNewMessages(int numNewMessages)
{

for (int i = 0; i < numNewMessages; i++)
  {
    String chat_id = bot.messages[i].chat_id;
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "")
      from_name = "Guest";
      if (text == "/gps")
    {
      String gps_data;
    gps_data = "https://maps.google.com/maps?q=";
    gps_data += String(gps.location.lat(), 6); // Format latitude to 6 decimal places
    gps_data += ",";
    gps_data += String(gps.location.lng(), 6); // Format longitude to 6 decimal places
    bot.sendMessage(chat_id, gps_data, "");
    }
if (text == "/pic")
    {
    capture_image();
    }
}}
void send_image() {
  if (millis() - bot_lasttime > BOT_MTBS) {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages)
    {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    bot_lasttime = millis();
  }
}
void capture_image()
{
 // digitalWrite(buzzer,HIGH);
  File file = fs.open(String(fs.getRootPath()) + String(FILENAME));
  delay(100);
  //digitalWrite(buzzer,LOW);
  Camera.getImage(CHANNEL, &img_addr, &img_len);
  file.write((uint8_t*)img_addr, img_len);
  file.close();
  delay(100);
  //digitalWrite(buzzer,HIGH);
  Serial.print("Image Captured");
  myFile = fs.open(file_name);

  if (myFile)
  {
    Serial.print(file_name);
    Serial.print("....");

    //Content type for PNG image/png
    String sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", myFile.size(),
                                        isMoreDataAvailable,
                                        getNextByte, nullptr, nullptr);

    if (sent)
    {
      Serial.println("was successfully sent");
    for (int i = 0; i < 5; i++) {
    digitalWrite(buzzer, HIGH);   // Turn buzzer on
    _delay();                     // Beep duration (adjust as needed)
    digitalWrite(buzzer, LOW);    // Turn buzzer off
    _delay();                     // Silence between beeps (adjust as needed)
  }
    }
    else
    {
      Serial.println("was not sent");
    }

    myFile.close();
  }
  else
  {
    Serial.println("error opening photo");
  }
  send_image();
}
void processGPSData() {
  while (Serial2.available() > 0) {
    gps.encode(Serial2.read());
    if (gps.location.isUpdated()) {
      Serial.print("Latitude= ");
      Serial.print(gps.location.lat(), 6);
      Serial.print(" Longitude= ");
      Serial.println(gps.location.lng(), 6);
    }
  }
}
void setup()
{
  Serial.begin(115200);
  pinMode(button, INPUT);
  pinMode(buzzer,OUTPUT);
  Serial.println();
  Camera.configVideoChannel(CHANNEL, config);
  Camera.videoInit();
  Camera.channelBegin(CHANNEL);
  fs.begin();
  Serial.println("done.");
  Serial.print("Connecting to Wifi SSID ");
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nWiFi connected. IP address: ");
  Serial.println(WiFi.localIP());
  bot.sendMessage(CHAT_ID, "Bot started up", "");
  Serial2.begin(9600);
   for (int i = 0; i < 5; i++) {
    digitalWrite(buzzer, HIGH);   // Turn buzzer on
    _delay();                     // Beep duration (adjust as needed)
    digitalWrite(buzzer, LOW);    // Turn buzzer off
    _delay();                     // Silence between beeps (adjust as needed)
  }
}


void loop()
{
  button_status = digitalRead(button);
  if (button_status == 1)
  {
      for (int i = 0; i < 2; i++) {
    digitalWrite(buzzer, HIGH);   // Turn buzzer on
    _delay();                     // Beep duration (adjust as needed)
    digitalWrite(buzzer, LOW);    // Turn buzzer off
    _delay();                     // Silence between beeps (adjust as needed)
  }
    Serial.print( "Button Pressed");
    String gps_data;
    gps_data = "https://maps.google.com/maps?q=";
    gps_data += String(gps.location.lat(), 6); // Format latitude to 6 decimal places
    gps_data += ",";
    gps_data += String(gps.location.lng(), 6); // Format longitude to 6 decimal places
    delay(300);
    digitalWrite(buzzer,LOW);
    bot.sendMessage(chat_id, gps_data, "");
    //digitalWrite(buzzer,LOW);
    capture_image();
  }

  processGPSData();
if (millis() - bot_lasttime > BOT_MTBS)
  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages)
    {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    bot_lasttime = millis();
  }
}

Credits

vinay y.n

vinay y.n

26 projects • 43 followers
An electronic product engineer with 8 years of experience in the field. The passion for electronics began as a hobby 11 years ago.

Comments