Salve a tutti. avrei la necessità di fare una chiamata HTTP direttamente da arduino cosi questo è il mio codice:
#include <LiquidCrystal_I2C.h>
// Load Wi-Fi library
#include <ESP8266WiFi.h>
#include <Wire.h>
// Replace with your network credentials
const char* ssid = "ssdi";
const char* password = "pwd";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Assign output variables to GPIO pins
#define ESP8266_LED 3
//const int relay2Pin = D8; // FOR GREEN LED
const int output4 = 4;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
IPAddress ip(192,168,110,80);
IPAddress gateway(192,168,110,1);
IPAddress subnet(255,255,255,0);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("non riesco a connettermi");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
testo = "Ip: ";
testo.concat(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
String readString = getValue(header, ' ', 1);
Serial.println("+++++++++++++++");
Serial.println(readString);
Serial.println("+++++++++++++++");
if (header.indexOf("IP-RELE") >= 0) {
//RECUPERO CIO CHE VIENE PASSATO DAL WEB
String IP = readString.substring(9,23);
String PIN = readString.substring(24);
Serial.println("IP " + IP + " PIN " + PIN);
client.print("GET 192.168.110.80/RELE=4");
client.println(" HTTP/1.1\n");
client.println();
Serial.println("RICHIESTA FATTA ");
} // Display the HTML web page
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}//fine metodo getvalue
non ho errori lato esecuzione ma non viene effettuata alcuna richiesta HTTP, dove sbaglio ?