Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By yoavshtainer
#36061 hello all!!

i'm trying to create a client, client/server, server system.

in the middle ESP i want to use AP_STA mode, and he need to connect to the server and be a server for the other client.

i have a messy code but the big problem i'm thinking is the ip... anyone do it before? can i see a working code?

this is my client/server code:
Code: Select all/*
 */

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
const char *ssid = "MyESPAP";
const char *password = "MyPassword";
const char *host = "192.168.4.1";

const char *ssid1 = "MyESPAPSERVER";
const char *password1 = "MyPasswordSERVER";
const char *host1 = "10.10.10.1";
ESP8266WebServer server(80);

int led = 2;
int blink_count = 0;
int blink_rate = 250;
int value_from_client = 0;
String client_to_server = "";
void handleRoot() {
  server.send(200, "text/html", "<html><body><h1>MyESPAP Home Page</h1>\r\n<a href=\"/frate\">Fast Rate</a><br>\r\n<a href=\"/srate\">Slow Rate</a><br><h2 href=\"/Msg\">num from client = </h2><br>\r\n</body></html>");
}

void handleMSG() {
  blink_rate =250;
  String num_from_client = server.arg("ClientNum");
  server.send(200, "text/html", "<html><head><script>window.onload =  function() { setInterval(function() {window.location.replace('/');}, 1000); };</script></head><body><h1>MSG mode</h1>Got " + num_from_client + " from client</body></html>");
//Serial.println(num_from_client);
String cloud = "AT#DWSEND=0,property.publish,key,gas,value," + num_from_client;
Serial.println(cloud);
delay(5);
client_to_server = num_from_client;
value_from_client=(num_from_client.toInt());
//Serial.println(value_from_client);
}

int rate_toogle = 0;
int client_num_val = 0;

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println();
   WiFi.softAP(ssid1, password1);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
   server.on("/Msg", handleMSG);
  server.begin();
}


void loop() {
  server.handleClient();
  delay(8000);
  if (rate_toogle == 0)
    rate_toogle = 1;
  else
    rate_toogle = 0;
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
//  String url = "/frate"; 
//  if (rate_toogle == 0)
//    url = "/srate";
//  Serial.print("Requesting URL: ");
//  Serial.println(url);
//  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
//  delay(10);
//    while(client.available()){
//    String line = client.readStringUntil('\r');
//    Serial.print(line);
//      }
int sensorValue = digitalRead(2);
Serial.println(sensorValue);
if(sensorValue==1)
{
   client_num_val = (sensorValue + client_num_val);
  Serial.println(client_num_val);
  }


  String url1 = "/Msg";
  url1 = url1 + "?ClientNum=" + client_to_server;
  //url1 = url1 + " ?ClientNum= " + String(sensorValue);
  Serial.print("Requesting URL: ");
  Serial.println(url1);
  client.print(String("GET ") + url1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  delay(10);
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  Serial.println();
  Serial.println("closing connection");
}



look on the setup....
i forget to tell that i dont konw nothing about IP so maybe the ip is forbidden...

edit: i'm sorry, i found the wifi.APConfig and how to change the ip...

you can remove this post...

best regard,
yoav
User avatar
By EdZamper
#68582 Hello guys,

I have been trying for some time to make a client, server/client, server arrangement and I have not been able to do so. The person on this thread apparently was successful in doing so, using the softAPConfig(), however he did never post the final code configuration. Is there any way to post working code for a connection of the type AP_STA? I want to connect an 8266-E12 to a Modem and at the same time communicate with other ESPs via UDP. So far I'm successful in the closed communication using UDP between ESPs but not in a Forward Node or simultaneous internet/client connections. Any help is appreciated.

Thanks
-EZ