Dependencies:   mbed

Fork of ESP8266-configuration-mbed-LPC1768 by jim hamblen

Revision:
6:2e8125399b54
Parent:
5:9f46b8cdd469
--- a/main.cpp	Fri Aug 28 01:21:15 2015 +0000
+++ b/main.cpp	Fri Mar 16 07:57:17 2018 +0000
@@ -1,4 +1,7 @@
 #include "mbed.h"
+#include <iostream>
+#include <string>
+#include <fstream>
 
 Serial pc(USBTX, USBRX);
 Serial esp(p28, p27); // tx, rx
@@ -8,17 +11,21 @@
 int  count,ended,timeout;
 char buf[1024];
 char snd[255];
+char ip[30] = "192.168.10.100";
 
-char ssid[32] = "mySSID";     // enter WiFi router ssid inside the quotes
-char pwd [32] = "myPASSWORD"; // enter WiFi router password inside the quotes
+char ssid[32];     
+char pwd [32];
 
-void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
+LocalFileSystem local("local");
 
+void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();
+
+using namespace std;
 
 int main()
 {
     reset=0; //hardware reset for 8266
-    pc.baud(115200);  // set what you want here depending on your terminal program speed
+    pc.baud(9600);  // set what you want here depending on your terminal program speed
     pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
     wait(0.5);
     reset=1;
@@ -26,38 +33,44 @@
     getreply();
 
     esp.baud(115200);   // change this to the new ESP8266 baudrate if it is changed at any time.
-
-    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
-
+     
     ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
 
 
 
     // continuosly get AP list and IP
     while(1) {
-        pc.printf("\n---------- Listing Acces Points ----------\r\n");
-        strcpy(snd, "AT+CWLAP\r\n");
-        SendCMD();
-        timeout=15;
-        getreply();
-        pc.printf(buf);
-        wait(2);
-        pc.printf("\n---------- Get IP and MAC ----------\r\n");
-        strcpy(snd, "AT+CIFSR\r\n");
-        SendCMD();
-        timeout=10;
-        getreply();
-        pc.printf(buf);
-        wait(2);
+    pc.printf("\n---------- Informations sur la connexion ----------\r\n");
+    strcpy(snd, "AT+CIPSTATUS\r\n");
+    SendCMD();
+    timeout=10;
+    getreply();
+    pc.printf(buf);
+    wait(15);
+    Donnee();
     }
-
+    
 }
 
-// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
-void ESPsetbaudrate()
+void WIFI()
 {
-    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
-    SendCMD();
+ using namespace std;
+    pc.printf("\f\r -------------lecture du fichier------------- \f\r");
+    pc.baud(9600);
+    
+    FILE *set = fopen("/local/ssid.txt", "r");
+    fscanf(set,"%s ",ssid);
+    
+    FILE *mdp = fopen("/local/pwd.txt", "r");
+    fscanf(mdp,"%s ",pwd);
+    
+    wait(2);
+    pc.printf("\f\r\n Lecture du SSID de connection : \f\r\n");
+    wait(2);
+    pc.printf(" ssid : %s    \r\n",ssid);
+    pc.printf(" password : %s    \r\n",pwd);
+    fclose(set);
+    fclose(mdp);
 }
 
 //  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
@@ -66,18 +79,10 @@
     wait(5);
     strcpy(snd,"AT\r\n");
     SendCMD();
-    wait(1);
-    strcpy(snd,"AT\r\n");
-    SendCMD();
-    wait(1);
-    strcpy(snd,"AT\r\n");
-    SendCMD();
     timeout=1;
     getreply();
     wait(1);
-    pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
-
-    pc.printf("---------- Reset & get Firmware ----------\r\n");
+    pc.printf("---------- Redemarrage du module ----------\r\n");
     strcpy(snd,"AT+RST\r\n");
     SendCMD();
     timeout=5;
@@ -86,7 +91,7 @@
 
     wait(2);
 
-    pc.printf("\n---------- Get Version ----------\r\n");
+    pc.printf("\n---------- Information sur la version ----------\r\n");
     strcpy(snd,"AT+GMR\r\n");
     SendCMD();
     timeout=4;
@@ -96,7 +101,7 @@
     wait(3);
 
     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
-    pc.printf("\n---------- Setting Mode ----------\r\n");
+    pc.printf("\n---------- Mode Wifi ----------\r\n");
     strcpy(snd, "AT+CWMODE=1\r\n");
     SendCMD();
     timeout=4;
@@ -106,7 +111,7 @@
     wait(2);
 
     // set CIPMUX to 0=Single,1=Multi
-    pc.printf("\n---------- Setting Connection Mode ----------\r\n");
+    pc.printf("\n---------- Choix de connexion multiple ou unique ----------\r\n");
     strcpy(snd, "AT+CIPMUX=1\r\n");
     SendCMD();
     timeout=4;
@@ -115,7 +120,7 @@
 
     wait(2);
 
-    pc.printf("\n---------- Listing Access Points ----------\r\n");
+    pc.printf("\n---------- Liste des points de connexion ----------\r\n");
     strcpy(snd, "AT+CWLAP\r\n");
     SendCMD();
     timeout=15;
@@ -123,8 +128,10 @@
     pc.printf(buf);
 
     wait(2);
+    WIFI();
+    wait(2);
 
-    pc.printf("\n---------- Connecting to AP ----------\r\n");
+    pc.printf("\n---------- Connexion au point d acces ----------\r\n");
     pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
     strcpy(snd, "AT+CWJAP=\"");
     strcat(snd, ssid);
@@ -138,7 +145,7 @@
 
     wait(5);
 
-    pc.printf("\n---------- Get IP's ----------\r\n");
+    pc.printf("\n---------- Adresse IP local ----------\r\n");
     strcpy(snd, "AT+CIFSR\r\n");
     SendCMD();
     timeout=3;
@@ -147,16 +154,12 @@
 
     wait(1);
 
-    pc.printf("\n---------- Get Connection Status ----------\r\n");
+    pc.printf("\n---------- Informations sur la connexion ----------\r\n");
     strcpy(snd, "AT+CIPSTATUS\r\n");
     SendCMD();
     timeout=5;
     getreply();
     pc.printf(buf);
-
-    pc.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
-    pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
-    pc.printf("  It saves the SSID and password settings internally\r\n");
     wait(10);
 }
 
@@ -165,6 +168,21 @@
     esp.printf("%s", snd);
 }
 
+void Donnee()
+{
+    pc.printf("\n---------- Envoie de donnees ----------\r\n");
+    strcpy(snd, "AT+CIPSEND=\"");
+    strcat(snd, "192.168.10.23");
+    strcat(snd, "\",\"");
+    strcat(snd, "1234");
+    strcat(snd, "\"\r\n");
+    SendCMD();
+    timeout=5;
+    getreply();
+    pc.printf(buf);
+    wait(15);   
+}
+
 void getreply()
 {
     memset(buf, '\0', sizeof(buf));