Go Down

Topic: Please help with a bit of code - only some loops (Read 1 time) previous topic - next topic



I'm using an wishield and SHT15 sensor to Tweet the sensor readings (temp and humidity) every 90 minutes.  The problem I'm encountering is the arduino is only looping part of the loop call

It sends the first Tweet and then gets stuck and tries to resend tweet1 continuously instead of moving on to tweet2 90 minutes later.  I'm hoping this is a minor error in my syntax.   :)

Code: [Select]

* A simple sketch that uses WiServer to send a tweet with the current system time every 90 minutes

#include <WiServer.h>
#include <SHT1x.h>

#define WIRELESS_MODE_INFRA      1
#define WIRELESS_MODE_ADHOC      2

#define dataPin  7
#define clockPin 6
SHT1x sht1x(dataPin,clockPin); //data and clock pins for SHT1x

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};      // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};      // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"2WIRE187"};            // max 32 bytes

unsigned char security_type = 3;      // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"XXXXXX"};      // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,      // Key 0
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 1
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 2
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00      // Key 3

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

// Auth string for the Twitter account
char* auth = "aXXXX2JAXXXXXXXXXXXXXozMTXXamI="; // Base64 encoded USERNAME:PASSWORD
long intervalHum = 30000; //set the interval for temp and humidity to be checked
long intervalTweet = 5400000; // Set the interval for tweets - 1.5 Hrs
int humidity; // Init humidty variable
int temp_f; // Init temp variable
int tweet = 0; /*Used to tweet once on initial startup. Otherwise
                would have to wait until <intervalTweet> expires */
long previousMillis = 0;
long previousMillisTweet = 0;
long tweetTime = 0; // Time (in millis) when the next tweet should be sent

// This function generates a message with the current system time
void currentTemp() {
  WiServer.printTime(millis()); // Append time Arduino has been running - gets around duplicate tweet filtering
  WiServer.print("] ");
  WiServer.print("Humidor temp and humidity is: ");
  WiServer.print("° F and ");

// A request that sends a Tweet using the currentTime function
TWEETrequest sentMyTweet(auth, currentTemp);

void setup()
   // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
 // Enable Serial output and ask WiServer to generate log messages (optional)

void loop()
 if (tweet == 0) // Tweets once at inital startup.  Next tweet won't happen for <intervalTweet>
   ++tweet; // Increment initial tweet count
// Set the temp and humidity to be checked every 30 seconds (defined by <intervalHum> global variable
// If you don't set this to a reasonable number the arduino seems to get so busy running the
// temp check functions it doesn't have time to run the WiShield TCP/IP stack - ergo all IP comms break
 if (millis() - previousMillis > intervalHum)
   previousMillis = millis();                        
   temp_f = sht1x.readTemperatureF();
   humidity = sht1x.readHumidity();
// Set a tweet to occur every two hours (defined by <intervalTweet> global variable)
 if (millis() - previousMillisTweet > intervalTweet)
    previousMillisTweet = millis();



Here's the output in the serial monitor:
Code: [Select]
Connected to twitter.com
TX 283 bytes
RX 0 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com
RX 164 bytes from twitter.com

And if you go to the Twitter feed here: www.twitter.com/myhumidor you'll see the time stamp of each Tweet is 39 ms, indicating the arduino posts the first tweet, gets stuck, reboots, and repeats.

Thanks in advance for your help!


i would use "unsigned long int"s for the time stamp/interval variables, because:
they react flawless on single overflows...

if there was a reset after the first tweet, there might be a hardware problem?
maybe low voltage?
that would explain the time stamp...

u could try the code without tweets but with Serial.print()-s...



Right now the code uses the time the wishield has been running as a reference for when to send tweets with: WiServer.printTime(millis())

Is there a way I can simply use how long the arduino has been running?  What code would that be?


Hi Riddick,

I tried setting the interval Tweet to unsigned long and that didn't change anything.  If the reset was happening after 30 days I could see that being an issue, but in the first minute, something else is going on.

I've tried running it on a 9v battery as well and the same problem occurs.


millis() says the time since reset (modulo appr. 49 days)...

did u try using the USB serial port?
or is there none?

if u cant use the serial port, u could toggle the pin#13 LED every 10sec with digitalWrite(13,...)...?


Go Up