Components and supplies
Arduino UNO
ENC28J60
Project description
Code
Ethernet
c_cpp
1// Using as client mode to send data to your website. 2 3// Simple demo for feeding some random data to Pachube. 4// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php 5 6// Handle returning code and reset ethernet module if needed 7// 2013-10-22 hneiraf@gmail.com 8 9// Modifing so that it works on my setup for www.thingspeak.com. 10// Arduino pro-mini 5V/16MHz, ETH modul on SPI with CS on pin 10. 11// Also added a few changes found on various forums. Do not know what the 12// res variable is for, tweaked it so it works faster for my application 13// 2015-11-09 dani.lomajhenic@gmail.com 14 15#include <EtherCard.h> 16 17// change these settings to match your own setup 18//#define FEED "000" 19#define APIKEY "beef1337beef1337" // put your key here 20#define ethCSpin 10 // put your CS/SS pin here. 21 22// ethernet interface mac address, must be unique on the LAN 23static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; 24const char website[] PROGMEM = "moty22.co.uk"; //Change to your domain name 25byte Ethernet::buffer[700]; 26uint32_t timer; 27Stash stash; 28byte session; 29String d1; 30//timing variable 31int res = 100; // was 0 32int analog=0; 33 34 35void setup () { 36 pinMode(3,INPUT_PULLUP); 37 Serial.begin(9600); 38 Serial.println("\ 39[ThingSpeak example]"); 40 41 //Initialize Ethernet 42 initialize_ethernet(); 43} 44 45 46void loop () { 47 //if correct answer is not received then re-initialize ethernet module 48 if (res > 220){ 49 initialize_ethernet(); 50 } 51 52 res = res + 1; 53 54 ether.packetLoop(ether.packetReceive()); 55 56 //200 res = 30 seconds (150ms each res) 57 if (res == 200) { 58 59 analog = analogRead(A0); 60 if(digitalRead(3)) {d1 = "OFF";} else {d1 = "ON";} 61 62 // generate 3 values as payload - by using a separate stash, 63 // we can determine the size of the generated message ahead of time 64 65 byte sd = stash.create(); 66 67 stash.print("field1="); 68 stash.print(highByte(analog)); 69 stash.print("&field2="); 70 stash.print(lowByte(analog)); 71 stash.print("&field3="); 72 stash.print(d1); 73 74 stash.save(); 75 76 // generate the header with payload - note that the stash size is used, 77 // and that a "stash descriptor" is passed in as argument using "$H" 78 Stash::prepare(PSTR("POST /a11.php HTTP/1.1" "\ \ 79" 80 "Host: $F" "\ \ 81" 82 "Connection: close" "\ \ 83" 84 "X-THINGSPEAKAPIKEY: $F" "\ \ 85" 86 "Content-Type: application/x-www-form-urlencoded" "\ \ 87" 88 "Content-Length: $D" "\ \ 89" 90 "\ \ 91" 92 "$H"), 93 website, PSTR(APIKEY), stash.size(), sd); 94 95 // send the packet - this also releases all stash buffers once done 96 session = ether.tcpSend(); 97 98 // added from: http://jeelabs.net/boards/7/topics/2241 99// int freeCount = stash.freeCount(); 100// if (freeCount <= 3) { Stash::initMap(56); } 101 } 102 103 const char* reply = ether.tcpReply(session); 104 105 if (reply != 0) { 106 res = 0; 107 Serial.println(F(" >>>REPLY recieved....")); 108 Serial.println(reply); 109 delay(300); 110 } 111 delay(150); 112} 113 114void initialize_ethernet(void){ 115 for(;;){ // keep trying until you succeed 116 117 if (ether.begin(sizeof Ethernet::buffer, mymac, ethCSpin) == 0){ 118 Serial.println( "Failed to access Ethernet controller"); 119 continue; 120 } 121 122 if (!ether.dhcpSetup()){ 123 Serial.println("DHCP failed"); 124 continue; 125 } 126 127 ether.printIp("IP: ", ether.myip); 128 ether.printIp("GW: ", ether.gwip); 129 ether.printIp("DNS: ", ether.dnsip); 130 131 if (!ether.dnsLookup(website)) 132 Serial.println("DNS failed"); 133 134 ether.printIp("SRV: ", ether.hisip); 135 136 //reset init value 137 res = 180; 138 break; 139 } 140} 141
Downloadable files
ethernet
ethernet
ethernet
ethernet
Comments
Only logged in users can leave comments
moty
5 Followers
•15 Projects
1
Anonymous user
5 years ago
Very nice example, but I am looking to push it to my own domain, how can I do it? I don't have API key?