How to Create own Arduino Library

I have to create my own Arduino library for WI-FI Module ESP8266.
I don't know how to make Arduino library.I have a complete program of my project but now i have to distribute it in library .Its my office's task and will be submitted on monday 7 december 2015.
kindly help me, I need it on urgent basis.

You didn't look too hard for help did you ?

http://playground.arduino.cc/Code/Library

You could post your code so we can have a look at it...

I attached file of my program.

sketch_dec06a.ino (1.88 KB)

Its often a good plan to wrap the library code up in a class - like SoftwareSerial which you use,
so that its setup code can be in a begin() method, the constructor gets the relevant pin numbers,
and any other actions are other methods on the class.

However its not necessary.

If you've not written classes before I suggest studying existing libraries to see how it all works,
both the one's that come with the Arduino IDE and others.

You have some code. It compiles or it doesn't. You couldn't be bothered to say which.

If it compiles, it does something. You couldn't be bothered to say what it does.

You want it to do something. You couldn't be bothered to say what.

Its my office's task and will be submitted on monday 7 december 2015.

Better start looking for a new job, then. One that does NOT involve communication, because you suck at that.

Moderator:
@PaulS - Please keep comments constructive.

Thank u Pauls for your advice i will pay attention on your advice :slight_smile:

But still i have to complete my task. I need some guidance about it!
I never work on Arduino UNO before it. i really don't know how to make library of some functions.
I saw some tutorials for guidance and also visited Arduino website, but failed to find proper solution of my problem.

My task is to make a library which includes different function like "connected to wifi" "receives data form sensor" "send data to webpage" so on. i also attached my program. please help

MehakFatima:
I attached file of my program.

Or you could have posted it, so that people on phones can see it.

#include <SoftwareSerial.h>
SoftwareSerial esp8266(7, 6);
#include <dht.h>
dht DHT;
#define dht_dpin 4
int sensorPin = A0;
#define DEBUG true
#include <avr/wdt.h>

void setup()
{
  wdt_enable (WDTO_8S);
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
 wdt_reset ();
  sendData("AT\r\n",2000,DEBUG); // reset module
  wdt_reset ();
  sendData("AT+CWMODE=3\r\n",2000,DEBUG); // configure as access point
  wdt_reset ();
  sendData("AT+CWJAP=\"ABC\",\"mypasword\"\r\n",10000,DEBUG);
wdt_reset ();
}

void loop()
{
  wdt_reset ();

 DHT.read11(dht_dpin);
float Hum=DHT.humidity;
float Tem=DHT.temperature;
 wdt_reset ();
int sensorValue = analogRead(sensorPin);
char buffer[10];
  String HumiF = dtostrf(Hum, 4, 1, buffer);
  String tempF = dtostrf(Tem, 4, 1, buffer);
  String LDRval = dtostrf(sensorValue,4,1,buffer);
   wdt_reset ();
String Data1="GET /update?key=FOH1BT59356JL75O&station1="+tempF+'&'+"station2="+HumiF+'&'+"station3="+LDRval;
Data1 +="\r\n";

 wdt_reset ();
sendData("AT+CIPSTART=\"TCP\",\"186.108.151.150\",80\r\n",1000,DEBUG);
String cipSend = "AT+CIPSEND=";
 wdt_reset ();
cipSend +=Data1.length();
cipSend +="\r\n";

 wdt_reset ();
sendData(cipSend,2000,DEBUG);
Serial.println(Data1);
sendData(Data1,5000,DEBUG); 
 wdt_reset ();
String closeCommand = "AT+CIPCLOSE";
closeCommand+="\r\n";    
sendData(closeCommand,1000,DEBUG);

 for(int a=0;a<10;a++){
 wdt_reset ();
delay(2000);
}
}

String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
 wdt_reset ();
