2

Hey all I have the ESP8266 module and I have already set it up and it's working good. However, when I run this HTML page code here:

<html>
    <head>
        <title>ESP8266 LED Control</title>
    </head>
    <body>
    <button id="6894" class="led">Toggle Pin 13</button> <!-- button for pin 13 -->

    <script src="jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(".led").click(function(){
                var p = $(this).attr('id'); // get id value (i.e. pin13, pin12, or pin11)

                $.get("http://192.168.9.60:80/", {pin:p}); // execute get request
            });
        });
    </script>
    </body>
</html>

I have to send the number 6894 instead of 13 because for some reason its not sending out the correct number. If i send 13 then what it receive is 114. When i send 6894 i receive 689.

My arduino code is this:

*/
//====================================================================================
//                                [The SSDI of the current wifi router]
//                                |              [The password of the current wifi router]
//                                |              |
//                                V              V
String SSIDandPW    = "AT+CWJAP=\"xxxxxxwifi\",\"xxxxxx123\"\r\n";

//                                      [The STATIC IP of the wifi module]
//                                      |                [The main IP of the router]
//                                      |                |              [The default submask]
//                                      |                |              |
//                                      V                V              V
String eIPrIPmask   = "AT+CIPSTA_DEF=\"192.168.9.60\",\"192.168.9.1\",\"255.255.255.0\"\r\n";
const int ledDimmer = 5;         // Used to dim the LED strip (digital Pin 3)

SoftwareSerial esp8266(9,10);    // Pin D10 to=> TX on ESP8266 | Pin D9 to=> RX on ESP8266
//================================= End define WiFi Module =================================
//==========================================================================================

void setup() {
  Serial.begin(19200);

  pinMode(13, OUTPUT); //Just for a wifi demo from html page

  //================================ Start Setup WiFi Module =================================
  //==========================================================================================
  esp8266.begin(19200);
  sendData("AT+RST\r\n",                    2000, true); // Reset module
  sendData("AT+CWMODE=1\r\n",               2000, true); // Configure as access point
  sendData(SSIDandPW,                       5000, true); // Pass of current wifi
  sendData("AT+CIPMUX=1\r\n",               2000, true); // Configure for multiple connections
  sendData("AT+UART_DEF=19200,8,1,0,0\r\n", 2000, true); // Set baud of COM
  sendData(eIPrIPmask,                      2000, true); // Default network mask IP
  sendData("AT+CIPSERVER=1,80\r\n",         2000, true); // Turn on server on port 80
  sendData("AT+GMR\r\n",                    2000, true); // Get firmware version
  sendData("AT+CIFSR\r\n",                  2000, true); // Get ip address
  Serial.print("Server is now running!");
  //================================ End Setup WiFi Module ===================================
  //==========================================================================================
}


void loop() {
  //=========================================== Start Loop WiFi Module ===========================================
  //==============================================================================================================
  if(esp8266.available()) {                               // check if the esp is sending a message 
    if(esp8266.find("+IPD,")) {
      delay(500);
      int connectionId = esp8266.read() - 48;             // subtract 48 because the read() function returns

      esp8266.find("pin=");                               // advance cursor to "pin="
      int retunedNum = (esp8266.read() - 48) * 100;       // get first number (example: 123.. this would be "1")
      retunedNum += (esp8266.read() - 48) * 10;           // get second number (example: 123.. this would be "2")
      retunedNum += (esp8266.read() - 48);                // get last number (example: 123.. this would be "3")
      String closeCommand = "AT+CIPCLOSE=";               // make close command
      Serial.print("returned: ");
      Serial.print(retunedNum);
      Serial.print("----------");
      closeCommand += connectionId;                       // append connection id
      closeCommand += "\r\n";     
      sendData(closeCommand,                250, true);   // close connection
      //digitalWrite(retunedNum, !digitalRead(retunedNum));   // toggle pin
      if (retunedNum == 6894) {
        Serial.print("OFF");
        digitalWrite(ledDimmer, LOW);
      } else if (retunedNum == 6904) {
        Serial.print("ON");
        digitalWrite(ledDimmer, HIGH);
      } else if (retunedNum == 689) {
        digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);              // wait for a second
        digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
        delay(1000);              // wait for a second 
      }
    }
  }
  //============================================ End Loop WiFi Module ============================================
  //==============================================================================================================
}

