ESP8266 Software for Android App communication via Wi-Fi

ESP8266 Software for Android App communication via WiFi

Unlock the full potential of your DSLR camera with this ESP8266 software for Android app communication via Wi-Fi written in Arduino IDE.

Our software allows you to wirelessly control your camera's Focus and Shutter functions with ease, giving you greater control over your photography.

With simple setup and intuitive controls, the software is perfect for amateur and professional photographers alike.

Discover a new level of flexibility and convenience with our cutting-edge technology.

This ESP8266 Software is used to communicate with Android App via Wi-Fi and was written in Arduino IDE.

It is used on the ESP-01 and ensures the communication between the Android Application and DSLR Remote Control F7

ESP8266 Software for Android App communication via WiFi

 

#include <EEPROM.h>
#include <ESP8266WiFi.h>

#define IP_Start	0
#define IP_Len		4
#define SSID_Start	IP_Len
#define SSID_Len	11
#define Pass_Start	IP_Len + SSID_Len
#define Pass_Len	17
//We allow only one device  to connect
#define MAX_SRV_CLIENTS 1
#define ZERO_CHAR 48
#define StrConnOK "GVI!\n"
#define StrDefaultSSID "RemoteSen"
#define StrDefaultPASS "01234567"
#define SignalReadyPin 2 // pin to inform STM that ESP is ready for action

//Default values for IP and gateway
byte myIP[4] = {10, 0, 0, 1};
const byte mySubNet[4] = {255, 255, 255, 0};
bool SoftAP_ready = false;
byte idx = 0;
String mySSID, myPASSWORD;
unsigned long myTime;

//Get the IP from EEPROM
void get_IP(){
	for(idx = IP_Start; idx < IP_Len; idx++){
		myIP[idx] = EEPROM.read(idx);
	}
}

//Save the IP in EEPROM
void setIP(){
	for(idx = IP_Start; idx < IP_Len; idx++){
		EEPROM.write(idx, myIP[idx]);
	}
	EEPROM.commit();	
}

//Read strings from EEPROM
String get_MemStr(byte StartPos, byte MaxLen){
	unsigned char crtChar;
	byte pos = StartPos;
	idx = 0;
	char data[MaxLen + 1]; 
	//Read until null character
	for(idx = 0; idx < MaxLen; idx++){
		crtChar = EEPROM.read(StartPos + idx);
		data[idx] = crtChar;
		if(crtChar == '\0') break;
	}
	return String(data);
}

//the lenght will be limited in Antroid app, no need to check it here
void set_MemStr(byte myType, String my_data){
	byte dataLen = my_data.length();
	byte pos = 0;
	if(myType){
		//PASSWORD
		pos = Pass_Start;
	} else {
		//SSID
		pos = SSID_Start;		
	}
	for(idx = 0; idx < dataLen; idx++){
		EEPROM.write(pos + idx, my_data[idx]);
	}
	//Add termination null character for String Data
	EEPROM.write(pos + idx, '\0');
	EEPROM.commit();
}

void resetData(){
	//Max len for password is 15
	//Max len for SSID is 11
	myIP[0] = 10;
	myIP[1] = 0;
	myIP[2] = 0;
	myIP[3] = 1;
	setIP();
	//Password must be min 8 chars for SoftAP to work
	set_MemStr(0, StrDefaultSSID);
	set_MemStr(1, StrDefaultPASS);
}

void arrayToIP(byte myArr[], byte len, byte pos){
	byte tmpIp = 0;
	switch(len){
		case 1:
			tmpIp = myArr[0] - ZERO_CHAR;
			break;
		case 2:
			tmpIp = 10*(myArr[0] - ZERO_CHAR) + (myArr[1] - ZERO_CHAR);
			break;
		case 3:
			tmpIp = 100*(myArr[0] - ZERO_CHAR) + 10*(myArr[1] - ZERO_CHAR) + (myArr[2] - ZERO_CHAR);
			break;       
	}
	myIP[pos] = tmpIp;
}

void processIP(byte myCredentials[], byte len){
	//First 2 bytes are !! and last 2 are \r\n
	byte tmpLenA = len - 1;
	byte tmpLenB = len - 2;
	byte j = 0;
	byte tmpIdx = 0;
	byte tmpArr[3];
	for(idx = 2; idx < tmpLenA; idx++){
		//if we have .
		if((myCredentials[idx] == 46) || (idx == tmpLenB)){
			//convert tmpArr to byte
			arrayToIP(tmpArr, tmpIdx, j);
			//Reset tmpIdx
			tmpIdx = 0;
			//Increment IP position
			j++;
		} else {
			//add the char in tmpArr
			tmpArr[tmpIdx] = myCredentials[idx];
			//Increment the index
			tmpIdx++;
		}
	}
	//Save IP in EEPROM
	setIP();
}