while( (time+timeout) > millis())
{
   wdt_reset ();
while(esp8266.available()) 
{
   wdt_reset ();
char c = esp8266.read(); // read the next character.
response+=c;
}  
}
 if(debug)
{
   wdt_reset ();
      Serial.print(response);
}
     wdt_reset ();
    return response;
}

Moderator edit. Code tags. Always, code tags.

I have to complete my task. I need some guidance about it!
I never work on Arduino UNO before it. i really don't know how to make library of some functions.
I saw some tutorials for guidance and also visited Arduino website, but failed to find proper solution of my problem.

My task is to make a library which includes different function like "connected to wifi" "receives data form sensor" "send data to webpage" so on. i also attached my program. please help

#include <SoftwareSerial.h>
SoftwareSerial esp8266(7, 6);
#include <dht.h>
dht DHT;
#define dht_dpin 4
int sensorPin = A0;
#define DEBUG true
#include <avr/wdt.h>

void setup()
{
wdt_enable (WDTO_8S);
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
wdt_reset ();
sendData("AT\r\n",2000,DEBUG); // reset module
wdt_reset ();
sendData("AT+CWMODE=3\r\n",2000,DEBUG); // configure as access point
wdt_reset ();
sendData("AT+CWJAP="ABC","mypasword"\r\n",10000,DEBUG);
wdt_reset ();
}

void loop()
{
wdt_reset ();

DHT.read11(dht_dpin);
float Hum=DHT.humidity;
float Tem=DHT.temperature;
wdt_reset ();
int sensorValue = analogRead(sensorPin);
char buffer[10];
String HumiF = dtostrf(Hum, 4, 1, buffer);
String tempF = dtostrf(Tem, 4, 1, buffer);
String LDRval = dtostrf(sensorValue,4,1,buffer);
wdt_reset ();
String Data1="GET /update?key=FOH1BT59356JL75O&station1="+tempF+'&'+"station2="+HumiF+'&'+"station3="+LDRval;
Data1 +="\r\n";

wdt_reset ();
sendData("AT+CIPSTART="TCP","186.108.151.150",80\r\n",1000,DEBUG);
String cipSend = "AT+CIPSEND=";
wdt_reset ();
cipSend +=Data1.length();
cipSend +="\r\n";

wdt_reset ();
sendData(cipSend,2000,DEBUG);
Serial.println(Data1);
sendData(Data1,5000,DEBUG);
wdt_reset ();
String closeCommand = "AT+CIPCLOSE";
closeCommand+="\r\n";
sendData(closeCommand,1000,DEBUG);

for(int a=0;a<10;a++){
wdt_reset ();
delay(2000);
}
}

String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
wdt_reset ();
while( (time+timeout) > millis())
{
wdt_reset ();
while(esp8266.available())
{
wdt_reset ();
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
wdt_reset ();
Serial.print(response);
}
wdt_reset ();
return response;
}

Please see Libraries - Arduino Reference and https://www.arduino.cc/en/Hacking/LibraryTutorial. And please use code tags. Cheers!

AND DO NOT CROSS-POST

  String LDRval = dtostrf(sensorValue,4,1,buffer);

You want to convert the integer to a string using double to string, formatted. Why? itoa() is much simpler to use, and you don't look like a doofus expecting the int to end up with one decimal place.

Wrapping the resulting string in a String instance is just pissing away resources needlessly.

And you STILL haven't explained what your problem is.

PaulS:

  String LDRval = dtostrf(sensorValue,4,1,buffer);

You want to convert the integer to a string using double to string, formatted. Why? itoa() is much simpler to use, and you don't look like a doofus expecting the int to end up with one decimal place.

Wrapping the resulting string in a String instance is just pissing away resources needlessly.

And you STILL haven't explained what your problem is.

Wow dude you are being kinda rough on this guy arent you? He did state his problem. If I understand him correctly he wishes to distribute this code and expose functions selectively by creating a library. He has written his code in a sketch basically and wants to break it out in to a library for these purposes. I would imagine that if situations were reversed you would reply to your question, "what is the problem?" with "Read!".

