#include // 1) enabling and disabling the serial port in the main loop seems to cause a disconnect/reset or something // 2) D7 seems like it gets used for something behind the scenes // OAuth Key #define TOKEN "29589296_REPLACE_ME_WITH_YOUR_TOKEN_PUoMeZz" // Twitter Proxy #define LIB_DOMAIN "arduino-tweet.appspot.com" // TCPClient allows us to connect via arduino-tweet.appspot.com to Twitter TCPClient client; //-------------------------------------------------------------------------- // Pin out // // MAX_AI_PIN Pin A7, AI input from the MaxBotix sensor // MAX_EN_PIN Pin D2, Enables the Maxbotix sensor // LOOP_INTERVAL_MS Time between tweets in milliseconds. Twitter likes this to be > 60 seconds so we use 120k = 2 minutes // // V_PER_ADC_CODE, CM_PER_V Scale factors //-------------------------------------------------------------------------- const int MAX_EN_PIN = D2; const int MAX_AI_PIN = A7; const int LOOP_INTERVAL_MS = 120000; const float V_PER_ADC_CODE = 805.66e-6; //3.3V / 4096 codes const float CM_PER_V = 303.0; // 3.3 V/1000 cm = 3.3 mV/cm, or 303.0 cm/mV //-------------------------------------------------------------------------- // Globals // // tweet test buffer that we use to store the tweet before we send it // rssi Wi-Fi signal strength // range_cm Measured range from the sensor // oldTime_ms timestamp of the last time we tweeted // i Twitter will silently discard identical tweets, so we add a count to make // sure all our tweets get posted //-------------------------------------------------------------------------- const int MAXSTRLEN = 256; int rssi_dbm=0; double range_cm=0; unsigned long oldTime_ms=0; unsigned long i=0; char tweet[MAXSTRLEN]; //-------------------------------------------------------------------------- // Send to Twitter! // // Courtesy https://community.spark.io/t/tutorial-sending-tweets-from-spark-core/2894/7 //-------------------------------------------------------------------------- void SendTweet() { delay(1000); client.connect(LIB_DOMAIN, 80); client.println("POST /update HTTP/1.0"); client.println("Host: " LIB_DOMAIN); client.print("Content-Length: "); client.println(strlen(tweet)+strlen(TOKEN)+14); client.println(); client.print("token="); client.print(TOKEN); client.print("&status="); client.println(tweet); } void setup() { //-------------------------------------------------------------------------- // Register web interface with the cloud // // range_cm measured range from the ultrasonic sensor // rssi received signal strength of the Wi-Fi SSID beacon // selftest stub diagnostic function for now //-------------------------------------------------------------------------- Spark.variable("range_cm", &range_cm, DOUBLE); Spark.variable("rssi_dbm", &rssi_dbm, INT); //-------------------------------------------------------------------------- // Configure IO pins on the module //-------------------------------------------------------------------------- digitalWrite(MAX_EN_PIN, HIGH); pinMode(MAX_EN_PIN, OUTPUT); digitalWrite(MAX_EN_PIN, HIGH); Serial.begin(9600); i=0; } void loop() { //-------------------------------------------------------------------------- // Wait until the full loop interval before doing all the stuff in the loop. // This is an inefficient busy wait, and there surely must be a better way // to sleep until exactly the right time, wake up, measure, then go back // to sleep. // FFV: improve this sleep efficiency //-------------------------------------------------------------------------- unsigned long newTime_ms = millis(); if ((newTime_ms - oldTime_ms) < LOOP_INTERVAL_MS) { delay(50); return; } oldTime_ms = newTime_ms; //-------------------------------------------------------------------------- // Read the range from the MAXBOTIX module and scale it to cm //-------------------------------------------------------------------------- int adcReading = analogRead(MAX_AI_PIN); range_cm = adcReading*V_PER_ADC_CODE*CM_PER_V; //-------------------------------------------------------------------------- // Do housekeeping chores //-------------------------------------------------------------------------- rssi_dbm=WiFi.RSSI(); //-------------------------------------------------------------------------- // Send the tweet, and write it out the UART too to simplify debugging //-------------------------------------------------------------------------- sprintf(tweet, "i: %d, wireless (dBm): %d, range (cm): %d", i, rssi_dbm, (unsigned long) ceil(range_cm)); Serial.write(tweet); Serial.write(", "); SendTweet(); i++; }