void processString(byte myCredentials[], byte len, byte id){
	byte strLen = len - 2;
	char myStr[strLen - 2];
	for (idx = 2; idx < strLen; idx++){
		myStr[idx - 2] = myCredentials[idx];
	}
	myStr[idx - 2] = '\0';
	set_MemStr(id, String(myStr));
}

WiFiServer server(80);
WiFiClient serverClient;

void setup() {
	//Set pin2 high to put STM on hold till we finish the setup
	pinMode(SignalReadyPin, OUTPUT);
	digitalWrite(SignalReadyPin, HIGH);
	//require ESP8266 >= 2.4.0 https://github.com/esp8266/Arduino/releases/tag/2.4.0-rc1
	Serial.setRxBufferSize(128);
	Serial.begin(115200);
	//IP = 4bytes; SSID = 12bytes; PASSWORD = min 8 max 16 bytes
	EEPROM.begin(32);

	//Serial.println();
	//Serial.println("Starting...");
	WiFi.mode(WIFI_AP);
  
	////Run this code only once to set default values for IP, SSID and password in EEPROM
	//resetData();
	////
  
	//Here we read from EEPROM the IP and gateway
	get_IP();
	//Here we read from EEPROM the network name (SSID) and password
	mySSID = get_MemStr(SSID_Start, SSID_Len);
	myPASSWORD = get_MemStr(Pass_Start, Pass_Len);
	//Serial.print("SSID...");Serial.println(mySSID);
	//Serial.print("Pass...");Serial.println(myPASSWORD);
	//Serial.print("IP...");Serial.print(myIP[0]);Serial.print(".");Serial.print(myIP[1]);Serial.print(".");Serial.print(myIP[2]);Serial.print(".");Serial.println(myIP[3]);
	//Wait 1s for AP_START...
	while(!SoftAP_ready){
		SoftAP_ready = WiFi.softAP(mySSID, myPASSWORD, 1, false, MAX_SRV_CLIENTS);
		//Serial.println(SoftAP_ready ? "Ready" : "Failed!");
		delay(500);
	}
	
	//Setup the IP...
	IPAddress Ip(myIP[0], myIP[1], myIP[2], myIP[3]);
	IPAddress GateWay(myIP[0], myIP[1], myIP[2], 1);
	IPAddress NetMask(mySubNet[0], mySubNet[1], mySubNet[2], mySubNet[3]);
	//Set new configuration
	WiFi.softAPConfig(Ip, GateWay, NetMask);
	server.begin();
	server.setNoDelay(true);
	WiFi.setSleepMode(WIFI_NONE_SLEEP); // disable WiFi sleep for more performance
	//Set pin2 low to remove STM from hold
	digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
	serverClient = server.available();
	if(serverClient){
		if(serverClient.connected()){
			//Inform STM board thet ESP is ready 
			serverClient.write(StrConnOK);
		}
		while(serverClient.connected()){
			size_t rxlen = serverClient.available();
			if (rxlen > 0){
				byte rx_buff[rxlen];
				serverClient.readBytes(rx_buff, rxlen);
				//reset the password, IP and SSID
				if((rx_buff[0] == '!') && (rx_buff[1] == '!')){
				  //we get the IP
          processIP(rx_buff, rxlen);
				} else if((rx_buff[0] == '#') && (rx_buff[1] == '#')){
          //we get the SSID
          processString(rx_buff, rxlen, 0);      
				}else if((rx_buff[0] == '$') && (rx_buff[1] == '$')){
          //we get the Password
          processString(rx_buff, rxlen, 1);     
       }else if((rx_buff[0] == '%') && (rx_buff[1] == '%')){
					//Reset all data
					resetData();
				} else {
					//We send the data directly to STM board
					Serial.write(rx_buff, rxlen);
				}
			}
     
			size_t txlen = Serial.available();
			if (txlen > 0){
				byte tx_buff[txlen];
				Serial.readBytes(tx_buff, txlen);
				serverClient.write(tx_buff, txlen);
			}
      delay(5); 
		}
	}
	delay(100);
}

Comments powered by CComment

Who’s online

We have 33 guests and no members online