Looking great! I only have the ESP8266 at hand so I can't test this but looks promising!!
Best regards, L
//
// uno w ethernet sketch by karl wachs May 7 2015 v 0.1
// 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:
// read:a1&read:d3& ==> will read analog pins 1 and 3
// write:d1=0&write:d4=0& ==> set digital pins 1 to 0 and digital pin4 to 1
// awrite:d3=55& ==> analogWrite(3,55) will set digital pin3 to 55
// prog:d5=O&prog:D4=I& ==> will reprogram digital pin 5 to output and digital pin 4 to input
//
// not yet done: pulse, moment, rampU, rampD
//
// here a more complex example:
// command curl 192.168.1.155"?reada1&write:d5=1&prog:d7=O&write:d7=1&?"
// you can combin multiple commands each command must end with a &
//
//
#include <SPI.h>
////////////// WiFi
#include <WiFi.h>
////////////// WiFi
////////////// for Ethernet
//#include <Ethernet.h>
////////////// for Ethernet
byte mac[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x51 }; // pick one thats not on your network
IPAddress ip(192,168,1, 155);
////////////// WiFi
char ssid[] = "xxx"; // your network SSID (name)
char pass[] = "xxxxxx."; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
//////////////
////////////// for Ethernet
//EthernetServer server(80);
////////////// for Ethernet
String line0;
String temp="";
String cmd="";
String command ="";
bool analog=true;
int pin = 0 ;
int value1 = -1;
int value2 = -1;
char mode = 'I';
int i =0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
/////////////// for Wifi
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 1 seconds for connection:
delay(1000);
}
//////////////
////////////// for Ethernet
// start the Ethernet connection and the server:
// Ethernet.begin(mac, ip);
// delay(10000);
////////////// for Ethernet
//////////////
server.begin();
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
// PWR pins: 3,5,9
// do not use 10-13, used for ethernet connection 10, 11, 12 and 13
/////////////// for Wifi
// 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");
/////////////// for Wifi
////////////// for Ethernet
// Serial.print("server is at ");
// Serial.println(Ethernet.localIP());
////////////// for Ethernet
}
void loop() {
// listen for incoming clients
/////////////// for Wifi
WiFiClient client = server.available();
/////////////// for
/////////////// for Ethernet
// EthernetClient client = server.available();
/////////////// for
bool endFound =false;
if (client) {
Serial.println("new client");
// an http request ends with a blank line
char lastC =' ';
char c=' ';
while (client.connected()) {
if (client.available()) {
lastC = c;
c = client.read();
//Serial.write(c);
if (c =='\n' and lastC=='\n' and endFound ==false) {
// if you've gotten to the end of the line (received a newline character) and the line is blank, the http request has ended,so you can send a reply
endFound=true;
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
String line0="";
}
else{
// now get commands
if (c != '\n' ) {
if ( lastC !='\n'){ line0 +=c; }
}
else{
Serial.println("\n here the line:>>>");
Serial.println(line0);
// found a cmplete line output the value of each analog input pin
temp = line0.substring(line0.indexOf('?')+1);
//Serial.println(temp);
line0 = temp.substring(0,temp.indexOf('?'));
Serial.println(line0);
Serial.println("<<<abc \n");
String out ="returnValues:";
while ( line0.length() > 3) {
nextCmd();
Serial.println (" ");
Serial.println (cmd);
getANALOG();
getPIN();
getCOMMAND();
if (command == "read"){
if (analog) {
int val = analogRead(pin);
Serial.print("command = A-read ");
Serial.println(val);
out+='a'+String(pin)+'='+String(val)+'&';
}
else{
int val = digitalRead(pin);
Serial.print("command = D-read ");
Serial.println(val);
out+='a'+String(pin)+'='+String(val)+'&';
}
}
else{
getVALUES();
Serial.print("value 1:"); Serial.print(value1);Serial.print("; 2:"); Serial.print(value2);Serial.print(" mode:");Serial.println(mode);
if (command == "write"){
Serial.println("command = write");
digitalWrite(pin,value1);
}
if (command == "awrite"){
Serial.println("command = awrite");
analogWrite(pin,value1);
}
if (command == "pulseU"){
Serial.println("command = pulseUP");
}
if (command == "pulseD"){
Serial.println("command = pulseDown");
}
if (command == "moment"){
Serial.println("command = moment");
}
if (command == "rampU"){
Serial.println("command = ramp UP");
}
if (command == "rampD"){
Serial.println("command = ramp DOWN");
}
if (command == "prog"){
Serial.println("command = set mode "+ String(pin) +" to " + mode );
if (mode =='I'){ pinMode(pin, INPUT);}
if (mode =='O'){ pinMode(pin, OUTPUT);}
}
}
}
line0="";
client.println(out);
// String readLine="";
// client.println("Connection: close"); // the connection will be closed after completion of the response
client.stop();
}
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
Serial.println("client disconnected");
}
}
void nextCmd() {
i = line0.indexOf('&');
if ( i <= 1 )
{ cmd = "" ; line0 ="" ; return ;}
cmd = line0.substring(0,i);
// Serial.println(cmd);
// Serial.println(i);
if (line0.length() > i+3) {
temp = line0.substring(i+1);
line0 = temp;
}
else {line0="";}
// Serial.println(line0);
}
void getANALOG() {
i = line0.indexOf(':');
if (cmd.charAt(i+1) =='a') {analog=true; }
else {analog=false;}
}
void getPIN() {
i = cmd.indexOf(':');
temp= cmd.substring(i+2);
i = temp.indexOf('=');
if (i == 2){
pin= temp.substring(0,2).toInt();
}
else{
pin= temp.substring(0,1).toInt();
}
}
void getCOMMAND() {
i = cmd.indexOf(':');
if (i>1){
command = cmd.substring(0,i);
}
else{ command="";}
}
void getVALUES() {
value1 =-1; value2 =-1;mode=' ';
i = cmd.indexOf('=');
if (i>3){
if (i == cmd.length()-1){return;}
String Values = cmd.substring(i+1);
i = Values.indexOf(',');
if (i>0 and i != Values.length()-1){
value1 = Values.substring(0,i).toInt();
value2 = Values.substring(i+1).toInt();
return;
}
else {
if (Values.charAt(0) =='I'){mode = 'I';return;}
if (Values.charAt(0) =='O'){mode = 'O';return;}
}
value1 = Values.toInt();
}
return ;
}
ready
AT+CWMODE=1 XXXXXXX
ܽ������5
wrong syntax
ERROR
void processIndigoResponse(){ //Call me to load a string from indigo. Returns the string
if(Serial.read() == ','){ //Find a comma, the dayta received from the ESP8266 has a comma, followed by no. of bits, followed by send.
int stringLength = Serial.parseInt()-1; //Get the length of the text to read (ignoring the newline character at the end
Serial.read(); //clear : Character //There's a space next, read and ditch that.
char instructor = Serial.read(); //Specific to my system, I have a function character, so this would be 'V' indicating a new volume update,
// could be 1 for update pin 1 for example.
if(instructor == 'T'){ //Playtime handles new input, so if "T" is the first character of the received string
playTime = Serial.parseInt(); // so - i would send like "T 3600" to display an hour elapsed, playTime would become 6300
if ((controllerState == 0) || (controllerState == 1)){
writeNewPlayTime(0, playTime,2);
} else
lastPlayTimeRecieved = millis();
}
else if(instructor == 'V'){ //Volume
int newVol = Serial.parseInt();
writeVolume(newVol);
}
else if(instructor == 'S'){ //New State
int newState = Serial.parseInt();
controllerState = newState;
}
else { //handles data without an initial character, in which case it's just returned.
char serialReadString[stringLength];
for(int x = 0; x <= stringLength; x++){
char inChar = Serial.read();
serialReadString[x] = inChar;
};
//indigoSend(serialReadString);
String outputString = serialReadString;
//return outputString;
if (instructor == '2'){
lcd.setCursor(0,2);
lcd.print(outputString);
}
else if (instructor == '3'){ //all this is application specific.
lcd.setCursor(0,3);
lcd.print(outputString);
};
};
serialFind('K', 500); //results in a delay being incorporated until the 'OK' signal is returned. I include this as I had problems
//with data disappearing when i tried to send too soon after a receive
Serial.flush();
};
};
void indigoSend(String TextToSend){
Serial.print("AT+CIPSEND=" + String(TextToSend.length() + 3, DEC) + "\r\n");
delay(10);
Serial.print(TextToSend + "\n");
delay(1);
Serial.print("\r\n");
serialFind('K',5000);
};
Users browsing this forum: No registered users and 1 guest