Re: Help! Tweet fails with code “0”
Home › Forums › Botanicalls Community › Help! Tweet fails with code "0" › Re: Help! Tweet fails with code “0”
Hey Joe,
It would be possible to completely remove the EthernetDHCP and EthernetDNS library requirement, I think, and with the changes I made, I’ve all but removed them.
Some variables need to be defined that are used, that are expected to be initialised by the inclusion of the above libraries.
After my above post about the “dirty” changes I made, I made some more changes, reprogrammed the device, and realised I had it checking the DHCP lease constantly.
I subsequently made some more changes, using millis() to only check the lease every hour, which could probably be increased more.
I made some large changes to the DHCPcheck function, replacing all the switch/case statements with the return codes from the new version.
I didn’t really want to post my code, since it’s pretty ugly, but in case there’s someone else with the same issue, I want to give them something to work with, so here goes..
// start Ethernet
//EthernetDHCP.begin(mac, true); // start ethernet DHCP in non-blocking polling mode
if (Ethernet.begin(mac) == 1) { // start ethernet DHCP in non-blocking polling mode
Serial.println("DHCP discovery success");
Serial.print("ip: ");
Serial.println(Ethernet.localIP());
ipState = DhcpStateLeased;
}
else {
while (Ethernet.begin(mac) == 0){
Serial.println("DHCP discovery failed");
delay(5000); // wait 5 seconds
}
}
void loop() // main loop of the program
{
moistureCheck(); // check to see if moisture levels require Twittering out
wateringCheck(); // check to see if a watering event has occurred to report it
buttonCheck(); // check to see if the debugging button is pressed
analogWrite(COMMLED,0); // douse comm light if it was on
if (millis() % 3600000 == 0){
// an hour has passed
delay(2000);
dhcpCheck(); // check and update DHCP connection
}
if (millis() % 60000 == 0 && ipState != DhcpStateLeased && ipState != DhcpStateRenewing) {
blinkLED(COMMLED,1,30); // quick blnk of COMM led if there's no ip address
}
}
// check and attempt to create a DHCP leased IP address
void dhcpCheck() {
//DhcpState prevState = ipState; // record the current state
//ipState = EthernetDHCP.poll(); // poll for an updated state
Serial.println("DHCP update..");
int State = Ethernet.maintain(); // poll for an updated state
//if (prevState != ipState) { // if this is a new state then report it
//switch (ipState) {
switch (State) {
//case DhcpStateDiscovering:
//Serial.println("DHCP disc");
//break;
//case DhcpStateRequesting:
//Serial.println("DHCP req");
//break;
//case DhcpStateRenewing:
//Serial.println("DHCP renew");
//break;
//case DhcpStateLeased:
case '0':
{
// 0: nothing happened
Serial.println("DHCP: nothing happened");
break;
}
case '1':
{
// 1: renew failed
Serial.println("DHCP: renew failed");
break;
}
case '2':
{
// 2: renew success
Serial.println("DHCP: renewed OK!");
// We have a DHCP lease, so print the info
Serial.print("ip: ");
Serial.println(Ethernet.localIP());
ipState = DhcpStateLeased;
//const byte* ipAddr = EthernetDHCP.ipAddress();
//const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
//const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
//Serial.print("ip: ");
//Serial.println(ip_to_str(ipAddr));
//Serial.print("gw: ");
//Serial.println(ip_to_str(gatewayAddr));
//Serial.print("dns: ");
//Serial.println(ip_to_str(dnsAddr));
break;
}
case '3':
{
// 3: rebind fail
Serial.println("DHCP: rebind failed");
break;
}
case '4':
{
// 4: rebind success
Serial.println("DHCP: rebind success");
// We have a DHCP lease, so print the info
Serial.print("ip: ");
Serial.println(Ethernet.localIP());
ipState = DhcpStateLeased;
break;
}
//}
}
}
New kits are here!
Categories
- Announcements (8)
- Press (5)
Pothos Plant Tweets

Flickr Feed
www.flickr.com
|
Meta

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.