So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Elronxx
#68434 Hello guys, Im 15y old and I have never ever tried programming so this is my first experience with this in Arduino IDE Im just trying to copy some codes and edit them so I can make what I want but now Im really stucked:
#include <SoftwareSerial.h>
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "MojeServa";
const char *password = "";
ESP8266WebServer server(80);

void handleRoot()
{
server.send(200, "text/html", "<h1>You are connected</h1>");
}


SoftwareSerial esp8266(5, 4); /

//define variables
#define DEBUG true //display ESP8266 messages on Serial Monitor
#define SERV1 D1
#define SERV2 D2

Servo s1; //servo 1
Servo s2; //servo 2
int pos1 = 90; //servo 1 current position
int pos2 = 90; //servo 2 current position
int vel = 10; //servos' speed (100 -> 90º in 1 s)(1 -> 90º in 9 s)
int pos1min = 0; //servo 1 minimum position
int pos2min = 0; //servo 2 minimum position
int pos1max = 180; //servo 1 maximum position
int pos2max = 180; //servo 2 maximum position





void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);

IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
//attach and set servos' angles
s1.attach(SERV1);
s2.attach(SERV2);
s1.write(pos1max);
s2.write(pos2max);
//detach servos
s1.detach();
s2.detach();

//start serial communication
esp8266.begin(19200);



sendData("AT+RST\r\n", 2000, DEBUG); //reset module
sendData("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
sendData("AT+CWJAP=\"XXXXX\",\"YYYYY\"\r\n", 2000, DEBUG); //connect wifi network
while(!esp8266.find("OK")) { //wait for connection
}
sendData("AT+CIFSR\r\n", 1000, DEBUG); //show IP address
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //allow multiple connections
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start web server on port 80
}

//**********
// Main Loop
//**********
void loop()
{
server.handleClient();
if (esp8266.available()) //check if there is data available on ESP8266
{
if (esp8266.find("+IPD,")) //if there is a new command
{
String msg;
esp8266.find("?"); //run cursor until command is found
msg = esp8266.readStringUntil(' '); //read the message
String command = msg.substring(0, 3); //command is informed in the first 3 characters. "sr1" = command to servo #1 and "sr2" = command to servo #2
String valueStr = msg.substring(4); //next 3 characters inform the desired angle
int value = valueStr.toInt(); //convert to integer
if (DEBUG) {
Serial.println(command);
Serial.println(value);
}
delay(100);

//************
// move servos
//************

//move servo1 to desired angle
if(command == "sr1") {
//limit input angle
if (value >= pos1max) {
value = pos1max;
}
if (value <= pos1min) {
value = pos1min;
}
s1.attach(SERV1); //attach servo
while(pos1 != value) {
if (pos1 > value) {
pos1 -= 1;
s1.write(pos1);
delay(100/vel);
}
if (pos1 < value) {
pos1 += 1;
s1.write(pos1);
delay(100/vel);
}
}
s1.detach(); //dettach
}

//move servo2 to desired angle
if(command == "sr2") {
//limit input angle
if (value >= pos2max) {
value = pos2max;
}
if (value <= pos2min) {
value = pos2min;
}
s2.attach(SERV2);
while(pos2 != value) {
if (pos2 > value) {
pos2 -= 1;
s2.write(pos2);
delay(100/vel);
}
if (pos2 < value) {
pos2 += 1;
s2.write(pos2);
delay(100/vel);
}
}
s2.detach();
}
}
}
}


//********************
// Auxiliary functions
//********************

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


--------------------- I want to make a programme which will receive data packages from my phone, but first ive got to make an AP from my esp8266 when i upload the code to the esp8266 i can see my wifi "mojeserva" just for a while but i cant even connect ... and then it dissapears i really dont know what to do so i will welcome any help ...