Monday, May 25, 2015

Android Phone, ESP8266 Serial WiFi, Arduino and PLC Modbus Application




Command of android phone is sent to the PLC via WiFi. WiFi from android phone connect to ESP8266 Serial WiFi and Arduino, and then transfer data from Arduino to PLC via serial and modbus protocol.
Text commands from android phone sent to ESP8266 and data in ESP8266 processed by Arduino. Data in Arduino converted into other commands and send commands to the PLC with Modbus communication.
Example Text command from Android: ON1 , ON1 for PLC Output Q0.1 become ON
Android phone is connected to the PLC via ESP8266 WiFi and Arduino:

Android Phone, ESP8266 Serial WiFi, Arduino and PLC Modbus Application





Hardware for WiFi applications on PLC and Android Phone:
1. Android Phone
2. ESP8266 serial WiFi module ESP_05 with External Antenna
3. Arduino Atmega 2560
4. PPI PLC Cable : RS232 PPI Multi Master Cable
5. Siemens S7-200 PLC, or others PLC with Modbus Support
6. Serial Port Module: TTL serial port to RS232 male
7. Power Plug Adapter DC 9V for Arduino

Hardware for WiFi applications on PLC and Android Phone


Hardware Connections for WiFi applications on PLC and Android Phone:

Hardware Connections for WiFi applications on PLC and Android Phone


Download Project File for Arduino, PLC and Android Phone:
1. Arduino libraries, click here
Copy-Paste folder: SimpleModbusMaster and EEPROMEx into folder C:\Program Files (x86)\Arduino\libraries
2. Arduino Project File, click here
3. Modbus PLC Ladder Programming, click here
4. Android Apps / APK File, click here

Install APK file to Android Phone, after installation is complete:
a. Connect to ESP8266 WiFi with SSID:program-plc.blogspot.com and Password:12345678
b. Click program LampOnOff on android phone
c. Edit IP Target to 192.168.4.1
d. Edit Port Target to 8888
e. Click Connect
f. Click Android Screen for Lamp ON/OFF and Change Lamp Num.

Android Apps for Lamp On Off


Link about Android Application:
http://program-plc.blogspot.com/2014/02/communication-between-android-computer.html http://program-plc.blogspot.com/2014/06/home-automation-with-raspberry-pi.html

Arduino Code for WiFi applications on PLC and Android Phone:
#include <SimpleModbusMaster.h>
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
//for more info program-plc.blogspot.com
#define Slave_Address 1
#define BaudRate 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2 
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS
};

Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
unsigned int CoilsRegs[1];

#include <EEPROMex.h>


#define AP_SSID       "program-plc.blogspot.com"
#define AP_PASSWORD   "12345678"
#define AP_CHL   1 
#define AP_ENC   2

int ID;
//Access Point IP=192.168.4.1
int IPport = 8888;
#define ESP8266_RST   13


void setup() {
  Serial.begin(9600);
  pinMode(ESP8266_RST,OUTPUT);
  digitalWrite(ESP8266_RST, HIGH);
  Serial2.begin(9600);
  //Serial2.begin(115200);
  Serial2.setTimeout(5000);  
  
  if(CommandWait("AT+RST","ready",5000)){ 
    delay(1000); 
    if(CommandWait("AT+CWMODE=2","no change",5000)==false)ESP8266_Hardware_Reset(1);    
    delay(1000);
    AT_CWSAP(AP_SSID,AP_PASSWORD,AP_CHL,AP_ENC);
    delay(1000);
    AT_CIPMUX(1);
    delay(1000);
    AT_CIPSERVER(1,IPport);
    delay(1000); 
    String IPme = AP_IP();
    Serial.println("ANDROID SETTING:");
    Serial.println("=========================");
    Serial.print("IP Target: ");
    Serial.println(IPme); 
    Serial.print("Port Target: ");  
    Serial.println(IPport); 
    Serial.println("=========================");    
  }else{         
      ESP8266_Hardware_Reset(30);
  }
  

  CoilsRegs[0] = EEPROM.readInt(0);  
  Serial.print("Coils Registers Value = "); 
  Serial.println(CoilsRegs[0]); 
  modbus_construct(packet1, Slave_Address, FORCE_MULTIPLE_COILS, 0, 16, CoilsRegs);
  modbus_configure(&Serial1, BaudRate, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS); }

void loop() {
  modbus_update();
  char Buffer[500];
  int IPDlen = ESP8266_Receive(Buffer);
  if(IPDlen > 0)
  {   
      //Lamp ON
      if(strncmp(Buffer, "ON", 2)==0){
  String num = String(Buffer[2])+String(Buffer[3]);
 int bitdata = num.toInt();       
        if(bitdata>=0 && bitdata<=15)bitWrite(CoilsRegs[0], bitdata, 1); 
        if(bitdata==16)CoilsRegs[0]=0xFFFF;       
      }
      
      //Lamp OFF
      if(strncmp(Buffer, "OFF", 3)==0){
  String num = String(Buffer[3])+String(Buffer[4]);
 int bitdata = num.toInt();       
        if(bitdata>=0 && bitdata<=15)bitWrite(CoilsRegs[0], bitdata, 0);
        if(bitdata==16)CoilsRegs[0]=0;     
      }

      AT_CIPSEND(ID,"OK");
      
      if(CoilsRegs[0] != EEPROM.readInt(0)) {
         EEPROM.writeInt(0,CoilsRegs[0]);
         Serial.print("Coils Registers Value = "); 
         Serial.println(CoilsRegs[0]);       
      }
  }
}


void ESP8266_Hardware_Reset(byte delays)
{
  Serial.println("ESP8266_Hardware_Reset");
  byte times = 0;
  while(times < delays){
    digitalWrite(ESP8266_RST, LOW); 
    delay(1000);
    times++;   
  }
  digitalWrite(ESP8266_RST, HIGH);
  asm volatile ("  jmp 0"); 
  while(1);  
} 

