0

I want to easily send a number from a phone to my esp8266. I have an app that sends the number in the format IP/number. How can I modify the code to give my the number behind the IP address as an integer?

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

ESP8266WebServer server(80);

String webPage = "";

void setup(void){


  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }

  server.on("/", [](){
    Serial.println("device connected");
  });
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
} 

So it would do something like this for and should be looking through every i between 0 and 200. :

server.on("/"+String(i), [](){
    Serial.println("i");
  });
1

First of all use the event function server.on with a path that corresponds to your function and use a url like this:

192.0.2.2/recordnumber?num=100

With a url parmeter for your number. Your code might look like this:

At the top:

int nums_size = 0;
const int nums_max = 128;
int nums[nums_max];

In setup:

server.on("/recordnumber", [](){
    int num;
    if(server.hasArg("num")) {
      String num_str = server.arg("num");
      if(num_str.charAt(0) == '0') {
        num = 0;
      } else {
        num = num_str.toInt();
        if(num == 0) {
          server.send(400, "text/plain", "not a number");
          return;
        }
      }
      if(nums_size < nums_max) {
        nums[nums_size++] = num;
        server.send(200, "text/plain", "saved");
      } else {
        server.send(500, "text/plain", "full");
      }
    } else {
      server.send(400, "text/plain", "bad params");
    }
});
0

First you need to extract the string that contains the number (after the /). Since it is already stored in consecutive memory you can pass the pointer to the character after the / symbol to the C function atoi to convert it to an integer.

I assume the pointer is already \0 terminated.

You can use the C function strchr to search for the / symbol.

Tip: use a C compiler on a desktop/PC to check how these functions work. Once you know how to use them, copy them in your Arduino code. This is much faster than uploading/debugging sketches probably.

4
  • My phone accesses the ESP via it's IP address for instance: 192.168.2.2/100 or 192.168.2.2/200. The ESP should than store these values 100 and 200 into integers but these numbers could be any number
    – luukito
    Jan 3 '18 at 13:06
  • I adapted the answer, use strchr to search for the / symbol, than use atoi Jan 3 '18 at 13:09
  • But in order to extract the right part of the string how do you store the whole incoming IP address in a string?
    – luukito
    Jan 3 '18 at 13:12
  • WiFi.localIP() is already pointing to a string, you can use this as input for strchr to search for the / symbol, than you can use atoi. Jan 3 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.