Smarthome Wifi Remote Command

From Wiki

Jump to: navigation, search

Kit List

Introduction:
Arduino advanced kit provided by ELECFREAKS is based on our starter kit to expand more and more popular modules owing the characteristics of plug and use without any requirement of soldering. We have well prepared six courses for learners to learn the usage of modules in an intersting environment which is better for stimulating your thinking mind. For example,Tetris games help us to study the usage of the LCD screen and Arduino program. Color picker used can help us to learn the color sensor as well as BLE Bluetooth communication and Android interface development ... Practical Course included LCD screen, sensor, MP3, BLE, WIFI, Android development, and image interaction (processing), make you open-minded!

Part6 Smarthome Wifi Remote Command

Product introduction:
AD-part-6.gif When your Arduino is connected to the network via wifi module, you can control your Arduino board via the Internet. Because of the limited conditions, here with LAN case.
Wifi 1.png Wifi 2.png
Materials:
an Arduino board, a wifi module, a relay, of course you still need a wireless router and an internet device, we also prepared an Android APK for demo.
*1xFreaduino Uno;
*1xESP Wifi Module;
*1x1 Channel Relay;
Parameter and IO definition:
1.Board requires a 3.3V power supply. Higher than this voltage will lead to unstable working of wifi module.
Uno 1.jpg 2.ESP WIFI access to board #define V V(power supply) #define TX D4(RX) // Please notice the Pin's define,D(Digital pin) #define RX D5(TX) #define G G(Ground) #ESP WIFI GIO0 used when updating firmware and you need to connect this pin to ground
Notes:
1、First install the part6 APK in the Android cell phone 2、upload code to UNO, and according to the card after connection and power 3、Search and connect WiFi with elecfreaks_ap 4、Open the app and click the X button on the app, if it turns into a V icon to indicate the connection is successful. 5、AT commands please view ESP WIFI reference materials below, or you can also downloaded part6.zip for full profile.
ESP WIFI reference materials:
ESP1.png ESP2.png ESP3.png ESP4.png ESP5.png ESP6.png ESP7.png

Arduino code

part6.zip


#include <SoftwareSerial.h>
#include <Timer.h>

#define TCPServer_IP  "192.168.1.100"
#define TCPServer_Port  "200"
#define TCPServer_OverTime  "1800"
#define      macUser_ESP8266_BulitApSsid                  "ELECFREAKS_AP"         
#define      macUser_ESP8266_BulitApEcn                   0              
#define      macUser_ESP8266_BulitApPwd                   "elecfreaks"         
#define wifiBaud 9600
#define wifiRx   4
#define wifiTx   5

Timer timer;
SoftwareSerial wifi(wifiRx,wifiTx);
 
int  sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
 
    return sendATcommand2(ATcommand, expected_answer1, " ", timeout);
}
 
 
 
int  sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2,
    unsigned int timeout){
 
    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;
 
    memset(response, '\0', 100);    // Initialize the string
 
    delay(100);
 
    while( wifi.available() > 0) wifi.read();    // Clean the input buffer
 
    wifi.println(ATcommand);    // Send the AT command 
 
 
    x = 0;
    previous = millis();
 
    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
//        if(mySerial.available() != 0){    
//            response[x] = mySerial.read();
//            x++;
            if(wifi.available())
            {
              do{
                response[x++] = wifi.read();
                }while(wifi.available());
            // check if the desired answer 1 is in the response of the module
            if (strstr(response, expected_answer1) != NULL)    
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            if (strstr(response, expected_answer2) != NULL)    
            {
                answer = 2;
            }
        }
        Serial.println(response);
    // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));    
 
    return answer;
}
 
void ESP8266_AT_Test ( void )
{
  while ( ! sendATcommand2 ( "AT", "OK", NULL, 200 ) ) ;	
 
}
 
void ESP8266_Net_Mode_Choose(void)
{
  sendATcommand2 ( "AT+CWMODE=2", "OK", "no change", 2500 );
}
 
