Jag har laborerat med WiFi modul och fått det att fungera. Arduinon kopplar upp sig mot WiFi och skickar ut min html kod när jag anropar den IP adressen.
Nu till mitt problem.
Jag har en avståndsmätare av enklaste modell som i exempelkoden skriver till serielmonitor. Det jag nu skulle vilja göra är att värdet från avståndsmätaren skall skickas till webbsidan.
I nuläget skickas bara exakt den text jag för in i html koden, har gjort några mediokra försök att hänvisa in värdet men den skriver bara "distance" på websidan.
Bifogar min kod här så kanske ni kan hjälpa mig. Jag har använt mig av koden som finns på kjell&co.
Längst ner finns avståndsmätarens kodsnutt.
Kod: Markera allt
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
// Create WiFi module object on GPIO pin 6 (RX) and 7 (TX)
SoftwareSerial Serial1(6, 7);
// Declare and initialise global arrays for WiFi settings
char ssid[] = "HD-EXT";
char pass[] = "********";
// Declare and initialise variable for radio status
int status = WL_IDLE_STATUS;
// Create a web server on port 80
WiFiEspServer server(80);
int inputPin=2; //ECHO pin
int outputPin=4; //TRIG pin
void setup() {
// Initialize serial for debugging
Serial.begin(115200);
// Initialize serial for ESP module
Serial1.begin(9600);
// Initialize ESP module
WiFi.init(&Serial1);
// Check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// Don't continue
while (true);
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
// Start the web server
server.begin();
}
void printWifiStatus() {
// Print the SSID of the network
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print the IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
// Listen for incoming clients
WiFiEspClient client = server.available();
if (client) {
Serial.println("New client");
// An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// If you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
Serial.println("Sending response");
// Send a standard HTTP response header
client.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n");
client.print("<!DOCTYPE HTML>\r\n");
client.print("<html>\r\n");
client.print("<head>\r\n");
client.print("<title>My Arduino</title>\r\n");
client.print("</head>\r\n");
client.print("<body>\r\n");
client.print("<h1>distance</h1>\r\n");
client.print("<p>We're online!</p>\r\n");
client.print("</body>\r\n");
client.print("</html>\r\n");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// Give the web browser time to receive the data
delay(10);
// Close the connection:
client.stop();
Serial.println("Client disconnected");
}
{
digitalWrite(outputPin, HIGH); //Trigger ultrasonic detection
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
int distance = pulseIn(inputPin, HIGH); //Read ultrasonic reflection
distance= distance/58; //Calculate distance
Serial.println(distance); //Print distance
delay(2000);
}
}