ARDUINO plugin discussion

Posted on
Fri May 08, 2015 2:33 am
DrLove offline
Posts: 260
Joined: Dec 12, 2014
Location: Sweden

Re: ARDUINO plugin discussion

Looking great! I only have the ESP8266 at hand so I can't test this but looks promising!!

Best regards, L

Love Kull (yes it's my name)
Blog (in Swedish)
Sweden

Posted on
Fri May 08, 2015 6:51 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

the code should only be different for the wifi part. just add the WIFI section and it should work..
at least for the standard wifi shield. I can test that, found my old mega and still have the old wifi board.

Posted on
Fri May 08, 2015 7:23 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

This SHOULD be the Wifi equivalent for the standard wifi shield

Code: Select all
//
//  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 ;   
}




Posted on
Thu May 14, 2015 11:34 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

I am trying the ESP8266 with not so overwhelming success:

bought 3
One works with "cooltalk" directly attached to a USB port via FTDI connector. Issuing some AT commands and the wifi router recognizes it and I can curl to it and see characters going forward and back. .. got really excited ..
BUT then when connecting it to an arduino I get bad character transmissions.
Like this:
Code: Select all
ready
AT+CWMODE=1 XXXXXXX

ܽ������5


wrong syntax

ERROR

Where XXXXX are non printable characters
tried several pins always the same, added a 10muF capacitor to the ESP to VCC-Ground .. no change, reduced the length of the cable to < 1 inch .. no change..

the other 2 do not work at all.

I bough them at amazon, may be buying them at spark fun .. would be better quality control?

Does anyone have positive experience with the ESP8266? (or also if you have negative experience ) any suggestions ..?

Karl

Posted on
Fri May 15, 2015 9:00 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

Fixed one problem. They all came with different firmware and baud rate setting. After firmware flash (learning new things avery day and some chinese too) all are in a usable state now.

I believe the bad communication is due to the speed limitation of "software serial" on the ARDUINO. Will try out on a mega that comes with multiple serial hardware connectors.

Karl

Posted on
Fri May 15, 2015 10:57 am
mattggarland offline
Posts: 49
Joined: Jan 06, 2013

Re: ARDUINO plugin discussion

I currently have 2 way communication with indigo using an esp8266. What I have could be adjusted to use any serial method so tcp over IP, Bluetooth, zigby etc, currently I use cynical network.

The idea being that I have an indigo variable that when updated is sent out to the device, the device receives something like

"Device.receive=6 Hello
"
So 6 characters, all the letters of hello and a newline. The script just sends the variable for the micro (Arduino) to decode so in my case 'V 40' for 'display volume 40 on the screen of my wifi remote. For a general gpio setup you could use pin / function so send
'1 HIGH'
For set pin 1 to vcc(5v).

I would suggest having indigo just communicating with an Arduino, and then package the plugin with a sketch to use. App design is not my strong point but I know you need a specific library for each Arduino model and the Arduino IDE is updated almost monthly with them. This can be done with a generic Arduino sketch.

I don't have time just now, but will send you the communication code I use for my project on the Arduino and the Indigo side.

There's a boatload of frighteningly clever people over on the Arduino forums, seek help and ye shall find!


Sent from my iPhone using Tapatalk

Posted on
Sat May 16, 2015 6:47 am
mattggarland offline
Posts: 49
Joined: Jan 06, 2013

Re: ARDUINO plugin discussion

...

so here is some of the code. This is a void which I call on every loop of the arduino program:

Code: Select all
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();
  };
};



So - once connected to a TCP connection with cynical network, this receives and processes the data.

Posted on
Sat May 16, 2015 8:22 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

Got hat , how do you transmit to the network, using software serial and which arduino are you using?

Posted on
Sat May 16, 2015 8:36 am
mattggarland offline
Posts: 49
Joined: Jan 06, 2013

Re: ARDUINO plugin discussion

Using an Atmel ATMega pretending to be an Uno - code should work with all though.

I am using the hardware serial ports (0,1) for the wifi card - software serial isn't fast enough for the high baud rate of the card so hardware serial is necessary, or you could change the baud rate of the esp8266 - can't remember how but google knows all!.

My code to send to indigo is:


Code: Select all
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);
};

sends the code followed by a newline (kinda standard).
Matt

Posted on
Sat May 16, 2015 9:11 am
DrLove offline
Posts: 260
Joined: Dec 12, 2014
Location: Sweden

Re: ARDUINO plugin discussion

Wow, this is going places! Looking forward to a complete instruction for setting this up. I have the esp8266 but have not tested it yet. I don't se any IP# to Indigo and I don't know how to set this up at Indigo so I guess I'm missing some important knowledge here :) But I'm willing to learn!

Keep up the good work!

//L

