예제 #1
0
boolean receivePacket(UDPpacket& packet, WiFly& wifly)
{
	boolean ret;
	char buf[100];
	ret = wifly.gets(buf, sizeof(buf));
	packet.data += buf;
// 	do {
// 		rByte = wifly.read();
// 		//Serial.println(rByte);
// 		packet.data = packet.data + (char) rByte;
// 		if((char)rByte == '\n') {
// 			break;
// 		}
// 	}while(rByte != -1);
	packet.dataLength = packet.data.length();
	wifly.getHostIP(packet.ip, sizeof(packet.ip));
	packet.port = wifly.getHostPort();

	return true;
	
// 	if(!packet.data.compareTo("-1"))
// 		return false;
// 	else
// 		return true;
}
void readResponse (char *buf, int max_len, int content_len) {
  // Read content.
  int len = wifly.readBytes(buf, max_len > content_len ? content_len : max_len);
  buf[len] = '\0'; // insurance
  
  // Flush buffer.
  while (wifly.available() > 0) {
    wifly.read();
  }
}
예제 #3
0
boolean sendPacket(UDPpacket& packet, WiFly& wifly)
{
	boolean ret;
	char buf[100];
	
	packet.data.toCharArray(buf, sizeof(buf));

	ret = wifly.sendto((const char *)buf, packet.ip, packet.port);
	wifly.flush();
	return ret;
}
/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
  while (1) {
    if (wifly.available() > 0) {
     Serial.write(wifly.read());
    }

    if (Serial.available() > 0) {
      wifly.write(Serial.read());
    }
  }
}
void debugWifiState () {
  char buf[32];
  Serial.print(F("MAC: "));
  Serial.println(wifly.getMAC(buf, sizeof(buf)));
  Serial.print(F("IP: "));
  Serial.println(wifly.getIP(buf, sizeof(buf)));
  Serial.print(F("Netmask: "));
  Serial.println(wifly.getNetmask(buf, sizeof(buf)));
  Serial.print(F("Gateway: "));
  Serial.println(wifly.getGateway(buf, sizeof(buf)));
  Serial.print(F("DeviceID: "));
  Serial.println(wifly.getDeviceID(buf, sizeof(buf)));
}
void JSONAPI::_headerStart (const char *method) {
  // If an old connection is active, close.
  if (wifly.isConnected()) {
    wifly.close();
  }

  if (!wifly.open(this->host, 80)) {
    Serial.println(F("Failed to connect to host."));
  }
  
  wifly.print(method);
  wifly.print(" ");
  // wifly.print("http://");
  // wifly.print(this->host);
}
예제 #7
0
bool wifiSetup(WiFly myWiFly) {
	// Scan, nouveau mode nécessite un firmware >= 2.22
	if (myWiFly.version() < 2.22)
	{
		return false;
	}
	
	if (myWiFly.sendCommand(wifiCompactModeStr))
	{
		return true;
	}
	else
	{
		return false;
	}
}
int JSONAPI::request ( js0n_user_cb_t cb ) {
  if (this->hasBody) {
    wifly.println("0");
    wifly.println();
  }
  
  js0n_parser_t parser;
  parser.buffer = this->buffer;
  parser.stream = &wifly;
  parser.user_cb = cb;
  
  int status_code = 0;
  readResponseHeaders(&status_code, (int *) &parser.length);
  if (status_code != 0 && status_code < 500) {
    int parse_status = js0n_parse ( &parser );
  }
  return status_code;
}
예제 #9
0
void loop() {
  int available;

  if (wifly.isConnected() == false) {
    Serial.println("Connecting");

    if (wifly.open("192.168.1.100", 4001)) {
      Serial.println("Connected");
    } else {
      Serial.println("Failed to open");
    }
  } else {
    available = wifly.available();

    if (available < 0) {
      Serial.println("Disconnected");
    } else if (available > 0) {
      Serial.write(wifly.read());
    }
  }
}
예제 #10
0
bool wifiScan(WiFly myWiFly) {
	// un scan wifi via le RN 171 met environ 2500ms par défaut:
	// - 200ms par canal
	// - 13 canaux
	if (myWiFly.sendCommand(wifiScanStr))
	{
		return true;
	}
	else
	{
		return false;
	}
}
예제 #11
0
void JSONAPI::_headerEnd () {
  wifly.println(" HTTP/1.1"); // paste your number here
  wifly.print("Host: ");
  wifly.println(this->host);
  wifly.println("User-Agent: lifegraph/0.0.1");
  if (this->hasBody) {
    wifly.println("Content-Type: application/x-www-form-urlencoded");
    wifly.println("Transfer-Encoding: chunked");
  }
  wifly.println();
}
예제 #12
0
void loop()
{

    if (wifly.available() > 0) {

    	/* See if there is a request */
		if (wifly.gets(buf, sizeof(buf))) {
		    if (strncmp_P(buf, PSTR("GET /ping"), 9) == 0) {

				/* GET request */
#ifdef DEBUG
				Serial.println(F("PONG XML requested"));
#endif
				while (wifly.gets(buf, sizeof(buf)) > 0) {
				    //Skip rest of request
				}

				sendPong();

	   	 	} else if (strncmp_P(buf, PSTR("GET /data"), 9) == 0) {

	        	/* POST request */
#ifdef DEBUG
	        	Serial.println(F("DATACOLLECTOR XML: sendind sensors data"));
#endif

				while (wifly.gets(buf, sizeof(buf)) > 0) {
				    //Skip rest of request
				}

                // discard rest of input
		    	// wifly.flushRx();		
				sendSensorsDataXML();

	    	} else {

	       		// Unexpected request
#ifdef DEBUG
				Serial.print(F("Unexpected: "));
				Serial.println(buf);
				Serial.println(F("Sending 404"));
#endif
				while (wifly.gets(buf, sizeof(buf)) > 0) {
				    //Skip rest of request
				}

                // discard rest of input
				wifly.flushRx();		
				send404();

	    	}
		}
    }
}
예제 #13
0
int LifegraphAPI::connect (uint8_t uid[], int uidLength) {
  this->hasBody = false;
  this->_headerStart("GET");
  this->_headerPath("tokens");
  wifly.print("/");
  char token[3];
  for (int i = 0; i < uidLength; i++) {
    snprintf(token, 3, "%02x", uid[i]);
    wifly.print(token);
  }
  wifly.print("?namespace=");
  wifly.print(this->ns);
  wifly.print("&key=");
  wifly.print(this->key);
  wifly.print("&secret=");
  wifly.print(this->secret);
  this->_headerEnd();
  return this->request( NULL );
}
예제 #14
0
int wifiScanReadLn(WiFly myWiFly, char *ptrLine) {
	bool endOfLine = false;
	char c=' ';
	int i=0;

	while (!endOfLine && i<(MAX_LENGTH_SCAN_LINE-1)) 	{
		if (myWiFly.receive((uint8_t *)&c, 1, 300) > 0) {
			if (c != '\r')
				ptrLine[i++] = c;
			else
				endOfLine = true;
		}
		else endOfLine = true; // Plus rien de dispo en entrée...
	}

	ptrLine[i] = '\0';

	return i;
}
예제 #15
0
/** Send an index HTML page with an input box for a username */
void sendPong()
{
    /* Send the header direclty with print */
    wifly.println(F("HTTP/1.1 200 OK"));
    wifly.println(F("Content-Type: text/xml"));
    wifly.println(F("Transfer-Encoding: chunked"));
    wifly.println();

    /* Send the body using the chunked protocol so the client knows when
     * the message is finished.
     * Note: we're not simply doing a close() because in version 2.32
     * firmware the close() does not work for client TCP streams.
     */
    wifly.sendChunkln(F("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
	snprintf_P(wbuf, sizeof(wbuf), PSTR("<pong>%s</pong>"), macaddr);
    wifly.sendChunkln(wbuf);

    wifly.sendChunkln();
}
예제 #16
0
boolean connectWifi (SoftwareSerial *wifiSerial, const char *ssid, const char *pass) {
  if (!wifly.begin(wifiSerial, &Serial)) {
    Serial.println(F("Could not start Wifly module."));
    terminal();
    return false;
  }
 
  // Join wifi network if not already associated
  if (!wifly.isAssociated()) {
    wifly.setSSID(ssid);
    wifly.setPassphrase(pass);
    wifly.enableDHCP();
    wifly.setDeviceID("Wifly-WebClient");
 
    if (!wifly.join()) {
      Serial.println(F("Failed to join wifi network"));
      terminal();
      return false;
    }
  }
  return true;
}
예제 #17
0
void readResponseHeaders (int *status_code, int *content_len) {
  boolean line_start = true;
  char toss[1], buf[64];
  int len;
  *content_len = *status_code = 0;

  // HTTP/1.1 xxx
  if (wifly.readBytes(buf, 9) == 0) {
    return;
  }
  if (wifly.readBytes(buf, 3) == 0) {
    return;
  }
  buf[3] = '\0';
  *status_code = strtol(buf, NULL, 10);
  if (wifly.readBytesUntil('\n', buf, sizeof(buf)) == 0) {
    return;
  }

  while (true) {
    // Read first character of line.
    if (wifly.readBytes(buf, 1) == 0) {
      return;
    }
    char ch = buf[0];
 
    if (line_start) {
      // Beginning content.
      if (ch == '\r') {
        // read ...\n\r\n
        if (wifly.readBytes(buf, 1) == 0) { 
          return;
        }
        break;
      }
    }
     
    // Read header name. 
    len = wifly.readBytesUntil(':', &buf[1], sizeof(buf) - 2);
    if (len == 0) {
      return;
    }
    buf[len + 1] = '\0';
    // space
    if (wifly.readBytes(toss, 1) == 0) { 
      return;
    }
    
    if (strncmp(buf, "Content-Length", len) == 0) {
      len = wifly.readBytesUntil('\n', buf, sizeof(buf));
      if (len == 0) {
        return;
      }
      buf[len] = '\0';
      *content_len = atoi(buf);
    } else {   
      if (wifly.readBytesUntil('\n', buf, sizeof(buf)) == 0) {
        return;
      }
    }
  }
}
예제 #18
0
void FacebookAPI::_headerPath (const char *path, const char *access_token) {
  wifly.print(this->base);
  wifly.print("/");
  wifly.print(path);
  if (access_token != 0) {
    wifly.print(strstr(path, "?") == 0 ? "?" : "&");
    wifly.print("access_token=");
    wifly.print(access_token);
  }
  wifly.print(strstr(path, "?") == 0 ? "?" : "&");
  wifly.print("namespace=");
  wifly.print(Lifegraph.ns);
  wifly.print("&key=");
  wifly.print(Lifegraph.key);
  wifly.print("&secret=");
  wifly.print(Lifegraph.secret);
}
예제 #19
0
void JSONAPI::_headerPath (const char *path) {
  wifly.print(this->base);
  wifly.print("/");
  wifly.print(path);
}
예제 #20
0
void JSONAPI::_chunk (const char *str, int len) {
  char lenstr[12];
  sprintf(lenstr, "%X", len);
  wifly.println(lenstr);
  wifly.println(str);
}
예제 #21
0
void sendSensorsDataXML() {
    /* Send the header directly with print */
    wifly.println(F("HTTP/1.1 200 OK"));
    wifly.println(F("Content-Type: text/xml"));
    wifly.println(F("Transfer-Encoding: chunked"));
    wifly.println();

    /* Send the body using the chunked protocol so the client knows when
     * the message is finished.
     */
    wifly.sendChunkln(F("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    wifly.sendChunkln(F("<datacollector>"));

	snprintf_P(wbuf, sizeof(wbuf), PSTR("<dcid>%s</dcid>"), macaddr);
    wifly.sendChunkln(wbuf);

    wifly.sendChunkln(F("<geoloc>"));
	int ind;

	char scanbufcpy[500];
	strncpy(scanbufcpy, scanbuf, 500);


	char *ap, *oap;
	char *apdesctok, *apdesc;
	ap = strtok_r(scanbufcpy, "|", &oap);
	while(ap != NULL) {

    	wifly.sendChunkln(F("<ap>"));
		
		ind = 0;
		apdesctok = strtok_r(ap, " ", &apdesc);	
		while(apdesctok != NULL) {
			ind++;
			switch(ind) {
				case 3:
					snprintf_P(wbuf, sizeof(wbuf), PSTR("<channel>%s</channel>"), apdesctok);
    				wifly.sendChunkln(wbuf);
					break;
				case 4:
					snprintf_P(wbuf, sizeof(wbuf), PSTR("<rssi>%s</rssi>"), apdesctok);
    				wifly.sendChunkln(wbuf);
					break;
				case 6:
					snprintf_P(wbuf, sizeof(wbuf), PSTR("<mac>%s</mac>"), apdesctok);
    				wifly.sendChunkln(wbuf);
					break;
				default:
					;
			}
			apdesctok = strtok_r(NULL, " ", &apdesc);	
		}

    	wifly.sendChunkln(F("</ap>"));
		
		//Serial.println();
		ap = strtok_r(NULL, "|", &oap);
	}
    wifly.sendChunkln(F("</geoloc>"));

    wifly.sendChunkln(F("<sensors>"));

    /* Include readings from Analog pins */
	dtostrf(Thermistor(analogRead(ThermistorPIN)), 5,2, convbuf);
    snprintf_P(wbuf, sizeof(wbuf), PSTR("<sensor><type>temp</type><value>%s</value></sensor>"), convbuf);
#ifdef DEBUG
    Serial.println(wbuf);
#endif
    wifly.sendChunkln(wbuf);

	dtostrf(Photoresistor(analogRead(PhotoresistorPIN)), 5,2, convbuf);
    snprintf_P(wbuf, sizeof(wbuf), PSTR("<sensor><type>light</type><value>%s</value></sensor>"), convbuf);
#ifdef DEBUG
    Serial.println(wbuf);
#endif
    wifly.sendChunkln(wbuf);
    
	sprintf(convbuf,"NA");
	snprintf_P(wbuf, sizeof(wbuf), PSTR("<sensor><type>humidity</type><value>%s</value></sensor>"), convbuf);
#ifdef DEBUG
    Serial.println(wbuf);
#endif
    wifly.sendChunkln(wbuf);
	
	sprintf(convbuf,"NA");
	snprintf_P(wbuf, sizeof(wbuf), PSTR("<sensor><type>pressure</type><value>%s</value></sensor>"), convbuf);
#ifdef DEBUG
    Serial.println(wbuf);
#endif
    wifly.sendChunkln(wbuf);

    wifly.sendChunkln(F("</sensors>"));
    wifly.sendChunkln(F("</datacollector>"));
    wifly.sendChunkln();

}
예제 #22
0
/** Send a 404 error */
void send404()
{
    wifly.println(F("HTTP/1.1 404 Not Found"));
    wifly.println(F("Content-Type: text/html"));
    wifly.println(F("Transfer-Encoding: chunked"));
    wifly.println();
    wifly.sendChunkln(F("<html><head>"));
    wifly.sendChunkln(F("<title>404 Not Found</title>"));
    wifly.sendChunkln(F("</head><body>"));
    wifly.sendChunkln(F("<h1>Not Found</h1>"));
    wifly.sendChunkln(F("<hr>"));
    wifly.sendChunkln(F("</body></html>"));
    wifly.sendChunkln();
}
예제 #23
0
void setup()
{

    Serial.begin(115200);

#ifdef DEBUG
    Serial.println(F("Starting"));
    Serial.print(F("Free memory: "));
    Serial.println(wifly.getFreeMemory(),DEC);
#endif

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
#ifdef DEBUG
        Serial.println(F("Failed to start wifly"));
#endif
	wifly.terminal();
    }

	wifly.leave();

	delay(1000);

	wifly.scan(scanbuf, sizeof(scanbuf));


#ifdef DEBUG
	Serial.println(scanbuf);
#endif
	
	snprintf_P(scanbuf, sizeof(scanbuf), PSTR("%s b 13 -71 c 00:1c:58:10:1c:90 | a b 10 -93 c 00:12:43:8a:ea:22 | a b 11 -92 c d8:30:62:5f:f2:b1"), "a");

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
	/* Setup the WiFly to connect to a wifi network */
#ifdef DEBUG
		Serial.println(F("Joining network"));
#endif
		wifly.setSSID(mySSID);
		wifly.setPassphrase(myPassword);
		wifly.enableDHCP();
		wifly.setDHCP(1);
		wifly.save();

		if (wifly.join()) {
#ifdef DEBUG
		    Serial.println(F("Joined wifi network"));
#endif
		} else {
#ifdef DEBUG
		    Serial.println(F("Failed to join wifi network"));
#endif
		    wifly.terminal();
		}
    } else {
#ifdef DEBUG
        Serial.println(F("Already joined network"));
#endif
    }

    wifly.setBroadcastInterval(0);	// Turn off UPD broadcast

#ifdef DEBUG
    Serial.print(F("MAC: "));
    Serial.println(wifly.getMAC(macaddr, sizeof(macaddr)));
    Serial.print(F("IP: "));
    Serial.println(wifly.getIP(buf, sizeof(buf)));
#endif

    wifly.setDeviceID("Wifly-WebServer");

    if (wifly.isConnected()) {
#ifdef DEBUG
        Serial.println(F("Old connection active. Closing"));
#endif
	    wifly.close();
    }

    wifly.setProtocol(WIFLY_PROTOCOL_TCP | WIFLY_PROTOCOL_UDP);
    if (wifly.getPort() != 80) {
        wifly.setPort(80);
	    wifly.save();
#ifdef DEBUG
	    Serial.println(F("Set port to 80, rebooting to make it work"));
#endif
	    wifly.reboot();
	    delay(3000);
    }
    Serial.println(F("Ready"));

}
예제 #24
0
void setup() {
  char buf[64];

  Serial.begin(115200);
  Serial.println("Starting");
  Serial.print("Free memory: ");
  Serial.println(wifly.getFreeMemory(),DEC);

  wifiSerial.begin(9600);
  if (!wifly.begin(&wifiSerial, &Serial)) {
    Serial.println("Failed to start wifly");
  }

  if (!wifly.isAssociated()) {
    Serial.println("Joining network");
    wifly.setSSID(mySSID);
    wifly.setPassphrase(myPassword);
    wifly.enableDHCP();

    if (wifly.join()) {
      Serial.println("Joined wifi network");
    } else {
      Serial.println("Failed to join wifi network");
    }
  } else {
    Serial.println("Already joined network");
  }

  Serial.println("WiFly ready");

  Serial.print("MAC: ");
  Serial.println(wifly.getMAC(buf, sizeof(buf)));
  Serial.print("IP: ");
  Serial.println(wifly.getIP(buf, sizeof(buf)));
  Serial.print("Netmask: ");
  Serial.println(wifly.getNetmask(buf, sizeof(buf)));
  Serial.print("Gateway: ");
  Serial.println(wifly.getGateway(buf, sizeof(buf)));
  Serial.println("Set DeviceID");
  wifly.setDeviceID("Wifly-TCP");
  Serial.print("DeviceID: ");
  Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

  wifly.setIpProtocol(WIFLY_PROTOCOL_TCP);

  if (wifly.isConnected()) {
    Serial.println("Old connection active. Closing");
    wifly.close();
  }
}