Chathuranga Liyanage
Published © GPL3+

Portable Monitor for Home Supplies

Monitor the levels of your essential supplies and notify or order them automatically when they are low.

IntermediateFull instructions provided3 hours1,320
Portable Monitor for Home Supplies

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Adafruit Force Sensitive Resistor
×1
Adafruit Sharp IR
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Slide Switch
Slide Switch
×2
Resistor 100 ohm
Resistor 100 ohm
×1
Resistor 22.1k ohm
Resistor 22.1k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
AWS Lambda
Amazon Web Services AWS Lambda
AWS IoT
Amazon Web Services AWS IoT

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

Enclosure

Schematics

Fritzing Diagram

Code

Email Notifier

Arduino
#include <ESP8266WiFi.h>
#include "Gsender.h"

#define measure A0    //Sensor Readings
#define red D3          
#define blue D0         //RGB pins of LED
#define green D1
#define LED_POWER D2
#define sw1_weight D5       //Switch 1 positions
#define sw2_power D6
#define sw2_calibrate D8          //Switch 2 positions
#define sw2_check D7



int low=10;
int high=50;
int reading;
int absolute_val=0;
int fal=1;

#pragma region Globals
const char* ssid = "Type your SSID";                           // WIFI network name
const char* password = "Type your password";                       // WIFI network password
uint8_t connection_state = 0;                    // Connected to WIFI or not
uint16_t reconnect_interval = 10000;             // If not connected wait time to try again
#pragma endregion Globals

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
    static uint16_t attempt = 0;
    Serial.print("Connecting to ");
    if(nSSID) {
        WiFi.begin(nSSID, nPassword);  
        Serial.println(nSSID);
    } else {
        WiFi.begin(ssid, password);
        Serial.println(ssid);
    }

    uint8_t i = 0;
    while(WiFi.status()!= WL_CONNECTED && i++ < 50)
    {
        delay(200);
        Serial.print(".");
    }
    ++attempt;
    Serial.println("");
    if(i == 51) {
        Serial.print("Connection: TIMEOUT on attempt: ");
        Serial.println(attempt);
        if(attempt % 2 == 0)
            Serial.println("Check if access point available or SSID and Password\r\n");
        return false;
    }
    Serial.println("Connection: ESTABLISHED");
    Serial.print("Got IP address: ");
    Serial.println(WiFi.localIP());
    return true;
}

void Awaits()
{
    uint32_t ts = millis();
    while(!connection_state)
    {
        delay(50);
        if(millis() > (ts + reconnect_interval) && !connection_state){
            connection_state = WiFiConnect();
            ts = millis();
        }
    }
}

void conn(){



    Gsender *gsender = Gsender::Instance();    // Getting pointer to class instance
    String subject = "Type the subject of the Email";
    if(gsender->Subject(subject)->Send("Receiver's Email", "Email Body")) {
        Serial.println("Message send.");
    } else {
        Serial.print("Error sending message: ");
        Serial.println(gsender->getError());
    }
}

void get_readings(){

  reading=0;
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);        //get readings from the sensor
    delay(5);
  }
  absolute_val = reading /20;
  
}
void calibrate(){
  reading=0;
  for(int i=0;i<20;i++)                                       //calibrate 
    reading = reading+analogRead(measure);

  absolute_val = reading /20;
  low = absolute_val;
  high = absolute_val;

  while (digitalRead(sw2_calibrate)==HIGH){
    reading=0;
  digitalWrite(blue,LOW);  
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);
    delay(5);
  }
  digitalWrite(blue,HIGH);
  absolute_val = reading /20;
    if (absolute_val<low) low = absolute_val;
    if(absolute_val>high) high=absolute_val;

   delay(100);  
      
  }

digitalWrite(blue,HIGH);

for (int a=0; a<150;a++){

  reading=0;
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);
    delay(5);
  }
  absolute_val = reading /20;

  if(absolute_val <= low) {
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
  }
  if(absolute_val > low+20) {
    digitalWrite(red,HIGH);
    digitalWrite(green,LOW);
  }

  delay(1);
}

  digitalWrite(red,HIGH);
  digitalWrite(green,HIGH);
  
}