void ESP8266_ExitUnvarnishSend ( void )
{
	delay ( 1000 );
 
	wifi.print( "+++" );
 
	delay ( 500 ); 
 
}
 
uint8_t ESP8266_CIPAP ( char * pApIp )
{
	char cCmd [ 30 ];
 
 
	sprintf ( cCmd, "AT+CIPAP=\"%s\"", pApIp );
 
  if ( sendATcommand2 ( cCmd, "OK", 0, 5000 ) )
		return 1;
 
	else 
		return 0;
 
}
 
int ESP8266_BuildAP ( char * pSSID, char * pPassWord, int mode )
{
	char cCmd [120];
 
	sprintf ( cCmd, "AT+CWSAP=\"%s\",\"%s\",1,%d", pSSID, pPassWord, mode );
 
	return sendATcommand2 ( cCmd, "OK", 0, 1000 );
 
}
 
int ESP8266_Enable_MultipleId ( int mode )
{
	char cStr [20];
 
	sprintf ( cStr, "AT+CIPMUX=%d", mode );
 
	return sendATcommand2 ( cStr, "OK", 0, 500 );
 
}
 
int ESP8266_StartOrShutServer ( int mode, char * pPortNum, char * pTimeOver )
{
	char cCmd1 [120], cCmd2 [120];
 
	if ( mode )
	{
		sprintf ( cCmd1, "AT+CIPSERVER=%d,%s", 1, pPortNum );
 
		sprintf ( cCmd2, "AT+CIPSTO=%s", pTimeOver );
 
		return ( sendATcommand2 ( cCmd1, "OK", 0, 500 ) &&
						 sendATcommand2 ( cCmd2, "OK", 0, 500 ) );
	}
 
	else
	{
		sprintf ( cCmd1, "AT+CIPSERVER=%d,%s", 0, pPortNum );
 
		return sendATcommand2 ( cCmd1, "OK", 0, 500 );
	}
 
}
 
String info;
void receiveInfo(){
	   if(wifi.available()){
	    	info=wifi.readString();
	    	Serial.print(info);
	    	if(info.lastIndexOf("ON")>info.lastIndexOf("OFF")){
	    	        digitalWrite(A0,1);
	    	  }else if(info.lastIndexOf("ON")<info.lastIndexOf("OFF")) {
	    	    	digitalWrite(A0,0);
	    	    		}
	    	  else return;
	    	//http response
	    	/*
	    	String id=info.substring(info.indexOf(',')+1,info.lastIndexOf(','));
	    	    	wifi.write("AT+CIPSEND=");
	    	    	wifi.write(id);
	    	   	    wifi.write(",126\r\n");
	    	    	wifi.println("HTTP/1.1 200 OK");
	    	    	wifi.println("Cache-Control: no-cache");
	    	    	wifi.println("Pragma: no-cache");
	    	    	wifi.println("Expires: -1");
	    	    	wifi.println("Content-Type: text/html");
	    	    	wifi.println("Content-Length: 12");
	    	    	wifi.println("                       ");
	    	    	wifi.println("it received");
	    	    	*/
	   }}
 
void setup(void)
{
  Serial.begin(9600);
  wifi.begin(9600);
  pinMode(A0,OUTPUT);
  //ESP8266_ExitUnvarnishSend();
  ESP8266_AT_Test();//AT command test
  ESP8266_Net_Mode_Choose();//set ap mode
  //while ( ! ESP8266_CIPAP ( TCPServer_IP ) );
  //ESP8266_CIPAP ( TCPServer_IP );
  ESP8266_BuildAP ( macUser_ESP8266_BulitApSsid, macUser_ESP8266_BulitApPwd, macUser_ESP8266_BulitApEcn );//set ap ssid passwd
  ESP8266_Enable_MultipleId(1);//set multi connect
  ESP8266_StartOrShutServer ( 1, TCPServer_Port, TCPServer_OverTime );//set server port,over time
}
 
void loop()
{
  receiveInfo();
  while(Serial.available()){
    wifi.print(Serial.readString());
  }
}

Personal tools
Namespaces
Variants
Actions
Navigation
Support
Toolbox