uno+esp8266+servo
hi, arduinos
i need little sketch working.
could please take @ code.
i need responce web buttons.
i need little sketch working.
could please take @ code.
i need responce web buttons.
code: [select]
#include <softwareserial.h>
#include <servo.h>
servo servolock;
int pos = 0; // variable store servo position
int lightpin = 5;
#define debug true
softwareserial esp8266(2,3); // make rx arduino line pin 2, make tx arduino line pin 3.
// means need connect tx line esp arduino's pin 2
// , rx line esp arduino's pin 3
void setup()
{
serial.begin(9600);
esp8266.begin(115200); // esp's baud rate might different
senddata("at+rst\r\n",2000,debug); // reset module
senddata("at+cwmode=2\r\n",1000,debug); // configure access point
senddata("at+cifsr\r\n",1000,debug); // ip address
senddata("at+cipmux=1\r\n",1000,debug); // configure multiple connections
senddata("at+cipserver=1,80\r\n",1000,debug); // turn on server on port 80
servolock.attach(9); // attaches servo on pin 9 servo object
pinmode(lightpin, output);
}
void loop()
{
if(esp8266.available()) // check if esp sending message
{
while(esp8266.available())
{
// esp has data display output serial window
char c = esp8266.read(); // read next character.
serial.write(c);
}
if(esp8266.find("+ipd,"))
{
delay(1000);
int connectionid = esp8266.read()-48; // subtract 48 because read() function returns
// ascii decimal value , 0 (the first decimal number) starts @ 48
string webpage = "<h1>doorlock</h1><h2>system</h2><button id='open'>lås op</button>";
string cipsend = "at+cipsend=";
cipsend += connectionid;
cipsend += ",";
cipsend +=webpage.length();
cipsend +="\r\n";
senddata(cipsend,1000,debug);
senddata(webpage,1000,debug);
webpage="<button id='close'>lås</button>";
cipsend = "at+cipsend=";
cipsend += connectionid;
cipsend += ",";
cipsend +=webpage.length();
cipsend +="\r\n";
senddata(cipsend,1000,debug);
senddata(webpage,1000,debug);
webpage="<button id='on'>tænd</button>";
cipsend = "at+cipsend=";
cipsend += connectionid;
cipsend += ",";
cipsend +=webpage.length();
cipsend +="\r\n";
senddata(cipsend,1000,debug);
senddata(webpage,1000,debug);
webpage="<button id='off'>sluk</button>";
cipsend = "at+cipsend=";
cipsend += connectionid;
cipsend += ",";
cipsend +=webpage.length();
cipsend +="\r\n";
senddata(cipsend,1000,debug);
senddata(webpage,1000,debug);
string closecommand = "at+cipclose=";
closecommand+=connectionid; // append connection id
closecommand+="\r\n";
senddata(closecommand,3000,debug);
}
}
}
string senddata(string command, const int timeout, boolean debug)
{
string response = "";
esp8266.print(command); // send read character esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// esp has data display output serial window
char c = esp8266.read(); // read next character.
response+=c;
}
}
if(debug)
{
serial.print(response);
}
return response;
}
Arduino Forum > Topics > Home Automation and Networked Objects > uno+esp8266+servo
arduino
Comments
Post a Comment