-->
Page 1 of 2

Change IP and port in AP mode

PostPosted: Mon Jan 02, 2017 7:02 pm
by akhgari
I want to control ESP-12 pins from android device. I create simple app that connect to ESP-12 and then send a string to ip (192.168.56.1) and port 900. I write below code but seem's not work correctly.
How to change IP and port in AP mode?
How to receive string ?

my code
Code: Select all#include <ESP8266WebServer.h>
//************************************
String LED;
String LED_mod;
//************************************
const char *ssid = "kc_test";
const char *ip = "192.168.54.1";

ESP8266WebServer server(900);

void setup() {
  delay(1000);
  Serial.begin(115200);
    Serial.println();
  Serial.print("Configuring access point...");
    WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
    server.begin();
//************************************
  pinMode(12, OUTPUT);

}
void loop() { 
  server.handleClient();   
 
if (LED=="A"){  digitalWrite(12, HIGH); }
if (LED=="a"){  digitalWrite(12, LOW);  }
}

Re: Change IP and port in AP mode

PostPosted: Wed Jan 04, 2017 9:17 am
by mrburnette
For one of my projects, I set up a softAP to use UDP broadcasts. Perhaps you can adapt:
Code: Select all/*
   GPS Portal, part of Tardis Time Server by: M. Ray Burnette 20150915
   Create a private 10. network and capture all DNS requests to the Time Portal
   Respond to UDP and DHCP and DNS
   Arduino GUI 1.6.7 on Linux Mint 17.3 on 20160206
   ESP8266 core: http://arduino.esp8266.com/staging/package_esp8266com_index.json
      Sketch uses 235,297 bytes (54%) of program storage space. Maximum is 434,160 bytes.
      Global variables use 32,153 bytes (39%) of dynamic memory, leaving 49,767 bytes for local variables.
*/
#define LEDpin 2                                                // ESP82660-1 8-pin module LED is on GPIO-02
#include <Streaming.h>                                          // \Documents\Arduino\libraries\Streaming (legacy) user-installed
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <DNSServer.h>
#include "./Utility.h"                                          // See Notes tab for credits

const byte   DNS_PORT  =   53;                                  // Listen DNS requests on port 53
int          ack_Count =    0;
uint8_t      hour, minute, seconds;                             // hour, minure, seconds,
uint8_t      day, month, year;                                  // year, month, day;
unsigned int localPort = 8888;                                  // any unused port on LAN
IPAddress    apIP(10, 10, 10, 1);                               // Private network address: local & gateway
IPAddress    broadcastIP(255, 255, 255, 255);                   // https://en.wikipedia.org/wiki/Multicast

char         packetBuffer[UDP_TX_PACKET_MAX_SIZE];              // buffer to hold incoming packet,
char         ReplyBuffer[] = "????????????";                    // a 12 character string to send back

DNSServer         dnsServer;                                    // Create the DNS object
WiFiUDP           UDP;                                          // UDP protocol on STA interface, localPort

extern "C" {
  #include "user_interface.h"                                   // used for diagnostic ESP.getFreeHeap()
}

// Forward declarations
void GPSstuff(char c);


void setup()
{
  Serial.begin(4800);                                           // Initialise Serial for older PMB-648
  Serial << (F("2015 Ray Burnette")) << endl;
  Serial << (F("Tardis Time Portal Version 0.20150915")) << endl;
  Serial << (F("Visit my project web page on http://www.hackster.io/rayburne")) << endl << endl;

  WiFi.mode(WIFI_AP_STA);                                       // AP + STA
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00
  WiFi.softAP("TardisTime");

  dnsServer.start(DNS_PORT, "*", apIP);                         // "*" creates a captive portal

  while (! UDP.begin(localPort) ) {                             // UDP protocol connected to localPort which is a variable
    Serial << "+" ;
    yield();                                                    // Play nicely with RTOS (alias = delay(0))
  }                                                             // This will loop forever if UDP fails

  Serial << endl;
  Serial << (F("Setting pin#2 to Output mode")) << endl;
  pinMode(2, OUTPUT);                                           // initialise pin mode
}


