// PROGRAMME COTE CARTE CAMERA
//programme controlant le mouvement du servo
// serveur web repondant depuis site web
// ou depuis envoi autre carte arduino depuis programme commande_web
//type de carte LOLIN (WEMOS) D1 R2 mini
// modifier la variable MODE_WIFI selon reseau en general en A comme AccessPoint
//https://www.aranacorp.com/fr/communication-entre-deux-esp8266-par-wifi/
#include <Arduino.h>
// Load Wi-Fi library
#include <ESP8266WiFi.h>
//#include <ESP8266mDNS.h>
//#include <StreamString.h>
#include <Servo.h>
#include <ESP8266mDNS.h> // Include the mDNS library
//mode wifi box
// choix du mode de connexion A pour AccesPoint ou B pour connexion à BOX
String MODE_WIFI = "B";
String CameraHostname = "Camera2RHB";
#define BOX_SSID "freebox_PHYZYT"
#define BOX_PSK "CF5AE5177A16379A5AF9977DAF4A5A"
//#define BOX_SSID "AndroidAP31CB"
//#define BOX_PSK "mkqn4199"
//#define BOX_SSID "OPPO A54 5G J"
//#define BOX_PSK "d5abekc5"
//fin mode wifi box
//mode wifi AP
//#ifndef APSSID
//#define APSSID "Camera2RHB"
//#define APPSK "LaBuvette2RHBToutA2Euros"
//#endif
#define AP_SSID "Camera2RHB"
#define AP_PSK "Buvette2RHB"
// Set your Static IP address
IPAddress local_IP(192, 168, 4, 184);
// Set your Gateway IP address
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
//fin mode wifi AP
Servo myservo; // create servo object to control a servo
// variables pour microsecondes
int myservopos = 1500; // variable to store the servo position
int origservogauche = 900 ;
int origservocentre = 1500 ;
int origservodroite = 2100 ;
int servogauche = origservogauche ;
int servocentre = origservocentre ;
int servodroite = origservodroite ;
int vitesse = 40;
int attente = 1000;
/*
int origservogauche = 500 ;
int origservocentre = 1500 ;
int origservodroite = 2500 ;
int servogauche = 500 ;
int servocentre = 1500 ;
int servodroite = 2500 ;
int vitesse = 20;
int attente = 1000;
*/
// valeurs utilisées pour les mvt de servo dans changedirection et dans le map de la valeur du potentiometre vitesse
int vitessemin = 1;
int vitessemax = 40;
int pot_max = 0;
int pot_vit = 2;
String inforeseau ;
/*
const int buttonPin1 = 5; // the number of the pushbutton pin
const int buttonPin2 = 6; // the number of the pushbutton pin
const int buttonPin3 = 7; // the number of the pushbutton pin
*/
/*
gnd
3.3v
bt1 d5 GPIO14
bt2 d6 GPIO12
bt3 d7 GPIO13
servo d8 GPIO15
pot a0 ADC0
*/
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int lastchangebuttonState1 = 0;
int lastchangebuttonState2 = 0;
int lastchangebuttonState3 = 0;
String buttonactif = "0" ;
String lastbuttonactif = "0" ;
String direction = "c" ;
String lastdirection = direction ;
const int pot_max_pin = A0; // pot de fin de course
int coeff = 0;
int coeff1 = 0;
int coeff2 = 0;
int coeff3 = 0;
int coeff4 = 0;
int lastcoeff = coeff;
//mode ap
//const char *apssid = AP_SSID;
//const char *appassword = AP_PSK;
//fin mode ap
//mode box
const char *boxssid = BOX_SSID;
const char *boxpassword = BOX_PSK;
//fin mode box
// Replace with your network credentials
const char* SSID = BOX_SSID;
const char* PASSWORD = BOX_PSK;
// Set web server port number to 80
WiFiServer server(80);
// Assign output variables to GPIO pins
const int output = 4;
String value = "c";
void setup() {
// myservo.attach(9); // attaches the servo on pin 9 to the servo object
//myservo.attach(D7); // attaches the servo on pin D7 to the servo object
// myservo.attach(D7); // attaches the servo on pin D7 to the servo object
myservo.attach(2); //D4
Serial.begin(9600);
// Initialize the output and set it to LOW
pinMode(output, OUTPUT);
digitalWrite(output, LOW);
// Connect to Wi-Fi network with SSID and PASSWORD
if (MODE_WIFI == "A") {
Serial.println("connection AP");
connect_AP();
} else if (MODE_WIFI == "B") {
Serial.println("connection BOX");
connect_BOX();
}
//mode wifi box
/*
Serial.print("Connecting to ");
//WiFi.begin(SSID, PASSWORD);
Serial.println(SSID);
WiFi.begin(BOX_SSID, BOX_PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address
Serial.println("");
Serial.print("Connected to ");
Serial.println(BOX_SSID);
Serial.println("WiFi connected at IP address:");
Serial.println(WiFi.localIP());
*/
//fin mode wifi box
/*
//mode wifi ap
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
//WiFi.begin(ssid, password);
WiFi.begin(AP_SSID, AP_PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("Acces Point");
Serial.print((String)"SSID : " + AP_SSID + "password : " + AP_PSK);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// server.begin();
//fin mode wifi ap
*/
// Start Web Server
//2603
WiFiClient browser ;
WiFiClient client = server.available();
server.begin();
}
// Main loop
void loop() {
// Create a client and listen for incoming clients
//pot_max = analogRead(pot_max_pin);
// val de 0 à 1024
//coeff = map(pot_max, 0, 1023, 0, 40); // coeff pour le servo en write
//coeff = map(pot_max, 0, 1023, 0, 400);
Serial.println("---------------------------- DEPART LOOP ----------------------------------------");
Serial.println((String) "adresse IP : " );
Serial.println( WiFi.localIP());
Serial.println(inforeseau);
coeff = map(analogRead(pot_max_pin), 0, 1023, 0, 400);
//2603
WiFiClient browser ;
WiFiClient client = server.available();
if (lastcoeff >= coeff - 5 && lastcoeff <= coeff + 5 )
{
Serial.println((String) "changedir par coeff nouveau : " + coeff + " ancien : " + lastcoeff);
lastcoeff = coeff;
changedirection(direction);
}
servogauche = origservogauche + coeff ;
servodroite = origservodroite - coeff ;
//Serial.println((String)" lastdirection : " + lastdirection + " direction : " + direction );
if (direction != lastdirection) {
Serial.println((String)" lastdirection : " + lastdirection + " direction : " + direction );
/* WiFiClient client;
const char * host = "192.168.4.1";
const int httpPort = 80;*/
/* changedirection(direction);
lastdirection = direction ;
*/
Serial.println((String) "changedir par diffdirection nouveau : " + direction + " ancienne : " + lastdirection);
changedirection(direction);
lastdirection = direction ;
}
//gestionpageweb
// Do nothing if server is not available
/* if (!client) {
return;
}
*/
// Wait a client
/*
while(!client.available()){}
*/
// A new client is connected, get the request
String request = client.readStringUntil('\r');
Serial.println(request);
// ancienne methode
client.flush();
// Display GPIO status
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
//client.flush();
// client.print( header );
// client.print( tmpString );
//insert
client.println("<head>");
client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'/>");
client.println("<meta charset='utf-8'>");
client.println("<meta http-equiv='refresh' content='5'>");
client.println("<style>body {font-size:100%;}#main {display: table; margin: auto; padding: 0 10px 0 10px; }h2 {text-align:center; }p { text-align:center; }</style>");
client.println("<title>Controle Camera dirigeable 2RHB</title>");
client.println("<meta http-equiv='refresh' content='5'>");
client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'></head>");
// client.println("</head>");
//fin insert
client.println("<body>");
client.println("<br>");
client.print(" Controle position Camera 2RHB : ");
if (value == "c") {
client.print("CENTRE");
}
if (value == "g") {
client.print("GAUCHE");
}
if (value == "d") {
client.print("DROITE");
}
client.println("<br><br>");
client.println("Position actuelle");
client.print(direction);
client.println("<br>");
client.println (" Coefficient de fin de course: ");
client.print (coeff);
client.println("<br><br>");
client.println(" Clic sur Direction ");
client.println("<br><br>");
/*client.println(" <- Max Gauche : ");
client.print(servogauche);
client.print("   |Position actuelle| : ");
client.print (myservopos);
client.print("   Max Droite : ");
client.print(servodroite);
client.print(" -> ");
client.println("<br><br>");*/
client.println(" Max Gauche | Position | Max Droite <br>");
client.println("<pre>");
client.print( " " );
client.print( servogauche );
client.print( " | " );
client.print( myservopos);
client.print( " | " );
client.print( servodroite );
client.println("<br>");
client.println("</pre>");
client.println("<br>");
client.println("Direction <a href=\"/direction=G\">GAUCHE</a>");
client.print("   Retour <a href=\"/direction=C\">CENTRE</a>");
client.print("   Direction <a href=\"/direction=D\">DROITE</a><br>");
client.println("<br><br>");
client.println("</body>");
client.println("</html>");
client.flush();
Serial.println((String)"request : " + request);
if (request.indexOf("/direction=G") != -1)
{
//digitalWrite(output, HIGH);
value = "g";
direction = value;
Serial.println((String) " value : " + value);
}
if (request.indexOf("/direction=C") != -1)
{
//digitalWrite(output, LOW);
value = "c";
direction = value;
Serial.println((String) " value : " + value);
}
if (request.indexOf("/direction=D") != -1)
{
//digitalWrite(output, LOW);
value = "d";
direction = value;
Serial.println((String) " value : " + value);
}
Serial.println((String)" getreq : " + value);
delay(100);
// lecture de la commande envoyée par la carte TELECOMMANDE
String directioncommande = "";
directioncommande = clientRequest();
Serial.println("********************************************************");
Serial.println(" LA COMMANDE A ENVOYE :" + directioncommande);
Serial.println("********************************************************");
if (directioncommande != "")
{
value = directioncommande;
direction = directioncommande;
Serial.println("on affecte la dir");
}
}
void changedirection(String dir) {
Serial.println((String) " /////////////////DIR/////////////");
Serial.println((String) "changement de direction vers : " + dir + " coeff : " + coeff);
if (dir == "c") {
if (myservopos < servocentre ) {
Serial.println((String) " C< myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos <= servocentre) {
if (servocentre - myservopos < vitessemax)
{
/* calcule le nouvel angle */
myservopos = myservopos + vitessemin ;
myservo.writeMicroseconds(myservopos);
Serial.println((String)"centre slow + 1" + myservopos);
}
else
{
vitesse = speed();
/* calcule le nouvel angle */
myservopos = myservopos + vitesse ;
myservo.writeMicroseconds(myservopos);
Serial.println((String)"centre + vitesse" + myservopos);
}
}
}
else if (myservopos > servocentre ) {
Serial.println((String) " C> myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos >= servocentre) {
if (myservopos - servocentre < vitessemax )
{
myservopos = myservopos - vitessemin ;
myservo.writeMicroseconds(myservopos);
Serial.println((String) " centre slow - 1 " + myservopos);
// myservo.write(servogauche + 1);
}
else
{
vitesse = speed();
myservopos = myservopos - vitesse ;
myservo.writeMicroseconds(myservopos);
Serial.println((String) "centre + vitesse " + myservopos);
//myservo.write(servogauche + coeff);
}
/*
vitesse = speed();
Serial.println((String) " myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
myservo.writeMicroseconds(myservopos);
myservopos = myservopos - vitesse ;
//vitesse = -1;
//myservo.write(servogauche + coeff);
*/
}
}
myservo.write(servocentre);
}
else if (dir == "g") {
if (myservopos > servogauche) {
Serial.println((String) " G> myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos > servogauche) {
Serial.println((String) " myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
//myservo.writeMicroseconds(myservopos);
/* calcule le nouvel angle */
if (myservopos - servogauche < vitessemax )
{
myservopos = myservopos - vitessemin ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + 1);
}
else
{
vitesse = speed();
myservopos = myservopos - vitesse ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + coeff);
}
// myservopos = myservopos - vitesse ;
//vitesse = -1;
// myservo.write(servogauche + coeff);
}
}
else if (myservopos < servogauche) {
Serial.println((String) " G< myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos < servogauche) {
Serial.println((String) " myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
// myservo.writeMicroseconds(myservopos);
/* calcule le nouvel angle */
if (servogauche - myservopos < vitessemax )
{
myservopos = myservopos + vitessemin ;
myservo.writeMicroseconds(myservopos);
// myservo.write(servogauche + 1);
}
else
{
vitesse = speed();
myservopos = myservopos + vitesse ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + coeff);
}
// myservopos = myservopos + vitesse ;
//vitesse = -1;
// myservo.write(servogauche + coeff);
}
}
}
else if (dir == "d") {
if (myservopos < servodroite) {
Serial.println((String) " D< myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos < servodroite) {
Serial.println((String) " myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
//myservo.writeMicroseconds(myservopos);
/* calcule le nouvel angle */
if (servodroite - myservopos < vitessemax )
{
myservopos = myservopos + vitessemin ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + 1);
}
else
{
vitesse = speed();
myservopos = myservopos + vitesse ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + coeff);
}
//myservopos = myservopos + vitesse ;--------
//vitesse = -1;
//myservo.write(servogauche + coeff);
}
// myservo.write(servodroite - coeff);
}
else if (myservopos > servodroite) {
Serial.println((String) " D> myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
while (myservopos > servodroite) {
Serial.println((String) " myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
// myservo.writeMicroseconds(myservopos);
/* calcule le nouvel angle */
if (myservopos - servodroite < vitessemax )
{
myservopos = myservopos - vitessemin ;
myservo.writeMicroseconds(myservopos);
// myservo.write(servogauche + 1);
}
else
{
vitesse = speed();
myservopos = myservopos - vitesse ;
myservo.writeMicroseconds(myservopos);
//myservo.write(servogauche + coeff);
}
// myservopos = myservopos + vitesse ;
//vitesse = -1;
// myservo.write(servogauche + coeff);
}
}
}
}
void changedirection_rotcont(String dir) {
Serial.println((String) " /////////////////DIR/////////////");
Serial.println((String) "debut deplacement myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
Serial.println((String) "changement de direction vers : " + dir + " coeff : " + coeff);
if (dir == "c") {
myservo.writeMicroseconds(servocentre);
}
else if (dir == "g") {
myservo.writeMicroseconds(servogauche);
}
else if (dir == "d") {
myservo.writeMicroseconds(servogauche);
}
Serial.println((String) "fin deplacement myservopos : " + myservopos + " servogauche : " + servogauche + " servodroite : " + servodroite);
}
int speed()
{
// calcul de la vitesse
int result;
/* pot_vit = analogRead(pot_vit_pin);
result = map(pot_vit, 0, 1023, vitessemin, vitessemax);
return result;
*/
result = vitesse;
return result;
}
void connect_BOX()
{
//mode wifi box
Serial.print("Connecting to ");
//WiFi.begin(SSID, PASSWORD);
Serial.println(SSID);
//Set new hostname
WiFi.hostname(CameraHostname);
//Get Current Hostname
// Serial.println("New hostname: %s\n", WiFi.hostname().c_str());
WiFi.begin(BOX_SSID, BOX_PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address
Serial.println("");
Serial.print("Connected to ");
Serial.println(BOX_SSID);
Serial.println("WiFi connected at IP address:");
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
if (!MDNS.begin(CameraHostname)) { // Start the mDNS responder for esp8266.local
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");
inforeseau = "Nom Camera : " + String(WiFi.hostname().c_str()) + " IP : " + String(WiFi.localIP().toString()) + " subnet : " + String(WiFi.subnetMask().toString()) + " gateway : " + String(WiFi.gatewayIP().toString()) + " BOX_SSID : " + String(BOX_SSID) + " BOX_PSK : " + String(BOX_PSK);
//fin mode wifi box
}
void connect_AP() {
//mode wifi ap
WiFi.mode(WIFI_AP_STA);
/*
*
IPAddress apIP(10, 10, 10, 1); // Private network address: local & gateway
<...>
void setup() {
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00
WiFi.softAP("TardisTime");
*/
//Set new hostname
WiFi.hostname(CameraHostname.c_str());
//Get Current Hostname
// Serial.println("New hostname: %s\n", WiFi.hostname().c_str());
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(AP_SSID);
//WiFi.begin(ssid, password);
//Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
//Serial.println(WiFi.softAP(AP_SSID, AP_PSK) ? "Ready" : "Failed!");
// WiFi.begin(AP_SSID, AP_PSK);
/* while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}*/
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("Acces Point");
Serial.print((String)"SSID : " + AP_SSID + "password : " + AP_PSK);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.println(WiFi.softAP(AP_SSID, AP_PSK) ? "Ready" : "Failed!");
Serial.println("IP obtenue : ");
Serial.println(WiFi.softAPIP());
/*
if (!MDNS.begin(CameraHostname)) { // Start the mDNS responder for esp8266.local
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");*/
inforeseau = "Nom Camera : " + String(WiFi.hostname().c_str()) + " IP : " + String(WiFi.softAPIP().toString()) + " subnet : " + String(WiFi.subnetMask().toString()) + " gateway : " + String(WiFi.gatewayIP().toString()) + " AP_SSID : " + String(AP_SSID) + " AP_PSK : " + String(AP_PSK);
//fin mode wifi ap
}
String clientRequest()
{ /* function clientRequest */
////Check if client connected
Serial.println( "ClientRequest fonction ");
//Variables
bool sendCmd = false;
String dir_commande ="";
String nom =" ";
WiFiClient client = server.available();
client.setTimeout(50);
if (client) {
if (client.connected()) {
//Print client IP address
Serial.print(" ->");Serial.println(client.remoteIP());
String request = client.readStringUntil('\r'); //receives the message from the client
//String request2 = client.readString(); //receives the message from the client
String reponseTELECOMMANDE = client.readString(); //receives the message from the client
//String request3 = client.
Serial.println(" request : ");
Serial.println(request);
//Serial.println(" request2 : ");
//Serial.println(request2);
Serial.println("-------affichage-------------");
//Serial.println(reponseTELECOMMANDE);
Serial.println(" index toto : ");
// formatage de la reponse pour recuperer le caractere de direction
int longueurtotal = reponseTELECOMMANDE.length();
String indexcherche = "TELECOMMANDE DIRECTION:";
int pos_indexcherche = reponseTELECOMMANDE.indexOf("TELECOMMANDE DIRECTION:");
int longueur_telecommdir = indexcherche.length();
int position_telecommdir = pos_indexcherche + longueur_telecommdir;
//Serial.println(reponseTELECOMMANDE.indexOf("TELECOMMANDE DIRECTION:"));
//Serial.println(String("longueurtotal: ") + longueurtotal + String(" indexcherche : ") + indexcherche + String(" longueur_telecommdir : ") + longueur_telecommdir + String(" position_telecommdir : ") +position_telecommdir );
dir_commande = reponseTELECOMMANDE.substring(position_telecommdir + 1, position_telecommdir + 2);
//Serial.println("dir_commande : " + dir_commande);
}
}
return(dir_commande);
}