Components and supplies
Arduino UNO
Arduino WiFi shield
Project description
Code
Web server for Arduino
arduino
When you upload and compile this file to your Arduino board, it will serve as a portable webserver which allows the board to be controlled remotely.
1/* 2 PROCE55 WiFi Server Example with JSON request parser 3 */ 4 5#include <SPI.h> 6#include <WiFi.h> 7#include <LiquidCrystal.h> 8 9char ssid[] = "P55"; // your network SSID (name) 10char pass[] = "PROCE55mobile"; // your network password 11//char ssid[] = "EG1"; // your network SSID (name) 12//char pass[] = "XML4SDXML4SD1"; // your network password 13//char ssid[] = "Tech_D0047758"; // your network SSID (name) 14//char pass[] = "WBTJZXJU"; // your network password 15// int keyIndex = 0; // your network key Index number (needed only for WEP) 16IPAddress ip(192, 168, 0, 90); // static IP for WiFi shield if needed 17 18int status = WL_IDLE_STATUS; 19WiFiServer server(80); // Listen on tcp port 80 20 21// initialize the library with the numbers of the interface pins 22// LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 23LiquidCrystal lcd(4, 9, 5, 8, 3, 2); // 13 instead of 4 (4,7,10,11 and 12 used by Wifi Shield) 24 25void setup() { 26 //Serial.begin(9600); // initialize serial communication - comment out for better perfomance 27 //while (!Serial) { 28 // wait serial port initialization 29 //} 30 31 //pinMode(10, OUTPUT); // HDG204 WiFi deactivate SD card pin 10, if necessary 32 //digitalWrite(10, HIGH); 33 34 // Turn of the LED by default 35 pinMode(6, OUTPUT); 36 digitalWrite(6, LOW); 37 38 //pinMode(LED_BUILTIN, OUTPUT); 39 //digitalWrite(LED_BUILTIN, LOW); 40 41 // Init the LCD 16x2 42 lcd.begin(16, 2); 43 lcd.clear(); 44 45 // check for the presence of the shield: 46 if (WiFi.status() == WL_NO_SHIELD) { 47 //Serial.println("WiFi shield not present"); 48 lcd.clear(); 49 lcd.setCursor(0, 1); 50 lcd.print(F("Error: no wifi!")); 51 while (true); // don't continue 52 } 53 54 /* 55 if (WiFi.firmwareVersion() != "1.1.0") { 56 Serial.println(F("Please upgrade the WiFi Shield firmware!")); 57 lcd.clear(); 58 lcd.setCursor(0, 1); 59 lcd.print(F("Firmware old!")); 60 } 61 */ 62 63 // attempt to connect to Wifi network: 64 lcd.clear(); 65 lcd.setCursor(0, 0); 66 lcd.print(F("Initializing...")); 67 lcd.setCursor(0, 1); 68 lcd.print(F("SSID:")); 69 lcd.setCursor(5, 1); 70 lcd.print(ssid); 71 72 // Comment the WiFi.config if static setup not needed, DHCP applies then 73 //WiFi.config(ip); // WiFi.config(ip, dns, gateway, subnet); 74 75 while (status != WL_CONNECTED) { 76 //Serial.print(F("Attempting to connect to WLAN SSID: ")); 77 //Serial.println(ssid); // print the network name (SSID); 78 status = WiFi.begin(ssid, pass); 79 // wait 6 seconds for connection: 80 delay(6000); 81 } 82 server.begin(); // start the web server on port 80 83 84 lcd.clear(); 85 lcd.setCursor(0, 0); 86 lcd.print("Ready"); 87 printWifiStatus(); 88} 89 90void loop() { 91 WiFiClient client = server.available(); // listen for incoming clients 92 93 if (client) { 94 //Serial.print(F("Client connected")); 95 String req_str = ""; 96 String currentLine = ""; 97 int data_length = 0; 98 int data_read_length = 0; 99 boolean skip = true; // Skip first CRLF in request header 100 int response_length = 0; // HTTP response Content-Length 101 int resp_value = -1; // LED status 102 103 lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Client")); lcd.setCursor(0, 1); lcd.print(F("connected")); 104 105 while (client.connected()) 106 { 107 if (client.available()) { 108 char c = client.read(); 109 //Serial.write(c); 110 111 if (data_length > data_read_length && !skip) { 112 // Read remaining request body data 113 req_str += c; 114 data_read_length++; 115 //Serial.println(data_read_length); 116 } 117 118 if (data_length <= data_read_length && !skip) { // c == '\n' && 119 // Request read completed, send response 120 121 // find the request commands manually based on a substring 122 if (req_str.lastIndexOf("\\"OFF\\"") >= 0) { 123 digitalWrite(6, LOW); 124 resp_value = 0; 125 //Serial.println(F("\ \ 126===> Setting LED off...")); 127 } 128 else if (req_str.lastIndexOf("\\"ON\\"") >= 0) { 129 digitalWrite(6, HIGH); 130 resp_value = 1; 131 //Serial.println(F("\ \ 132===> Setting LED on...")); 133 } 134 else { 135 // GET 136 if (digitalRead(6) == 1) { 137 resp_value = 1; // HIGH 138 } 139 else { 140 resp_value = 0; // LOW 141 } 142 } 143 144 // Send the response 145 //Serial.println(F("===> Request read completed, sending response...")); 146 147 response_length = 56; // = 51 + 1 + 4; 148 149 client.write("HTTP/1.1 200 OK\ \ 150"); 151 client.write("Content-Length: "); 152 client.print(response_length); // sizeof(resp) 153 client.write("\ \ 154"); 155 client.write("Connection: close\ \ 156"); 157 client.write("Access-Control-Allow-Origin: *\ \ 158"); 159 client.write("Content-type: application/json; charset=utf-8\ \ 160"); 161 client.write("\ \ 162"); 163 client.write("{\\"Parameters\\":[{\\"ParamName\\":\\"STATUS\\",\\"ParamValue\\":\\""); // 51 characters 164 client.print(resp_value); // 1 character 165 client.write("\\"}]}"); // 4 characters 166 client.write("\ \ 167"); 168 break; 169 } 170 else if (c == '\n' && currentLine.length() == 0 && skip) { 171 // Start of the request body after this line 172 //Serial.println(F("===> Start of the request body.")); 173 skip = false; 174 } 175 else if (c == '\n' && currentLine.length() > 15 && currentLine.startsWith(F("Content-Length:")) && skip) { 176 //skip = false; 177 //String temp = currentLine.substring(15); 178 //temp.trim(); 179 //Serial.print(F("===> Content length detected: ")); 180 //data_length = temp.toInt(); 181 data_length = currentLine.substring(15).toInt(); 182 //Serial.println("Content-length: "); 183 //Serial.println(data_length); 184 } 185 186 if (c == '\n') { 187 // new line 188 currentLine = ""; 189 } else if (c != '\r') { 190 // add current character to the current line 191 currentLine += c; 192 } 193 194 } // if data available 195 } // client still connected 196 197 // give the client time to receive the data 198 delay(50); 199 // close the connection: 200 client.stop(); 201 //Serial.println(F("===> Client disconnected.")); 202 printWifiStatus(); 203 } 204} 205 206/* 207POST / HTTP/1.1 208Content-Length: 328 209Content-Type: application/json 210 211{"FunctionName":"TEST","Status":"","StatusText":"","TextEncoding":"","SecurityToken":"","Parameters":[{"ParamName":"function_name","ParamValue":"TEST"},{"ParamName":"requestencoding","ParamValue":"UTF-8"},{"ParamName":"responseencoding","ParamValue":"UTF-8"},{"ParamName":"PARAM1","ParamValue":"Test passed!"}],"TableValues":[]} 212 213 */ 214 215void printWifiStatus() { 216 // print the SSID of the network you're attached to: 217 Serial.print(F("SSID: ")); 218 Serial.println(WiFi.SSID()); 219 220 // print your WiFi shield's IP address: 221 IPAddress ip = WiFi.localIP(); 222 Serial.print(F("IP Address: ")); 223 Serial.println(ip); 224 225 // Status to LCD 226 lcd.clear(); 227 lcd.setCursor(0, 0); 228 lcd.print(WiFi.SSID()); 229 230 lcd.setCursor(0, 1); 231 lcd.print(IpAddress2String(ip)); 232 233 // print the received signal strength: 234 Serial.print(F("Signal strength (RSSI):")); 235 Serial.print(WiFi.RSSI()); 236 Serial.println(F(" dBm")); 237 // print where to go in a browser: 238 Serial.print(F("Send your HTTP requests to http://")); 239 Serial.println(ip); 240 return; 241} 242 243String IpAddress2String(const IPAddress& ipAddress) 244{ 245 return String(ipAddress[0]) + String(".") +\\ 246 String(ipAddress[1]) + String(".") +\\ 247 String(ipAddress[2]) + String(".") +\\ 248 String(ipAddress[3]) ; 249}
Downloadable files
Example circuit
Example circuit
Example circuit
Example circuit
Comments
Only logged in users can leave comments