void loop()
{
  dnsServer.processNextRequest();                               // TCP Address handler when requested
  yield();                                                      // yield for RTOS
//  Listener();                                                   // UPD (can you hear me now?)
//  yield();

  if (Serial.available() > 0) {                                 // anything in the serial hw buffer?
      char c = Serial.read();                                   // if so, fetch the next character from FIFO
      GPSstuff(c);
  }

  yield();
}                                                               // loop repeats forever unless stack crashes or uC hangs


void GPSstuff(char c) {                                         // GPSbuffer[] is global
  static int i, j;                                              //   persistent within function scope
  static char q;
  static bool flag = false;
  static char GPSbuffer[120];                                   // GPS serial line input buffer
  q = c;

  if ( q == 0x24 )                                              // '$'
  {
    i = 0;                                                      // brute force sync on '$' to GPSbuffer[0]
    // Serial << "Found $" << endl;
  }

  if ( i < 120) GPSbuffer[i++] = q;

  if (q == 0x0d) {
    flag = true;                                                // is the character a CR for eol
    i = 0;
  }

  if (flag) {                                                   // test for end of line and if the right GPSbuffer
    flag = false;                                               // reset for next time
    digitalWrite(LEDpin, !(digitalRead(LEDpin)));               // blink LED (Warning: using ESP8266-01 module)
    UDP.beginPacketMulticast(broadcastIP, localPort, apIP);
    // Serial << "We are in the flag routine..." << GPSbuffer[3] << GPSbuffer[4] << GPSbuffer[5] << endl;
    // Serial << "Analyzing " << GPSbuffer[3] << GPSbuffer[4] << GPSbuffer[5] << endl;
    if ( (GPSbuffer[3] == 0x52) && (GPSbuffer[4] == 0x4d) && (GPSbuffer[5] == 0x43)) // 'R' && 'M' && 'C'
    {
      long int RAM = ESP.getFreeHeap();
      Serial << "Free= " << RAM << endl;

      for (j = 0; j < 120 ; j++) {
        UDP.write(GPSbuffer[j]);
      }

      UDP.write("\r\n");                                        // terminate the line
      UDP.endPacket();                                          // clear UDP buffer
      Serial << GPSbuffer << endl;
    }
  }
}



Ray
Projects

Re: Change IP and port in AP mode

PostPosted: Wed Jan 11, 2017 3:49 pm
by akhgari
Thanks mrburnette for your reply.

I wrote android application that connect to ip "192.168.56.1". this app have two buttons that send char "A" and "a" on port "8888". flash my esp8266 with "esp8266 flasher" and use AT commands to set AP mode, IP and port.
I receive "A" and "a" in therminal. It seams my android app is work correctly.
I wanna read string from my android app. can you help me to find my fault?
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "Test_ESP";
const char *password = "testtest";

unsigned int localPort = 8888;                                  // any unused port on LAN
IPAddress    apIP(192, 168, 56, 1);                               // Private network address: local & gateway
IPAddress    broadcastIP(255, 255, 255, 255);                   // https://en.wikipedia.org/wiki/Multicast
struct ip_addr *IPaddress;
IPAddress address;
ESP8266WebServer server(8888);


void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Configuring access point...");

  WiFi.mode(WIFI_AP);       
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  Serial.print(" MAC Address is:");
  Serial.println (WiFi.softAPmacAddress());

  server.begin();
  Serial.println("HTTP server started");
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    // Read the first line of the request
    String req = client.readStringUntil('\r');
    Serial.println(req);
    client.flush();
    Serial1.println("Client disonnected");

  }

}

Re: Change IP and port in AP mode

PostPosted: Wed Jan 11, 2017 4:42 pm
by martinayotte
Fiest, you are talking about AT command, but you are using an Arduino sketch.
Beware that when you upload Arduino sketch, the AT firmware is gone/erased.

For you code, I don't see any client.available(), so it doesn't check if characters are received.
Check the following code instead :

Code: Select allvoid loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    if (client.available()) { // <--- here is the missing part
      // Read the first line of the request
      String req = client.readStringUntil('\r');
      Serial.println(req);
      client.flush();
      Serial1.println("Client disonnected");
    }
  }
}