void setup() {

  Serial.begin(115200);
  
  pinMode(measure, INPUT);  
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(sw2_power,OUTPUT);
  pinMode(LED_POWER,OUTPUT);

  digitalWrite(sw2_power,HIGH);
  digitalWrite(red,HIGH);
  digitalWrite(green,HIGH);
  digitalWrite(blue,HIGH);
  digitalWrite(LED_POWER,HIGH);

      
  connection_state = WiFiConnect();
    if(!connection_state)  // if not connected to WIFI
        Awaits();          // constantly trying to connect

}

void loop() {

  while(digitalRead(sw2_calibrate)!=HIGH){delay(100);}             //jump in to calibration mode

  if(digitalRead(sw2_calibrate)==HIGH) calibrate();

  while(digitalRead(sw2_calibrate)!=HIGH){

    get_readings();                                     
    
    if(absolute_val <= low){
      int count = 0;                                        //Detects low values
      for (int z=0;z<50;z++){
        get_readings();
        if(absolute_val <= low) count++;
      }
      if (count > 45 && fal == 1){
        Serial.println("LOW DETECT");
        digitalWrite(red,LOW);    
        fal = 0;
        conn();                                         //Low supplies email reminder
      }

      while(absolute_val < (low+high)/2){
        get_readings();                                   //Supply refill checker
        
      }

      if(absolute_val > (low+high)/2){
        digitalWrite(red,HIGH);
        fal = 1;
      }
        
      else count=0;
    }
  }

}

Gsender.h

Arduino
#ifndef G_SENDER
#define G_SENDER
#define GS_SERIAL_LOG_1         // Print to Serial only server responce
//#define GS_SERIAL_LOG_2       //  Print to Serial client commands and server responce
#include <WiFiClientSecure.h>

class Gsender
{
    protected:
        Gsender();
    private:
        const int SMTP_PORT = 465;
        const char* SMTP_SERVER = "smtp.gmail.com";
        const char* EMAILBASE64_LOGIN = "Email address in 64 base";
        const char* EMAILBASE64_PASSWORD = "Password in 64 base";
        const char* FROM = "Low Supplies";
        const char* _error = nullptr;
        char* _subject = nullptr;
        String _serverResponce;
        static Gsender* _instance;
        bool AwaitSMTPResponse(WiFiClientSecure &client, const String &resp = "", uint16_t timeOut = 10000);

    public:
        static Gsender* Instance();
        Gsender* Subject(const char* subject);
        Gsender* Subject(const String &subject);
        bool Send(const String &to, const String &message);
        String getLastResponce();
        const char* getError();
};
#endif // G_SENDER

Gsender.cpp

C/C++
#include "Gsender.h"
Gsender* Gsender::_instance = 0;
Gsender::Gsender(){}
Gsender* Gsender::Instance()
{
    if (_instance == 0) 
        _instance = new Gsender;
    return _instance;
}

Gsender* Gsender::Subject(const char* subject)
{
  delete [] _subject;
  _subject = new char[strlen(subject)+1];
  strcpy(_subject, subject);
  return _instance;
}
Gsender* Gsender::Subject(const String &subject)
{
  return Subject(subject.c_str());
}

bool Gsender::AwaitSMTPResponse(WiFiClientSecure &client, const String &resp, uint16_t timeOut)
{
  uint32_t ts = millis();
  while (!client.available())
  {
    if(millis() > (ts + timeOut)) {
      _error = "SMTP Response TIMEOUT!";
      return false;
    }
  }
  _serverResponce = client.readStringUntil('\n');
#if defined(GS_SERIAL_LOG_1) || defined(GS_SERIAL_LOG_2) 
  Serial.println(_serverResponce);
#endif
  if (resp && _serverResponce.indexOf(resp) == -1) return false;
  return true;
}

String Gsender::getLastResponce()
{
  return _serverResponce;
}

