//CARTE CONTROLE
//https://electronics-fun.com/send-data-to-web-server-using-esp8266/
// CARTE WEMOS D1 mini clone
// constants won't change. They're used here to set pin numbers:
//#include <ESP8266WiFi.h>
/*WIFI*/
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
//#include <ESP8266WebServer.h>
//#include <uri/UriBraces.h>
/*const char* WIFI_SSID = "ESPWebServer";
const char* WIFI_PASSWORD = "12345678";
*/
/*
#ifndef STASSID
#define STASSID "freebox_PHYZYT"
#define STAPSK "CF5AE5177A16379A5AF9977DAF4A5A"
#endif
#ifndef APSSID
#define APSSID "ESPap"
#define APPSK "thereisnospoon"
#endif
//mode ap
const char *ssid = APSSID;
const char *password = APPSK;
//fin mode ap
//mode box
const char *boxssid = STASSID;
const char *boxpassword = STAPSK;
//fin mode box
const char* WIFI_SSID = STASSID;
const char* WIFI_PASSWORD = STAPSK;
*/
//mode wifi box
#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"
const char* BOX_ipserveurweb = "192.168.0.40";
const char* BOX_serveurweb = "http://192.168.0.40";
//fin mode wifi box
//mode wifi AP
//#ifndef APSSID
//#define APSSID "Camera2RHB"
//#define APPSK "LaBuvette2RHBToutA2Euros"
//#endif
#define AP_SSID "Camera2RHB"
#define AP_PSK "Buvette2RHB"
const char* AP_ipserveurweb = "192.168.4.184";
const char* AP_serveurweb = "http://192.168.4.184";
/*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
*/
// choix du mode de connexion A pour AccesPoint ou B pour connexion à BOX
String MODE_WIFI = "A";
//String nom = "transmetteur";
String nom = "TELECOMMANDE";
//const char* serverName = "http://192.168.4.40";
//const char* serverName = "http://192.168.4.184";
/*FINWIFI*/
/*
const int buttonPin1 = 25; // the number of the pushbutton pin
const int buttonPin2 = 26; // the number of the pushbutton pin
const int buttonPin3 = 27; // the number of the pushbutton pin
*/
const int buttonPin1 = D6; // the number of the pushbutton pin
const int buttonPin2 = D7; // the number of the pushbutton pin
//const int buttonPin3 = D3; // the number of the pushbutton pin
//pas utiliser D3 pin de hardreset
const int buttonPin3 = D8; // the number of the pushbutton pin
/*
const int ledPin1 = 14; // the number of the LED pin
const int ledPin2 = 12; // the number of the LED pin
const int ledPin3 = 13; // the number of the LED pin
*/
const int ledPin1 = D5; // the number of the LED pin
const int ledPin2 = D0; // the number of the LED pin
const int ledPin3 = D1; // the number of the LED pin
// 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;
int buttonactif = 0 ;
String host;
String serverName;
String direction = "c" ;
String lastdirection = direction ;
// variable envoi HTTP
String httpRequestData = "";
int httpResponseCode = 0;
// direction
// c centre
// g gauche
// d droite
// Le potentiomètre est connecté au GPIO 36 (Pin VP)
const int potPin = 36;
// Valeur du potentiomètre
int potValue = 0;
WiFiClient master;
HTTPClient http;
void setup() {
if (MODE_WIFI == "A"){
host = AP_ipserveurweb;
serverName = AP_serveurweb ;
Serial.println("connection AP");
}
if (MODE_WIFI == "B"){
host = BOX_ipserveurweb;
serverName = BOX_serveurweb ;
Serial.println("connection BOX");
}
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
pinMode(potPin,INPUT_PULLUP);
/* WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
*/
//WiFiClient client;
/*IPAddress server(192, 168, 1, 44);*/
//IPAddress server(192,168,4,22);
//IPAddress server(192,168,4,40);
/*Serial.println (" ");
Serial.println ((String)" connected to Wifi Network with IP Address : " + WiFi.localIP());
*/
if (MODE_WIFI == "A") {
Serial.println("connection AP");
connect_AP();
} else if (MODE_WIFI == "B") {
Serial.println("connection BOX");
connect_BOX();
}
Serial.println((String) "debut bouton1 : " + buttonState1 + " bouton2 : " + buttonState2 + " bouton3 : " + buttonState3 + "valeur pot : " + potValue);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
//Serial.println((String) "debut bouton1 : " + buttonState1 + " bouton2 : " + buttonState2 + " bouton3 : " + buttonState3 + "valeur pot : " + potValue);
potValue = analogRead(potPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH ) {
// turn LED on:
//digitalWrite(ledPin1, 1);
buttonactif = 1;
//direction = 'g' ;
} else {
// turn LED off:
//digitalWrite(ledPin1, 0);
}
if (buttonState2 == HIGH) {
// turn LED on:
//digitalWrite(ledPin2, HIGH);
buttonactif = 2 ;
//direction = 'c' ;
} else {
// turn LED off:
//digitalWrite(ledPin2, LOW);
}
if (buttonState3 == HIGH) {
// turn LED on:
//digitalWrite(ledPin3, HIGH);
buttonactif = 3 ;
//direction = 'd' ;
} else {
// turn LED off:
//digitalWrite(ledPin3, LOW);
}
// buttonState2 = digitalRead(pushButton);
// print out the state of the button:
/*Serial.println(buttonState1);
Serial.println(buttonState2);
Serial.println(buttonState3);
Serial.println(buttonactif);*/
//Serial.println((String) "bouton1 : " + buttonState1 + " bouton2 : " + buttonState2 + " bouton3 : " + buttonState3 + " buttonactif = " + buttonactif + " direction : " + direction + "valeur pot : " + potValue);
//Serial.println(potValue);
if (buttonactif == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
direction = "g" ;
}
else if (buttonactif == 2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
direction = "c" ;
}
else if (buttonactif == 3) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
direction = "d" ;
}
if (direction != lastdirection) {
Serial.println((String) "bouton1 : " + buttonState1 + " bouton2 : " + buttonState2 + " bouton3 : " + buttonState3 + " buttonactif = " + buttonactif + " direction : " + direction + "valeur pot : " + potValue);
//WiFiClient client;
// const char * host = "192.168.4.1";
const int httpPort = 80;
envoichangedirection(direction);
lastdirection = direction;
}
delay(100); // delay in between reads for stability
}
void envoichangedirection(String dir){
//wifi status
if (WiFi.status() == WL_CONNECTED){
//2603
//WiFiClient client;
HTTPClient http;
WiFiClient client;
// domain name avec url et ip adresse
//2603
//http.begin(Client, serverName);
//http.begin(serverName);
http.begin(master, serverName.c_str());
//http.begin(client, serverPath.c_str());
//data to send with HTTP Post
Serial.println("servername : " );
Serial.println(serverName);
if (dir == "g"){
//request : GET /direction=g HTTP/1.1
//POST / HTTP/1.1
httpRequestData = "DIRECTION: g" ;
//httpRequestData = "GET /direction=g HTTP/1.1" ;
} else if (dir == "c"){
httpRequestData = "DIRECTION: c" ;
//httpRequestData = "GET /direction=c HTTP/1.1" ;
} else if (dir == "d"){
httpRequestData = "DIRECTION: d" ;
//httpRequestData = "GET /direction=d HTTP/1.1" ;
}
Serial.println("httpRequestData : " );
Serial.println(httpRequestData);
//envoi de la direction
//httpResponseCode = http.POST(httpRequestData);
// test = http.send(200, "ygyuyuhuhuhiji", "ok");
/* HTTPClient.println("GET /search?q=arduino HTTP/1.1");
HTTPClient.println("Host: www.google.com");
HTTPClient.println("Connection: close");
*/
http.begin(client,serverName);
//httpResponseCode = http.POST("direction=g ");
//fonctionne
//httpResponseCode = http.POST(String("GET /direction=g sais pas trop HTTP/1.1 ") + "\r\n");
httpResponseCode = http.POST(String(nom)+ " " + String(httpRequestData) + "\r\n");
//httpResponseCode = http.POST("GET /direction=g HTTP/1.1");
Serial.println("httpRequestData envoyé : ");
Serial.println(httpRequestData);
Serial.println("httpResponseCode obtenu : ");
Serial.println(httpResponseCode);
String url = "/data/";
url += "?sensor_reading=";
//url += intToPrint;
// This will send the request to the server
//client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
//client.print(String("GET ")+ httpRequestData + "Host: " + serverName + "Connection : close\r\n\r\");
client.println(String("GET /direction=g sddsfdsfsdfsdfsdfsdHTTP/1.1 ") + "\r\n");
//Request to server to activate the led
client.println(String("GET ") +"/Led"+" HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
}
}
/*
void changedirection(String dir)
{
Serial.println("(string) changement de direction vers : " + dir);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const char * host = "192.168.4.1"; //default IP address
const int httpPort = 80;
String url = "/data/";
url += "?direction=";
url += dir;
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
delay(500);
}
*/
void connect_BOX()
{
//mode wifi box
Serial.print("Connecting to ");
//WiFi.begin(SSID, PASSWORD);
Serial.println(BOX_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());
// 2603
IPAddress server(192, 168, 1, 44);
//IPAddress server(BOX_ipserveurweb);
//fin mode wifi box
}
void connect_AP(){
//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(AP_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());
// 2603
IPAddress server(192, 168, 1, 44);
//IPAddress server(AP_ipserveurweb);
//fin mode wifi ap
}