Example #1
0
void MqttFlare::checkEthernet(){
  static unsigned long blinkTime = millis();
  static int ledState = LOW;
  if (millis()-blinkTime>500){
    if (ledState==LOW){
      ledState=HIGH;
    }else{
      ledState=LOW;
    }
    digitalWrite(LED_RED,ledState);
    blinkTime=millis();
  }

  // listen for incoming clients
  //
  // Code from the WiFly Server Example
  //
  WiFlyClient client = configServer->available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    String request="";
    int index =0 ;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        request +=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");  // the connection will be closed after completion of the response
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          client.println("<body>");
          /*client.println("<form role='form' method='get'>");
           client.println("<div class='form-group'> <label class='col-sm-2'> MQTT Broker </label> <input type='col-sm-10  text' class='form-control' value='150.140.5.11'/> </div>");
           client.println("<div class='form-group'> <label class='col-sm-2'> IP </label> <input type='col-sm-10  text' class='form-control' value='150.140.5.11'/> </div>");
           //client.println("<div class='form-group'> <label class='col-sm-2'> IP </label> <select class='col-sm-10 form-control'><option>DHCP</option><option>STATIC</option></select> </div>");
           client.println("<div class='form-group'> <label class='col-sm-2'> Static IP </label> <input type='col-sm-10  text' class='form-control' value='192.168.1.10'/> </div>");
           client.println("<div class='form-group'> <label class='col-sm-2'> Netmask </label> <input type='col-sm-10  text' class='form-control' value='255.255.0.0'/> </div>");
           client.println("<div class='form-group'> <label class='col-sm-2'> Gateway </label> <input type='col-sm-10  text' class='form-control' value='192.168.1.254'/> </div>");
           client.println("<div class='form-group'> <input type='submit' class='btn btn-primary' value='Save and Restart' class='col-sm-2'/> </div>");
           client.println("</form>");   
           client.println("</div>");*/
          client.println("<center>");
          client.println("<form role='form' method='get' action='save'>");
          client.print("Broker:<input type='text' name='s' value='");
          client.print(server);
          client.println("'/><br/>");
          client.print("Port:<input type='text' name='p' value='");
          client.print(port);
          client.println("'/><br/>");
          //          client.println("IP:<input type='text' name='i' value='");
          //          client.print(flare->static_ip[0]);
          //          client.print(".");
          //          client.print(flare->static_ip[1]);
          //          client.print(".");
          //          client.print(flare->static_ip[2]);
          //          client.print(".");
          //          client.print(flare->static_ip[3]);
          //          client.println("'/><br/>");
          //          client.println("Netmask:<input type='text' name='n' value='");
          //          client.print(flare->netmask[0]);
          //          client.print(".");
          //          client.print(flare->netmask[1]);
          //          client.print(".");
          //          client.print(flare->netmask[2]);
          //          client.print(".");
          //          client.print(flare->netmask[3]);
          //          client.println("'/><br/>");
          //          client.println("Gateway:<input type='text' name='g' value='");
          //          client.print(flare->gateway[0]);
          //          client.print(".");
          //          client.print(flare->gateway[1]);
          //          client.print(".");
          //          client.print(flare->gateway[2]);
          //          client.print(".");
          //          client.print(flare->gateway[3]);
          //          client.println("'/><br/>");
          client.print("SSID:<input type='text' name='w' value='");
          client.print(ssid);
          client.println("'/><br/>");
          client.print("Phrase:<input type='text' name='c' value='");
          client.print(code);
          client.println("'/><br/>");
          client.println("<input type='submit' value='Save and Restart'/>");
          client.println("</form>"); 
          client.println("</center>");  
          client.println("</body>");   
          client.println("</html>");   
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          //Serial.println(request);
          //Serial.println(request.indexOf("/save"));
          if (request[5]=='s' && request[6]=='a' && request[7]=='v' && request[8]=='e') {  // see if checkbox was clicked

            //Serial.println(request);
            //Serial.println(request.indexOf(" HTTP"));
            //String rest = request.substring(10,request.indexOf(" HTTP"));

            //rest+="&";

            for (int i=0;i<request.length();i++){
              EEPROM.write(2+i,request[10+i]);
            }
            EEPROM.write(2+request.indexOf(" HTTP")-10,'&');

            initFromProgmem();
            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();
            client.println("<!DOCTYPE HTML>");
            client.println("<html><body><center><p>Done!<br>");
            client.println(server);
            client.println("<br>");
            client.println(port);
            client.println("<br>");            
            client.println(ssid);
            client.println("<br>");
            client.println(code);
            client.println("<br>");
            client.println("</p></center></body></html>");
            EEPROM.write(1,0);
            wdt_enable(WDTO_2S);
            break;
          }
          request="";

          //if (strncmp("GET /save",line,8)==0){
          //  Serial.println("Handle Save");
          //}

          //line[index]='\0';
          //index=0;
          //Serial.write(line);
        } 
        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);
    client.stop();
  }
}
Example #2
0
void loop()
{
	//char* message;

	WiFlyClient client = server.available();
	if(client)
	{
		Serial.println( "Client available...");
		boolean current_line_is_blank = true;
		message[0] ='\0';
		HTTPRequest req;
		while(client.connected())
		{
			//Serial.println( "Client connected...");
			if (client.available())
			{
					char c = client.read();
					if(c != '\n')
					{
						int len = strlen(message);
						if(len < MAX_SIZE_MESSAGE)
						{
							message[len] = c;
							message[len+1] = '\0';
						}
					}
					// if we've gotten to the end of the line (received a newline
			        // character) and the line is blank, the http request has ended,
			        // so we can send a reply
			        if (c == '\n' && current_line_is_blank) {
			        	req.ValidateMessage(message);
			        	Serial.println(req.type);
			        	Serial.print(message);
			        	Serial.println();
			          // send a standard http response header
			        	Serial.println("Returning response header");
			          client.println("HTTP/1.1 200 OK");
			          client.println("Content-Type: text/html");
			          client.print("\n");
			          printValuesAsJSON();

			          /*
			          client.println("<html><body>");
			          client.print("<p>SHT Temperature:");
			          client.print(temp_sensor.readTemperatureC());
			          client.print("</p><p>SHT Humidity:");
			          client.print(temp_sensor.readHumidity());
			          client.print("</p><p>Luminosity Data:");
			          client.print(analogRead(LIGHT_DATA_PIN));
			          client.print("</p><p>Mic Data:");
			          client.print(analogRead(MIC_DATA_PIN));
			          client.print("</p><p>SHARP Data:");
			          client.print(analogRead(SHARP_DATA_PIN));
			          client.println("</p></body></html>");*/

			          break;
			        }
			        if (c == '\n') {
			          // we're starting a new line
			          current_line_is_blank = true;
			        } else if (c != '\r') {
			          // we've gotten a character on the current line
			          current_line_is_blank = false;
			        }
			}
		}
		delay(100);
		client.stop();
		Serial.println("Client killed");
	}

}