const char* Gsender::getError()
{
  return _error;
}

bool Gsender::Send(const String &to, const String &message)
{
  WiFiClientSecure client;
#if defined(GS_SERIAL_LOG_2)
  Serial.print("Connecting to :");
  Serial.println(SMTP_SERVER);  
#endif
  if(!client.connect(SMTP_SERVER, SMTP_PORT)) {
    _error = "Could not connect to mail server";
    return false;
  }
  if(!AwaitSMTPResponse(client, "220")) {
    _error = "Connection Error";
    return false;
  }

#if defined(GS_SERIAL_LOG_2)
  Serial.println("HELO friend:");
#endif
  client.println("HELO friend");
  if(!AwaitSMTPResponse(client, "250")){
    _error = "identification error";
    return false;
  }

#if defined(GS_SERIAL_LOG_2)
  Serial.println("AUTH LOGIN:");
#endif
  client.println("AUTH LOGIN");
  AwaitSMTPResponse(client);

#if defined(GS_SERIAL_LOG_2)
  Serial.println("EMAILBASE64_LOGIN:");
#endif
  client.println(EMAILBASE64_LOGIN);
  AwaitSMTPResponse(client);

#if defined(GS_SERIAL_LOG_2)
  Serial.println("EMAILBASE64_PASSWORD:");
#endif
  client.println(EMAILBASE64_PASSWORD);
  if (!AwaitSMTPResponse(client, "235")) {
    _error = "SMTP AUTH error";
    return false;
  }
  
  String mailFrom = "MAIL FROM: <" + String(FROM) + '>';
#if defined(GS_SERIAL_LOG_2)
  Serial.println(mailFrom);
#endif
  client.println(mailFrom);
  AwaitSMTPResponse(client);

  String rcpt = "RCPT TO: <" + to + '>';
#if defined(GS_SERIAL_LOG_2)
  Serial.println(rcpt);
#endif
  client.println(rcpt);
  AwaitSMTPResponse(client);

#if defined(GS_SERIAL_LOG_2)
  Serial.println("DATA:");
#endif
  client.println("DATA");
  if(!AwaitSMTPResponse(client, "354")) {
    _error = "SMTP DATA error";
    return false;
  }
  
  client.println("From: " + String(FROM) );
  client.println("To: <" + to + '>');
  
  client.print("Subject: ");
  client.println(_subject);
  
  client.println("Mime-Version: 1.0");
  client.println("Content-Type: text/html; charset=\"UTF-8\"");
  client.println("Content-Transfer-Encoding: 7bit");
  client.println();
  String body = "<!DOCTYPE html><html lang=\"en\">" + message + "</html>";
  client.println(body);
  client.println(".");
  if (!AwaitSMTPResponse(client, "250")) {
    _error = "Sending message error";
    return false;
  }
  client.println("QUIT");
  if (!AwaitSMTPResponse(client, "221")) {
    _error = "SMTP QUIT error";
    return false;
  }
  return true;
}

DRS

Arduino
#include <ESP8266WiFi.h>         
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>         
//needed for Json Response parse
#include <ArduinoJson.h>

//needed for eeprom access
#include <EEPROM.h>
#include <Arduino.h>
#include "EEPROMAnything.h"

WiFiClientSecure client;


#define measure A0    //Force Sensitive Resistor
#define red D1          
#define blue D0         //RGB pins of LED
#define green D3
#define LED_POWER D2
#define sw1_weight D5       //Switch 1 positions
#define sw2_power D6
#define sw2_calibrate D8          //Switch 2 positions
#define sw2_check D7


int re1_rep_flag = 0;
int re2_rep_flag = 0;
int re3_rep_flag = 0;
int rst_flag = 0;
int rst_slot = 0;

//AMAZON DRS parameters
const char* device_model = "xxxxxxxxxxxxxxxxxxxxxxxx"; //Product model ID of the device
const char* device_identifier = "xxxxxxxxxxxxxxxxx"; // Serial No of the device
const char* client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Client ID
const char* client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Secret Code of the Developer Account
const char* slot_id = "xxxxxxxxxxxxxxxxx"; //Slot ID of the product

