Twitter post temperature

Guys

I'm at a bit of a loss as to what is wrong. If I ommit the call to the "Temp" function then it all works, If I leave it in then the temp part works but it will not post to twitter or even report the error it just looks to pause the code.

I'll post the full code in a comment below as it exceeds the post length here but here is the bit I think is relevant I know its a bit messy at the mo, once its working I'll slim it down.

else if (totaltime >= (450) && totaltime <= (1325)) // 465 = 07:45 1305 = 21:45 DAYTIME if it is later than 07:30 and earlier than 21:45 execute "daytime"
{
char msg[] = "Tanktweet Daytime: %d:%d %dC ";

Temp();

sprintf(msg, "Tanktweet Daylight time: %d:%d %DC ", hour(), minute() , (tweettemp)); //(tweettemp)

Serial.println("about to tweet");

Serial.println(msg);
//seems to stop here note - check Twitter libaries re: twitter.post ? is this the actual command to post? there must be a simplier way????
// Serial.println((twitter.post(msg)));
if (twitter.post(msg))
{
Serial.println("if (twitter.post(msg))");
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);

if (status == 200)
{
Serial.println("OK.");
}
else
{
Serial.print("failed : code ");
Serial.println(status);
}
}
else
{
Serial.println("connection failed.");
}

All help greatly appreciated :slight_smile:

Steve

Full Code pt1

#include <OneWire.h>
#include <SPI.h>
#include <Wire.h>
#include <Ethernet.h>
#include <sha1.h>
#include <EthernetDNS.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <Twitter.h>
#define whiteon 4
#define stormswitch 2
#define manualswitch 3
#define whiteled 11
#define BETWEEN 2579
#define TIMES 7
#define DURATION 43

OneWire  ds(10);// on pin 10 (a 4.7K resistor is necessary)

float celsius = 0.0;
int tweettemp = 0;
unsigned long lastTime = 0;
int waitTime = 0;
int totaltime = 0;
int bright = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };//mac address
 
IPAddress timeServer(97, 107, 128, 58);//timeserver ipaddress

const long timeZoneOffset = -0L;  //offset in seconds from GMT

unsigned int ntpSyncTime = 21600; //number of seconds between time sync 21600=6hours      

unsigned int localPort = 8888;// local port to listen for UDP packets

const int NTP_PACKET_SIZE= 48;// NTP time stamp is in the first 48 bytes of the message      

byte packetBuffer[NTP_PACKET_SIZE];// Buffer to hold incoming and outgoing packets  

EthernetUDP Udp;  // A UDP instance to let us send and receive packets over UDP                  

unsigned long ntpLastUpdate = 0; // Keeps track of how long ago we updated the NTP server   

time_t prevDisplay = 0;  // Check last time clock displayed (Not in Production)          

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("edited out :-)"); 

// Message to post
char msg[] = "Tanktweet Bootup Time:"; 




void setup() 
{
  totaltime = (hour()*60) + minute();
  pinMode(whiteon, INPUT);
  digitalWrite(whiteon, HIGH);
  pinMode(stormswitch, INPUT);
  digitalWrite(stormswitch, HIGH);                                                                               // Add resistor and remove this
  pinMode(manualswitch, INPUT);
  digitalWrite(manualswitch, HIGH);                                                                              // and here
  pinMode(whiteled, OUTPUT);
  Serial.begin(9600);
   Serial.println("Aquiring IP address from DHCP server, this may take a few mins.......");
   // Ethernet shield and NTP setup
   int i = 0;
   int DHCP = 0;
   DHCP = Ethernet.begin(mac);
   //Try to get dhcp settings 30 times before giving up
   while( DHCP == 0 && i < 30)
   {
     delay(1000);
     DHCP = Ethernet.begin(mac);
     i++;
   }
   if(!DHCP)
   {
    Serial.println("DHCP FAILED");
     for(;;); //Infinite loop because DHCP Failed
   }
   Serial.println("DHCP Success");
   Serial.println("Getting Time.........");
   //Try to get the date and time
   int trys=0;
   while(!getTimeAndDate() && trys<10) 
   {
     trys++;
   }
   Serial.println("Connecting to Twitter.....");
   sprintf(msg, "Tanktweet bootup %lu",now());
    if (twitter.post(msg)) 
    {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) 
      {
        Serial.println("OK.");
      } 
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
      } 
    else 
      {
        Serial.println("connection failed.");
      }
}
 
