Project tutorial
IoT with ENC28J60

IoT with ENC28J60 © GPL3+

Control Arduino outputs and read input data on your website using Ethernet module ENC28J60.

  • 2,083 views
  • 0 comments
  • 8 respects

Components and supplies

About this project

This project can be useful for you if you have a website that you can use to keep the data and you can't get static IP address from your internet provider. The common way is to set the ENC28J60 to server mode and connect to it from the internet by entering your router static IP address in a browser.

In this project the Ethernet is set to Client mode and it sends data of the Arduino analogue input A0 and digital input D3 to a website. At the same time the Arduino reads the buttons state which selected on the website and switches output 5. I can browse at the Arduino inputs data or control the Arduino output by logging to a page in my website from anywhere.

A disadvantage of this system is that you can't access the Ethernet module directly from a browser on the internet. Changes to the code can allow for transfer of more data. The idea is taken from here: https://thingspeak.com/ The process is to request a file and to add the data using URL parameters. The PHP file (a21.php) records the data on another file a22.php. To read inputs and select output log to page a23.php (a24.php records the output state). I added a suggestion for a PHP script. To see the data log to page: http://moty22.co.uk/a23.php. The Arduino's output is updated when the Ethernet module sends data every 20 seconds.

In the code change the name of my website to yours. You can use my website to test your project. The code is based on a sample in github/jcw/ethercard.

You can download the php file and get more info on my website: http://moty22.co.uk/wIfI.php

Code

IoTC/C++
// IoT by using module in client mode to send and receive data to your website.

// Simple demo for feeding some random data to Pachube.
// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

// Handle returning code and reset ethernet module if needed
// 2013-10-22 hneiraf@gmail.com

// Modifing so that it works on my setup for www.thingspeak.com.
// Arduino pro-mini 5V/16MHz, ETH modul on SPI with CS on pin 10.
// Also added a few changes found on various forums. Do not know what the 
// res variable is for, tweaked it so it works faster for my application
// 2015-11-09 dani.lomajhenic@gmail.com

#include <EtherCard.h>

// change these settings to match your own setup
//#define FEED "000"
#define APIKEY "beef1337beef1337" // put your key here
#define ethCSpin 10 // put your CS/SS pin here.

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
const char website[] PROGMEM = "moty22.co.uk";  //Change to your domain name 
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;
byte session;
String d1;
//timing variable
int res = 100; // was 0
int analog=0;


void setup () {
  pinMode(3,INPUT_PULLUP);
  pinMode(5,OUTPUT);
  Serial.begin(9600);
  Serial.println("\n[ThingSpeak example]");

  //Initialize Ethernet
  initialize_ethernet();
}


void loop () { 
  //if correct answer is not received then re-initialize ethernet module
  if (res > 220){
    initialize_ethernet(); 
  }
  
  res = res + 1;
  
  ether.packetLoop(ether.packetReceive());
  
  //200 res = 30 seconds (150ms each res)
  if (res == 200) {

    analog = analogRead(A0);
    if(digitalRead(3)) {d1 = "OFF";} else {d1 = "ON";}  

    // generate 3 values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    
    byte sd = stash.create();

    stash.print("field1=");
    stash.print(highByte(analog));
    stash.print("&field2=");
    stash.print(lowByte(analog));
    stash.print("&field3=");
    stash.print(d1);
    
    stash.save();

    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("POST /a21.php HTTP/1.1" "\r\n"
      "Host: $F" "\r\n"
      "Connection: close" "\r\n"
      "X-THINGSPEAKAPIKEY: $F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),
      website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    session = ether.tcpSend(); 

  }
  
   const char* reply = ether.tcpReply(session);
   
   if (reply != 0) {
     res = 0;
     Serial.println(F(" >>>REPLY recieved...."));
     Serial.println(reply);
      if(find(reply,"output=ON")){digitalWrite(5, HIGH);}
      if(find(reply,"output=OFF")){digitalWrite(5, LOW);}
     delay(300);
   }
   delay(150);
}

boolean find(String string, String value){
  if(string.indexOf(value)>=0)
    return true;
  return false;
}

void initialize_ethernet(void){  
  for(;;){ // keep trying until you succeed 

    if (ether.begin(sizeof Ethernet::buffer, mymac, ethCSpin) == 0){ 
      Serial.println( "Failed to access Ethernet controller");
      continue;
    }
    
    if (!ether.dhcpSetup()){
      Serial.println("DHCP failed");
      continue;
    }

    ether.printIp("IP:  ", ether.myip);
    ether.printIp("GW:  ", ether.gwip);  
    ether.printIp("DNS: ", ether.dnsip);  

    if (!ether.dnsLookup(website))
      Serial.println("DNS failed");

    ether.printIp("SRV: ", ether.hisip);

    //reset init value
    res = 180;
    break;
  }
}

Schematics

Comments

Similar projects you might like

IoT Blink - Getting started with IoT

Project showcase by AppShed Support

  • 2,028 views
  • 0 comments
  • 11 respects

ENC28J60 on the Web

Project tutorial by moty

  • 1,959 views
  • 0 comments
  • 9 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 297,277 views
  • 98 comments
  • 706 respects

Android App-Based Home Automation System Using IOT

Project tutorial by Team Autoshack

  • 24,838 views
  • 17 comments
  • 75 respects

IoT Treat Dispenser For Pets

Project tutorial by Arduino “having11” Guy

  • 12,130 views
  • 0 comments
  • 47 respects
Add projectSign up / Login