int startup = 1,con_flag=0;
int low=10;
int high=50;
int reading;
int absolute_val=0;
int fal=1;



void get_readings(){

  reading=0;
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);        //get readings from the sensor
    delay(5);
  }
  absolute_val = reading /20;
  
}
void calibrate(){
  reading=0;
  for(int i=0;i<20;i++)                                       //calibrate 
    reading = reading+analogRead(measure);

  absolute_val = reading /20;
  low = absolute_val;
  high = absolute_val;

  while (digitalRead(sw2_calibrate)==HIGH){
    reading=0;
  digitalWrite(blue,LOW);  
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);
    delay(5);
  }
  digitalWrite(blue,HIGH);
  absolute_val = reading /20;
    if (absolute_val<low) low = absolute_val;
    if(absolute_val>high) high=absolute_val;

   delay(100);  
      
  }

digitalWrite(blue,HIGH);

for (int a=0; a<1500;a++){

  reading=0;
  for(int i=0;i<20;i++){
    reading = reading+analogRead(measure);
    delay(5);
  }
  absolute_val = reading /20;

  if(absolute_val <= low) {
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
  }
  if(absolute_val > low+20) {
    digitalWrite(red,HIGH);
    digitalWrite(green,LOW);
  }

  delay(1);
}

  digitalWrite(red,HIGH);
  digitalWrite(green,HIGH);
  
}

unsigned int refresh_access_token() 
{
  char _tmp_token[500];
  unsigned int _len_str;
  Serial.println();
  Serial.println("REQUEST NEW ACCESS TOKEN BY PROVIDING REFRESH TOKEN:\r\nConnecting to Host: api.amazon.com");
  _len_str = eepromReadString(105, 500, _tmp_token);
  //Serial.println(_tmp_token);
  // Use WiFiClientSecure class to create TCP connections
  if (!client.connect("api.amazon.com", 443)) {
    Serial.println("Error 5 --> Connection failed");
    return 5;
  }
  Serial.println("Connected\r\nSending Request for obtain Tokens");
  // This will send request for refresh of Access token to the server
  client.println("POST /auth/o2/token HTTP/1.1");
  client.println("Host: api.amazon.com"); 
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Cache-Control: no-cache");
  client.print("Content-Length: ");client.println(_len_str+197);
  client.println();
  client.print("grant_type=refresh_token");
  client.print("&refresh_token=Atzr%7C");
  client.print(_tmp_token);
  client.print("&client_id=");
  client.print(client_id);
  client.print("&client_secret=");
  client.println(client_secret);
  Serial.println("Request Send");
  
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 35000) {
      Serial.println("Error 4 --> Connection Timeout !");
      client.stop();
      return 4;
    }
  }
  // Read all the lines of the reply from server and print them to Serial
  String _line = "temp";
  unsigned int _line_no = 1;
  while(client.available())
  {
    _line = client.readStringUntil('\r');
    Serial.print(_line);
    if ((_line_no == 1)&& (_line != "HTTP/1.1 200 OK"))
    {
      Serial.println();
      Serial.println("Error 3 --> Invalid Response");
      client.stop();
      return 3;
    }
    _line_no++;
  }
  DynamicJsonBuffer jsonBuffer;
  // Responce last line have access token and referesh token json oblect 
  JsonObject& root = jsonBuffer.parseObject(_line);

  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("Error 2 --> Json parseObject() failed");
    return 2;
  }
  const char* access_token = root["access_token"];
  // Print values
  Serial.println();
  Serial.print("Access token: ");
  Serial.println(access_token);
  _len_str = eepromWriteString1(600, 500, access_token);
  EEPROM.commit();
  Serial.print("Length: ");
  Serial.println(_len_str);
  //len_str = eepromReadString(605,500, _tmp_token);
  //Serial.print("Access token in EEPROM: ");
  //Serial.println(_tmp_token);
  //Serial.println(_len_str);
  Serial.println("SUCCESS");
  return 1;
}