// Do not alter this function, it is used by the system
int getTimeAndDate() {
   int flag=0;
   Udp.begin(localPort);
   sendNTPpacket(timeServer);
   delay(10000);
   if (Udp.parsePacket()){
     Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
     unsigned long highWord, lowWord, epoch;
     highWord = word(packetBuffer[40], packetBuffer[41]);
     lowWord = word(packetBuffer[42], packetBuffer[43]);  
     epoch = highWord << 16 | lowWord;
     epoch = epoch - 2208988800 + timeZoneOffset;
     flag=1;
     setTime(epoch);
     ntpLastUpdate = now();
   }
   return flag;
}
 
// Do not alter this function, it is used by the system
unsigned long sendNTPpacket(IPAddress& address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  packetBuffer[0] = 0b11100011;
  packetBuffer[1] = 0;
  packetBuffer[2] = 6;
  packetBuffer[3] = 0xEC;
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;                  
  Udp.beginPacket(address, 123);
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}
 
// Clock display of the time and date (Basic)
void clockDisplay(){
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
 
// Utility function for clock display: prints preceding colon and leading 0
void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void storm()
{ 
  if (digitalRead(stormswitch) == LOW)
    {
      do
      {
    Serial.println("lightning");
    if (millis() - waitTime > lastTime)
    {
      lastTime += waitTime;
      waitTime = (random(BETWEEN)*1.5);

      for (int i=0; i< random(TIMES); i++)
        {
           digitalWrite(whiteled, HIGH);
           delay(100 + random(DURATION));
           digitalWrite(whiteled, LOW);
           delay(10);
        }
       }
      }while (digitalRead(stormswitch) == LOW);
    }
  else
  {
  loop();
  }
}  

void manual()
{
  if (digitalRead(manualswitch) == LOW)
    {
      do
        {
          bright = (analogRead(0)/4);
          Serial.print("manual  - ");
          Serial.println(bright);
          
          if (digitalRead(whiteon) == LOW)
            {
              Serial.println("white on");
              analogWrite (whiteled, (bright));
            }
          else
            {
              Serial.println ("white off");
              analogWrite(whiteled, 0);
            }
          delay(1000);
         }while (digitalRead(manualswitch) == LOW);   
    }
   else
    loop();
}

Full code pt2

void Temp(void)
{
        byte Tempi;
        byte Temppresent = 0;
        byte Temptype_s;
        byte Tempdata[12];
        byte Tempaddr[8];
      //  float celsius;
  
        if ( !ds.search(Tempaddr)) 
          {
            Serial.println("No more addresses.");
            Serial.println();
            ds.reset_search();
            delay(250);
            return;
          }
  
  //Serial.print("ROM =");
  //for( Tempi = 0; Tempi < 8; Tempi++) {
  //  Serial.write(' ');
  //  Serial.print(Tempaddr[Tempi], HEX);
  //}

        if (OneWire::crc8(Tempaddr, 7) != Tempaddr[7]) 
          {
            Serial.println("CRC is not valid!");
            return;
          }
 
  // the first ROM byte indicates which chip
        switch (Tempaddr[0]) 
          {
            case 0x10:
            //  Serial.println("  Chip = DS18S20");  // or old DS1820
            Temptype_s = 1;
            break;
            case 0x28:
            //Serial.println("  Chip = DS18B20");
            Temptype_s = 0;
            break;
            case 0x22:
           // Serial.println("  Chip = DS1822");
            Temptype_s = 0;
            break;
            default:
            Serial.println("Device is not a DS18x20 family device.");
            return;
          } 

        ds.reset();
        ds.select(Tempaddr);
        ds.write(0x44, 1);        // start conversion, with parasite power on at the end
        delay(1000);     // maybe 750ms is enough, maybe not
        // we might do a ds.depower() here, but the reset will take care of it.
  
        Temppresent = ds.reset();
        ds.select(Tempaddr);    
        ds.write(0xBE);         // Read Scratchpad

        //Serial.print("  Data = ");
        //Serial.print(Temppresent, HEX);
        Serial.print(" ");
      
        for ( Tempi = 0; Tempi < 9; Tempi++) 
        {           // we need 9 bytes
        Tempdata[Tempi] = ds.read();
       // Serial.print(data[Tempi], HEX);
       // Serial.print(" ");
        }
  
   //Serial.print(" CRC=");
  //Serial.print(OneWire::crc8(data, 8), HEX);
  //Serial.println();  

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (Tempdata[1] << 8) | Tempdata[0];
  if (Temptype_s) 
  {
    raw = raw << 3; // 9 bit resolution default
    if (Tempdata[7] == 0x10) 
    {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - Tempdata[6];
    }
  }
  else 
  {
    byte cfg = (Tempdata[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 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 = (float)raw / 16.0;
  tweettemp = ((celsius) +0.5);
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.println("C");
}


// This is where all the magic happens...
void loop() 
{
 // Temp();
    // Update the time via NTP server as often as the time you set at the top
    if(now()-ntpLastUpdate > ntpSyncTime) 
    {
      int trys=0;
      while(!getTimeAndDate() && trys<10)
      {
        trys++;
      }
      if(trys<10)
      {
        Serial.println("ntp server update success");
      }
      else
      {
        Serial.println("ntp server update failed");
      }
    }
   
    // Display the time if it has changed by more than a second.
    //if( now() != prevDisplay)
    //{
      //prevDisplay = now();
      //clockDisplay();  
    //}  
    
  totaltime = (hour()*60) + minute();
    //Serial.print("totaltime ");
    //Serial.println(totaltime);
  if (totaltime >= (1365) || totaltime <= (420))
  {
    Serial.print(hour());
    printDigits(minute());
    Serial.print(" - Night - ");
    Serial.print(totaltime);
    Serial.println(" min since midnight");
    analogWrite (whiteled, 0);
sprintf(msg, "Tanktweet Night, time: %d:%d  %d/%d/%d", hour(), minute(),day(), month(), year());
    if (twitter.post(msg)) 
    {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) 
      {
        Serial.println("OK.");
      } 
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
      } 
    else 
      {
        Serial.println("connection failed.");
      }
    delay (6000);
  }

Full code pt3

 else if (totaltime >= (1335) && totaltime <=1365)       // 1320 = 22:00 1350 = 22:15  MOONLIGHT         if later than 22:00 and earlier than 22:30 execute "moonlight"
            {
 sprintf(msg, "Tanktweet Moonlight time: %d:%d  %d/%d/%d", hour(), minute(),day(), month(), year());
    if (twitter.post(msg)) 
    {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) 
      {
        Serial.println("OK.");
      } 
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
      } 
    else 
      {
        Serial.println("connection failed.");
      }
            do
              {
               totaltime = (hour()*60) + minute();
               Serial.print(hour());
               printDigits(minute());
	       Serial.print (" -  moonlight  - ");
               Serial.print(totaltime);
               Serial.println(" mins since midnight");
               analogWrite (whiteled, 20);
               delay (4000);
              }while (totaltime <= (1365)); // 1350 = 22:15 execute while it is before 22:15
            }
            
            
            
            
            
         else if (totaltime >= (1325) && totaltime <= (1335))          //1275 = 21:45 1320 = 22:00  SUNSET  if later than 21:45 and earlier than 22:00 execute "sunset"
	      {
sprintf(msg, "Tanktweet Sunset time: %d:%d  %d/%d/%d", hour(), minute(),day(), month(), year());
                if (twitter.post(msg)) 
    {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) 
      {
        Serial.println("OK.");
      } 
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
      } 
    else 
      {
        Serial.println("connection failed.");
      }
                do
	          {  
		   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) 
		     {     
                      totaltime = (hour()*60) + minute();  
                      Serial.print(hour());
                      printDigits(minute());
		      Serial.print (" -  Sunset  - ");
                      Serial.print(totaltime);
                      Serial.println(" mins since midnight");  
                      Serial.print("fadevalue=");
  		      Serial.println(fadeValue);
                      analogWrite(whiteled, fadeValue); 
	              delay(4000); 
		      }             
                }while (totaltime <= (1335)); //1320 = 22:00 execute while it is before 22:00
             }
             
             
           
      else if (totaltime >= (450) && totaltime <= (1325)) // 465 = 07:45 1305 = 21:45 DAYTIME if it is later than 07:30 and earlier than 21:45 execute "daytime"
 {     
   char msg[] = "Tanktweet Daytime: %d:%d %dC "; 
   
  Temp();

   sprintf(msg, "Tanktweet Daylight time: %d:%d %dC ", hour(), minute(), (Tweettemp) );  //(tweettemp)
   
   Serial.println("about to tweet");
      

   Serial.println(msg);  
                                  //seems to stop here note - check Twitter libaries re: twitter.post ? is this the actual command to post? there must be a simplier way????
    // Serial.println((twitter.post(msg)));
   if (twitter.post(msg)) 
    {
      Serial.println("if (twitter.post(msg))");
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    
    if (status == 200) 
      {
        Serial.println("OK.");
      }
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
//    else 
//      {
//        Serial.println("connection failed.");
//      }
    }
               if (digitalRead (stormswitch) == HIGH)
               {
               do
	{
                totaltime = (hour()*60) + minute();
                Serial.print(hour());
                     printDigits(minute());
		     Serial.print (" -  Daytime  - ");
                     Serial.print(totaltime);
                     Serial.print(" mins since midnight ");
                     Serial.print(celsius);
                     Serial.println("C");
		     analogWrite(whiteled, 255);
                     delay(5000);
                }while(digitalRead (stormswitch) == HIGH);
               }
                else if (digitalRead (stormswitch) == LOW)
                      {
                       storm();
                        }
   //stuff here
                      }while (totaltime <= (1325));
                
                
                
   if (totaltime >= (420) && totaltime <= (450))           // 450 = 07:30 465 = 07:45 after 07:30 and before 07:45 execute "sunrise"
            {
sprintf(msg, "Tanktweet Sunrise time: %d:%d  %d/%d/%d", hour(), minute(),day(), month(), year());
    if (twitter.post(msg)) 
    {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) 
      {
        Serial.println("OK.");
      } 
     else 
      {
        Serial.print("failed : code ");
        Serial.println(status);
       }
      } 
    else 
      {
        Serial.println("connection failed.");
      }     
              do
		{
		for(int fadeValue = 0; fadeValue < 255; fadeValue +=1)
		  {
                   totaltime = (hour()*60) + minute();
                   Serial.print(hour());
                   printDigits(minute());
		   Serial.print (" -  Sunrise  - ");
                   Serial.print(totaltime);
                   Serial.println(" mins since midnight");
                   Serial.print("fadevalue=");
  		   Serial.println(fadeValue);
		   analogWrite(whiteled, fadeValue);
		   delay (7050);
		  }
		}while (totaltime <= (525)); // 450 = 07:30 execute until 07:30
            }
    
