Hello
I 'm using Sparkfun ESP8266. I have followed this tutorial :
[ https://learn.sparkfun.com/tutorials/ … 6351479 ]
I have run the ESP8266_SHIELD_DEMO project. The device is responding properly.
The server mode is working. The board has an ip adress.
When I go to his Ip adress I get the values of analogic ports. Everything works fine.
I would like to send an http request to the board with a parameter for example : http://192.168.0.38/?param1=P1
This parameter must be retrieved in the Arduino and fire an action.
My question :
What is the code for receiving the httprequest ?
What is the syntax of httprequest ? I have tried
Here below this is the code I have tested without success.
Thanks for your help.
Mario
void serverDemo()
{
ESP8266Client client = server.available(500);
if (client)
{
Serial.println(F("Client Connected!"));
boolean currentLineIsBlank = true;// an http request ends with a blank line
while (client.connected())
{ if (client.available())
{
char c = client.read();
header += c;
if (c == '\n' && currentLineIsBlank)
{ Serial.println(header);
if (header.indexOf("P1") >= 0) {
Serial.println("IT WORKS");}
Serial.println(F("Sending HTML page"));
String htmlBody;
client.print("test");
break;
}
if (c == '\n')
{
currentLineIsBlank = true; // you're starting a new line
}
else if (c != '\r')
{
currentLineIsBlank = false; // you've gotten a character on the current line
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
Serial.println(F("Client disconnected"));
}
}
Hello,
This is the entire code
The readString variable which should contain the parameter, is empty.
Thanks for your help.
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
const char mySSID[] = "";
const char myPSK[] = "";
String readString; //create readString class
String cm1;
String cm2;
ESP8266Server server = ESP8266Server(80);
void setup()
{
Serial.begin(9600);
initializeESP8266();
connectESP8266();
displayConnectInfo();
serverSetup();
}
void loop()
{
serverDemo();
}
void initializeESP8266()
{
int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));
}
void connectESP8266()
{
// The ESP8266 can be set to one of three modes:
// 1 - ESP8266_MODE_STA - Station only
// 2 - ESP8266_MODE_AP - Access point only
// 3 - ESP8266_MODE_STAAP - Station/AP combo
// Use esp8266.getMode() to check which mode it's in:
int retVal = esp8266.getMode();
if (retVal != ESP8266_MODE_STA)
{ // If it's not in station mode.
// Use esp8266.setMode([mode]) to set it to a specified mode.
// retVal = esp8266.setMode(ESP8266_MODE_STA);
retVal = esp8266.setMode(ESP8266_MODE_AP);
if (retVal < 0)
{
Serial.println(F("Error setting mode."));
errorLoop(retVal);
}
}
Serial.println(F("Mode set to station"));
retVal = esp8266.status();
if (retVal <= 0)
{
Serial.print(F("Connecting to "));
Serial.println(mySSID);
// esp8266.connect([ssid], [psk]) connects the ESP8266
// to a network.
// On success the connect function returns a value >0
// On fail, the function will either return:
// -1: TIMEOUT - The library has a set 30s timeout
// -3: FAIL - Couldn't connect to network.
retVal = esp8266.connect(mySSID, myPSK);
if (retVal < 0)
{ Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}
}
void displayConnectInfo()
{
char connectedSSID[24];
memset(connectedSSID, 0, 24);
int retVal = esp8266.getAP(connectedSSID);
if (retVal > 0)
{ Serial.print(F("Connected to: "));
Serial.println(connectedSSID);
}
IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP: ")); Serial.println(myIP);
}
void serverSetup()
{ server.begin();
Serial.print(F("Server started! Go to "));
Serial.println(esp8266.localIP());
Serial.println();
}
void serverDemo()
{
ESP8266Client client = server.available(500);
if (client)
{
Serial.println(F("Client Connected!"));
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) { //read char by char HTTP request
readString += c; //store characters to string
}
//if HTTP request has ended
if (c == '\n' && currentLineIsBlank)
{
client.println("HTTP/1.1 200 OK");// send a standard http response header
client.println("Content-Type: text/html");
client.println();
//=======================================================================
cm1 = readString.substring(6,8);
cm2 = readString.substring(8,11);
Serial.print("readString = ");Serial.print(readString);Serial.println();
Serial.print("cm1 = ");Serial.print(cm1);Serial.println();
Serial.print("cm2 = ");Serial.print(cm2);Serial.println();
if(cm1 == "CM")
{
if(cm2 == "BZ1")
{ Serial.print("buzzer 1");Serial.println();
readString = "";cm1 = "";cm2= "";client.stop(); // close connection:
}
}
break;
}
} // end if HTTP request has ended
} // while client connected
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
Serial.println(F("Client disconnected"));
}
}
void errorLoop(int error)
{
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;
}
Try to use the server.on(…, …) approach as in the AdvancedWebServer example of the ESP8266. it will enable to call a certain routine in your code depending on the extension of the URL.
Hello thanks for your reply.
ESP8266WebServer.h seems a good solution.
But I can not make it work.
-
I use the Sparkfun ESP8266 WIFI shield on an Arduino Uno. First I have selected the board : Arduino Uno. But it seems it is not correct. I have to select : SparkFun ESP8266 Thing. Is it correct ?
-
The code below is compiled without error. But after upload to the board I get the following error :
File “C:/Users/Utilisateur/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 468, in connect
raise FatalError(‘Failed to connect to %s: %s’ % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Invalid head of packet (0x00)
Does someone knows what does that mean ?
Thanks for your help.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
int ledPin = 16;
bool ledState = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
WiFi.begin("",""); //Connect to the WiFi network
while(WiFi.status()!= WL_CONNECTED){ //Wait for connection
delay(500);
Serial.println("Waiting to connect…");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //Print the local IP
server.on("/on", turnOn); https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247https://forum.sparkfun.com/viewtopic.php?f=121&t=50247 //Associate the handler function to the path
server.on("/off", turnOff);
server.on("/toggle", toggle);
server.begin(); //Start the server
Serial.println("Server listening");
}
void loop() {
server.handleClient();
}
void turnOn(){
ledState = HIGH;
digitalWrite(ledPin, ledState);
server.send(200, "text/plain", "LED on");
}
void turnOff(){
ledState = LOW;
digitalWrite(ledPin, ledState);
server.send(200, "text/plain", "LED off");
}
void toggle(){
ledState = !ledState;
digitalWrite(ledPin, ledState);
server.send(200, "text/plain", "LED toggled");
}
HI,
My error. Thought you were using an ESP8266 directly to the PC, but it is the shield on-top of an Arduino. So you have to select the correct Arduino .
More than a year ago I spend lots of time on this and have documented that on https://github.com/paulvha/PVH_ESP8266_ … no_Library
Look at the pvh_ESP8266_Shield_Server_gpio or pvh_ESP8266_rfid_reader examples. Focus on the server_demo or Server_Comms routines. Each use different approach to achieve the same : gpio uses the content of the message, RFID is using the URL parameters. There is lots of documentation in the extras folder about the different sketches and updated Sparkfun driver.
Thanks Paul,
I have spent a lot of hours without result and I was considering to use NodeMCU.
But I will analyse the files you sent me and try to understand.
Thanks a lot for your help.