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