void setup() {
  int cnt;
  Serial.begin(115200); //  Initlize UART for Serial debug
  
  pinMode(measure, INPUT);  
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(sw2_power,OUTPUT);
  pinMode(LED_POWER,OUTPUT);

  digitalWrite(sw2_power,HIGH);
  digitalWrite(red,HIGH);
  digitalWrite(green,HIGH);
  digitalWrite(blue,HIGH);
  digitalWrite(LED_POWER,HIGH);


}

void loop()
{
  if(is_wifi_connected() == 1)
  {
    if(startup==1)
    {
     
      Serial.println("connecting to AP.....");
      delay(2000);
      unsigned int x = obtain_access_and_refresh_token();
      Serial.println(x);
      Serial.println("Connected...");
      delay(4000);
      startup++;
    }


  }

    while(digitalRead(sw2_calibrate)!=HIGH){delay(100);}             //jump in to calibration mode

  if(digitalRead(sw2_calibrate)==HIGH) calibrate();

  while(digitalRead(sw2_calibrate)!=HIGH){

    get_readings();                                     
    
    if(absolute_val <= low){
      int count = 0;                                        //Detects low values
      for (int z=0;z<50;z++){
        get_readings();
        if(absolute_val <= low) count++;
      }
      if (count > 45 && fal == 1){
        Serial.println("LOW DETECT");
        digitalWrite(red,LOW);
        refresh_access_token();
        end_point_request_replenishment(slot_id);    
        fal = 0;
        
      }

      while(absolute_val < (low+high)/2){
        get_readings();                                   //Supply refill checker
        
      }

      if(absolute_val > (low+high)/2){
        digitalWrite(red,HIGH);
        fal = 1;
      }
        
      else count=0;
    }
  }

}

unsigned int end_point_request_replenishment(const char* _slot_id)
{
  char _tmp_token[500];
  unsigned int _len_str;
  Serial.println();
  Serial.println("REQUEST FOR REPLENISHMENT:\r\nConnecting to Host: dash-replenishment-service-na.amazon.com");
  _len_str = eepromReadString(605,500, _tmp_token);
  Serial.println(_tmp_token);
  // Use WiFiClientSecure class to create TCP connections
  if(!client.connect("dash-replenishment-service-na.amazon.com", 443)) 
  {
    Serial.println("Error 5 --> Connection failed");
    return 5;
  }
  Serial.println("Connected\r\nSending Request for Replenishment");
  //This will send the replenishment request to the Amazon DRS server
  client.print("POST /replenish/");client.print(_slot_id); client.println(" HTTP/1.1");
  client.println("Host: dash-replenishment-service-na.amazon.com");
  client.print("Authorization: Bearer Atza|");
  client.println(_tmp_token);
  client.println("Content-Length: 0");
  client.println("X-Amzn-Accept-Type: com.amazon.dash.replenishment.DrsReplenishResult@1.0");
  client.println("X-Amzn-Type-Version: com.amazon.dash.replenishment.DrsReplenishInput@1.0");
  client.println();
  Serial.println("Request Send");


  // Read all the lines of the reply from server and print them to Serial
  String _line = "temp";
  unsigned int _line_no = 1;
  while(client.available())
  {
    _line = client.readStringUntil('\r');
    Serial.print(_line);
    if ((_line_no == 1)&& (_line != "HTTP/1.1 200 OK"))
    {
      client.stop();
      return 3;
    }
    _line_no++;
  }
  DynamicJsonBuffer jsonBuffer;
  // Responce last line have access token and referesh token json oblect 
  JsonObject& root = jsonBuffer.parseObject(_line);

  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("Error 2 --> Json parseObject() failed");
    return 2;
  }
  const char* _detailCode = root["detailCode"];
 
  if(strcmp(_detailCode,"STANDARD_ORDER_PLACED")==0)
  {
    return 11;
  }
  else if(strcmp(_detailCode,"TEST_ORDER_PLACED")==0)
  {
    return 12;
  }
  else if(strcmp(_detailCode,"ORDER_INPROGRESS")==0)
  {
    return 13;
  }
  else
  {
    return 0;
  }
}

