/*
 * Test Arduino Manager for iPad / iPhone / Mac
 *
 * A simple test program to show the Arduino Manager
 * features.
 *
 * Author: Fabrizio Boco - fabboco@gmail.com
 *
 * Version: 1.10
 *
 * 04/04/2013
 *
 * All rights reserved
 *
 */

/*
*
 * IOSController library, IOSControllerWiFi library  (“The Software”) and the related documentation (“The Documentation”) are supplied to you 
 * by the Author in consideration of your agreement to the following terms, and your use or installation of The Software and the use of The Documentation 
 * constitutes acceptance of these terms.  
 * If you do not agree with these terms, please do not use or install The Software.
 * The Author grants you a personal, non-exclusive license, under author's copyrights in this original software, to use The Software. 
 * Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by the Author, including but not limited to any 
 * patent rights that may be infringed by your derivative works or by other works in which The Software may be incorporated.
 * The Software and the Documentation are provided by the Author on an "AS IS" basis.  THE AUTHOR MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT 
 * LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR ITS USE AND OPERATION 
 * ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
 * REPRODUCTION AND MODIFICATION OF THE SOFTWARE AND OR OF THE DOCUMENTATION, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 
 * STRICT LIABILITY OR OTHERWISE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <SPI.h>
#include <WiFi.h>
#include <SD.h>
#include <Servo.h>
#include <IOSControllerWiFi.h>

/*
 *
 * WiFI Library configuration
 *
 */
char ssid[] = "homewirelessnetwork";      //  your network SSID (name) 
char pass[] = "Fabrizio22Camilla22Laura26";  // your network password
int keyIndex = 0;               // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

#define CHIPSELECT 4
/**
 *
 * Other initializations
 *
 */
#define YELLOWLEDPIN 8
int yellowLed = HIGH;

#define TEMPERATUREPIN 0
float temperature;

#define LIGHTPIN 1
int light;

#define SERVOPIN 9
Servo servo;
int servoPos;


#define CONNECTIONPIN 6
int connectionLed = LOW;

#define POTENTIOMETERPIN 2
int pot;

/*
 *
 * Prototypes of IOSController’s callbacks
 *
 *
 */
void doWork();
void doSync(char *variable);
void processIncomingMessages(char *variable, char *value);
void processOutgoingMessages();
void processAlarms(char *variable);
void deviceConnected();
void deviceDisconnected();


/*
 *
 * IOSController Library initialization
 *
 */
#ifdef ALARMS_SUPPORT 
  IOSControllerWiFi iosController(&server,&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,&processAlarms,&deviceConnected,&deviceDisconnected);
#else
  IOSControllerWiFi iosController(&server,&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,&deviceConnected,&deviceDisconnected);
#endif

void setup()
{
  Serial.begin(9600);

  /*
   * SD Card inizialization
   *
   *
   * On the Ethernet Shield, CS is pin 4. It's set as an output by default.
   * Note that even if it's not used as the CS pin, the hardware SS pin 
   * (10 on most Arduino boards, 53 on the Mega) must be left as an output 
   * or the SD library functions will not work. 
  */
  pinMode(10, OUTPUT);     // change this to 53 on a mega
  digitalWrite(10, HIGH);  // but turn off the W5100 chip!
  
  if (!SD.begin(CHIPSELECT))  {
    
    Serial.println("SD init failed.");
  } else {
    
    Serial.println("Card present."); 
  }
  
  pinMode(53,OUTPUT);

  // 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 5 seconds for connection:
    delay(5000);
  } 
  
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
    
  server.begin();
  
  /**
   *
   * Other initializations
   *
   */   

  // Yellow LED on
  pinMode(YELLOWLEDPIN,OUTPUT);
  digitalWrite(YELLOWLEDPIN,yellowLed);


  // Servo position at 90 degrees 
  servo.attach(SERVOPIN);
  servoPos = 90;
  servo.write(servoPos);

  // Red LED OFF
  pinMode(CONNECTIONPIN,OUTPUT);
  digitalWrite(CONNECTIONPIN,connectionLed);
}


