Android controlled arduino (ethernet)

A simple example on how to control an arduino board from a smartphone, this time using ethernet NOT bluetooth. The app was created using MIT's app inventor. The module used here is based on the ENC28J60 chip (~$5 on fleabay )

enjoy

Arduino sketch

// A simple web server that shows temperature and controlls an LED

#include "etherShield.h"
#include "ETHER_28J60.h"
#include <OneWire.h>
OneWire  ds(A3);  // on pin A3
int celsius,celsiusF;
int outputPin = 3; //the LED pin
static uint8_t mac[6] = {
  0x54, 0x55, 0x58, 0x10, 0x00, 0x24};   // this just needs to be unique for your network, 
// so unless you have more than one of these boards
// connected, you should be fine with this value.

static uint8_t ip[4] = {
  192, 168, 1, 15};                       // the IP address for your board. Check your home hub
// to find an IP address not in use and pick that
// this or 10.0.0.15 are likely formats for an address
// that will work.

static uint16_t port = 80;                                      // Use port 80 - the standard for HTTP

ETHER_28J60 e;

void setup()
{ 
  Serial.begin(9600);
  e.setup(mac, ip, port);
}

void loop()
{
  char* params;

  if (params = e.serviceRequest())
  {
    e.print("<H1>Temperature
");
    e.print(celsius);
    e.print(",");
    e.print(celsiusF);
    e.print(" C</H1>");
    e.print("

");
    e.print("

");
    e.print("<H1>LED control</H1>");

    e.print("<A HREF='?cmd=off'>Turn off</A>
");
    e.print("<A HREF='?cmd=on'>Turn on</A>

");

    if (strcmp(params, "?cmd=on") == 0)
    {
      digitalWrite(outputPin, HIGH);
      e.print("LED is ON");

    }
    else if (strcmp(params, "?cmd=off") == 0) 
    {
      digitalWrite(outputPin, LOW);
      e.print("LED is OFF");

    }





    e.respond();
    delay(100);
  }
  else
  {
    { 
      byte i;
      byte present = 0;
      byte type_s;
      byte data[12];
      byte addr[8];


      if ( !ds.search(addr)) {
        Serial.println("No more addresses.");
        Serial.println();
        ds.reset_search();
        delay(250);
        return;
      }

      Serial.print("ROM =");
      for( i = 0; i < 8; i++) {
        Serial.write(' ');
        Serial.print(addr[i], HEX);
      }

      if (OneWire::crc8(addr, 7) != addr[7]) {
        Serial.println("CRC is not valid!");
        return;
      }
      Serial.println();

      // the first ROM byte indicates which chip
      switch (addr[0]) {
      case 0x10:
        Serial.println("  Chip = DS18S20");  // or old DS1820
        type_s = 1;
        break;
      case 0x28:
        Serial.println("  Chip = DS18B20");
        type_s = 0;
        break;
      case 0x22:
        Serial.println("  Chip = DS1822");
        type_s = 0;
        break;
      default:
        Serial.println("Device is not a DS18x20 family device.");
        return;
      } 

      ds.reset();
      ds.select(addr);
      ds.write(0x44,1);         // start conversion, with parasite power on at the end

      delay(10);     // maybe 750ms is enough, maybe not
      // we might do a ds.depower() here, but the reset will take care of it.

      present = ds.reset();
      ds.select(addr);    
      ds.write(0xBE);         // Read Scratchpad

      Serial.print("  Data = ");
      Serial.print(present,HEX);
      Serial.print(" ");
      for ( i = 0; i < 9; i++) {           // we need 9 bytes
        data[i] = ds.read();
        Serial.print(data[i], HEX);
        Serial.print(" ");
      }
      Serial.print(" CRC=");
      Serial.print(OneWire::crc8(data, 8), HEX);
      Serial.println();

      // convert the data to actual temperature

      unsigned int raw = (data[1] << 8) | data[0];
      if (type_s) {
        raw = raw << 3; // 9 bit resolution default
        if (data[7] == 0x10) {
          // count remain gives full 12 bit resolution
          raw = (raw & 0xFFF0) + 12 - data[6];
        }
      } 
      else {
        byte cfg = (data[4] & 0x60);
        if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
        else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
        else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
        // default is 12 bit resolution, 750 ms conversion time
      }
      celsius = abs(raw / 16.0);
      celsiusF= (raw / 16.0 - celsius) * 100;
      Serial.print("  Temperature = ");
      Serial.print(celsius);
      Serial.print(",");
      Serial.println(celsiusF);
    }
  }
}

The .apk file is the app iteslf, the zipped file is a source that can be uploaded into MIT's app inventor for experimenting with it. The source file is an archive and it should be uploaded to app inventor as an archive.

arduino_appinventor_version_with_temp(1).apk (1.26 MB)

arduino_appinventor_version_with_temp(1).zip (3.93 KB)

thumbnailforvideoonethernetcontrolledarduino.jpg

  if (params = e.serviceRequest())

Is this intentional?

Or should it be:

  if (params == e.serviceRequest())

Yes, that's intentional - the variable params stores the query string

If you type into the address bar of the browser

192.168.1.15/blahblahblah

and hit enter
the variable params stores 'blahblahblah'

kerimil:
Yes, that's intentional

But very bad programming practice.

Stuff like that isn't clever. ..

Now that you pointed it out I realize it might be confusing.

Kerimil,

Seems old thread, but I was just curious how to upload an image from SD card to web-server using this library ?

if you can guide on that, it would be really helpful.

Thanks
Gaurav

how about control for 4 relays? please

Hallo,

first of all than you for this work.

I try to do the same in the video. every thing worked but when I put the IP in the Web-browse, it didnt open any thing. I tried that with different Browsers, but also it dosent work :~ , can any body help me !!!

thanks :slight_smile:

@kerimil
How to open source code?
appinventor request .aia file.
Tnx

this error appears when i try to upload the code

Arduino: 1.6.11 (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Users\hp\Documents\Arduino\libraries\etherShield/etherShield.h:25:0,

from C:\Users\hp\Desktop\ether\ether.ino:3:

C:\Users\hp\Documents\Arduino\libraries\etherShield/ip_arp_udp_tcp.h:32:66: error: 'prog_char' does not name a type

extern uint16_t fill_tcp_data_p(uint8_t *buf,uint16_t pos, const prog_char *progmem_s);

^

In file included from C:\Users\hp\Desktop\ether\ether.ino:3:0:

C:\Users\hp\Documents\Arduino\libraries\etherShield/etherShield.h:34:65: error: 'prog_char' does not name a type

uint16_t ES_fill_tcp_data_p(uint8_t *buf,uint16_t pos, const prog_char *progmem_s);

^

C:\Users\hp\Desktop\ether\ether.ino: In function 'void loop()':

C:\Users\hp\Desktop\ether\ether.ino:36:34: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("

Temperature
");

^

C:\Users\hp\Desktop\ether\ether.ino:38:15: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print(",");

^

C:\Users\hp\Desktop\ether\ether.ino:40:21: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print(" C

");

^

C:\Users\hp\Desktop\ether\ether.ino:41:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("

");

^

C:\Users\hp\Desktop\ether\ether.ino:42:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("

");

^

C:\Users\hp\Desktop\ether\ether.ino:43:34: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("

LED control

");

^

C:\Users\hp\Desktop\ether\ether.ino:45:49: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("Turn off
");

^

C:\Users\hp\Desktop\ether\ether.ino:46:51: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("Turn on

");

^

C:\Users\hp\Desktop\ether\ether.ino:51:25: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("LED is ON");

^

C:\Users\hp\Desktop\ether\ether.ino:57:26: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

e.print("LED is OFF");

^

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

kerimil:
A simple example on how to control an arduino board from a smartphone, this time using ethernet NOT bluetooth. The app was created using MIT's app inventor. The module used here is based on the ENC28J60 chip (~$5 on fleabay )

kerimil can you upload the app inventor file in .aia format ?
Thank you