11
Revision 1:e8b9ee1156c8, committed 2022-06-24
- Comitter:
- yezhong
- Date:
- Fri Jun 24 01:32:32 2022 +0000
- Parent:
- 0:a94222cedd2e
- Commit message:
- 1
Changed in this revision
esp8266.cpp | Show annotated file Show diff for this revision Revisions of this file |
esp8266.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/esp8266.cpp Fri May 01 14:46:00 2015 +0000 +++ b/esp8266.cpp Fri Jun 24 01:32:32 2022 +0000 @@ -1,302 +1,56 @@ #include "esp8266.h" #include <algorithm> #include <cctype> - -// This file implements the methods described in the esp8266 header file -// Copyright: Adhithya Rajasekaran -// License: MIT License +#include "wifi_example.h" +#include "data_pc.h" -/* General Note About ESP8266 - - ESP8266 is controlled by using AT commands sent via Serial. Most of the API - is implemented with AT commands. You can find the list of all AT commands at - https://nurdspace.nl/ESP8266 - -*/ -ESP8266::ESP8266(MODSERIAL *input, char* inputSSID, char* inputPassword){ - /* - Inputs: - 1. MODSERIAL* input - Pass in a reference to a MODSERIAL object. This MODSERIAL object should be connected to the - ESP8266 module's pins 3 and 4. MODSERIAL is used instead of regular serial because it has buffering capability. - 2. char* inputSSID - Pass in a reference to a character array consisting of the SSID of the wireless network you - want to connect to. Currently the library assumes that you are passing in the correct SSID and won't do any error - checking. - 3. char* inputPassword - Pass in a reference to a character array consisting of the password/shared key of the wireless network you - want to connect to. Currently the library assumes that you are passing in the correct password and won't do any error - checking. - - TODO: - 1. Error checking of SSIDs by calling listOfAccessPoints() method and checking if the inputSSID is in the output of - listOfAccessPoints() method. - 2. Error checking of passwords by trying to actually connect the access point - */ - wifi = input; - wifi->baud(115200); // This is the baud rate required to communicate the ESP8266 chip - connection_successful = false; - if(wiredCorrectly()){ - //connection_successful = true; - if(joinAccessPoint(inputSSID,inputPassword)){ - getMyIP(); - string error = "ERROR"; - if(ip != error){ - connection_successful = true; - } - } - } +void moshi(void) +{ + wifi.printf("AT+CWMODE=3\r\n"); } -string removeSpaces(string input) +void mima(void) { - input.erase(std::remove(input.begin(),input.end(),'\r'),input.end()); - input.erase(std::remove(input.begin(),input.end(),'\n'),input.end()); - return input; + wifi.printf("AT+CWJAP=\"%s\",\"%s\"\r\n","prc1","Prc123456"); } - -bool ESP8266::wiredCorrectly(){ - // This method checks if the ESP8266 module is wired correctly. - - wifi->printf("AT\r\n"); - wait(2); - char buf[20]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); - size_t found = actual_output.find("OK"); - if (found!=std::string::npos){ - if(reset() == true){ - setMode(3); - return true; - }else{ - return false; - } - }else{ - return false; - } -} - -bool ESP8266::reset(){ - // This method resets the settings inside the ESP8266 chip - - wifi->printf("AT+RST\r\n"); + +void getMyIP(void) + { + wifi.printf("AT+CIFSR\r\n"); wait(2); char buf[2000]; int counter = 0; while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); + if(wifi.readable()){ + buf[counter] = wifi.getc(); counter = counter + 1; }else{ wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); - size_t found = actual_output.find("rst"); - if (found!=std::string::npos){ - return true; - }else{ - return false; - } -} - -char* ESP8266::firmwareVersion(){ - // Incomplete Method. Just use for firmware debugging purposes - wifi->printf("AT+GMR\r\n"); - wait(2); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ + if(!wifi.readable()){ break; } } } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); - const char* output = actual_output.c_str(); - return (char*) output; + + return ; } -void ESP8266::setMode(int val){ - // ESP8266 is set to mode 3 which will allow it to act - // both as a client and server. - wifi->printf("AT+CWMODE=3\r\n"); - wait(2); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); -} - -bool ESP8266::checkConnection(){ - return connection_successful; +/* +void getMyIP(void) +{ + wifi.printf("AT+CIFSR\r\n"); } - -bool ESP8266::gotAnIPAddress(){ - wifi->printf("AT+CIFSR\r\n"); - wait(2); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); - size_t found = actual_output.find("ERROR"); - if (found!=std::string::npos){ - return false; - }else{ - return true; - } -} +*/ -bool ESP8266::joinAccessPoint(char *inputSSID, char *inputPassword){ - // This method establishes connection with the requested access point - // All authentication methods are supported (WEP, WPA) - wifi->printf("AT+CWJAP=\"%s\",\"%s\"\r\n",inputSSID,inputPassword); - wait(7); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - //delete[] buf; - printf("\n"); - if(gotAnIPAddress() == true){ - return true; - }else{ - return false; - } -} -string ESP8266::getMyIP(){ - // Fetches the assigned IP address of the module - wifi->printf("AT+CIFSR\r\n"); - wait(2); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } +void lianjie(void) +{ + wifi.printf("AT+CIPMUX=0\r\n"); } - string test(buf); - string actual_output = test.substr(0,counter); - printf(actual_output.c_str()); - string s = actual_output; - //delete[] buf; - s.erase( remove_if( s.begin(), s.end(),std::isspace ), s.end() ); - string ip_address = s.substr(8,s.length()); - string error = "ERROR"; - if(ip_address == error){ - ip = error; - }else{ - ip = ip_address; - } - return ip; -} - -bool ESP8266::checkSSID(string inputSSID){ - // This method checks the presence of the input SSID - // with all the SSIDs that the ESP8266 chip detects - wifi->printf("AT+CWLAP\r\n"); - wait(10); - char buf[2000]; - int counter = 0; - while(1){ - if(wifi->readable()){ - buf[counter] = wifi->getc(); - counter = counter + 1; - }else{ - wait(2); - if(!wifi->readable()){ - break; - } - } - } - string test(buf); - string actual_output = test.substr(0,counter); - printf(inputSSID.c_str()); - printf(actual_output.c_str()); - wait(5); - size_t found = actual_output.find(inputSSID); - printf("I am here"); - if (found!=std::string::npos){ - return false; - }else{ - return true; - } - //delete[] buf; -} \ No newline at end of file + +void UDP(void) +{ + wifi.printf("AT+CIPSTART=\"%s\",\"%s\",%d,%d,%d\r\n","UDP","127.0.0.1",8585,8585,0); + } \ No newline at end of file
--- a/esp8266.h Fri May 01 14:46:00 2015 +0000 +++ b/esp8266.h Fri Jun 24 01:32:32 2022 +0000 @@ -10,27 +10,14 @@ #include "mbed.h" #include <string> -#include "MODSERIAL.h" +#include "wifi_example.h" + +void moshi(); + +void mima(); -class ESP8266{ - private: - char *ssid; - char *password; - string ip; - bool connection_successful; - MODSERIAL *wifi; - char* sendCmd(char* cmd); - char* firmwareVersion(); - bool reset(); - void setMode(int modeVal); - bool wiredCorrectly(); - bool joinAccessPoint(char *inputSSID, char *inputPassword); - bool gotAnIPAddress(); - bool checkSSID(string inputSSID); - public: - ESP8266(MODSERIAL *input, char* inputSSID, char* inputPassword); - bool checkConnection(); - string listAccessPoints(); - bool disconnect(); - string getMyIP(); -}; \ No newline at end of file +void getMyIP(); + +void lianjie(); + +void UDP(); \ No newline at end of file