/**
 * 
 * Standard loop function
 *
 */
void loop()
{
  //iosController.loop();
  iosController.loop(20);
}

/**
*
*
* This function is called periodically and its equivalent to the standard loop() function
*
*/
void doWork() {

  temperature = getVoltage(TEMPERATUREPIN);  //getting the voltage reading from the temperature sensor
  temperature = (temperature - 0.5) * 100;  // converting from 10 mv per degree with 500 mV offset
                                            // to degrees ((voltage – 500mV) times 100  
                                                                                       
  digitalWrite(YELLOWLEDPIN,yellowLed);                                            

  servo.write(servoPos);

  light = analogRead(LIGHTPIN);
  
  pot = analogRead(POTENTIOMETERPIN);
}


/**
*
*
* This function is called when the ios device connects and needs to initialize the position of switches and knobs
*
*/
void doSync (char *variable) {

  if (strcmp(variable,"Knob1")==0) {

    iosController.writeMessage(variable,map(servo.read(),0,180,0,1023));
  }

  if (strcmp(variable,"S1")==0) {

    iosController.writeMessage(variable,digitalRead(YELLOWLEDPIN));
  }
   
  if (strcmp(variable,"Msg")==0) {
    
    iosController.writeTxtMessage("Msg","Hello, I'm your Arduino board");
  } 

}

/**
*
*
* This function is called when a new message is received from the iOS device
*
*/
void processIncomingMessages(char *variable, char *value) {  
  
  if (strcmp(variable,"S1")==0) {

    yellowLed = atoi(value);
  }

  if (strcmp(variable,"Knob1")==0) {

      servoPos = atoi(value);  
      servoPos = map(servoPos, 0, 1023, 0, 180);      
  }

  if (strcmp(variable,"Push1") == 0) {
    
    iosController.temporaryDigitalWrite(CONNECTIONPIN,LOW,500);
  }

  if (strcmp(variable,"Cmd_01")==0) {
    
    iosController.log("Command: "); iosController.logLn(value);
    
    Serial.print("Command: ");
    Serial.println(value);
  } 
  
  if (strcmp(variable,"Cmd_02")==0) {
    
     iosController.log("Command: "); iosController.logLn(value);
     
    Serial.print("Command: ");
    Serial.println(value);
  } 
  
  if (strcmp(variable,"R1")==0) {
   
      yellowLed = atoi(value);
  } 
  
}

/**
*
*
* This function is called periodically and messages can be sent to the iOS device
*
*/
void processOutgoingMessages() {


  iosController.writeMessage("T",temperature);

  iosController.writeMessage("L",light);

  iosController.writeMessage("Led13",yellowLed);
  
  iosController.writeMessage("Pot",pot);
  
}

/**
*
*
* This function is called when a Alarm is fired
*
*/
void processAlarms(char *alarm) {
  
  Serial.print(alarm);
  Serial.println(" fired");
  
  servoPos = 0;
}

/**
*
*
* This function is called when the iOS device connects
*
*/
void deviceConnected () {

  digitalWrite(CONNECTIONPIN,HIGH);
  
  //iosController.writeTxtMessage("Msg","Hello, I'm your Arduino board");
  
  Serial.println("Device connected");
}

/**
*
*
* This function is called when the iOS device disconnects
*
*/
void deviceDisconnected () {

  Serial.println("Device disconnected");
  digitalWrite(CONNECTIONPIN,LOW);
}

/**
*  
* Auxiliary functions
*
*/

/*
 * getVoltage() – returns the voltage on the analog input defined by pin
 * 
 */
float getVoltage(int pin) {

  return (analogRead(pin) * .004609375);  // converting from a 0 to 1023 digital range
                                          // to 0 to 4.72 volts (each 1 reading equals ~ 5 millivolts)
}