//put code here


}

This is what I get out of the Serial port

Aquiring IP address from DHCP server, this may take a few mins.......
DHCP Success
Getting Time.........
Connecting to Twitter.....
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Mon, 17 Feb 2014 15:05:35 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

OKOK.
Temperature = 25.94C
about to tweet
Tanktweet Daylight time: 15:5 26C

however if I comment out the call to the Temp function I get

Aquiring IP address from DHCP server, this may take a few mins.......
DHCP Success
Getting Time.........
Connecting to Twitter.....
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Mon, 17 Feb 2014 15:09:02 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

OKOK.
about to tweet
Tanktweet Daylight time: 15:8 0C
if (twitter.post(msg))
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Mon, 17 Feb 2014 15:09:03 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

OKOK.
15:08 - Daytime - 908 mins since midnight 0.00C
15:08 - Daytime - 908 mins since midnight 0.00C
15:09 - Daytime - 909 mins since midnight 0.00C
15:09 - Daytime - 909 mins since midnight 0.00C

This is correct and the code will continue to run, however if i leave the call to the temp function in it just sits at what I quoted above

char msg[] = "Tanktweet Bootup Time:";

The size of the array is fixed.

 sprintf(msg, "Tanktweet Moonlight time: %d:%d  %d/%d/%d", hour(), minute(),day(), month(), year());

There is no way that all of the will fit in the array.

Hi PAuls

Thanks for your reply, I had redeclared the size just before the "daytime" mode however just to be sure I went through and ensured it was set at the max possible length.

I still get the same issue :frowning:

Thanks

STeve

Post your current code as an attachment, instead of splattering it across multiple posts. Use the Additional OPTIONS link below the text entry window.

Hi Paul

Code attached only thing different is the twitter key.

Thanks

Steve

postable.ino (16.4 KB)

char msg[] = "Tanktweet Moonlight Time 99:99 99C";

No.

char msg[80];

Yes.

  else
  {
  loop();
  }

NEVER! You call storm() from loop() and loop() from storm(). That's a sure way to run out of stack space.

Thanks paul, I hadnt spotter my faux pax on the loop storm part d'oh I wasnt sure how to do the Char [80] part, I'll try it now :slight_smile:

D'oh still didnt work, gets to the same point then fails.

Aquiring IP address from DHCP server, this may take a few mins.......
DHCP Success
Getting Time.........
Connecting to Twitter.....
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Mon, 17 Feb 2014 18:40:48 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic

OKOK.
Temperature = 26.00C
about to tweet
Tanktweet Daylight time: 18:40 26C

the OKOK part is where it tweets the bootup message, then after Tanktweet Daylight time: 18:40 26C it should tweet that info but it just sits there :-S

I made my Char msg [40] as I shouldnt need to use more that 40 characters, have i misinterpreted this? does it mean the number of characters?

Thanks

Steve

I just noticed some things.

#define whiteled 11
OneWire  ds(10);// on pin 10 (a 4.7K resistor is necessary)

Pin 10 is the chip select pin for the Ethernet card. You can't use it for the OneWire bus pin.
Pin 11 is one of the SPI pins, which is the mechanism that the Arduino and Ethernet shield use to communicate. You can't use it for the LED pin.

awesome - didnt know that, Thanks you've been really helpful :slight_smile: I'll try changing them and test again :slight_smile:

OK- put your underwear on over your trousers - YOUR A SUPERHERO !!!

I could kiss you it works!

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: