Hello,
Have an Arduino UNO with ethernet, how do I do, so that it is possible control 4x relays?
It was necessary to install any type of server software on the Mac ??
Get this error when upploding it to UNO
Arduino:1.7.6 (Mac OS X), Kort:"Arduino Uno"
unoMega.ino:126:18: error: expected '}' before ':' token
unoMega.ino:126:18: error: expected ',' or ';' before ':' token
unoMega.ino:126:34: error: expected declaration before '}' token
Fel vid kompilering.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
And this is what i typed:
//
// UNO/MEGA w ethernet/WIFI sketch by karl wachs August 28 2015 v 1.2
// this is used together with INDIGO plugin
// command to be send: curl 192.168.1.155"?--command-string--?"
// command string has the form command:pin=value& eg: command:pin=value1,value2&command:pin&command:pin=value1&
// commands implemented:
// rd:A1&rd:D3& ==> will read analog pins 1 and 3
// wr:D1=0&wr:D4=0& ==> set digital pins 1 to 0 and digital pin4 to 1
// aw:D3=55& ==> analogWrite(3,55) will set digital pin3 to 55
// pg:D5=O&pgD4=I& ==> will reprogram digital pin 5 to output and digital pin 4 to input
// mU:D5=100& ==> will reprogram digital pin 5 to do: 500msec up then down
// mD:D5=5555& ==> will reprogram digital pin 5 to do: 5555 msec down then up
// pU:D5=100,5555& ==> will reprogram digital pin 5 to do: 100msec up, then 5555 msec down and repeat
// pD:D5=2000,3000& ==> will reprogram digital pin 5 to do: 3000 msec down and 2000 msec up and repeat
// rd:S1& ==> will read contents of variable S1
// aw:S1=abc -=; def& ==> will write abc -=; def to varibale S1
// aw:F0=123.7& ==> will write 123.7 to varibale F0
// aw:N4=123& ==> will write 123 to varibale N4
//
// pins can be D0..Dxx A0.. Axx F0..F4, N0..N4, S0..S4
// Dx are digital i/o pins they can be read ==> 0/1; digital write 0/1 or analogWrite 0..255
// Ax are anlog input pins delivery values 0..1023
// Fx are float variables can be written to and read from eg 1.23 or -45.3
// Nx are integer variables can be written to and read from eg 0, 100 ..
// Sx are String variables can be written to and read from eg kajshdlkjh=-: but NOT a & that will end the string
//
// here a more complex example:
// command curl
http://192.168.1.155/"?rd:a1&wr:d5=1&pg:d7=O&wr:d7=1&?" note the "
// you can combine multiple commands, each command must end with a &
// in python it is
// ret= urllib2.urlopen("http://ipnumber/?rd:a1&wr:d5=1&pg:d7=O&wr:d7=1&?").read()
// would send ret =">>rd:A1=311&wr:D5=1&pg:D7=O&wr:D7=0&wr:D7=1&"
//
// excution times: for mega reading 70 pins ... use this for time out setting, default is 5 seconds
// debug on ethernet: 0.4 secs
// debug off 0.2 secs
// debug on wifi : 0.9 secs
// debug off : 0.6 secs
// UNO reading 15 pins
// debug off ethernet: 0.05 secs
// debug on : 0.2 secs
// debug off wifi : 0.3
// debug on : 0.5 secs
//
// this needs to be set : 1. #define DEBUG only for testing
// 2. #define ETHERNET or WIFI not both
// 3. #define UNO or MEGA not both --- in the tools menue in ARDUINO IDE you have to select the proper board also
// for ethernet you need to set the IP number and the MAC number, WIFI gets it from the router, so you have to figure out the IP number either in debug mode or on the router
//
// byte mac[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x53 }; // pick one thats not on your network
// IPAddress ip(192,168,1, 183);
// some of the pins are diabled / enabled automatically: pin D10,11,12,13 are used to communicate to the ethernet/wifi boards
//
// pins d0 d1 are used for usb communication, so in debug mode they are diabled, debug off they can used as all other pins
//
////////////////////////////////////////////////////////////////////////////////////
// enable serial monitor for debugging, if in production do not send data to serial port
#define DEBUG
//
// select which board you use
//#define MEGA
#define UNO
//
//
#include <SPI.h> // needed for WIFI and ethernet, not for ESP
//select which network card:
#define ETHERNET
#include <Ethernet.h>
//
//#define WIFI
//#include <WiFi.h>
//
//
//
//
char cmd[20];
char command[3];
int pin = 0 ;
int value1 = -1;
int value2 = -1;
String valueS="";
int cmdL = 0;
char c=' ';
char mode = 'I';
int kk=1;
int eIndex=-1;
int kIndex=-1;
int cIndex=-1;
// free values to read from and write to
String S[10]={"","","","","","","","","",""};
String out ; // this can get: 68* 12 = 800+ for mega and 14*12 = 170 for uno
#if defined(MEGA)
int activeDPins =54;
int activeAPins =16;
char dConfig[60] ;
char pulseCMD[60] ;
unsigned long pulseEND[60] ;
unsigned long pulseV1[60] ;
unsigned long pulseV2[60] ;
bool doNotUsePin[60];
#endif
#if defined(UNO)
//char buf1[115] ={'\0'}; // this is for input buffer; 16 pins, 8 char per pin max = 112 should be enough for uno!!
int activeDPins =14;
int activeAPins =6;
char dConfig[14] ;
char pulseCMD[14] ;
unsigned long pulseEND[14] ;
unsigned long pulseV1[14] ;
unsigned long pulseV2[14] ;
bool doNotUsePin[14];
#endif
String configured ="no";
//////////////////////////////// configure network here
#if defined(ETHERNET)
byte mac[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x53 }; // pick one thats not on your network
IPAddress ip(192,168,0, 185);
EthernetServer server(80);
#endif
//#if defined(WIFI)
//char ssid[] = "xxxx"; // your network SSID (name)
//char pass[] = "yyyy"; // your network password
//int keyIndex = 0; // your network key Index number (needed only for WEP)
//int status = WL_IDLE_STATUS;
//WiFiServer server(80);
#endif
//////////////////////////////// configure network here END
void setup() {
for (int i =0;i< activeDPins;i++){
dConfig[i] ='0';
pulseCMD[i] ='0';
pulseEND[i] =0;
pulseV1[i] =0;
pulseV2[i] =0;
doNotUsePin[i]=false;
}
#if defined(DEBUG)
doNotUsePin[0] = true;
doNotUsePin[1] = true;
Serial.begin(57600);
Serial.print("server starting");
#endif
// start the Ethernet connection and the server:
#if defined(ETHERNET)
doNotUsePin[10] = true;
doNotUsePin[11] = true;
doNotUsePin[12] = true;
doNotUsePin[13] = true;
Ethernet.begin(mac, ip);
server.begin();
delay(1000);
// Open serial communications and wait for port to open:
#if defined(DEBUG)
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
#endif
#endif // ETHERNET
#if defined(WIFI)
// check for the presence of the shield:
doNotUsePin[10] = true;
doNotUsePin[11] = true;
doNotUsePin[12] = true;
doNotUsePin[13] = true;
if (WiFi.status() == WL_NO_SHIELD) {
#if defined(DEBUG)
Serial.println("WiFi shield not present");
#endif
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
#if defined(DEBUG)
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
#endif
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
printWifiStatus();
#endif // WIFI
}
void doMyStuff(){
// examples of what can be done
//S[0]= "Temperature= "+String(analogRead(A0)/100.)+"C"; // do a read of variable S0
//S[1]= "Humidity: 50%"; // do a read of variable S0
//S[2]= analogRead(A0); // do a read of variable S0
//if (S[5]== "switch D5 on" ){ digitalWrite(5,1) ;S[5]="switched D5 on" ;} // do a write to variable S5
//if (S[5]== "switch D5 off"){ digitalWrite(5,0) ;S[5]="switched D5 off";} // do a write to variable S5
}
void loop(){
delay(1);
// here the stuff that should be done at every loop besides listening to the network commands
SpecialOutputs();
delay(1);
// put your code here use and fill the N and F vaiables to communicate your struff eg temp to the plugin.
//doMyStuff();
////////////////////////
// listen for incoming clients
#if defined(WIFI)
WiFiClient client = server.available();
#endif
#if defined(ETHERNET)
EthernetClient client = server.available();
#endif
if (client) {
#if defined(DEBUG)
Serial.println("new client");
#endif
char c4 =' '; char c3=' ' ; char c2=' '; char c1=' '; c=' ';int GETfound =0;
while (client.connected()) {
if (client.available()) {
c4=c3;c3=c2;c2=c1;c1=c;
c = client.read();
#if defined(DEBUG)
Serial.write(c);
#endif
if (GETfound ==0){ if ( c4=='G' and c3=='E' and c2=='T' and c1 ==' ' and c=='/'){ // "GET /"
#if defined(DEBUG)
Serial.println("getfound =1");
#endif
GETfound =1; c = client.read();// skip "?"
#if defined(DEBUG)
Serial.write(c);
#endif
cmdL=-1; c = client.read(); out =">>";
#if defined(DEBUG)
Serial.write(c);
#endif
} // skip "?" and read first char (avoid another loop)
}
if (GETfound ==1) {
if (c != '?' and out !=">>notConfigured" ) { // skip if not configured, otherwise read until ending "?"
cmdL+=1;
cmd[cmdL]=c;
if (c==':' and kIndex ==-1){kIndex=cmdL;} // use the first occurence
if (c=='=' and eIndex ==-1){eIndex=cmdL;}
if (c==',' and cIndex ==-1){cIndex=cmdL;}
if (c=='&'){
cmd[cmdL+1]= '\0';
parseCMD(); doNextCMD();
cmdL=-1;cIndex=-1,eIndex=-1;kIndex=-1;}
}
else{ GETfound=2;
#if defined(DEBUG)
Serial.println(" GETfound =2" );
#endif
}
}
}
if (GETfound ==2) { // here we send the header and the composed data
GETfound=3;
client.println("HTTP/1.1 200 OK");
client.print("Content-Length: "); client.println(out.length());
client.println("Content-Type: text/html");
client.println();
client.print(out);
client.stop();
delay(5); // 10 msecs
#if defined(DEBUG)
Serial.println(out.length());
Serial.println(out);
Serial.println("GETFOUND: 3");
#endif
}
}
//yield();// give ESP time to do its background stuff
#if defined(DEBUG)
Serial.println("client disconnected");
#endif
}
}
void doNextCMD(){
if ( (configured == "no") and (strcmp(command,"pg") !=0 ) ) {
out=">>notConfigured";
#if defined(DEBUG)
Serial.println(">>notConfigured");
#endif
return;
}
if ( strcmp(command,"st") ==0 and configured =="yes" ) { out=">>Configured"; return; }
if ( strcmp(command,"st") ==0 and configured =="no" ) { out=">>notConfigured"; return; }
if (doNotUsePin[pin] and cmd[3]=='D' ) { out+=command;out+=":D";out+=String(pin);out+="=notUseable&"; return; }
if ( cmd[3]=='I' and pin >=activeAPins ) { return; }
if ( cmd[3]=='D' and pin >=activeDPins ) { return; }
if ( cmd[3]=='S' and pin >9 ) { return; }
configured="yes";
if ( strcmp(command,"cf") ==0 or strcmp(command,"lc")==0 ) {
if ( strcmp(command,"cf") ==0 ) {
#if defined(DEBUG)
Serial.println("command = cf ");
#endif
for (int i=0;i<activeDPins;i++){out+="cf:D"+String(i)+'='+dConfig[i]+'&';}
}
return ;
}
if ( strcmp(command, "i2") ==0 ){ // not implemented yet, dummy function
if (cmd[3]=='I') { //= read i2c devices
return;
}
}
if (strcmp(command, "pg")==0 and cmd[3]=='D' ){
#if defined(DEBUG)
Serial.println("command =pg "+ String(pin) +" to mode=" + mode +", cmd="+ cmd);
#endif
if (doNotUsePin[pin] ) { return; }
if (mode =='I'){ pinMode(pin, INPUT); dConfig[pin] ='I';out+=cmd ; out+='&';}
if (mode =='O'){ pinMode(pin, OUTPUT);dConfig[pin] ='O';out+=cmd ; out+='&';}
pulseCMD[pin]='0';
pulseEND[pin]=0;
pulseEND[pin]=0;
return;
}
if ( strcmp(command, "rd")==0 ){
if ( cmd[3]=='A') { // ANALOG
int val = analogRead(A0); // ESP has only one pin and it must be address as A0
#if defined(DEBUG)
Serial.print("command = A-read "); Serial.println(val);
#endif
out+="rd:A"+String(pin)+'='+String(val)+'&';
return;
}
if (cmd[3]=='D'){ // Digital
int val = digitalRead(pin);
#if defined(DEBUG)
Serial.print("command = D-read ");Serial.println(val);
#endif
out+="rd:D"+String(pin)+'='+String(val)+'&';
pulseCMD[pin]='0';
pulseEND[pin]=0;
pulseEND[pin]=0;
return;
}
if (cmd[3]=='S'){ // this will return the S[pin] string
#if defined(DEBUG)
Serial.print("command = S-read ");Serial.println(S[pin]);
#endif
out+="rd:S"+String(pin)+'='+S[pin]+'&';
return;
}
}
if (strcmp(command, "wr") ==0 and cmd[3]=='S'){
S[pin]= valueS;
out+=cmd; out+='&';
return;
}
if ( cmd[3]=='D'){
if (strcmp(command, "wr")==0 ){
//#if defined(DEBUG)
Serial.println("command = wr");
//#endif
digitalWrite(pin,value1);
pulseCMD[pin]='0';
out+=cmd; out+='&';
return;
}
if (strcmp(command, "aw") ==0 and cmd[3]=='D'){
#if defined(DEBUG)
Serial.print("command = aw, value=" );Serial.println(value1);
#endif
analogWrite(pin,value1);
pulseCMD[pin]='0';
out+=cmd; out+='&';
return;
}
if (strcmp(command, "pU")==0 ){ // pulse Down / pulse Up
pulseEND[pin]= millis()+ value1;
pulseV1[pin]=value1;
pulseV2[pin]=value2;
pulseCMD[pin]='P';
//analogWrite(pin,255);
digitalWrite(pin,1);
#if defined(DEBUG)
Serial.println("command = pU");
#endif
out+=cmd; out+='&';
return;
}
if (strcmp(command, "pD")==0 ){ // pulse up / pulse down
pulseEND[pin]= millis()+ value2;
pulseV1[pin]=value1;
pulseV2[pin]=value2;
pulseCMD[pin]='p';
//analogWrite(pin,0);
digitalWrite(pin,0);
#if defined(DEBUG)
Serial.println("command = pD");
#endif
out+=cmd; out+='&';
return;
}
if (strcmp(command, "mU")==0 ){ // moment UP
pulseEND[pin]= millis()+ value1;
pulseCMD[pin]='M';
#if defined(DEBUG)
Serial.println("command = mU");Serial.println(value1);
#endif
digitalWrite(pin,1);
out+=cmd; out+='&';
return;
}
if (strcmp(command, "mD")==0 ){ // moment Down
pulseEND[pin]= millis()+ value1;
pulseCMD[pin]='m';
#if defined(DEBUG)
Serial.println("command = mD");Serial.println(value1);
#endif
digitalWrite(pin,0);
out+=cmd; out+='&';
return;
}
if (strcmp(command, "rD")==0 ){ // ramp up
pulseEND[pin]= millis()+ value1;
pulseV1[pin]=value1;
pulseCMD[pin]='r';
#if defined(DEBUG)
Serial.println("command = rD");Serial.println(value1);
#endif
analogWrite(pin,255);
out+=cmd; out+='&';
return;
}
if (strcmp(command, "rU")==0 ){ // ramp down
pulseEND[pin]= millis()+ value1;
pulseV1[pin]=value1;
pulseCMD[pin]='R';
#if defined(DEBUG)
Serial.println("command = rU");Serial.println(value1);
#endif
analogWrite(pin,0);
out+=cmd; out+='&';
return;
}
}
}
void SpecialOutputs() {
for ( int j = 0; j< activeDPins; j++){ // check for all digital pins
if (pulseCMD[j] != '0') {
////// moment up or down
if (pulseCMD[j] =='M'){ // UP
if (millis() > pulseEND[j]) { pulseCMD[j] ='0'; digitalWrite(j,0);}
}
if (pulseCMD[j] =='m'){ // down
if (millis() > pulseEND[j]) { pulseCMD[j] ='0'; digitalWrite(j,1);}
}
/////// pulses up and down
if (pulseCMD[j] =='P'){ // UP
if (millis() > pulseEND[j]) { pulseCMD[j] ='p'; digitalWrite(j,0); pulseEND[j] = millis()+ pulseV2[j];}
}
if (pulseCMD[j] =='p' ){ //down
if (millis() > pulseEND[j]) { pulseCMD[j] ='P'; digitalWrite(j,1); pulseEND[j] = millis()+ pulseV1[j];}
}
/////// ramp up or down
if (pulseCMD[j] =='R'){ // UP
if (millis() > pulseEND[j] ) { pulseCMD[j]='0';}
else {analogWrite(j, (int) ( 255. * (1.- ((float)pulseEND[j] - (float) millis()) / ((float)pulseV1[j]) ) ));}
}
if (pulseCMD[j] =='r'){ // down
if (millis() > pulseEND[j] ) { pulseCMD[j]='0';}
else {analogWrite(j, (int) ( 255. * ( ((float)pulseEND[j] - (float) millis()) / ((float)pulseV1[j]) ) ));}
}
}
}
}
int findCinCMD( char x){
for ( int i=0;i<cmdL;i++){ if (cmd[i] == x){ return i;}}
return -1;
}
void parseCMD() { // structure: rd:D09& or wr:D55=1234& or mU:d3=1234,4321& or pg:d33=I&
int i; String xx="";
value1 =-1; value2 =-1;mode=' ';
command[0]=cmd[0]; command[1]= cmd[1];command[2]='\0';
//Serial.print("\n ki: ");Serial.print(kIndex);Serial.print(" ei: ");Serial.print(eIndex);Serial.print(" c: ");Serial.print(cIndex);
if (eIndex==-1){eIndex=cmdL;}; if (cIndex==-1){cIndex=cmdL;} // if no "=" or "," signs
// get pin # is integer 0.. 99 allowed
if (eIndex > 0) { xx=""; for (i = kIndex+2; i< eIndex; i++ ) { xx+=cmd[i]; }; pin=xx.toInt(); }
// now get values
if (eIndex>3){ // any "=" sign?
if (eIndex == cmdL-1){return;} // no values given
// this is for programming to Input or Output
if (cmd[eIndex+1] =='I'){mode = 'I';return;}
if (cmd[eIndex+1] =='O'){mode = 'O';return;}
// get integer numbers may by one or 2 , if "," present then it has 2 numbers
if (cmd[3]=='S'){
xx=""; for ( i = eIndex+1; i< cmdL; i++ ){ xx+=cmd[i];}; valueS=xx; // ignore , for strings
return;
}
if (cIndex>0 and eIndex <cmdL-1){ // we have 2 number separated by a ","
xx=""; for ( i = eIndex+1; i< cIndex; i++ ){ xx+=cmd[i];}; value1=xx.toInt(); //
xx=""; for ( i = cIndex+1; i< cmdL; i++ ){ xx+=cmd[i];}; value2=xx.toInt(); //
return;
}
// just one number/ string, no "," present
xx=""; for ( i = eIndex+1; i< cmdL; i++ ) { xx+=cmd[i];}; value1=xx.toInt(); return;
}
return ;
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.println(" text ");
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// Jens