simple web server nort working with arduino wifi shield - arduino

I have recently bought an arduino wifi shield(Atmal chip 32UC3A1512-U), which I connected with
my Arduino Mega ADK R3 board)...It is getting connected to my wifi network, But when I run the
SimpleWebServer Example provided in the library to on/off the LED is not working. The code is given below...
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "belkin.E33"; // your network SSID (name)
char pass[] = "abc123cde456"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(9, OUTPUT); // set the LED pin mode
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out 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
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();
// the content of the HTTP response follows the header:
client.print("Click here turn the LED on pin 9 on<br>");
client.print("Click here turn the LED on pin 9 off<br>");
// 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
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(9, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(9, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
The result that I am getting in the serial monitor is
Attempting to connect to Network named: belkin.E33
SSID: belkin.E33
IP Address: 192.168.2.5
strength (RSSI):-56 dBm
To see this page in action, open a browser to http://192.168.2.5
But When I am opening the browser with the specified IP address, It is showing
Could not Connect to 192.168.2.5
I have tried this in mozilla and chrome from my ubuntu machine...also tried from some other machines in the same network but with the same result. But when I am pinging to 192.168.2.5 it is pinging...What went wrong??? . My friend adviced to change the firmware...Is it an issue,bcas as told earlier simple examples for establishing the connection are working...Please guide me

I've got the same problem after upgrading Arduino IDE to lastest version (v2) from v1.0.8 which is doing fine with the wifi shield tests (client and server).
Going to try the nightly build now and see if it's fixed.
Edit: Yeap, Nightly build solves this issue.

Related

Communication between Postman and Arduino

I plan to send from a server a POST request to my Arduino Uno WiFi Rev2. More correctly, when the server sends this request a servomotor controlled by the Arduino should start moving. Right now, I'm using Postman to try and connect with the Arduino but I can't get it to work. So first I connect the Arduino to WiFi using my smartphone as a hotspot. This should be the unit's IP address, right?
I then try to send a POST request to this IP but it doesn't work. I'm also unsure which port number I should use, so I have just been trying with the standard ones (80, 4430, etc).
What am I doing wrong and how should I proceed?
EDIT: Here is my code.
#include <SPI.h>
#include <Servo.h>
#include <WiFiNINA.h>
char ssid[] = "MyNetwork"; // The network SSID
char pass[] = "testtest"; // The network password
int status = WL_IDLE_STATUS; // The Wifi radio's connection status
Servo servo_9; // Initializes the servomotor
WiFiServer server(80); // Server socket
//WiFiClient client;
WiFiClient client = server.available();
void setup() {
// Connects the servomotor to pin 9
servo_9.attach(9, 500, 2500);
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
enable_WiFi();
connect_WiFi();
server.begin();
printCurrentNet();
printWifiData();
}
void loop() {
// Check the network connection once every 10 seconds:
delay(10000);
client = server.available();
if(client){
printWEB();
}
}
void enable_WiFi(){
// Check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// Don't continue
while (true);
}
// Check if the latest Firmware version is installed
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
}
void connect_WiFi(){
// Attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// Wait 10 seconds for connection:
delay(10000);
}
// Now the arduino is connected, so print out the data:
Serial.print("You're connected to the network: ");
Serial.println();
}
void printCurrentNet() {
// Print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// Print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
// Print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
void printWifiData() {
Serial.println("Your board's IP and MAC address: ");
// Print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
Serial.println();
}
// Find the MAC adress for your Arduino board
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
void printWEB() {
if (client) { // if you get a client,
Serial.println("new client"); // print a message out 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
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();
// 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
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void servomotorGate(){
int position = 0;
for (position = 0; position <= 90; position += 1) {
servo_9.write(position);
Serial.println("Opening the gate");
}
delay(5000); // Wait for 5000 millisecond(s)
for (position = 90; position >= 0; position -= 1) {
servo_9.write(position);
Serial.println("Closing the gate");
}
}
I added the server client to listen for connections. However, the main reason why I couldn't connect to my IP address with Postman was that I was trying to get WiFi from my phone as a hotspot. I guess there is a problem with the port forwarding when using a hotspot. In the end, I solved the issue by connecting to a normal router WiFi network.

Unstable Arduino Web Server

I have been trying to setup a web server using Arduino. I have an UNO and a HanRun HR91105A I got off the internet, and I am using a modified version of the WebServer example to test my code. It did in fact work at first. But after setting up port forwarding, the connection suddenly became unstable. It connects and works for a few minutes, then suddenly I can't even ping it. Trying to ping the Arduino results in request time out. Research online suggests 2 possibilities:
1.) All the RAM is used up
2.) Ethernet shield is faulty
Below is my code
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x44, 0x00, 0x10, 0x20, 0x8C, 0x0A
};
IPAddress ip(192,168,1,90);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(8081);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 2");
client.println();
client.println("<!DOCTYPE HTML>");
//-----------------Type in outputs below-------------------------------------
client.println("<html>");
client.print("Hello World!");
client.print("<p id='Header'>");
client.print("Sensor Data");
client.println("</p>");
client.print("<p id='Pressure'>");
client.print("Pressure:");
client.println("</p>");
client.print("<p id='Acceleration'>");
client.print("Acceleration:");
client.println("</p>");
client.println("<br /)");
client.println("</html>");
break;
//-----------------End of outputs--------------------------------------------
}
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(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Additionally, the arduino does have a static IP, so I'm pretty sure it is not an issue of DHCP lease expiry.
I highly suspect that the shield is faulty as it gets very hot while in operation. Plus it IS a knockoff. But I can't dismiss the possibility that my coding is just inefficient as I am not very experienced. Any help would be appreciated. Thanks.
Your Arduino will opperate unpredictably when RAM is not enough.
In this code, there are many constant string. you should store those strings in FLASH memory to save RAM. In order to to so, use F() macro. For example.
client.println(F("HTTP/1.1 200 OK"));
If your program is big, I recommend to use the shield that equips an embedded web sever (e.g PHPoC Shield)

MQTT on Arduino not working

I'm using Arduino Uno and Wi-Fi shield. I'm trying to implement MQTT protocol for communication and I tried two libraries for Arduino. First one is Knolleary's PubSubClient: http://pubsubclient.knolleary.net/ . I modified original example a little bit to use WiFi module instead of ethernet. Sending works but not every time (sometimes, the message is sent and sometimes not). But receiving via callback function doesn't work at all.
Here is my code:
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the desired topic
- subscribes to the desired topic
*/
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "[DELETED]"; // your network SSID (name)
char pass[] = "[DELETED]"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Update these with values suitable for your network.
IPAddress server(85, 119, 83, 194);
WiFiClient WifiClient;
void callbackFunc(char* topic, byte* payload, unsigned int length) {
Serial.println("test message received");
/*Serial.println();
Serial.println("=============== MESSAGE RECEIVED ================================");
Serial.print("Topic: ");
Serial.print(topic);
Serial.println();
Serial.println((const char *) payload);*/
}
PubSubClient client(server, 1883, callbackFunc, WifiClient);
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.println("Starting....");
Serial.println("Initializing Wifi...");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
}
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// If you are connected, print out success message
else
Serial.println("Connected to network");
if (client.connect("arduinoClient")) {
Serial.println("Connected to server! Sending message...");
client.publish("randy/test","hello world");
client.subscribe("randy/test");
Serial.println("Sent!");
}
else
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
}
void loop()
{
client.loop();
delay(1000);
if (!client.connected())
{
if(!client.connect("arduinoClient"))
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
else
{
client.subscribe("randy/test");
Serial.println("INFO: reconnected!");
}
}
}
As you can see, I'm using http://test.mosquitto.org/ for testing. I'm also using mosquitto on Ubuntu where I'm subscribed to the same topic and from where (another terminal window) I'm publishing to the same topic. Here is the picture of both windows:
As you can see, "hello world" message from Arduino is received successfully (but not every time), and when I publish "bla" message from another window, it's successfully received on mosquitto (on image) but NOT on Arduino. Is there something wrong with my code or? I found similar problem here: Arduino Knolleary PubSubClient will publish messages but can't receive them
It's worth noticing that it keeps getting reconnected all the time. As you can see, in loop() I put a part of code which will connect and subscribe again if connection is lost. On Serial Monitor I get "INFO: Reconnected!" message every 2-3 seconds.
Adafruit library
Then I tried the Adafruit library, but it won't connect at all! I tried with test.mosquitto.org and with io.adafruit.com from their example, but I just keep getting "Connection failed!" error. I tried making many changes and combinations but no luck so far. Once I managed to get "Failed to subscribe" instead of "Connection failed" but this was only once and next time with the same code I get "Connection failed" again.
Here's my code:
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "[DELETED]"; // your network SSID (name)
char pass[] = "[DELETED]"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Update these with values suitable for your network.
IPAddress server(85, 119, 83, 194);
WiFiClient WifiClient;
void callbackFunc(char* topic, byte* payload, unsigned int length) {
Serial.println("test message received");
/*Serial.println();
Serial.println("=============== MESSAGE RECEIVED ================================");
Serial.print("Topic: ");
Serial.print(topic);
Serial.println();
Serial.println((const char *) payload);*/
}
PubSubClient client(server, 1883, callbackFunc, WifiClient);
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.println("Starting....");
Serial.println("Initializing Wifi...");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
}
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// Ff you are connected, print out success message
else
Serial.println("Connected to network");
if (client.connect("arduinoClient")) {
Serial.println("Connected to server! Sending message...");
client.publish("randy/test","hello world");
client.subscribe("randy/test");
Serial.println("Sent!");
}
else
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
}
void loop()
{
client.loop();
delay(1000);
if (!client.connected())
{
if(!client.connect("arduinoClient"))
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
else
{
client.subscribe("randy/test");
Serial.println("INFO: reconnected!");
}
}
}
Any idea what's wrong with those libraries or my code?
As mentioned in the comments
When using example code on public brokers make sure you change the client id to something random as the odds of clashing with somebody else are pretty high. Because client ids have to be unique else they will cause a reconnection storm

