Sending an array from one Nodemcu to another Nodemcu? - serial-port

I am working on a gesture control car project and I find everyone on the internet using RF modules or Bluetooth modules to do it, I had two NodeMCU lying around so I thought why not use them and bring a change. But I am finding difficulty in sending the accelerometer values from one Nodemcu to another, For connecting the two Nodemcu I took help from here
https://www.instructables.com/id/WiFi-Communication-Between-Two-ESP8266-Based-MCU-T/
So instead of a string (which they use), I tried sending an array of two variables, but I fail to send/receive it from the transmitter to the receiver. I have spent a lot of time on it, but I think I need help.
I now know ( just this morning ) that Nodemcu has only one analog pin, I have ordered for a 74HC4051 IC. So I haven't changed the code for that, so please bear that in mind. Also, I have used the xAxis only now just to test if the data is being sent, but it is not.
If there is any other easier way of sending the two variable values, please do suggest.
This is the actual project, I asked the author for help, but no reply yet.
https://www.youtube.com/watch?v=svJwmjplm4c
I have given below the edited code that I have now
Transmitter code
#include <SPI.h>
#include <ESP8266WiFi.h>
int xAxis = A0;
int yAxis = D2;
int data[1];
int x;
int ledPin = D8;
char ssid[] = "********"; // SSID
char pass[] = "********"; // password I removed them while posting the question
WiFiServer server(80);
IPAddress ip(192, 168,1, 80); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
server.begin(); // starts the server
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // some parameters from the network
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
pinMode(ledPin, OUTPUT);
}
void loop () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
client.flush();
Serial.println("Data send");
x= analogRead(xAxis);
data[0]= analogRead(xAxis);
client.println(data[x,yAxis]);
Serial.println(x);
Serial.println(data[0]); //This appears on the screen only if i have the line data[0]= analogRead(xAxis);
digitalWrite(ledPin, HIGH);
}
client.stop(); // tarminates the connection with the client
}delay(2000);
}
Receiver Code
#include <SPI.h>
#include <ESP8266WiFi.h>
int ENA = D1;
int ENB = D2;
int MotorA1 = D3;
int MotorA2 = D4;
int MotorB1 = D5;
int MotorB2 = D6;
int data[1];
int xAxis;
int yAxis;
int ledPin = D8;
char ssid[] = "*****";
char pass[] = "*****";
unsigned long askTimer = 0;
IPAddress server(192,168,1,80); // the fix IP address of the server
WiFiClient client;
void setup() {
Serial.begin(115200); // only for debug
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
/* Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());*/
pinMode(ledPin, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(MotorA1, OUTPUT);
pinMode(MotorA2, OUTPUT);
pinMode(MotorB1, OUTPUT);
pinMode(MotorB2, OUTPUT);
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
void loop () {
client.connect(server, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
data[xAxis,yAxis] = client.read(); // receives the answer from the sever
Serial.println(" Received from Server ");
client.flush();
digitalWrite(ledPin, HIGH);
Serial.println(xAxis);
if(yAxis > 400) {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(yAxis < 320) {
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
} else if(xAxis < 320){
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(xAxis > 400){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
delay(2000); // client will trigger the communication after two seconds
}

Related

Issues regarding IR sensor controlled car

I'm here regarding the issue I'm having building my project which is IR sensor controlled car with a LCD screen. I have built only 2 projects before this one that I have coded and designed their circuits, hence, I'm fairly new to all this. I expected this car to go forward, turn left & right, and to stop. I first used the code to identify the key code of my IR remote for the buttons I want to use to control my project, then I wrote the code given below. But, whenever I use the buttons I have programmed, the project respond only once to the IR remote and then freezes and continues on. Suppose, If I click the button which I have programmed it to go forward, it starts going forward, but then it stops responding to other buttons. I've tried using higher voltage and current batter but that doesn't seem to help.
Here is the code:
// Setting up LCD Display Here.
#include<LiquidCrystal.h>
int RS = 13;
int E = 12;
int D4 = 11;
int D5 = 10;
int D6 = 6;
int D7 = 2;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);
// Setting up IR reciver sensor here.
#include<IRremote.h>
int IR_Reciver_Pin = A5;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;
// Setting up DC motor pins.
/* Motor A connections */
int enA = 9;
int in1 = 8;
int in2 = 7;
/* Motor B connections */
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup() {
// Initiating LCD display here.
lcd.begin(16,2);
lcd.print("Welcome!");
// Initiating IR reciver sensor here.
irrecv.enableIRIn();
// Initiating Serial Monitor.
Serial.begin(9600);
// Initiating DC motors.
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Turning motors off - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
// Reading IR remote value.
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
irrecv.resume();
}
// Code for providing 5V to L293D H-Brigde.
analogWrite(enA, 255);
analogWrite(enB, 255);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
This is the circuit diagram.
At first glance, I would say that the problem is that your if statments in loop should be nested inside : if(irrecv.decode(&results)){}
So, your loop should look something like this:
void loop() {
analogWrite(enA, 255);
analogWrite(enB, 255);
while(!(irrecv.decode(&results))); //It waits until no button is pressed
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
irrecv.resume();
}
}

Aurdino coding to control a motor

//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
If we input a in the serial monitor the motor should rotate in one direction and if we input s the motor should rotate in another direction but it is not happening the motor is idle but i get the output like this:
a
one way
s
other way
There is no problem with hardware connections.
Could you please help me with this.Thanks in advance
According to the comments in your code, you are conecting arduino-pin-6 to the l293-pin-6, and arduino-pin-9 to the l293-pin-3.
According to this datasheet, the control pins in L293 are: 2, 7, 10, 15. So, I believe you are connecting it wrong. Also the pulses are being done in the wrong order (from HIGH to LOW and so on).
This should be the correct code (please look at the comments in the code):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
Please note that I added the motorenablepin that was missing. It must be conected to the l293-pin-1.
Also, since the LOW and LOW states are common to the code, you can simplify it as I did.
There was small mistake in the logic:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);

A simple problem with arduino UNO about optimizing code

It this a really simple code about turning on an off some LEDs, but I want it not to be so repetititve
I've tried to make a loop, but i couldn't get it, I did my best, but I'm really bad at this :(( pls need someone's jelp
`
#define LED 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
void setup()
{
pinMode(LED, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED4, HIGH);
delay(125);
digitalWrite(LED5, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED3, LOW);
delay(250);
digitalWrite(LED4, LOW);
delay(125);
digitalWrite(LED5, LOW);
delay(500);
}`
I'll rewrite what Oleg Mazurov said in the comment:
#define NUMBER_OF_LEDS 5
static const uint8_t a_led[NUMBER_OF_LEDS] = {2, 3, 4, 5, 6};
static const uint16_t a_delay[NUMBER_OF_LEDS] = {1000, 500, 250, 125, 500};
void setup() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
pinMode(a_led[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
digitalWrite(a_led[i], !digitalRead(a_led[i]));
delay(a_delay[i]);
}
}

Go to link using if condition using Arduino IDE

I'm just trying to get myself here "http://192.168.1.103:30000/?k=23&v=capture" when an if condition meet its requirement.
#include <ESP8266WiFi.h>
// I purposely don't include the ssid and ssid1 here
WiFiServer server(80);
void setup() {
pinMode(1, INPUT);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
String link = "http://192.168.1.103:30000/?k=23&v=capture";
WiFiClient client = server.available();
if (!client) {
return;
}
int var = digitalRead(1);
if (var == HIGH) {
client.print(link);
}
Let's say:
I already run Chrome.
How can that link above be called without even typing it on Chrome? I want to connect to it automatically.
Any method you could teach? I got the feeling this code itself is wrong.
Thanks.
-- EDIT --
NEW CODE FOR UNO
//language c++
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Scanning address
LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
Servo Servo1;
int servopin = 9;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
// initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
lcd.setCursor(3, 0); //Start at character 4 on line 0
lcd.print("WAITING...");
pinMode(12, OUTPUT); // pin LaserLight
pinMode(11, INPUT); // pin LaserDetector
pinMode(10, INPUT); // pin PIR
pinMode(9, OUTPUT); // pin Servo
pinMode(8, OUTPUT); // MCU PIN GPIO2
Servo1.attach(servopin);
}
void loop() {
digitalWrite(12, HIGH);
boolean inputlaser = digitalRead(11);
boolean inputpir = digitalRead(10);
Serial.println(inputlaser);
Serial.println(inputpir);
if (inputlaser < 1) {
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("camera on");
lcd.setCursor(0, 1);
lcd.print("robber!");
delay(5000);
Servo1.write(180);
} else if (inputpir > 0) {
Servo1.write(180);
lcd.setCursor(0, 0);
lcd.print("robber inside!");
lcd.setCursor(0, 1);
lcd.print("HELP ROBBER!");
delay(500);
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("standby...");
delay(500);
}
}
NEW CODE FOR MCU
#include <ESP8266WiFi.h>
char server[] = "192.168.1.103";
WiFiClient client;
void setup() {
pinMode(4, INPUT);
digitalWrite(4, LOW);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
boolean var = digitalRead(4);
if (var == HIGH) {
client.connect(server, 30000);
Serial.println("connected");
// Make your API request:
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println(digitalRead(4));
}
First understand a fact that you doesn't need google chrome for requesting a website.
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
What you do in the above line is that you request for /?k=23&v=capture addressed content in the ip address 192.168.1.103, Actully this is what you do when you use a google chrome. On PC you require a google chrome (or any other browser) for web site request because its difficult to request using commands each time (Think of requesting for a single page using a hell of http commands instead of using chrome, Ohh that's mess). So understand chrome isn't needed to access a site.

Arduino PINs not behaving equally

I have burned this code on my arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>
using namespace std;
#define BUFFSIZE 16
#define PIN0 0
#define PIN1 1
#define PIN2 2
#define PIN3 3
#define PIN4 4
#define PIN5 5
#define PIN6 6
#define PIN7 7
#define PIN8 8
#define PIN9 9
#define PIN10 10
#define PIN11 11
#define PIN12 12
#define PIN13 13
// For mac address please view the Ethernet shield.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD5};
IPAddress server(192, 168, 0, 61); // IP address of RAAS server
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 62);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 8 is selected for RAAS):
EthernetClient client;
String GetNextCommand()
{
int count,i;
int temp;
String command;
Serial.println("Waiting for next Command.");
while((count = client.available()) < 1 );
for(i = 0; i < count ; i++)
command += (char) client.read();
command.replace("\r","");
command.replace("\n","");
Serial.println(command);
return command;
}
bool ConnectServer() {
int resCount,i;
String response;
Serial.println("Trying to connect...");
// if you get a connection, report back via serial:
if (client.connect(server, 8)) {
Serial.println("connected");
// Make a Handshake request:
client.println("EHLO");
response = GetNextCommand();
if (response == "EHLO" ) {
Serial.println("Server Response OKAY");
return true;
}
} else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
return false;
}
void InitializeBoard() {
pinMode(PIN1, OUTPUT);
pinMode(PIN2, OUTPUT);
pinMode(PIN3, OUTPUT);
pinMode(PIN4, OUTPUT);
pinMode(PIN5, OUTPUT);
pinMode(PIN6, OUTPUT);
pinMode(PIN7, OUTPUT);
pinMode(PIN8, OUTPUT);
pinMode(PIN9, OUTPUT);
pinMode(PIN10, OUTPUT);
pinMode(PIN11, OUTPUT);
pinMode(PIN12, OUTPUT);
pinMode(PIN13, OUTPUT);
digitalWrite(PIN1, LOW);
digitalWrite(PIN2, LOW);
digitalWrite(PIN3, LOW);
digitalWrite(PIN4, LOW);
digitalWrite(PIN5, LOW);
digitalWrite(PIN6, LOW);
digitalWrite(PIN7, LOW);
digitalWrite(PIN8, LOW);
digitalWrite(PIN9, LOW);
digitalWrite(PIN10, LOW);
digitalWrite(PIN11, LOW);
digitalWrite(PIN12, LOW);
digitalWrite(PIN13, LOW);
}
void ParseCommand(String str) {
int pinNum;
int pinState = LOW;
String switchNo,operation;
int temp;
temp=0;
temp = str.indexOf(':',temp);
switchNo = str.substring(0,temp);
operation = str.substring(temp+1);//,str.length()-temp-2);
Serial.println("Port: " + switchNo);
Serial.println("Operation: " + operation);
if(operation == "OFF;")
pinState = LOW;
else if(operation == "ON;")
pinState = HIGH;
else Serial.println("Invalid Command from server!");
pinNum = str.toInt();
Serial.print("Setting ");
Serial.print( pinNum);
Serial.print( " to ");
Serial.println( pinState);
switch (pinNum){
case PIN1:
digitalWrite(PIN1, pinState);
break;
case PIN2:
digitalWrite(PIN2, pinState);
break;
case PIN3:
digitalWrite(PIN3, pinState);
break;
case PIN4:
digitalWrite(PIN4, pinState);
break;
case PIN5:
digitalWrite(PIN5, pinState);
break;
case PIN6:
digitalWrite(PIN6, pinState);
break;
case PIN7:
digitalWrite(PIN7, pinState);
break;
case PIN8:
digitalWrite(PIN8, pinState);
break;
case PIN9:
digitalWrite(PIN9, pinState);
break;
case PIN10:
digitalWrite(PIN10, pinState);
break;
case PIN11:
digitalWrite(PIN11, pinState);
break;
case PIN12:
digitalWrite(PIN12, pinState);
break;
case PIN13:
digitalWrite(PIN13, pinState);
break;
default:
Serial.println("Invalid Pin Address!");
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
} else {
Serial.println("Bound on given Mac");
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
//Keep trying to connect to the server until the server response okay!
while (!ConnectServer());
//While Server is connected, keep listening for incoming commands from server.
while (client.connected()) {
String command;
command = GetNextCommand();
ParseCommand(command);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Server Disconnected.");
client.stop();
}
}
Now I have to connect Relays on all the pins from 1-13. But pins are not behaving properly. When I send command 13:ON; or 13:OFF; pins on my Ethernet shield does not change its state in each case. But when I send 03:ON; or 03:OFF; it changes its state exactly w.r.t the command. Similarly some pins are responding like pin 3 and other pins are not (like pin13). Is this some thing to do with the code?
The issue is the Ethernet shield uses pins 13, 12, 11, as SPI . Also 10 and 4, to select SD or ethernet.
pinNum = str.toInt();
And then you have a switch statement to mach pinNum with the correct operation.
I would like to see how the value of str looks before "ParseCommand()" function. Can you show me what prints in the "GetNextCommand()"

Resources