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"); debugClient.print("SSID: "); debugClient.println(WiFi.SSID()); // print your WiFi shield's IP address: debugClient.print("IP Address: "); debugClient.println(ip); // print the received signal strength: debugClient.print("signal strength (RSSI):"); debugClient.print(rssi); debugClient.println(" dBm"); }
void Iotfy_postdata(String group_iotfy, String id_iotfy, String tag_iotfy, String value_iotfy) { debug("in postdata"); debug("setting id"); String X_IOTFY_ID="X-IOTFY-ID: "+IOTFY_ID; String X_IOTFY_CLIENT="X-IOTFY-CLIENT: "+IOTFY_CLIENT; debug(X_IOTFY_CLIENT); debug(X_IOTFY_ID); String data="\{\"group\":\""; data+=group_iotfy; data+="\", \"device\":\""; data+=id_iotfy; data+="\", \"data\":[{"; data+="\"tag\":\""; data+=tag_iotfy; data+="\""; data+=",\"text\":\""; data+=value_iotfy; data+="\"}]}"; debug("Trying to connect"); if (client.connect(server, 80)) { debug("connected and now sending post request"); client.println("POST /api/telemetry/v1/post_text_data HTTP/1.0"); //client.println("POST /api/telemetry/v1/post_text_data HTTP/1.1"); client.println("User-Agent: arduino-ESP"); client.println("Host: cloud.iotfy.co"); client.println(X_IOTFY_ID); client.println(X_IOTFY_CLIENT); client.println("Content-Type: application/json"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.println(data); client.println("Connection: close"); if(DEBUG) { debug("connected"); debug("POST /api/telemetry/v1/post_text_data HTTP/1.0"); //debug("POST /api/telemetry/v1/post_text_data HTTP/1.1"); debug("User-Agent: arduino-ESP"); debug("Host: cloud.iotfy.co"); debug(X_IOTFY_ID); debug(X_IOTFY_CLIENT); debug("Content-Type: application/json"); debug("Content-Length: "); debug((String)data.length()); debug(""); debug(data); } } }
void print_json() { client.println("{"); for (int i=0; i<sensors; i++) { client.print("\""); client.print(keys[i], HEX); client.print("\": "); client.print(vals[i]); if (i+1 < sensors) { client.println(","); } else { client.println(); } } client.println("}"); }
void Humure::jsonValue(WiFiClient client, String str_value, String str_name) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Connection: close"); client.println(); if ( str_value.length() > 0 ) { client.print("{\""); client.print(str_name); client.print("\": "); client.print(str_value); client.println("}"); } }
// FLRDDE626>APRS,qAS,EGHL:/074548h5111.32N/00102.04W'086/007/A=000607 id0ADDE626 -019fpm +0.0rot 5.5dB 3e -4.3kHz void WiFi_forward_to_cloud() { tmElements_t tm; char str_lat[8]; char str_lon[8]; breakTime(fo.timestamp, tm); dtostrf_workaround(take_minutes(fo.latitude), 5, 2, str_lat); dtostrf_workaround(take_minutes(fo.longtitude), 5, 2, str_lon); //Serial.print(fo.latitude); Serial.print(" "); Serial.println(fo.longtitude); snprintf(UDPpacketBuffer, sizeof(UDPpacketBuffer), \ "FLR%X>APRS,qAS,%s:/%02d%02d%02dh%02d%s%s/%03d%s%s/A=%05u TS:%d RAW:%s", \ fo.addr, STATION_ID, tm.Hour, tm.Minute, tm.Second, \ abs(take_degrees(fo.latitude)), str_lat, (fo.latitude < 0 ? "S" : "N"), \ abs(take_degrees(fo.longtitude)), str_lon, (fo.longtitude < 0 ? "W" : "E"), \ fo.altitude, fo.timestamp, fo.raw.c_str() ); #ifdef SERIAL_VERBOSE Serial.println(UDPpacketBuffer); #endif client.println(UDPpacketBuffer); }
void loop() { static boolean packetActive = false; static uint8_t index = 0; static boolean readingReturn = false; static uint8_t rindex = 0; if (!client.connected()) { // try to connect to a new client client = server.available(); } else { // read data from the connected client if (client.available() > 0) { char c = client.read(); if (packetActive) { commandBuffer[index] = c; commandBuffer[++index] = 0; if (c == EOP) { comHandler(); packetActive = false; } } else if (c == SOP) { index = 0; commandBuffer[index] = c; commandBuffer[++index] = 0; packetActive = true; } if (returnReady) { client.println(returnBuffer); server.println(returnBuffer); Serial.println(returnBuffer); returnReady = false; } } } if (Serial.available() && !returnReady) { char s = Serial.read(); if (s == SOP) { readingReturn = true; rindex = 0; } if (readingReturn) { returnBuffer[rindex] = s; returnBuffer[++rindex] = 0; if (s == EOP) { returnReady = true; } } } }
void httpRequest() { // close any connection before send a new request. // This will free the socket on the WiFi shield client.stop(); // if there's a successful connection: if (client.connect("www.hasthelargehadroncolliderdestroyedtheworldyet.com", 80)) { Serial5.println("connecting..."); // send the HTTP PUT request: client.println("GET / HTTP/1.1"); client.println("Host: www.hasthelargehadroncolliderdestroyedtheworldyet.com"); //client.println("User-Agent: ArduinoWiFi/1.1"); client.println("Connection: close"); client.println(); if( client.connected() ) { Serial5.println("\tClient connected"); while( client.available() == 0 ) { //Waiting for data if( !client.connected() ) { Serial5.println("\tClient disconnected !"); break; } } } else { Serial5.println("\tClient not connected"); } } else { // if you couldn't make a connection: Serial5.println("\tconnection failed"); } }
void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); // don't continue: while(true); } // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } Serial.println("Connected to wifi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected to server"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host:www.google.com"); client.println("Connection: close"); client.println(); } }
void next_client() { // Listenning for new clients client = server.available(); if (client) { Serial.println("New client"); // bolean to locate when the http request ends boolean blank_line = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && blank_line) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Access-Control-Allow-Origin: *"); client.println("Connection: close"); client.println(); print_json(); break; } if (c == '\n') { // when starts reading a new line Serial.println(); blank_line = true; } else if (c != '\r') { // when finds a character on the current line Serial.write(c); blank_line = false; } } } // closing the client connection delay(1); client.stop(); Serial.println("Client disconnected."); } }
/** * 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 waitForTarget() { //define a client object to connect to the server WiFiClient mainClient; //wait for a client to connect to the server while(!alreadyConnectedMain) { //attempt to save a client connecting to the server mainClient = mainServer.available(); //if a client is connected, begin communication if (mainClient) { if (!alreadyConnectedMain) { // clead out the input buffer: mainClient.flush(); Serial.println("We have a new client"); debugServer.println("We have a new client"); mainClient.println("Hello, client!"); alreadyConnectedMain = true; } } delay(100); delay(100); } Serial.println("writing"); debugClient.println("writing"); //mainServer.println("ready"); delay(1000); //Strings to read in latitude and longitude from the client char lat[50] = ""; char lon[50] = ""; int ind = 0; //Wait for input to be on the buffer while(!mainClient.available()); char destNum = '0'; while(!(destNum == '1' || destNum == '2' || destNum == '3')) { destNum = mainClient.read(); Serial.println(destNum); } if(destNum == '1') { tarLat = LAT1; tarLon = LON1; } if(destNum == '2') { tarLat = LAT2; tarLon = LON2; } if(destNum == '3') { tarLat = LAT3; tarLon = LON3; } /* //Read in characters from the input buffer until a new line character is reached //this will be the latitude while(mainClient.available()) { char c = mainClient.read(); lat[ind] = c; if(c == '\n') { lat[ind] = NULL; break; } ind++; } ind = 0; //Read in characters from the input buffer until a new line character is reached //this will be the longitude while(mainClient.available()) { char c = mainClient.read(); lon[ind] = c; if(c == '\n') { lon[ind] = NULL; break; } ind++; } mainClient.stop(); //convert from a string to a float tarLat = strtof(lat, NULL); tarLon = strtof(lon, NULL); //digitalWrite(LED1, LOW); //tarLat = atof(lat); //tarLon = atof(lon);*/ Serial.print("Lat: "); Serial.print(lat); Serial.print(" "); Serial.println(tarLat, 6); Serial.print("Lon: "); Serial.print(lon); Serial.print(" "); Serial.println(tarLon, 6); debugClient.print("Lat: "); debugClient.print(lat); debugClient.print(" "); debugClient.println(tarLat, 6); debugClient.print("Lon: "); debugClient.print(lon); debugClient.print(" "); debugClient.println(tarLon, 6); //Erick's //tarLat = 28.504906f; //tarLon = -81.457456f; //apt //tarLat = 28.582183f; //tarLon = -81.202770f; //apt 2 //tarLat = 28.582373f; //tarLon = -81.202996f; //curLat = 28.628811f; //curLon = -81.199479f; //mem mall //tarLat = 28.603710f; //tarLon = -81.199371f; //matt's //tarLat = 28.628391; //tarLon = -81.200013; }
void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //server.begin(); // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /5/on") >= 0) { Serial.println("GPIO 5 on"); output5State = "on"; digitalWrite(output5, HIGH); } else if (header.indexOf("GET /5/off") >= 0) { Serial.println("GPIO 5 off"); output5State = "off"; digitalWrite(output5, LOW); } else if (header.indexOf("GET /4/on") >= 0) { Serial.println("GPIO 4 on"); output4State = "on"; digitalWrite(output4, HIGH); } else if (header.indexOf("GET /4/off") >= 0) { Serial.println("GPIO 4 off"); output4State = "off"; digitalWrite(output4, LOW); } // Display the HTML web page client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println(".button2 {background-color: #77878A;}</style></head>"); // Web Page Heading client.println("<body><h1>ESP8266 Web Server</h1>"); // Display current state, and ON/OFF buttons for GPIO 5 client.println("<p>GPIO 5 - State " + output5State + "</p>"); // If the output5State is off, it displays the ON button if (output5State=="off") { client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>"); } // Display current state, and ON/OFF buttons for GPIO 4 client.println("<p>GPIO 4 - State " + output4State + "</p>"); // If the output4State is off, it displays the ON button if (output4State=="off") { client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>"); } client.println("</body></html>"); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }
void Humure::sendFullPage(WiFiClient client) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE html>"); client.println("<html xmlns='http://www.w3.org/1999/xhtml'>"); client.println("<head>\n<meta charset='UTF-8'>"); client.println("<title>ESP8266 Temperature & Humidity DHT11 Sensor</title>"); client.println("</head>\n<body>"); client.println("<H2>ESP8266 & DHT11 Sensor</H2>"); client.println("<H3>Humidity / Temperature</H3>"); client.println("<pre>"); client.print("Humidity (%) : "); client.println((float)h, 2); client.print("Temperature (°C) : "); client.println((float)t, 2); client.println("</pre>"); client.print("</body>\n</html>"); }
bool ESPWebServer::handleClientRequest(ServoControl &servoInteraction, String htmlPage) { bool clientHandled = false; WiFiClient client = m_server.available(); // Listen for incoming clients if (client) { Serial.println("New Client."); String header = ""; String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { if (client.available()) { char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // 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) { // checks if the client signaled an interaction int index; servoInteraction.type = NONE; if ((index = header.indexOf("GET /openBlinds")) >= 0) { servoInteraction.type = OPEN_BLINDS; } else if ((index = header.indexOf("GET /closeBlinds")) >= 0) { servoInteraction.type = CLOSE_BLINDS; } else if ((index = header.indexOf("GET /setOpenBlinds")) >= 0) { String passedValue; index += 20; char newChar = header.charAt(index); while(newChar != '"') { passedValue += newChar; index++; newChar = header.charAt(index); } servoInteraction.type = SET_OPEN; servoInteraction.setting = passedValue.toInt(); } else if ((index = header.indexOf("GET /setCloseBlinds")) >= 0) { String passedValue; index += 21; char newChar = header.charAt(index); while(newChar != '"') { passedValue += newChar; index++; newChar = header.charAt(index); } servoInteraction.type = SET_CLOSE; servoInteraction.setting = passedValue.toInt(); } // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // Display the HTML web page client.println(htmlPage); // Break out of the while loop clientHandled = true; 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."); Serial.println(""); } return clientHandled; }
//////////////////////////////////// // Scrape UTC Time from server //////////////////////////////////// char* updateCurTime(void) { static int timeout_busy=0; int ipos; timeout_busy=0; //reset const char* timeServer = "aws.amazon.com"; // send a bad header on purpose, so we get a 400 with a DATE: timestamp const char* timeServerGet = "GET example.com/ HTTP/1.1"; String utctime; String GmtDate; static char dateStamp[20]; static char chBuf[200]; char utctimeraw[80]; char* dpos; WiFiClient client; if (client.connect(timeServer, 80)) { //Send Request client.println(timeServerGet); client.println(); while((!client.available())&&(timeout_busy++<5000)){ // Wait until the client sends some data delay(1); } // kill client if timeout if(timeout_busy>=5000) { client.flush(); client.stop(); Serial.println("timeout receiving timeserver data\n"); return dateStamp; } // read the http GET Response String req2 = client.readString(); // Serial.println(""); // Serial.println(""); // Serial.print(req2); // Serial.println(""); // Serial.println(""); // close connection delay(1); client.flush(); client.stop(); ipos = req2.indexOf("Date:"); if(ipos>0) { GmtDate = req2.substring(ipos,ipos+35); // Serial.println(GmtDate); utctime = GmtDate.substring(18,22) + getMonth(GmtDate.substring(14,17)) + GmtDate.substring(11,13) + GmtDate.substring(23,25) + GmtDate.substring(26,28) + GmtDate.substring(29,31); // Serial.println(utctime.substring(0,14)); utctime.substring(0,14).toCharArray(dateStamp, 20); } } else { Serial.println("did not connect to timeserver\n"); } timeout_busy=0; // reset timeout return dateStamp; // Return latest or last good dateStamp }