//                     [The data/command to send]
//                     |                  [The time to wait for a response]
//                     |                  |                [Print to Serial window (true = yes | false = no)]
//                     |                  |                |
//                     V                  V                V
String sendData(String command, const int timeout, boolean debug) {
  String response = "";
  long int time   = millis();

  esp8266.print(command);          // send the read character to the esp8266

  while((time + timeout) > millis()) {
    while(esp8266.available()) {
                                   // The esp has data so display its output to the serial window
        char c = esp8266.read();   // read the next character.
        response += c;
    }
  }

  if (debug) { Serial.print('Returned: ' + response); }
  return response;
}

And the console output is this:

14880AT+RST


OK
14880
"qXÑ{ÂB!É
1±þ)ÒBÙa
+ò“H!a5)�#)ÒÂia%YakN‘ J!ÐCÊ!�
÷þˆ2ô
ready
14880AT+CWJAP="xxxxxxwifi","xxxxxx123"


OK
14880AT+CIPMUX=1


OK
14880AT+UART_DEF=19200,8,1,0,0


OK
14880AT+CIPSTA_DEF="192.168.9.60","192.168.9.1","255.255.255.0"


OK
14880AT+CIPSERVER=1,80


OK
14880AT+GMR

AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:56

OK
14880AT+CIFSR

+CIFSR:STAIP,"192.168.9.60"
+CIFSR:STAMAC,"18:fe:34:9b:4e:68"

OK
Server is now running!

returned: 689----------148804 HTTP/1.1
Host: 192.168.9.60
Connection: AT+CIPCLOSE=0

0,CLOSED

OK

What I really am wanting to do is have a way to send a 10 charater string to the arduino instead of the limited 3 numbers above. I just am not sure what i need to add/modify in order to make it see letters/numbers and only the first 10 that i send.

Any help would be great!

  • Just do this straight on the ESP8266 no need for an arduino. You should try it as the ESP8266 is not a Wifi add on for arduino but is a controller with wifi. – Namphibian Jun 22 '15 at 3:20
3
  int retunedNum = (esp8266.read() - 48) * 100;       // get first number (example: 123.. this would be "1")
  retunedNum += (esp8266.read() - 48) * 10;           // get second number (example: 123.. this would be "2")
  retunedNum += (esp8266.read() - 48);                // get last number (example: 123.. this would be "3")

Your code always expects the pin number to have 3 digits.

In case of 13, it takes a space character as the last digit.
It reads charachters '1', '3' and ' '. Their ASCII codes are 49, 51 and 32. With this, the calculated number is

(49 - 48) * 100 + (51 - 48) * 10 + (32 - 48) = 114.

With 6894, it just uses the first three digits and ignores the last.

For 1 or 2 digit long number you could just pad it with '0' character i.g. "013".

If you want to read an arbitrary string, you should replace the above code with this code:

String retunedStr = "";
for(;;) {
    char c = esp8266.read();
    if (c == ' ')
        break;
    retunedStr += c;
}

And obviously replace

Serial.print(retunedNum);

with

Serial.print(retunedStr);

With this, you should only use letters (A-Z, a-z), numbers (0-9), dash (-) and an underscore (_) for the string.
If you use other characters, you could have problems with the URL character encoding.

  • This is what i get when sending "testing123".... returned: 0,298:GET – StealthRT Jun 28 '15 at 2:59
  • Actually i take that back. I moved the code to a different part and i do get the correct text from what i sent... but now it takes about 21 seconds to show up when it used to be once i clicked the button 1 second latter it was there in the output. If the code looping too much to cause this? – StealthRT Jun 28 '15 at 3:18

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.