Arduino servo control over wi-fi doesn't work

Hi I'm using arduino uno and wifi shield. I'm trying to create using the wifi shield to connect to a servo motor through webserver. To be honest I dont really know what im doing. This code is able to control the servo motor once and the whole system is shutdown.
can someone look into my codes and see what is wrong? If can I hope the code is able to control the servo multiple time without stopping.
#include <SPI.h>
#include <WiFi.h>
#include <Servo.h>
char ssid[] = "wifi"; // your network SSID (name)
char pass[] = "asdfghjkl"; // your network password
int keyIndex = 0;
// your network key Index number (needed only for WEP)
Servo myservo;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
myservo.attach(9);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(1000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out 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
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();
// the content of the HTTP response follows the header:
client.print("Click here turn the Open the door <br>");
client.print("Click here turn the Close the door <br>");
// 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
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
// scale it to use it with the servo (value between 0 and 180)
myservo.write(800); // sets the servo position according to the scaled value
delay(15); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
// scale it to use it with the servo (value between 0 and 180)
myservo.write(2500); // sets the servo position according to the scaled value
delay(15); // GET /L turns the LED off
}
}
}
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}

Can't reliably get Arduino Ethernet shield to accept connections

I have been playing with the Arduino Ethernet shield. I can't get it to reliably accept connections when running a webserver type application. When I can't connect to it, I can't ping it. The message I get is "Reply from 192.168.1.14: Destination host unreachable.". Where 192.168.1.14 is the ip of the computer I am trying to connect from. Why would an ip on my local subnet be unreachable? Is the shield not correctly identifying itself on the network? Is there anything I can do in Arduino code?
Here is my code. If you see anything wrong, please let me know.
Update: I plugged it into my main home router instead of a switch and it worked immediately there. At any rate, once I can connect to it, it keep working as long as its powered on.
#include <WString.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
#define DOOR_BUTTON 2
String readString = String(100); //string for fetching data from address
void setup()
{
Serial.begin(9600);
digitalWrite(DOOR_BUTTON, LOW);
pinMode(DOOR_BUTTON, OUTPUT);
// start the Ethernet connection and the server:
Serial.println("Initiaizing ethernet...");
// this uses a fixed address
Ethernet.begin(mac, ip);
// get an address with DHCP
//if (Ethernet.begin(mac) == 0)
// Serial.println("Failed to configure Ethernet using DHCP");
// give the card a second to initialize
delay(1000);
server.begin();
Serial.print("Garage Door Opener Control Ready at IP address ");
Serial.println(Ethernet.localIP());
}
void loop()
{
// command received (one character) '1' - activate garage door button
char cmd = 0; // 1 - pulse button
boolean done = false; // set to indicate that response is complete
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
readString = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
//int i = c;
//Serial.print("(");
//Serial.print(i);
//Serial.print(")");
// store character received in receive string
if (readString.length() < 100) {
readString += (c);
}
// check for end of line
if (c == '\n') {
//Serial.print("Receved line: ");
//Serial.print(readString);
// process line if its the "GET" request
// a request looks like "GET /?1" or "GET /?2"
if (readString.indexOf("GET") != -1) {
if (readString.indexOf("?1") != -1)
cmd = '1';
// check for other commands here. ie turn on light, etc.
//if (readString.indexOf("?2") != -1)
// cmd = '2';
}
// if a blank line was received (just cr lf, length of 2), then its the end of the request
if (readString.length() == 2) {
if (cmd == '1'){
Serial.println("Activate Button");
digitalWrite(DOOR_BUTTON, HIGH);
delay(1000);
digitalWrite(DOOR_BUTTON, LOW);
}
// add other commands here
// send web page back to client
Serial.println("sending web page");
SendWebPage(client);
Serial.println("web page sent");
cmd = 0;
// break out and disconnect. This will tell the browser the request is complete without having to specify content-length
break;
} // end of request reached
// start line over
readString = "";
} // end of line reached
} // end data is available from client
} // end cient is connected
// give the web browser time to receive the data
Serial.println("delay before disconnect");
delay(100);
// close the connection:
client.stop();
Serial.println("client disonnected");
} // end client has been created
}
// send web page
void SendWebPage(EthernetClient client)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
// to specify the length, wooul have to construct the entire string and then get its length
//client.println("Content-Length: 1234");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>Garage Door Control</title>");
client.println("<style type='text/css'>");
client.println(".label {font-size: 40px; text-align:center;}");
client.println("button {width: 200px; height: 100px; font-size: 40px; -webkit-appearance: none; background-color:white; }");
client.println("</style>");
client.println("<script type='text/javascript'>");
client.println("function OnButtonClicked(parm) { window.location.href=\"X?\" + parm; }");
client.println("</script>");
client.println("</head>");
client.println("<body style=\"background-color:orange\">");
client.println("<div class=\"label\">");
client.println("Garage Door Control<br/><br/>");
// future idea: could read a limit switch on the garage door here and tell the user if the door is currently open or closed
/*
if (digitalRead(DOOR_OPEN_INPUT) == HIGH)
client.println("Door is Open");
else
client.println("Door is Closed");
client.println("<br>");
*/
// door open / close button
client.println("<button onclick=\"OnButtonClicked('1');\">Activate</button><br/>");
// add more buttons here
// button separator
//client.println("<div style=\"height:20px\"></div>");
//client.println("<button onclick=\"OnButtonClicked('2');\">Off</button>");
client.println("</div>");
client.println("</body>");
client.println("</html>");
client.println("");
}
The code specifies IPAddress ip(192,168,1,177); but in the question post you refer to the address 192.168.1.14. sS this a typo or are you just pinging the wrong address?

Resources