Pages: [1]   Go Down
Author Topic: Android controlled arduino (ethernet)  (Read 4115 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 5
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
// 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<br/>");
    e.print(celsius);
    e.print(",");
    e.print(celsiusF);
    e.print(" C</H1>");
    e.print("<br><br>");
    e.print("<br><br>");
    e.print("<H1>LED control</H1>");

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

    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 (1288.4 KB - downloaded 272 times.)
* arduino_appinventor_version_with_temp(1).zip (3.93 KB - downloaded 356 times.)

* thumbnailforvideoonethernetcontrolledarduino.jpg (79.09 KB, 640x332 - viewed 263 times.)
« Last Edit: November 10, 2013, 11:49:05 pm by kerimil » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (params = e.serviceRequest())

Is this intentional?

Or should it be:

Code:
  if (params == e.serviceRequest())
Logged

0
Offline Offline
Full Member
***
Karma: 5
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

If you type into the address bar of the browser
Quote
192.168.1.15/blahblahblah
and hit enter
the variable params stores 'blahblahblah'
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 142
Posts: 5283
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that's intentional

But very bad programming practice.

Stuff like that isn't clever. ..
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

0
Offline Offline
Full Member
***
Karma: 5
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: