/******************************************************************** connect establishes a connection to the Slack RTM API ********************************************************************/ bool ArduinoSlackBot::connect() { // Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.start) HTTPClient http; String slackAddr = "https://slack.com/api/rtm.start?token="; slackAddr = String(slackAddr + slackToken); PRINTLN(slackAddr); http.begin(slackAddr.c_str(), slackSSLFingerprint); int httpCode = http.GET(); if (httpCode != HTTP_CODE_OK) { PRINTF("HTTP GET failed with code %d\n", httpCode); return false; } WiFiClient *client = http.getStreamPtr(); client->find("wss:\\/\\/"); String host = client->readStringUntil('\\'); String path = client->readStringUntil('"'); path.replace("\\/", "/"); // Step 2: Open WebSocket connection and register event handler PRINTLN("WebSocket Host=" + host + " Path=" + path); webSocket.beginSSL(host, 443, path, "", ""); webSocket.onEvent(webSocketEvent); return true; }
void EIoTCloudRestApi::sendParameter(const char * instaceParamId, String value) { WiFiClient client; while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) { debug("connection failed"); wifiConnect(); } String url = ""; // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value] url += "/RestApi/SetParameter/" + String(instaceParamId) + "/" + value; // generate EasIoT cloud update parameter URL debug("POST data to URL: "); #ifdef DEBUG char buff[300]; url.toCharArray(buff, 300); debug(buff); #endif client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + "Connection: close\r\n" + "Content-Length: 0\r\n" + "\r\n"); delay(100); while(client.available()){ #ifdef DEBUG String line = client.readStringUntil('\r'); line.toCharArray(buff, 300); debug(buff); #endif } }
void loop() { // NETWORKING // TODO FIGURE OUT WHETHRE THIS IS BLOCKING WiFiClient client = server.available(); if (!client) return; // TODO CONTINUE LOOPING int timeout = COMM_TIMEOUT; // TODO LESS BUSY WAIT? logMsg("New client"); while(!client.available() && --timeout>0) { delay(1); } if (timeout <= 0) { logMsg("TIMEOUT"); client.flush(); return; } // GET MSG // TODO FIGURE OUT WHY DOESN'T WORK DIRECTLY String str = client.readStringUntil(0); client.flush(); logMsg(String("Got message [") + str + "]"); // SEND REPLY client.print(processRequest(str).toString()); delay(1); logMsg("Client disonnected"); }
String DHwifi::HttpRequest( String req ) { WiFiClient client; char host[99]; m_remoteServer.toCharArray( host, 99); const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.print( host ); Serial.println(" connection failed"); return ""; } client.print(String("GET ") + req + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); String result; // Read all the lines of the reply from server and print them to Serial while (client.available()) { result = client.readStringUntil('\r'); } return result; }
/** * Send the supplied message then read back the other node's response * and pass that to the user-supplied handler. * * @target_ssid The name of the AP the other node has set up. * @message The string to send to the node. * @returns: True if the exchange was a succes, false otherwise. * */ bool ESP8266WiFiMesh::exchangeInfo(String message, WiFiClient curr_client) { curr_client.println( message.c_str() ); if (!waitForClient(curr_client, 1000)) return false; String response = curr_client.readStringUntil('\r'); curr_client.readStringUntil('\n'); if (response.length() <= 2) return false; /* Pass data to user callback */ _handler(response); return true; }
void sync_led(WiFiClient client) { int new_brightness = -1; while (client.available()) { String line = client.readStringUntil('\r'); int ix_magic_ld = line.indexOf("ld"); int ix_colon = line.indexOf(":"); if (ix_magic_ld != -1) { // Sync the LEDs int ix_led = line.substring(ix_magic_ld + 2, ix_colon).toInt(); // The RGB value are encoded in Base 10 (not base 16) with "zero" padding. // LD<LED_INDEX>: FFFFFF // | | | | // 0 2 C 2 String hexval = line.substring(ix_colon + 2); long value = strtol(hexval.c_str(), NULL, 16); int led_r = value >> 16; int led_g = value >> 8 & 0xFF; int led_b = value & 0xFF; pixels.setPixelColor(ix_led, pixels.Color(led_r, led_g, led_b)); pixels.show(); #if DEBUG Serial.print("LED#"); Serial.print(ix_led); Serial.print(" <rgb>:"); Serial.print(led_r); Serial.print(','); Serial.print(led_g); Serial.print(','); Serial.print(led_b); Serial.print('\n'); #endif } int ix_magic_br = line.indexOf("brightness"); if (ix_magic_br != -1) { // Update the Brightness Values new_brightness = line.substring(ix_colon + 2).toInt(); } }
void TimeClient::updateTime() { WiFiClient client; const int httpPort = 80; if (!client.connect("google.com", httpPort)) { Serial.println("connection failed"); return; } // This will send the request to the server client.print(String("GET / HTTP/1.1\r\n") + String("Host: google.com\r\n") + String("Connection: close\r\n\r\n")); int repeatCounter = 0; while(!client.available() && repeatCounter < 10) { delay(1000); Serial.println("."); repeatCounter++; } String line; int size = 0; client.setNoDelay(false); while(client.connected()) { while((size = client.available()) > 0) { line = client.readStringUntil('\n'); line.toUpperCase(); // example: // date: Thu, 19 Nov 2015 20:25:40 GMT if (line.startsWith("DATE: ")) { Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31)); int parsedHours = line.substring(23, 25).toInt(); int parsedMinutes = line.substring(26, 28).toInt(); int parsedSeconds = line.substring(29, 31).toInt(); Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds)); localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds); Serial.println(localEpoc); localMillisAtUpdate = millis(); } } } }
void APServer(){ WiFiClient client = server.available(); // Check if a client has connected if (!client) { return; } Serial.println(""); Serial.println("New client"); // Wait for data from client to become available while (client.connected() && !client.available()) { delay(1); } // Read the first line of HTTP request String req = client.readStringUntil('\r'); Serial.println(req); String s; String gsid; String gpwd; //if the form has been submitted if(req.indexOf("ssid")!=-1){ //TODO Make this a POST request //I'm basically hoping they're being nice int x = req.indexOf("ssid")+5; gsid = req.substring(x,req.indexOf("&",x)); x = req.indexOf("pwd")+4; gpwd = req.substring(x,req.indexOf(" ",x)); s = gsid; saveWifiConfig(gsid,gpwd); Serial.print("Restarting"); client.print("Thanks! Restarting to new configuration"); client.flush(); ESP.restart(); } else{ s = "<h1>Welcome to your ESP</h1><form action='/' method='get'><table><tr><td>SSID</td><td><input type='text' name='ssid'></td></tr><tr><td>Password</td><td><input type='text' name='pwd'></td><tr><td /><td><input type='submit' value='Submit'></td></tr></form>"; client.print(s); } client.flush(); delay(1); //Serial.println("Client disonnected"); }
bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); //reset header value for (int i = 0; i < _headerKeysCount; ++i) { _currentHeaders[i].value =String(); } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Invalid request: "); DEBUG_OUTPUT.println(req); #endif return false; } String methodStr = req.substring(0, addr_start); String url = req.substring(addr_start + 1, addr_end); String versionEnd = req.substring(addr_end + 8); _currentVersion = atoi(versionEnd.c_str()); String searchStr = ""; int hasSearch = url.indexOf('?'); if (hasSearch != -1){ searchStr = urlDecode(url.substring(hasSearch + 1)); url = url.substring(0, hasSearch); } _currentUri = url; _chunked = false; HTTPMethod method = HTTP_GET; if (methodStr == "POST") { method = HTTP_POST; } else if (methodStr == "DELETE") { method = HTTP_DELETE; } else if (methodStr == "OPTIONS") { method = HTTP_OPTIONS; } else if (methodStr == "PUT") { method = HTTP_PUT; } else if (methodStr == "PATCH") { method = HTTP_PATCH; } _currentMethod = method; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("method: "); DEBUG_OUTPUT.print(methodStr); DEBUG_OUTPUT.print(" url: "); DEBUG_OUTPUT.print(url); DEBUG_OUTPUT.print(" search: "); DEBUG_OUTPUT.println(searchStr); #endif //attach handler RequestHandler* handler; for (handler = _firstHandler; handler; handler = handler->next()) { if (handler->canHandle(_currentMethod, _currentUri)) break; } _currentHandler = handler; String formData; // below is needed only when POST type request if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_PATCH || method == HTTP_DELETE){ String boundaryStr; String headerName; String headerValue; bool isForm = false; bool isEncoded = false; uint32_t contentLength = 0; //parse headers while(1){ req = client.readStringUntil('\r'); client.readStringUntil('\n'); if (req == "") break;//no moar headers int headerDiv = req.indexOf(':'); if (headerDiv == -1){ break; } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 1); headerValue.trim(); _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("headerName: "); DEBUG_OUTPUT.println(headerName); DEBUG_OUTPUT.print("headerValue: "); DEBUG_OUTPUT.println(headerValue); #endif if (headerName == "Content-Type"){ if (headerValue.startsWith("text/plain")){ isForm = false; } else if (headerValue.startsWith("application/x-www-form-urlencoded")){ isForm = false; isEncoded = true; } else if (headerValue.startsWith("multipart/")){ boundaryStr = headerValue.substring(headerValue.indexOf('=')+1); isForm = true; } } else if (headerName == "Content-Length"){ contentLength = headerValue.toInt(); } else if (headerName == "Host"){ _hostHeader = headerValue; } } if (!isForm){ size_t plainLength; char* plainBuf = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT); if (plainLength < contentLength) { free(plainBuf); return false; } if (contentLength > 0) { if (searchStr != "") searchStr += '&'; if(isEncoded){ //url encoded form String decoded = urlDecode(plainBuf); size_t decodedLen = decoded.length(); memcpy(plainBuf, decoded.c_str(), decodedLen); plainBuf[decodedLen] = 0; searchStr += plainBuf; } _parseArguments(searchStr); if(!isEncoded){ //plain post json or other data RequestArgument& arg = _currentArgs[_currentArgCount++]; arg.key = "plain"; arg.value = String(plainBuf); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Plain: "); DEBUG_OUTPUT.println(plainBuf); #endif free(plainBuf); } } if (isForm){ _parseArguments(searchStr); if (!_parseForm(client, boundaryStr, contentLength)) { return false; } } } else { String headerName; String headerValue; //parse headers while(1){ req = client.readStringUntil('\r'); client.readStringUntil('\n'); if (req == "") break;//no moar headers int headerDiv = req.indexOf(':'); if (headerDiv == -1){ break; } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("headerName: "); DEBUG_OUTPUT.println(headerName); DEBUG_OUTPUT.print("headerValue: "); DEBUG_OUTPUT.println(headerValue); #endif if (headerName == "Host"){ _hostHeader = headerValue; } } _parseArguments(searchStr); } client.flush(); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Request: "); DEBUG_OUTPUT.println(url); DEBUG_OUTPUT.print(" Arguments: "); DEBUG_OUTPUT.println(searchStr); #endif return true; }
bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Parse Form: Boundary: "); DEBUG_OUTPUT.print(boundary); DEBUG_OUTPUT.print(" Length: "); DEBUG_OUTPUT.println(len); #endif String line; int retry = 0; do { line = client.readStringUntil('\r'); ++retry; } while (line.length() == 0 && retry < 3); client.readStringUntil('\n'); //start reading the form if (line == ("--"+boundary)){ RequestArgument* postArgs = new RequestArgument[32]; int postArgsLen = 0; while(1){ String argName; String argValue; String argType; String argFilename; bool argIsFile = false; line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("Content-Disposition")){ int nameStart = line.indexOf('='); if (nameStart != -1){ argName = line.substring(nameStart+2); nameStart = argName.indexOf('='); if (nameStart == -1){ argName = argName.substring(0, argName.length() - 1); } else { argFilename = argName.substring(nameStart+2, argName.length() - 1); argName = argName.substring(0, argName.indexOf('"')); argIsFile = true; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg FileName: "); DEBUG_OUTPUT.println(argFilename); #endif //use GET to set the filename if uploading using blob if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename"); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Name: "); DEBUG_OUTPUT.println(argName); #endif argType = "text/plain"; line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("Content-Type")){ argType = line.substring(line.indexOf(':')+2); //skip next line client.readStringUntil('\r'); client.readStringUntil('\n'); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Type: "); DEBUG_OUTPUT.println(argType); #endif if (!argIsFile){ while(1){ line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("--"+boundary)) break; if (argValue.length() > 0) argValue += "\n"; argValue += line; } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Value: "); DEBUG_OUTPUT.println(argValue); DEBUG_OUTPUT.println(); #endif RequestArgument& arg = postArgs[postArgsLen++]; arg.key = argName; arg.value = argValue; if (line == ("--"+boundary+"--")){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.println("Done Parsing POST"); #endif break; } } else { _currentUpload.status = UPLOAD_FILE_START; _currentUpload.name = argName; _currentUpload.filename = argFilename; _currentUpload.type = argType; _currentUpload.totalSize = 0; _currentUpload.currentSize = 0; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Start File: "); DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.println(_currentUpload.type); #endif if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); _currentUpload.status = UPLOAD_FILE_WRITE; uint8_t argByte = _uploadReadByte(client); readfile: while(argByte != 0x0D){ if (!client.connected()) return _parseFormUploadAborted(); _uploadWriteByte(argByte); argByte = _uploadReadByte(client); } argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if (argByte == 0x0A){ argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); goto readfile; } else { argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); _uploadWriteByte((uint8_t)('-')); goto readfile; } } uint8_t endBuf[boundary.length()]; client.readBytes(endBuf, boundary.length()); if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); _currentUpload.totalSize += _currentUpload.currentSize; _currentUpload.status = UPLOAD_FILE_END; if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("End File: "); DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.print(_currentUpload.type); DEBUG_OUTPUT.print(" Size: "); DEBUG_OUTPUT.println(_currentUpload.totalSize); #endif line = client.readStringUntil(0x0D); client.readStringUntil(0x0A); if (line == "--"){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.println("Done Parsing POST"); #endif break; } continue; } else { _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); _uploadWriteByte((uint8_t)('-')); _uploadWriteByte((uint8_t)('-')); uint32_t i = 0; while(i < boundary.length()){ _uploadWriteByte(endBuf[i++]); } argByte = _uploadReadByte(client); goto readfile; } } else { _uploadWriteByte(0x0D); goto readfile; } break; } } } } int iarg; int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount; for (iarg = 0; iarg < totalArgs; iarg++){ RequestArgument& arg = postArgs[postArgsLen++]; arg.key = _currentArgs[iarg].key; arg.value = _currentArgs[iarg].value; } if (_currentArgs) delete[] _currentArgs; _currentArgs = new RequestArgument[postArgsLen]; for (iarg = 0; iarg < postArgsLen; iarg++){ RequestArgument& arg = _currentArgs[iarg]; arg.key = postArgs[iarg].key; arg.value = postArgs[iarg].value; } _currentArgCount = iarg; if (postArgs) delete[] postArgs; return true; } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Error: line: "); DEBUG_OUTPUT.println(line); #endif return false; }
void Humure::loop() { // Check if a client has connected WiFiClient client = this->server.available(); if (!client) { return; } Serial.println(""); Serial.println("New client"); // Wait for data from client to become available while(client.connected() && !client.available()){ delay(1); } // Read the first line of HTTP request String req = client.readStringUntil('\r'); // GET or POST? bool req_get; if ( req.startsWith("GET") ) { req_get = true; } else if ( req.startsWith("POST") ) { req_get = false; } else { Serial.print("Invalid request: "); Serial.println(req); return; } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { Serial.print("Invalid request: "); Serial.println(req); return; } req = req.substring(addr_start + 1, addr_end); Serial.print("Request "); if (req_get) { Serial.print("GET"); } else { Serial.print("POST"); } Serial.print(" : "); Serial.println(req); client.flush(); if ( req_get ) { if ( req == "/" ) { readSensor(); sendFullPage(client); } else if ( req == "/lamp") { jsonValue(client, String( ! led_status) ); } else if ( req == "/temperatur") { readSensor(); jsonValue(client, String( this->t) ); } else if ( req == "/humidity") { readSensor(); jsonValue(client, String( this->h) ); } else { client.print("HTTP/1.1 404 Not Found\r\n\r\n"); } } else { if ( req == "/lamp/on" ) { led_status = 0; jsonValue(client, String( ! led_status) ); } else if ( req =="/lamp/off" ) { led_status = 1; jsonValue(client, String( ! led_status) ); } else { client.print("HTTP/1.1 404 Not Found\r\n\r\n"); } digitalWrite(this->led, led_status); } Serial.println("Done with client"); }
void ESP8266WebServer::handleClient() { WiFiClient client = _server.available(); if (!client) { return; } #ifdef DEBUG Serial.println("New client"); #endif // Wait for data from client to become available while(client.connected() && !client.available()){ delay(1); } // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); HTTPMethod method = HTTP_GET; if (req.startsWith("POST")) { method = HTTP_POST; } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { #ifdef DEBUG Serial.print("Invalid request: "); Serial.println(req); #endif return; } req = req.substring(addr_start + 1, addr_end); String formData; if (method == HTTP_POST) { int contentLength = -1; int headerCount = 0; while(headerCount < 1024) { // there shouldn't be that much really String line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.length() > 0) { // this is a header ++headerCount; if (contentLength < 0 && line.startsWith("Content-Length")) { // get content length from the header int valuePos = line.indexOf(' ', 14); if (valuePos > 0) { String valueStr = line.substring(valuePos+1); contentLength = valueStr.toInt(); #ifdef DEBUG Serial.print("Content-Length: "); Serial.println(contentLength); #endif } } } else { break; } } #ifdef DEBUG Serial.print("headerCount="); Serial.println(headerCount); #endif if (contentLength >= 0) { formData = ""; int n = 0; // timeout counter while (formData.length() < contentLength && ++n < 3) formData += client.readString(); } else { formData = client.readStringUntil('\r'); // will return after timing out once } } else if (method == HTTP_GET) { int args_start = req.indexOf('?'); if (args_start != -1) { formData = req.substring(args_start + 1); req = req.substring(0, args_start); } } client.flush(); #ifdef DEBUG Serial.print("Request: "); Serial.println(req); Serial.print("Args: "); Serial.println(formData); #endif _parseArguments(formData); _handleRequest(client, req, method); }
bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { #ifdef DEBUG DEBUG_OUTPUT.print("Invalid request: "); DEBUG_OUTPUT.println(req); #endif return false; } String methodStr = req.substring(0, addr_start); String url = req.substring(addr_start + 1, addr_end); String searchStr = ""; int hasSearch = url.indexOf('?'); if (hasSearch != -1){ searchStr = url.substring(hasSearch + 1); url = url.substring(0, hasSearch); } _currentUri = url; HTTPMethod method = HTTP_GET; if (methodStr == "POST") { method = HTTP_POST; } else if (methodStr == "DELETE") { method = HTTP_DELETE; } else if (methodStr == "PUT") { method = HTTP_PUT; } else if (methodStr == "PATCH") { method = HTTP_PATCH; } _currentMethod = method; #ifdef DEBUG DEBUG_OUTPUT.print("method: "); DEBUG_OUTPUT.print(methodStr); DEBUG_OUTPUT.print(" url: "); DEBUG_OUTPUT.print(url); DEBUG_OUTPUT.print(" search: "); DEBUG_OUTPUT.println(searchStr); #endif String formData; // below is needed only when POST type request if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_PATCH || method == HTTP_DELETE){ String boundaryStr; String headerName; String headerValue; bool isForm = false; uint32_t contentLength = 0; //parse headers while(1){ req = client.readStringUntil('\r'); client.readStringUntil('\n'); if (req == "") break;//no moar headers int headerDiv = req.indexOf(':'); if (headerDiv == -1){ break; } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); if (headerName == "Content-Type"){ if (headerValue.startsWith("text/plain")){ isForm = false; } else if (headerValue.startsWith("multipart/form-data")){ boundaryStr = headerValue.substring(headerValue.indexOf('=')+1); isForm = true; } } else if (headerName == "Content-Length"){ contentLength = headerValue.toInt(); } } if (!isForm){ if (searchStr != "") searchStr += '&'; //some clients send headers first and data after (like we do) //give them a chance int tries = 100;//100ms max wait while(!client.available() && tries--)delay(1); size_t plainLen = client.available(); char *plainBuf = (char*)malloc(plainLen+1); client.readBytes(plainBuf, plainLen); plainBuf[plainLen] = '\0'; #ifdef DEBUG DEBUG_OUTPUT.print("Plain: "); DEBUG_OUTPUT.println(plainBuf); #endif if(plainBuf[0] == '{' || plainBuf[0] == '[' || strstr(plainBuf, "=") == NULL){ //plain post json or other data searchStr += "plain="; searchStr += plainBuf; } else { searchStr += plainBuf; } free(plainBuf); } _parseArguments(searchStr); if (isForm){ _parseForm(client, boundaryStr, contentLength); } } else { _parseArguments(searchStr); } client.flush(); #ifdef DEBUG DEBUG_OUTPUT.print("Request: "); DEBUG_OUTPUT.println(url); DEBUG_OUTPUT.print(" Arguments: "); DEBUG_OUTPUT.println(searchStr); #endif return true; }
void GPSUploader::_sendNextGpsData() { int startTimeUploading = millis(); if(!this->_initialized) return; Serial.printf("Upload: Pending messages: %d\n", this->_sdQueue.getCount()); Serial.println("Upload: preparation"); this->_sdQueue.flush();//avoid parallel flush() during server connection (too much mem) if(this->_sdQueue.getCount()==0) { Serial.println("Upload: no data to send"); return; } //connect to server and send various POST on the same connection WiFiClient client; // Serial.printf("Host=%s:%d\n", this->_configNode.getUploadServerHost().c_str(), this->_configNode.getUploadServerPort()); _watchdog.ping(); int startTime = millis(); if (!client.connect(this->_uploadServerHost.get(), this->_uploadServerPort.get())) { this->_totalUploadCountError++; this->_totalUploadTimeError+=(millis()-startTime); Serial.println("Upload: server connection failed"); _watchdog.ping(); return; } _watchdog.ping(); //send //If this takes more than 10s, abort and loop again so that Wifi and MQTT won't lose connection for(int i=0; i<10 && ((millis()-startTimeUploading)<10000); i++) { // int len = 0; int sdRecordsCount = 0; int sdRecordsOK = 0; int sdRecordsError = 0; strcpy(this->_gpsUploadBuffer, ""); // Serial.println("Upload: Preparing chunk"); while(this->_sdQueue.peek(this->_gpsRecord, sdRecordsCount++) && strlen(this->_gpsUploadBuffer) < 1150) { //fill post with ~1200 bytes if(GPSUtils::validateNmeaChecksum(this->_gpsRecord)) { strcat(this->_gpsUploadBuffer, this->_gpsRecord); strcat(this->_gpsUploadBuffer, "\n"); sdRecordsOK++; } else { Serial.printf("Upload: crc error %s\n", this->_gpsRecord); sdRecordsError++; } yield(); } if(strlen(this->_gpsUploadBuffer)>0) { Serial.printf("Upload: records=%d err=%d\n", sdRecordsOK, sdRecordsError); Serial.println("POST /" + String(this->_uploadServerUri.c_str())); int startTime = millis(); client.printf("POST /%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\n\r\n", this->_uploadServerUri.c_str(), "api.devices.stutzthings.com", // this->_uploadServerHost.get(), strlen(this->_gpsUploadBuffer) ); client.print(this->_gpsUploadBuffer); _watchdog.ping(); // Serial.println("SENT"); //wait for response available int timeout = millis(); while (client.available() < 15) { if (millis() - timeout > 5000) { Serial.println("Upload: server response timeout"); client.stop(); this->_totalUploadBytesError += strlen(this->_gpsUploadBuffer); this->_totalUploadCountError++; this->_totalUploadTimeError+=(millis()-startTime); return; } delay(50); _watchdog.ping(); } // Serial.println("RECEIVED RESPONSE"); //process response bool success = false; if(client.readStringUntil(GCHAR_SPACE).length()>0) { String code = client.readStringUntil(GCHAR_SPACE); _watchdog.ping(); if(code == "201") { Serial.println("Upload: 201 Created"); success = true; this->_totalUploadBytesSuccess += strlen(this->_gpsUploadBuffer); this->_totalUploadCountSuccess++; this->_totalUploadTimeSuccess+=(millis()-startTime); this->_totalUploadRecordsSuccess+=sdRecordsOK; this->_totalUploadRecordCRCError+=sdRecordsError; this->_sdQueue.removeElements(sdRecordsCount); // Serial.println("Upload: sent records removed from disk"); } else { this->_totalUploadBytesError += strlen(this->_gpsUploadBuffer); this->_totalUploadCountError++; this->_totalUploadTimeError+=(millis()-startTime); Serial.println("Upload: server error " + String(code)); } } else { this->_totalUploadBytesError += strlen(this->_gpsUploadBuffer); this->_totalUploadCountError++; this->_totalUploadTimeError+=(millis()-startTime); Serial.println("Upload: invalid server response"); } //drain response data // Serial.println("RESPONSE DRAIN0"); _watchdog.ping(); while(client.available()>0) { client.read(); } // Serial.println("RESPONSE DRAIN1"); // Serial.println("Upload: post finished"); } else { Serial.println("Upload: no data pending"); if(sdRecordsError>0) { this->_sdQueue.removeElements(sdRecordsError); this->_totalUploadRecordCRCError+=sdRecordsError; } break; } _watchdog.ping(); yield(); _watchdog.ping(); // Serial.println("FINISHED POST"); } client.stop(); }
int WiFiManager::serverLoop() { // Check for any mDNS queries and send responses mdns.update(); String s; WiFiClient client = server_s.available(); if (!client) { return(WM_WAIT); } DEBUG_PRINT("New client"); // Wait for data from client to become available while(client.connected() && !client.available()){ delay(1); } // Read the first line of HTTP request String req = client.readStringUntil('\r'); // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { DEBUG_PRINT("Invalid request: "); DEBUG_PRINT(req); return(WM_WAIT); } req = req.substring(addr_start + 1, addr_end); DEBUG_PRINT("Request: "); DEBUG_PRINT(req); client.flush(); if (req == "/") { s = HTTP_200; String head = HTTP_HEAD; head.replace("{v}", "Config ESP"); s += head; s += HTTP_SCRIPT; s += HTTP_STYLE; s += HTTP_HEAD_END; int n = WiFi.scanNetworks(); DEBUG_PRINT("scan done"); if (n == 0) { DEBUG_PRINT("no networks found"); s += "<div>No networks found. Refresh to scan again.</div>"; } else { for (int i = 0; i < n; ++i) { DEBUG_PRINT(WiFi.SSID(i)); DEBUG_PRINT(WiFi.RSSI(i)); String item = HTTP_ITEM; item.replace("{v}", WiFi.SSID(i)); s += item; delay(10); } } s += HTTP_FORM; s += HTTP_END; DEBUG_PRINT("Sending config page"); } else if ( req.startsWith("/s") ) { String qssid; qssid = urldecode(req.substring(8,req.indexOf('&')).c_str()); DEBUG_PRINT(qssid); DEBUG_PRINT(""); req = req.substring( req.indexOf('&') + 1); String qpass; qpass = urldecode(req.substring(req.lastIndexOf('=')+1).c_str()); setEEPROMString(0, 32, qssid); setEEPROMString(32, 64, qpass); EEPROM.commit(); s = HTTP_200; String head = HTTP_HEAD; head.replace("{v}", "Saved config"); s += HTTP_STYLE; s += HTTP_HEAD_END; s += "saved to eeprom...<br/>resetting in 10 seconds"; s += HTTP_END; client.print(s); client.flush(); DEBUG_PRINT("Saved WiFiConfig...restarting."); return WM_DONE; } else { s = HTTP_404; DEBUG_PRINT("Sending 404"); } client.print(s); DEBUG_PRINT("Done with client"); return(WM_WAIT); }
void mainloop() { /////////////////// // do data logging /////////////////// if (millis() >= ulNextMeas_ms) { ds18b20(); sendTeperatureTS(temp1, temp2, temp3, temp4); ulNextMeas_ms = millis() + ulMeasDelta_ms; pfTemp1[ulMeasCount % ulNoMeasValues] = temp1; pfTemp2[ulMeasCount % ulNoMeasValues] = temp2; pfTemp3[ulMeasCount % ulNoMeasValues] = temp3; pfTemp4[ulMeasCount % ulNoMeasValues] = temp4; pulTime[ulMeasCount % ulNoMeasValues] = millis() / 1000 + ulSecs2000_timer; Serial.print("Logging Temperature1: "); Serial.print(pfTemp1[ulMeasCount % ulNoMeasValues]); Serial.print(" deg Celsius - Temperature2: "); Serial.print(pfTemp2[ulMeasCount % ulNoMeasValues]); Serial.print(" deg Celsius - Temperature3: "); Serial.print(pfTemp3[ulMeasCount % ulNoMeasValues]); Serial.print(" deg Celsius - Temperature4: "); Serial.print(pfTemp4[ulMeasCount % ulNoMeasValues]); Serial.print(" deg Celsius - Time: "); Serial.println(pulTime[ulMeasCount % ulNoMeasValues]); ulMeasCount++; } // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); unsigned long ultimeout = millis() + 250; while (!client.available() && (millis() < ultimeout) ) { delay(1); } if (millis() > ultimeout) { Serial.println("client connection time-out!"); return; } // Read the first line of the request String sRequest = client.readStringUntil('\r'); //Serial.println(sRequest); client.flush(); // stop client, if request is empty if (sRequest == "") { Serial.println("empty request! - stopping client"); client.stop(); return; } // get path; end of path is either space or ? // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1 String sPath = "", sParam = "", sCmd = ""; String sGetstart = "GET "; int iStart, iEndSpace, iEndQuest; iStart = sRequest.indexOf(sGetstart); if (iStart >= 0) { iStart += +sGetstart.length(); iEndSpace = sRequest.indexOf(" ", iStart); iEndQuest = sRequest.indexOf("?", iStart); // are there parameters? if (iEndSpace > 0) { if (iEndQuest > 0) { // there are parameters sPath = sRequest.substring(iStart, iEndQuest); sParam = sRequest.substring(iEndQuest, iEndSpace); } else { // NO parameters sPath = sRequest.substring(iStart, iEndSpace); } } } /////////////////////////////////////////////////////////////////////////////// // output parameters to serial, you may connect e.g. an Arduino and react on it /////////////////////////////////////////////////////////////////////////////// if (sParam.length() > 0) { int iEqu = sParam.indexOf("="); if (iEqu >= 0) { sCmd = sParam.substring(iEqu + 1, sParam.length()); Serial.print("Die Eingabe ist: "); Serial.println(sCmd); } } /////////////////////////// // format the html response /////////////////////////// // Startseite //////////////////////////////// String sResponse, sResponse2, sHeader; if (sPath == "/") { ulReqcount++; int iIndex = (ulMeasCount - 1) % ulNoMeasValues; sResponse = F("<html>\n<head>\n<title>WLAN Logger für Pufferspeichertemperatur</title>\n"); sResponse += F("</head>\n<body>\n<font color=\"#000000\"><body bgcolor=\"#d0d0f0\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\"><h1>WLAN Logger für Pufferspeichertemperatur</h1>SMASE<BR><BR><FONT SIZE=+1>Letzte Messung um "); sResponse += F("Seite: Temperaturen -> Zeigt die gemessenen Temperaturen an <br>"); sResponse += F("<hr>Seite: Grafik -> Zeigt den Temperaturverlauf (Diagramm) der letzten 24 h an <br>"); sResponse += F("<hr>Seite: Grafik -> Zeigt den Temperaturverlauf (Tabelle) der letzten 24 h an <br>"); sResponse += F("<hr>Seite: Settings -> Einstellungen <br>"); sResponse += F(" <div style=\"clear:both;\"></div>"); sResponse2 = F("<p>Temperaturverlauf - Seiten laden länger:<BR> <a href=\"/anzeige\"><button>Temperaturen</button></a> <a href=\"/grafik\"><button>Grafik</button></a> <a href=\"/tabelle\"><button>Tabelle</button></a> <a href=\"/settings\"><button>Settings</button></a></p>"); sResponse2 += MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); } if (sPath == "/anzeige") { ulReqcount++; int iIndex = (ulMeasCount - 1) % ulNoMeasValues; sResponse = F("<html>\n<head>\n<title>WLAN Logger für Pufferspeichertemperatur</title>\n<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n"); sResponse += F("<script type=\"text/javascript\">\ngoogle.charts.load('current', {'packages':['gauge']});\n"); sResponse += F(" google.charts.setOnLoadCallback(drawGauge); \n"); sResponse += F("\nvar gaugeOptions = {min: 0, max: 100, greenFrom: 50, greenTo:75, yellowFrom: 75, yellowTo: 90,redFrom: 90, redTo: 100, minorTicks: 10, majorTicks: ['0','10','20','30','40','50','60','70','80','90','100']};\n"); sResponse += F(" var gauge; \n"); sResponse += F(" function drawGauge() { \n"); sResponse += F(" gaugeData = new google.visualization.DataTable(); \n"); sResponse += F(" gaugeData.addColumn('number', 'oben'); \n"); sResponse += F(" gaugeData.addColumn('number', 'mitte'); \n"); sResponse += F(" gaugeData.addColumn('number', 'unten'); \n"); sResponse += F(" gaugeData.addColumn('number', 'vorlauf'); \n"); sResponse += F(" gaugeData.addRows(2); \n"); sResponse += F(" gaugeData.setCell(0, 0, "); sResponse += pfTemp1[iIndex]; sResponse += F(" ); \n"); sResponse += F(" gaugeData.setCell(0, 1, "); sResponse += pfTemp2[iIndex]; sResponse += F(" ); \n"); sResponse += F(" gaugeData.setCell(0, 2, "); sResponse += pfTemp3[iIndex]; sResponse += F(" ); \n"); sResponse += F(" gaugeData.setCell(0, 3, "); sResponse += pfTemp4[iIndex]; sResponse += F(" ); \n"); sResponse += F(" gauge = new google.visualization.Gauge(document.getElementById('gauge_div')); \n"); sResponse += F(" gauge.draw(gaugeData, gaugeOptions); \n"); sResponse += F(" } \n"); sResponse += F(" </script> \n </head> \n <body> \n<font color=\"#000000\"><body bgcolor=\"#d0d0f0\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\"><h1>WLAN Logger für Pufferspeichertemperatur</h1>SMASE<BR><BR><FONT SIZE=+1>Letzte Messung um "); sResponse += epoch_to_string(pulTime[iIndex]).c_str(); sResponse += F(" UTC<BR>\n"); sResponse += F("<fieldset><legend>Pufferspeicher</legend>"); sResponse += F(" <div id=\"gauge_div\" style=\"width:140px; height: 560px;\"></div> \n"); sResponse += F("</fieldset>"); sResponse += F(" <div style=\"clear:both;\"></div>"); sResponse2 = F("<p>Temperaturverlauf - Seiten laden länger:<BR> <a href=\"/lauf\"><button>Vorlauf</button></a> <a href=\"/grafik\"><button>Grafik</button></a> <a href=\"/tabelle\"><button>Tabelle</button></a> <a href=\"/settings\"><button>Settings</button></a></p>"); sResponse2 += MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); } // Tabelle //////////////////////////////// else if (sPath == "/tabelle") //////////////////////////////////// // format the html page for /tabelle //////////////////////////////////// { ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Letzte Messungen im Abstand von "); sResponse += ulMeasDelta_ms; sResponse += F("ms<BR>"); // here the big table will follow later - but let us prepare the end first // part 2 of response - after the big table sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client - delete strings after use to keep mem low client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length() + ulSizeList).c_str()); client.print(sResponse); sResponse = ""; MakeTable(&client, true); client.print(sResponse2); } // Diagramm //////////////////////////////// else if (sPath == "/grafik") /////////////////////////////////// // format the html page for /grafik /////////////////////////////////// { ulReqcount++; unsigned long ulSizeList = MakeList(&client, false); // get size of list first sResponse = F("<html>\n<head>\n<title>WLAN Logger für Pufferspeichertemperatur</title>\n<script type=\"text/javascript\" src=\"https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}\"></script>\n"); sResponse += F("<script type=\"text/javascript\"> google.setOnLoadCallback(drawChart);\nfunction drawChart() {var data = google.visualization.arrayToDataTable([\n['Zeit / UTC', 'Temperatur1', 'Temperatur2', 'Temperatur3', 'Temperatur4'],\n"); // here the big list will follow later - but let us prepare the end first // part 2 of response - after the big list sResponse2 = F("]);\nvar options = {title: 'Verlauf',\n"); sResponse2 += F("curveType:'function',legend:{ position: 'bottom'}};"); sResponse2 += F("var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));chart.draw(data, options);}\n</script>\n</head>\n"); sResponse2 += F("<body>\n<font color=\"#000000\"><body bgcolor=\"#d0d0f0\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\"><h1>WLAN Logger für Pufferspeichertemperatur</h1><a href=\"/\"><button>Startseite</button></a><BR>"); sResponse2 += F("<BR>\n<div id=\"curve_chart\" style=\"width: 600px; height: 400px\"></div>"); sResponse2 += MakeHTTPFooter().c_str(); // Send the response to the client - delete strings after use to keep mem low client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length() + ulSizeList).c_str()); client.print(sResponse); sResponse = ""; MakeList(&client, true); client.print(sResponse2); } // Einstellungen //////////////////////////////// else if (sPath == "/settings") { EEPROM.begin(512); delay(10); String apiKey = ""; for (int i = 81; i < 100; i++) { //DEBUG_PRINT(i); apiKey += char(EEPROM.read(i)); } EEPROM.end(); Serial.println("Thinkspeak: " + apiKey); EEPROM.begin(512); delay(10); String zeit = ""; for (int i = 100; i < 105; i++) { zeit += char(EEPROM.read(i)); } EEPROM.end(); Serial.print("Das ist die Zeitverschiebung: "); Serial.println(zeit); String zeittext = ""; if (zeit != "0000") { zeittext = "Sommerzeit"; } else { zeittext = "Winterzeit"; } ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Thingspeak API ist momentan: "); sResponse += apiKey; sResponse += F("<BR><BR>Das ist die Zeitverschiebung: "); sResponse += zeittext; sResponse += F("<fieldset><legend>EEPROM Setting</legend>"); sResponse += F("<p><a href=\"/reset\"><button>RESET</button></a> <a href=\"?pin=FUNCTION2ON\"><button>AUSLESEN</button></a></p>"); sResponse += F("</fieldset>"); sResponse += F("<fieldset><legend>Allgemein Setting</legend>"); sResponse += F("<p>Zeitumstellung <a href=\"?pin=SOMMERZEIT\"><button>SOMMERZEIT</button></a> <a href=\"?pin=WINTERZEIT\"><button>WINTERZEIT</button></a></p>"); sResponse += F("<form name=\"input\" action=\"\" method=\"get\">THINGSPEAK-API: <input type=\"text\" name=\"$\"><input type=\"submit\" value=\"Submit\"></form>"); sResponse += F("</fieldset>"); sResponse += F("<fieldset><legend>Temperatur kalibrieren</legend>"); sResponse += F("<p><a href=\"/temp1\"><button>Speicher oben</button></a> <a href=\"/temp2\"><button>Speicher mitte</button></a> <a href=\"/temp3\"><button>Speicher unten</button></a> <a href=\"/temp4\"><button>Vorlauf</button></a></p>"); sResponse += F("</fieldset>"); sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); delay(100); ////////////////////// // react on parameters ////////////////////// if (sCmd.length() > 0) { // write received command to html page sResponse += "Kommando:" + sCmd + "<BR>"; // EEPROM RESET //////////////////////////////// if (sCmd.indexOf("FUNCTION1ON") >= 0) { EEPROM.begin(512); // write a 0 to all 512 bytes of the EEPROM for (int i = 0; i < 512; i++) { EEPROM.write(i, 0); EEPROM.end(); } } // SHOW EEPROM //////////////////////////////// else if (sCmd.indexOf("FUNCTION2ON") >= 0) { EEPROM.begin(512); delay(10); String string3 = ""; for (int i = 0; i < 150; i++) { //DEBUG_PRINT(i); string3 += char(EEPROM.read(i)); } EEPROM.end(); Serial.println(string3); } // SOMMERZEIT EINSTELLEN //////////////////////////////// else if (sCmd.indexOf("SOMMERZEIT") >= 0) { String sommer = "3600"; Serial.print("Das wird gespeichert in der seite: "); Serial.println(sommer); EEPROM.begin(512); delay(10); int si = 0; for (int i = 100; i < 105; i++) { char c; if (si < sommer.length()) { c = sommer[si]; } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sommer); } // WINTERZEIT EINSTELLEN //////////////////////////////// else if (sCmd.indexOf("WINTERZEIT") >= 0) { String winter = "0000"; Serial.print("Das wird gespeichert in der seite: "); Serial.println(winter); EEPROM.begin(512); delay(10); int si = 0; for (int i = 100; i < 105; i++) { char c; if (si < winter.length()) { c = winter[si]; } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + winter); } // SET THINGSPEAK API //////////////////////////////// else { Serial.print("Das wird gespeichert in der seite: "); Serial.println(sCmd); EEPROM.begin(512); delay(10); int si = 0; for (int i = 81; i < 100; i++) { char c; if (si < sCmd.length()) { c = sCmd[si]; //DEBUG_PRINT("Wrote: "); //DEBUG_PRINT(c); } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sCmd); } } } // Kalibrieren Temperatur 1 //////////////////////////////// else if (sPath == "/temp1") //////////////////////////////////// // format the html page for /tabelle //////////////////////////////////// { EEPROM.begin(512); delay(10); String temp1k = ""; for (int i = 110; i < 115; i++) { temp1k += char(EEPROM.read(i)); } EEPROM.end(); Serial.print("Das ist die Zeitverschiebung: "); Serial.println(temp1k); // settings(); ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Speicher oben: "); sResponse += temp1k; sResponse += F("Grad C<BR>"); sResponse += F("<form name=\"input\" action=\"\" method=\"get\">Speicher oben: <input type=\"text\" name=\"$\"><input type=\"submit\" value=\"Submit\"></form>"); sResponse += F("<p>Temperatur kalibrieren: <a href=\"/temp1\"><button>Speicher oben</button></a> <a href=\"/temp2\"><button>Speicher mitte</button></a> <a href=\"/temp3\"><button>Speicher unten</button></a> <a href=\"/temp4\"><button>Vorlauf</button></a> </p>"); sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); delay(100); ////////////////////// // react on parameters ////////////////////// if (sCmd.length() > 0) { // write received command to html page sResponse += "Kommando:" + sCmd + "<BR>"; // SET THINGSPEAK API //////////////////////////////// if (sCmd.toInt() != 0) { Serial.print("Das wird gespeichert in der seite: "); Serial.println(sCmd); EEPROM.begin(512); delay(10); int si = 0; for (int i = 110; i < 115; i++) { char c; if (si < sCmd.length()) { c = sCmd[si]; //DEBUG_PRINT("Wrote: "); //DEBUG_PRINT(c); } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sCmd); } else { Serial.println("Der Wert " + sCmd + " war keine Zahl!!!"); } } } // Kalibrieren Temperatur 2 //////////////////////////////// else if (sPath == "/temp2") //////////////////////////////////// // format the html page for /tabelle //////////////////////////////////// { EEPROM.begin(512); delay(10); String temp2k = ""; for (int i = 115; i < 120; i++) { temp2k += char(EEPROM.read(i)); } EEPROM.end(); Serial.print("Das ist die Zeitverschiebung: "); Serial.println(temp2k); // settings(); ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Speicher mitte: "); sResponse += temp2k; sResponse += F("Grad C<BR>"); sResponse += F("<form name=\"input\" action=\"\" method=\"get\">Speicher mitte: <input type=\"text\" name=\"$\"><input type=\"submit\" value=\"Submit\"></form>"); sResponse += F("<p>Temperatur kalibrieren: <a href=\"/temp1\"><button>Speicher oben</button></a> <a href=\"/temp2\"><button>Speicher mitte</button></a> <a href=\"/temp3\"><button>Speicher unten</button></a> <a href=\"/temp4\"><button>Vorlauf</button></a> </p>"); sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); delay(100); ////////////////////// // react on parameters ////////////////////// if (sCmd.length() > 0) { // write received command to html page sResponse += "Kommando:" + sCmd + "<BR>"; // SET THINGSPEAK API //////////////////////////////// if (sCmd.toInt() != 0) { Serial.print("Das wird gespeichert in der seite: "); Serial.println(sCmd); EEPROM.begin(512); delay(10); int si = 0; for (int i = 115; i < 120; i++) { char c; if (si < sCmd.length()) { c = sCmd[si]; //DEBUG_PRINT("Wrote: "); //DEBUG_PRINT(c); } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sCmd); } else { Serial.println("Der Wert " + sCmd + " war keine Zahl!!!"); } } } // Kalibrieren Temperatur 3 //////////////////////////////// else if (sPath == "/temp3") //////////////////////////////////// // format the html page for /tabelle //////////////////////////////////// { EEPROM.begin(512); delay(10); String temp3k = ""; for (int i = 120; i < 125; i++) { temp3k += char(EEPROM.read(i)); } EEPROM.end(); Serial.print("Das ist der aktuelle Korrekturwert: "); Serial.println(temp3k); // settings(); ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Speicher unten "); sResponse += temp3k; sResponse += F("Grad C<BR>"); sResponse += F("<form name=\"input\" action=\"\" method=\"get\">Speicher unten: <input type=\"text\" name=\"$\"><input type=\"submit\" value=\"Submit\"></form>"); sResponse += F("<p>Temperatur kalibrieren: <a href=\"/temp1\"><button>Speicher oben</button></a> <a href=\"/temp2\"><button>Speicher mitte</button></a> <a href=\"/temp3\"><button>Speicher unten</button></a> <a href=\"/temp4\"><button>Vorlauf</button></a> </p>"); sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); delay(100); ////////////////////// // react on parameters ////////////////////// if (sCmd.length() > 0) { // write received command to html page sResponse += "Kommando:" + sCmd + "<BR>"; // SET THINGSPEAK API //////////////////////////////// if (sCmd.toInt() != 0) { Serial.print("Das wird gespeichert in der seite: "); Serial.println(sCmd); EEPROM.begin(512); delay(10); int si = 0; for (int i = 120; i < 125; i++) { char c; if (si < sCmd.length()) { c = sCmd[si]; //DEBUG_PRINT("Wrote: "); //DEBUG_PRINT(c); } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sCmd); } else { Serial.println("Der Wert " + sCmd + " war keine Zahl!!!"); } } } // Kalibrieren Temperatur 4 //////////////////////////////// else if (sPath == "/temp4") //////////////////////////////////// // format the html page for /tabelle //////////////////////////////////// { EEPROM.begin(512); delay(10); String temp4k = ""; for (int i = 125; i < 130; i++) { temp4k += char(EEPROM.read(i)); } EEPROM.end(); Serial.print("Das ist die Zeitverschiebung: "); Serial.println(temp4k); // settings(); ulReqcount++; unsigned long ulSizeList = MakeTable(&client, false); // get size of table first sResponse = F("<html><head><title>WLAN Logger für Pufferspeichertemperatur</title></head><body>"); sResponse += F("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">"); sResponse += F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"); sResponse += F("<h1>WLAN Logger für Pufferspeichertemperatur</h1>"); sResponse += F("<FONT SIZE=+1>"); sResponse += F("<a href=\"/\"><button>Startseite</button></a><BR><BR>Anpassung Vorlauf: "); sResponse += temp4k; sResponse += F("Grad C<BR>"); sResponse += F("<form name=\"input\" action=\"\" method=\"get\">Vorlauf: <input type=\"text\" name=\"$\"><input type=\"submit\" value=\"Submit\"></form>"); sResponse += F("<p>Temperatur kalibrieren: <a href=\"/temp1\"><button>Speicher oben</button></a> <a href=\"/temp2\"><button>Speicher mitte</button></a> <a href=\"/temp3\"><button>Speicher unten</button></a> <a href=\"/temp4\"><button>Vorlauf</button></a> </p>"); sResponse2 = MakeHTTPFooter().c_str(); // Send the response to the client client.print(MakeHTTPHeader(sResponse.length() + sResponse2.length()).c_str()); client.print(sResponse); client.print(sResponse2); delay(100); ////////////////////// // react on parameters ////////////////////// if (sCmd.length() > 0) { // write received command to html page sResponse += "Kommando:" + sCmd + "<BR>"; // SET THINGSPEAK API //////////////////////////////// if (sCmd.toInt() != 0) { Serial.print("Das wird gespeichert in der seite: "); Serial.println(sCmd); EEPROM.begin(512); delay(10); int si = 0; for (int i = 125; i < 130; i++) { char c; if (si < sCmd.length()) { c = sCmd[si]; //DEBUG_PRINT("Wrote: "); //DEBUG_PRINT(c); } else { c = 0; } EEPROM.write(i, c); si++; } EEPROM.end(); Serial.println("Wrote " + sCmd); } else { Serial.println("Der Wert " + sCmd + " war keine Zahl!!!"); } } } // Send the response to the client client.print(sHeader); client.print(sResponse); // and stop the client client.stop(); Serial.println("Client disconnected"); }
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version) { t_httpUpdate_return ret = HTTP_UPDATE_FAILED; WiFiClient tcp; DEBUG_HTTP_UPDATE("[httpUpdate] connected to %s:%u %s .... ", host, port, url); if(!tcp.connect(host, port)) { DEBUG_HTTP_UPDATE("failed.\n"); return ret; } DEBUG_HTTP_UPDATE("ok.\n"); // set Timeout for readBytesUntil and readStringUntil tcp.setTimeout(2000); tcp.setNoDelay(true); String req = "GET "; req += url; req += " HTTP/1.1\r\n" "Host: "; req += host; req += "\r\n" "Connection: close\r\n" "User-Agent: ESP8266-http-Update\r\n" "x-ESP8266-STA-MAC: "; req += WiFi.macAddress(); req += "\r\n" "x-ESP8266-AP-MAC: "; req += WiFi.softAPmacAddress(); req += "\r\n" "x-ESP8266-free-space: "; req += ESP.getFreeSketchSpace(); req += "\r\n" "x-ESP8266-sketch-size: "; req += ESP.getSketchSize(); req += "\r\n" "x-ESP8266-chip-size: "; req += ESP.getFlashChipRealSize(); req += "\r\n" "x-ESP8266-sdk-version: "; req += ESP.getSdkVersion(); if(current_version[0] != 0x00) { req += "\r\n" "x-ESP8266-version: "; req += current_version; } req += "\r\n" "\r\n"; tcp.write(req.c_str(), req.length()); uint32_t code = 0; size_t len = 0; while(true) { String headerLine = tcp.readStringUntil('\n'); headerLine.trim(); // remove \r if(headerLine.length() > 0) { DEBUG_HTTP_UPDATE("[httpUpdate][Header] RX: %s\n", headerLine.c_str()); if(headerLine.startsWith("HTTP/1.")) { // 9 = lenght of "HTTP/1.x " code = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt(); } else if(headerLine.startsWith("Content-Length: ")) { // 16 = lenght of "Content-Length: " len = headerLine.substring(16).toInt(); } } else { break; } } DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n"); DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n"); DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code); DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len); DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n"); DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace()); DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize()); if(current_version[0] != 0x00) { DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version); } switch(code) { case 200: ///< OK (Start Update) if(len > 0) { if(len > ESP.getFreeSketchSpace()) { ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); } else { WiFiUDP::stopAll(); WiFiClient::stopAllExcept(&tcp); delay(100); if(ESP.updateSketch(tcp, len, false, false)) { ret = HTTP_UPDATE_OK; DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); tcp.stop(); ESP.restart(); } else { ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n"); } } } else { ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0?!\n"); } break; case 304: ///< Not Modified (No updates) ret = HTTP_UPDATE_NO_UPDATES; break; case 403: ///< Forbidden // todo handle login default: ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code); break; } return ret; }
bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); //reset header value for (int i = 0; i < _headerKeysCount; ++i) { _currentHeaders[i].value =String(); } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Invalid request: "); DEBUG_OUTPUT.println(req); #endif return false; } String methodStr = req.substring(0, addr_start); String url = req.substring(addr_start + 1, addr_end); String versionEnd = req.substring(addr_end + 8); _currentVersion = atoi(versionEnd.c_str()); String searchStr = ""; int hasSearch = url.indexOf('?'); if (hasSearch != -1){ searchStr = url.substring(hasSearch + 1); url = url.substring(0, hasSearch); } _currentUri = url; _chunked = false; HTTPMethod method = HTTP_GET; if (methodStr == F("POST")) { method = HTTP_POST; } else if (methodStr == F("DELETE")) { method = HTTP_DELETE; } else if (methodStr == F("OPTIONS")) { method = HTTP_OPTIONS; } else if (methodStr == F("PUT")) { method = HTTP_PUT; } else if (methodStr == F("PATCH")) { method = HTTP_PATCH; } _currentMethod = method; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("method: "); DEBUG_OUTPUT.print(methodStr); DEBUG_OUTPUT.print(" url: "); DEBUG_OUTPUT.print(url); DEBUG_OUTPUT.print(" search: "); DEBUG_OUTPUT.println(searchStr); #endif //attach handler RequestHandler* handler; for (handler = _firstHandler; handler; handler = handler->next()) { if (handler->canHandle(_currentMethod, _currentUri)) break; } _currentHandler = handler; String formData; // below is needed only when POST type request if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_PATCH || method == HTTP_DELETE){ String boundaryStr; String headerName; String headerValue; bool isForm = false; //bool isEncoded = false; uint32_t contentLength = 0; //parse headers while(1){ req = client.readStringUntil('\r'); client.readStringUntil('\n'); if (req == "") break;//no moar headers int headerDiv = req.indexOf(':'); if (headerDiv == -1){ break; } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 1); headerValue.trim(); _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("headerName: "); DEBUG_OUTPUT.println(headerName); DEBUG_OUTPUT.print("headerValue: "); DEBUG_OUTPUT.println(headerValue); #endif if (headerName.equalsIgnoreCase(FPSTR(Content_Type))){ using namespace mime; if (headerValue.startsWith(FPSTR(mimeTable[txt].mimeType))){ isForm = false; } else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){ isForm = false; //isEncoded = true; } else if (headerValue.startsWith(F("multipart/"))){ boundaryStr = headerValue.substring(headerValue.indexOf('=') + 1); boundaryStr.replace("\"",""); isForm = true; } } else if (headerName.equalsIgnoreCase(F("Content-Length"))){ contentLength = headerValue.toInt(); } else if (headerName.equalsIgnoreCase(F("Host"))){ _hostHeader = headerValue; } } // always parse url for key/value pairs _parseArguments(searchStr); if (!isForm) { if (contentLength) { // add key=value: plain={body} (post json or other data) size_t plainLength; char* plainBuf = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT); if (plainLength < contentLength) { free(plainBuf); return false; } RequestArgument& arg = _currentArgs[_currentArgCount++]; arg.key = F("plain"); arg.value = String(plainBuf); free(plainBuf); } } else { // isForm is true if (!_parseForm(client, boundaryStr, contentLength)) { return false; } } } else { String headerName; String headerValue; //parse headers while(1){ req = client.readStringUntil('\r'); client.readStringUntil('\n'); if (req == "") break;//no moar headers int headerDiv = req.indexOf(':'); if (headerDiv == -1){ break; } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print(F("headerName: ")); DEBUG_OUTPUT.println(headerName); DEBUG_OUTPUT.print(F("headerValue: ")); DEBUG_OUTPUT.println(headerValue); #endif if (headerName.equalsIgnoreCase("Host")){ _hostHeader = headerValue; } } _parseArguments(searchStr); } client.flush(); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print(F("Request: ")); DEBUG_OUTPUT.println(url); DEBUG_OUTPUT.print(F("Arguments: ")); DEBUG_OUTPUT.println(searchStr); DEBUG_OUTPUT.println(F("final list of key/value pairs:")); for (int i = 0; i < _currentArgCount; i++) DEBUG_OUTPUT.printf(" key:'%s' value:'%s'\r\n", _currentArgs[i].key.c_str(), _currentArgs[i].value.c_str()); #endif return true; }