unsigned int obtain_access_and_refresh_token() 
{
  char _tmp_token[500];
  unsigned int _len_str;
  _len_str = eepromReadString(0, 30, _tmp_token);
  client.println("POST /auth/o2/token HTTP/1.1");
  client.println("Host: api.amazon.com"); 
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Cache-Control: no-cache");
  client.println();
  client.print("grant_type=authorization_code");
  client.print("&code=");
  client.print(_tmp_token);
  client.print("&client_id=");
  client.print(client_id);
  client.print("&client_secret=");
  client.print(client_secret);
  Serial.println("Request Send");

  // Read all the lines of the reply from server and print them to Serial
  String _line = "temp";
  unsigned int _line_no = 1;
  while(client.available())
  {
    _line = client.readStringUntil('\r');
    Serial.print(_line);
    if ((_line_no == 1)&& (_line != "HTTP/1.1 200 OK"))
    {
      Serial.println();
      Serial.println("Error 3 --> Invalid Response");
      client.stop();
      return 3;
    }
    _line_no++;
  }
  DynamicJsonBuffer jsonBuffer;
  // Responce last line have access token and referesh token json oblect 
  JsonObject& root = jsonBuffer.parseObject(_line);

  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("Error 2 --> Json parseObject() failed");
    return 2;
  }
  const char* _refresh_token = root["refresh_token"];
  // Print values
  Serial.println();
  Serial.print("Refresh token: ");
  Serial.println(_refresh_token);
  _len_str = eepromWriteString1(100, 500, _refresh_token);
  EEPROM.commit();
  return 1;
}


unsigned int is_wifi_connected()
{
  if (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.print(".");
    return 0;
  }
  else
  {
     return 1;
  }  
}

EEPROMAnything.h

Arduino
int eepromReadInt(int address){
   int value = 0x0000;
   value = value | (EEPROM.read(address) << 8);
   value = value | EEPROM.read(address+1);
   return value;
}
 
void eepromWriteInt(int address, int value){
   EEPROM.write(address, (value >> 8) & 0xFF );
   EEPROM.write(address+1, value & 0xFF);
}
 
float eepromReadFloat(int address){
   union u_tag {
     byte b[4];
     float fval;
   } u;   
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);
   return u.fval;
}
 
void eepromWriteFloat(int address, float value){
   union u_tag {
     byte b[4];
     float fval;
   } u;
   u.fval=value;
 
   EEPROM.write(address  , u.b[0]);
   EEPROM.write(address+1, u.b[1]);
   EEPROM.write(address+2, u.b[2]);
   EEPROM.write(address+3, u.b[3]);
}

unsigned int eepromReadString(int offset, int bytes, char *buf){
  char c = 0;
  for (int i = offset; i < (offset + bytes); i++) {
    c = EEPROM.read(i);
    buf[i - offset] = c;
    if (c == 0) 
    {
      return i-offset;
    }
  }
}



unsigned int eepromWriteString(int offset, int bytes, char *buf){
  char c = 0;
  //int len = (strlen(buf) < bytes) ? strlen(buf) : bytes;
  for (int i = 0; i < bytes; i++) {
    c = buf[i];
    EEPROM.write(offset + i, c); 
    if (c == 0) 
    {
      return i;
    }
  }
}
unsigned int eepromWriteString1(int offset, int bytes, const char *buf){
  char c = 0;
  //int len = (strlen(buf) < bytes) ? strlen(buf) : bytes;
  for (int i = 0; i < bytes; i++) {
    c = buf[i];
    EEPROM.write(offset + i, c); 
    if (c == 0) 
    {
      return i;
    }
  }
}

Credits

Chathuranga Liyanage

Chathuranga Liyanage

10 projects • 56 followers
Entrepreneur | Founder - SRQ Robotics | Roboticist

Comments