Love Kull (yes it's my name)
Blog (in Swedish)
Sweden

Posted on
Sun May 17, 2015 5:12 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

Yes, I have to agree that the software serial connection to the esp 8266 is too slow. With a mega we could construct something using the TX1...TX3 pins (serial1,2,or 3 hardware) ..There are now boards available that have either lua and/or arduino IDE available.. BUT >>The old ARDUINOs uno/ mega work day and night and NO failure, no reboot in over a year!!<<

One other possibility is to load the code in a regular way then disconnect the USB cable and connect the ESP8266 to the FTDI pins. That should open up UNO and all the mini / micro ARDUINO boards, but it makes it difficult to develop. One way offing it: use the MEAG for development and then pour to the smaller boards with just one hardware TX/RX.


I have the plugin version for
- UNO+ ethernet or WIFI
- MEGA +ethernet or wifi
done. They do everything I want.. input /output and stable and can now be used as moment switches, relay fronds, temp sensors .. etc..

I will try the new things out but from my experience with the SPARK(*) there is a long way between it kind of works to no reboot in > 1 year.

Karl
(*) its now call http://www.particle.io/ the spark is still not stable enough after a year of development by the experts..

Posted on
Mon May 18, 2015 4:23 am
DomoPat offline
User avatar
Posts: 210
Joined: Jul 17, 2010
Location: Toulouse, France

Re: ARDUINO plugin discussion

This is a very interesting plugin, and it opens nicely the world of Arduino !

I see the plugin is polling the board with an adjustable rate of 1 to several seconds, but is there a reason why the Arduino could not push info like the change of state of an input to the plugin and Indigo ? I like 'push' better because it is faster and less resource intensive, would it be possible with the Arduino ?

Thank you and congratulation for the good work.

Patrick

Posted on
Mon May 18, 2015 8:20 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

You would need to add a web server to your mac. That's possible actually very simple. But you still need to listen to something somewhere. ... Either in indigo, on the mac or on the network ....

Anyway my experience is that the overhead is very small. I am pulling 3 arduinos every second. The network traffic is <3 kbit/ second per ARDUINO. Just to compare on the Ethernet I have about 1 mbit continuously due to the cameras.


Sent from my iPhone using Tapatalk

Posted on
Mon May 18, 2015 8:20 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

I will post a release today that does wifi Ethernet uno and mega.


Sent from my iPhone using Tapatalk

Posted on
Mon May 18, 2015 3:43 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: ARDUINO plugin discussion

how to attach ESP8266 to arduino (UNO/MEGA) 5 V boards:

parts:

1. level shifter 3.3V <-> 5V sparkfun.com has a board with 4 level shifters you will just use 2 of them
2. 10 uF capacitor
3. 3.3 V power supply chip, I used AMS 1117-3.3, they come in a roll of 50 for a few $'s.. If you need some I still have 40 left..
4. 2 x 2 pins to connect to the UNO/MEGA board and 2 for +5V and ground. They come with the level shifter
5. the ESP8266 board
6. some thin copper wires and 2 cables that go to the 5V and ground pins (the red and the black one).

You MUST know how to solder !!

steps:
1. put in the 2 x2 pins on the opposite sides into the level shifter board on the high /5V side and solder them. (HV1, HV2 and HV,GND)
2. solder the +5V pin of the power chip to the 5V pin on the level shifter on the bottom side
3. connect a copper wire from the ground pin of the power chip to the ground pin of the level shifter board (5V side)
see pin layout screen shot of the ESP chip attached:
4. solder the 2 pins together that have to be "high" to the +3.3V input on the ESP board, see picture
5. now you need to cut off 2 pins on the ESP board the +3.3V and the one next to it the are solder together with the 3. pin (CH_PD, that one stays)
6. bend the TX D pin on the ESP board 90 degrees to the side
7. bend the 2 GPIO pins 90 degrees
8. now the CH_PD (soldered to the 3.3V input) and the RX pin fit into the level shifter board to LVL1 and LV
9. connect the capacitor as shown and connect the 3.3V pin of the power chip to the 3.3 out of the level shifter board
10. connect TX pin on the ESP board to the TX pin of the level shifter with a short copper wire.
11. connect the +5V and the ground cable to the 2 pins

now connect the 5v and ground and check the voltages on the ESP chip. the red light should go on.
next put the 2 pins of the level shifter HIGh side into the RX/TX socket on the UNO/MEGA and you are good to go.

It took me longer to write this down than to actually do it. Just familiarizes your self with the layouts and do a dry run (put things together without soldering)

Karl
Attachments
IMG_3128.jpg
IMG_3128.jpg (149.23 KiB) Viewed 4872 times
Screen Shot 2015-05-18 at 4.22.17 PM.png
Screen Shot 2015-05-18 at 4.22.17 PM.png (18.17 KiB) Viewed 4872 times
IMG_3122.jpg
IMG_3122.jpg (620.64 KiB) Viewed 4872 times
IMG_3125.jpg
IMG_3125.jpg (448.9 KiB) Viewed 4872 times
IMG_3126.jpg
IMG_3126.jpg (331.12 KiB) Viewed 4872 times
IMG_3124.jpg
IMG_3124.jpg (1.05 MiB) Viewed 4872 times

Who is online

Users browsing this forum: No registered users and 2 guests

cron