Arduino WiFi Shield : JSON Parsing

char ssid[] = "Kazi-House"; // your network SSID (name)
char pass[] = "123456789"; // your network password
int keyIndex = ; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
	//Initialize serial and wait for port to open:
	Serial.begin(9600);

	// 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);
	}
	server.begin();
	// you're connected now, so print out the status:
	printWifiStatus();
	pinMode(45, OUTPUT);
	pinMode(46, OUTPUT);
	pinMode(47, OUTPUT);
	pinMode(48, OUTPUT);
	pinMode(49, OUTPUT);
	pinMode(51, OUTPUT);
}

void loop() {
	// listen for incoming clients
	WiFiClient client = server.available();
	if (client) {
		//Serial.println("new client");
		// an http request ends with a blank line
		boolean currentLineIsBlank = true;
		while (client.connected()) {
			if (client.available()) {
				char c = client.read();
				//Serial.write(c);
				if(c=='1'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("Center Bulb On");
							digitalWrite(45,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("Center Bulb Off");
							digitalWrite(45,LOW);
						}
					}
				}
				else if(c=='2'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("No Device 1 On");
							digitalWrite(46,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("No Device 1 Off");
							digitalWrite(46,LOW);
						}
					}
				}
				else if(c=='3'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("BathRoom TubeLight On");
							digitalWrite(47,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("BathRoom TubeLight Off");
							digitalWrite(47,LOW);
						}
					}
				}
				else if(c=='4'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("No Device 2 On");
							digitalWrite(48,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("No Device 2 Off");
							digitalWrite(48,LOW);
						}
					}
				}
				else if(c=='5'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("Room TubeLight On");
							digitalWrite(49,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("Room TubeLight Off");
							digitalWrite(49,LOW);
						}
					}
				}
				else if(c=='6'){
					if(client.read() == '=') {
						char onoff = client.read();
						if( onoff == '1'){
							Serial.println("No Device 3 On");
							digitalWrite(51,HIGH);
						}
						else if(onoff == '0'){
							Serial.println("No Device 3 Off");
							digitalWrite(51,LOW);
						}
					}
				}
				// 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("Connnection: close");
					client.println();
					break;
				}
				if (c == '\n') {
					// you're starting a new line
					currentLineIsBlank = true;
				}
				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);
		// close the connection:
		client.stop();
		//Serial.println("client disonnected");
	}
}

void printWifiStatus() {
	// print your WiFi shield's IP address:
	IPAddress ip = WiFi.localIP();
	Serial.print("IP Address: ");
	Serial.println(ip);
}
comments powered by Disqus