-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arduino-client-ethernet-python_sendind_ANALOG_DIGI.ino
115 lines (95 loc) · 3.32 KB
/
Arduino-client-ethernet-python_sendind_ANALOG_DIGI.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <SPI.h>
#include <Ethernet.h>
int analogA0 = A0, analogA1 = A1, analogA2 = A2, analogA3 = A3;
int digi2 = 2, digi3 = 3, digi4 = 4, digi5 = 5;
String val = ""; // variable to store the value read
int con = 0;// connection status
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0xFE, 0xAC
};
IPAddress ip(192, 168, 1, 21);
// Enter the IP address of the server you're connecting to:
IPAddress server(192, 168, 1, 2);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use port 10002):
EthernetClient client;
void setup() {
pinMode(7,OUTPUT);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
// while (Ethernet.linkStatus() == LinkOFF) {
// Serial.println("Ethernet cable is not connected.");
// delay(500);
// }
if (Ethernet.linkStatus() == Unknown) {
// Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
}
else if (Ethernet.linkStatus() == LinkON) {
Serial.println("Link status: On");
}
else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Link status: Off");
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 65432)) {
Serial.println("connected");
digitalWrite(7, HIGH);
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
analogA0 = analogRead(A0); // read the input pin
analogA1 = analogRead(A1); // read the input pin
analogA2 = analogRead(A2); // read the input pin
analogA3 = analogRead(A3); // read the input pin
digi2 = digitalRead(2); // read the input pin
digi3 = digitalRead(3); // read the input pin
digi4 = digitalRead(4); // read the input pin
digi5 = digitalRead(5); // read the input pin
val = "A0:I:"+String(analogA0)+"-A1:I:"+String(analogA1)+"-A2:I:"+String(analogA2)+"-A3:I:"+String(analogA3)+"-D2:I:"+String(digi2)+"-D3:I:"+String(digi3)+"-D4:I:"+String(digi4)+"-D5:I:"+String(digi5);
Serial.println(val); // debug value
if (client.connected()) {
client.println(val);
}
//Serial.println(val);
if (!client.connected()) {
digitalWrite(7, LOW);
if (client.connect(server, 65432)) {
Serial.println("connected");
digitalWrite(7, HIGH);
con = 0;
} else {
if(con==0){
Serial.println("Waiting connection...");
con = 1;
}
delay(1);
}
}
delay(1000);
}