boolean CommandWait(String AT, char *Wait,int delays){
  Serial2.println(AT);
  Serial.print(AT);
  unsigned long starttime;
  starttime = millis();
  while (millis()-starttime<delays) {                            
     if(Serial2.find(Wait)==true)
        {
          Serial.println(" OK");
          return true;
        }
    }
    Serial.println(" NOT OK");
    return false;
}


boolean AT_CWSAP(String ssid , String pwd , byte chl , byte ecn)
{
    Serial2.print("AT+CWSAP=");  
    Serial2.print("\"");     //"ssid"
    Serial2.print(ssid);
    Serial2.print("\"");

    Serial2.print(",");

    Serial2.print("\"");      //"pwd"
    Serial2.print(pwd);
    Serial2.print("\"");

    Serial2.print(",");
    Serial2.print(String(chl));

    Serial2.print(",");
    Serial2.println(String(ecn));
    Serial.print("AT+CWSAP");
 unsigned long start;
 start = millis();
    while (millis()-start<3000) {                            
        if(Serial2.find("OK")==true )
        {
          Serial.println(" OK");
           return true;
        }
     }
  Serial.println(" NOT OK");
  return false;

}



String AP_IP(void)
{
    String data;
    unsigned long start;
    for(int a=0; a<3;a++)
    {
 Serial2.println("AT+CIFSR"); 
        Serial.println("IP CHECK");
        delay(2000);
 start = millis();
 while (millis()-start<3000) {
     while(Serial2.available()>0)
     {
     char a =Serial2.read();
     data=data+a;
     }
     if (data.indexOf("AT+CIFSR")!=-1)
     {
         break;
     }
 }
 if(data.indexOf(".") != -1)
 {
  break;
 }
 data = "";
    }
    char head[4] = {0x0D,0x0A};   
    char tail[7] = {0x0D,0x0D,0x0A};        
    data.replace("AT+CIFSR","");
    data.replace(tail,"");
    data.replace(head,"");
    data.replace("OK","");
    return data;
}

boolean AT_CIPMUX(boolean a)
{
 Serial2.print("AT+CIPMUX=");
 Serial2.println(a);
        Serial.print("AT+CIPMUX");
 unsigned long start;
 start = millis();
 while (millis()-start<3000) {                            
        if(Serial2.find("OK")==true )
        {
          Serial.println(" OK");
           return true;
        }
     }
  Serial.println(" NOT OK");
  return false;
}


boolean AT_CIPSERVER(byte mode, int port)
{
    Serial2.print("AT+CIPSERVER=");  
    Serial2.print(String(mode));
    Serial2.print(",");
    Serial2.println(String(port));
    Serial.print("AT+CIPSERVER");
    String data;
    unsigned long start;
 start = millis();
 boolean found = false;
 while (millis()-start<3000) {
     if(Serial2.available()>0)
     {
     char a =Serial2.read();
     data=data+a;
     }
     if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1)
     {
        Serial.println(" OK");
 found = true;
        break;
     }
  }
  return found;
}



int ESP8266_Receive(char *Buffer)
{
 String data = "";
 if (Serial2.available()>0)
 {
  
  unsigned long start;
  start = millis();
  char c0 = Serial2.read();
  if (c0 == '+')
  {
   
   while (millis()-start<5000) 
   {
    if (Serial2.available()>0)
    {
     char c = Serial2.read();
     data += c;
    }
    if (data.indexOf("\nOK")!=-1)
    {
     break;
    }
   }

   int sLen = strlen(data.c_str());
   int i,j;
   for (i = 0; i <= sLen; i++)
   {
    if (data[i] == ':')
    {
     break;
    }
    
   }
   boolean found = false;
   for (j = 4; j <= i; j++)
   {
    if (data[j] == ',')
    {
     found = true;
     break;
    }
    
   }
   int iSize;
   if(found ==true)
   {
   String _id = data.substring(4, j);
   ID = _id.toInt();
   String _size = data.substring(j+1, i);
   iSize = _size.toInt();
   String str = data.substring(i+1, i+1+iSize);
   strcpy(Buffer, str.c_str());      
   }
   else
   {   
   String _size = data.substring(4, i);
   iSize = _size.toInt();
   String str = data.substring(i+1, i+1+iSize);
   strcpy(Buffer, str.c_str());
   }
   return iSize;
  }
 }
 
 return 0;
}


void AT_CIPCLOSE(byte id)
{
    Serial2.print("AT+CIPCLOSE=");
    Serial2.println(String(id));
    Serial.println("AT+CIPCLOSE");
    String data;
    unsigned long start;
 start = millis();
 while (millis()-start<3000) {
     if(Serial2.available()>0)
     {
     char a =Serial2.read();
     data=data+a;
     }
     if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1)
     {
       Serial.println(" OK");  
       break;
     }
  }

}


boolean AT_CIPSEND(byte id, String str)
{
    Serial2.print("AT+CIPSEND=");
    Serial2.print(String(id));
    Serial2.print(",");
    Serial2.println(str.length());
    unsigned long start;
 start = millis();
 bool found;
 while (millis()-start<5000) {                          
        if(Serial2.find(">")==true )
        {
     found = true;
            break;
        }
     }
  if(found)
  Serial2.print(str);
 else
 {
  AT_CIPCLOSE(id);
  return false;
 }


    String data;
    start = millis();
 while (millis()-start<5000) {
     if(Serial2.available()>0)
     {
     char a =Serial2.read();
     data=data+a;
     }
     if (data.indexOf("SEND OK")!=-1)
     {
         return true;
     }
  }
  return false;
}




Labels:








Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner