Post topics, source code that relate to the Arduino Platform

User avatar
By bidyutdas
#31220 I have programmed arduino board for relay operation, which accept command from Serial TCP client mobile app(Available in play store) or any serial command connected to module IP. This app or any other external serial send command for particular relay load that are defined in microcontroller program. Module accept I/P command process it & gives corresponding O/P.

Problems:
a) As it support serial command, I am sending serial command through TCP client app which is send command for e,g case N1, which should trigger CH1(pin 2) in arduino, sometime it responding well trigger pin2.correspondingly it turns off when i send case F command.
But Sometime it trigger N5, or jump to case F , trigger F3.
These serial command behavior i watched through Arduino serial window & tested out couple of time but unable to rectify it. Also used logic shift ckt...both in TX & RX side but didn't workout.




Arduino codes here:
#include <SoftwareSerial.h>
#define DST_IP "192.168.4.1"
#define DEBUG true
#define CH1 2
#define CH2 3
#define CH3 4
#define CH4 5
#define CH5 5
#define CH6 7
#define CH7 8
#define CH8 9

char temp[20];
int o;
SoftwareSerial esp8266(10, 11); // RX, TX
void setup()
{
Serial.begin(115200);
esp8266.begin(115200);
pinMode(CH1,OUTPUT);
pinMode(CH2,OUTPUT);
pinMode(CH3,OUTPUT);
pinMode(CH4,OUTPUT);
pinMode(CH5,OUTPUT);
pinMode(CH6,OUTPUT);
pinMode(CH7,OUTPUT);
pinMode(CH8,OUTPUT);


Serial.println("ESP8266 startup wait..");

sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CWSAP=\"bidyut\",\"4142430607080945\",3,0\r\n",3000,DEBUG);//setting up AP
delay(10000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,9999\r\n",1000,DEBUG); // turn on server on port 9999

delay(1000);
Serial.println("Ready");
}
void loop()
{
o=0;
while(!esp8266.available());
while (esp8266.available())
{
Serial.write(esp8266.read());
temp[o++] = esp8266.read();
delay(15);

}
Serial.println(temp);
temp[o] = '\0';
for(o=0;temp[o] !=':';o++);
o++;
char v = temp[o];
switch (v)
{
case 'N':
o++;
if(temp[o] == '1')
{
Serial.println("ON one...");
digitalWrite(CH1,HIGH);
}
else if(temp[o] == '2')
{
Serial.println("ON two...");
digitalWrite(CH2,HIGH);
}
else if(temp[o] == '3')
{
Serial.println("ON three...");
digitalWrite(CH3,HIGH);
}
else if(temp[o] == '4')
{
Serial.println("ON four...");
digitalWrite(CH4,HIGH);
}
else if(temp[o] == '5')
{
Serial.println("ON five...");
digitalWrite(CH5,HIGH);
}
else if(temp[o] == '6')
{
Serial.println("ON six...");
digitalWrite(CH6,HIGH);
}
else if(temp[o] == '7')
{
Serial.println("ON seven...");
digitalWrite(CH7,HIGH);
}
else if(temp[o] == '8')
{
Serial.println("ON eight...");
digitalWrite(CH8,HIGH);
}

else if(temp[o] == 'A')
{
Serial.println("ON ALL...");
digitalWrite(CH1,HIGH);
digitalWrite(CH2,HIGH);
digitalWrite(CH3,HIGH);
digitalWrite(CH4,HIGH);
digitalWrite(CH5,HIGH);
digitalWrite(CH6,HIGH);
digitalWrite(CH7,HIGH);
digitalWrite(CH8,HIGH);
}

break;
case 'F':
o++;
if(temp[o] == '1')
{
Serial.println("OFF one...");
digitalWrite(CH1,LOW);
}
else if(temp[o] == '2')
{
Serial.println("OFF two...");
digitalWrite(CH2,LOW);
}
else if(temp[o] == '3')
{
Serial.println("OFF three...");
digitalWrite(CH3,LOW);
}
else if(temp[o] == '4')
{
Serial.println("OFF four...");
digitalWrite(CH4,LOW);
}
else if(temp[o] == '5')
{
Serial.println("OFF five...");
digitalWrite(CH5,LOW);
}
else if(temp[o] == '6')
{
Serial.println("OFF six...");
digitalWrite(CH6,LOW);
}
else if(temp[o] == '7')
{
Serial.println("OFF seven...");
digitalWrite(CH7,LOW);
}
else if(temp[o] == '8')
{
Serial.println("OFF eight...");
digitalWrite(CH8,LOW);
}
else if(temp[o] == 'A')
{
Serial.println("ON ALL...");
digitalWrite(CH1,LOW);
digitalWrite(CH2,LOW);
digitalWrite(CH3,LOW);
digitalWrite(CH4,LOW);
digitalWrite(CH5,LOW);
digitalWrite(CH6,LOW);
digitalWrite(CH7,LOW);
digitalWrite(CH8,LOW);
}
break;
}

}

String sendCommand(String command, const int timeout, boolean debug)
{
String response = "";

esp8266.print(command); // send the read character to the esp8266

long int time = millis();

while( (time+timeout) > millis())
{
while(esp8266.available())
{

// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}

if(debug)
{
Serial.print(response);
}

return response;
}
User avatar
By Menox
#42447 Well I see a couple problems here:

while(!esp8266.available());
You need a delay here so the background Wifi processes can do their work.

You're reading one character off of the stream and storing the next one into temp[0++]. Is this intentional?
Serial.write(esp8266.read());
temp[o++] = esp8266.read();

Also there is no bounds checking for temp[]. There is also no memset() or anything to clear out temp[] before reading again. This could leave old reads in your buffer.