ESP8266 wifi module sending data via jQuery
Posted: Mon Jun 22, 2015 12:40 am
Hey all I have the ESP8266 module and I have already set it up and it's working good using this as my guide (http://allaboutee.com/2015/01/02/esp8266-arduino-led-control-from-webpage/). However, when I run this HTML page code here:
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 112. When i send 6894 i receive 689.
My arduino code is this:
And the console output is this:
What I really am wanting to do is have a way to send a 10 charater and/or number string to the module 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.
Example:
Should be shown in the serial as "testing123".
And I am also unsure as to where this "14880" is coming from???
Any help would be great!
Code: Select all
<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 112. When i send 6894 i receive 689.
My arduino code is this:
Code: Select all
*/
//====================================================================================
// [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:
Code: Select all
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:50:0b:4e:08"
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 and/or number string to the module 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.
Example:
Code: Select all
var p = 'testing123';
$.get("http://192.168.9.60:80/", {pin:p}); // execute get request
Should be shown in the serial as "testing123".
And I am also unsure as to where this "14880" is coming from???
Any help would be great!