Honestly man....why hang out and read a forum just to put ppl down or make them feel "less than". Congrats you know what your doing and how to program. So do a million other ppl. And there are millions that dont ....yet. They will learn and they learn by asking questions as Im sure you did at some point in your journey...no?

To the original poster:

I will say though that this code is pretty messy and its obviously copied in bits and pieces from several public examples. Taking someones elses code, breaking it up, and putting it in to a library doesnt change that this is not your code. Use public code as an example. Take it apart and learn from it and then write your own. Otherwise you will never learn and worse than that one day you have a need where there is no public example. Without taking the time to learn you....well you wont learn! I will also say that your questions have been answered 3 times by my count. Each answer contains a link to a document that will fully explain how to create a library. The fact is (and I find this a little bothersome also) that you dont want to know how to do this. You want it done for you. If not than open the links, read the how-to's, and write your library. Whoever is paying you would prefer that anyways I would imagine :wink:

OH GOD ! :frowning:
Just tell me how to split this program into different functions?
My program is in running condition. My project is also running through this program. but the task has given to me is to convert it into library.
Got it?

Follow the below steps to create your own arduino library

first step: The essential representation for the library is writen....

Its simply like making limits before topping off the hues in the canvas! Later from this we will be making the required library.

So my code will look like this....

#include<xyz.h>/The uniquely designed library as we need to name it

xyz abc;/pronouncing a xyz class object abc

void setup()

{

abc.initial();/introducing with the choice gave in the class definition

}

void circle()

{

abc.proceed();/things to at each circle emphasis

}

So taking a gander at above code, capacity beginning() is for instating the variables(ports and so on.) and continue() capacity for do some operational work at every circling cycle.

Presently make a rundown what all going to require, all the info parameters you need, furthermore yield params. also.....

Presently its opportunity to venture up for composing our own Arduino Library for the above required operation....

Arduino may be develop in JAVA, yet its programing linguistic structure is fundamentally the same to C++.

second step:

To construct the library you are required to make two records, one with expansion .h and other .cpp.

In the .h(Header document), you have to compose as the following....

/guarantee this library portrayal is just included once

#ifndef Testing_h

#define Testing_h

/incorporate sorts and constants of Wiring center API

#include "WConstants.h"

/Start of meaning of class xyz

class xyz {

open:/open announced variables and capacities, available outside of the class

/the class constructor

xyz();/Default constructor of class xyz, may contain anything for introducing article

void beginning();/characterize your first capacity here

void proceed();//characterize your second capacity here

private:/private pronounced variables and capacities, open inside the class

int input_process();/to process and reach inference from data

int callibrationdata(int n=0);/you might need to align your gadget

};/end of class xyz definition

#endif

/finishing with end if

third step:

Save the above code with an augmentation ".h". It will end up being your header document.

Next step is to think of you possess .cpp record, it will contain the fundamental() capacity,

which is the driver capacity in a C++ program.

It will look like as...

#include<xyz.h>/including the hand crafted header document

xyz::xyz()

{/calling the default constructor

}

void xyz::initial()

{/the assortment of beginning() capacity

/setup your sensor here

}

void xyz::proceed()

{/the body of the continue() capacity

int input_data= input_process();

int output=callibrationdata(input_data);

}

int xyz::input_process()

{

/analogRead or proportional or whatever you have to do

}

int ExampleLib::fancyAlgorithm(int inputData)

{

/some perplexing stuff

}

Spare it as .cpp augmentation.

Take both these files...(.cpp and .h) under one organizer, and your own particular hand crafted library

is prepared!

To introduce it, in your Arduino IDE go to arduino root organizer for my situation it is

"E:\arduino-1.0.1"

Open Libraries Folder and glue it, now you will have the capacity to utilize your own particular specially designed

Arduino library by going to "Portrays" then selecting the "Import Library" choice,

at that point selecting the library you hae just made.