Simple code to demonstrate how to make a Twitter Post (No OAuth required -> via SuperTweet service). MBed will auto-post an initial Twitter message during boot, also useful to discover the microcontroller IP address when using DHCP for the ethernet configuration. Additional button functionality to send custom Twitter message (Pull up resistor connected to P20, button directly to pin and Ground - Push button will set GND to P20). Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en

Dependencies:   EthernetNetIf mbed SuperTweet

Committer:
botdream
Date:
Thu Oct 27 22:46:39 2011 +0000
Revision:
0:0cbe113243b8
GeekSessionLab talk.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
botdream 0:0cbe113243b8 1 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 2 /*
botdream 0:0cbe113243b8 3 Copyright (c) 2010 ARM Ltd
botdream 0:0cbe113243b8 4
botdream 0:0cbe113243b8 5 Permission is hereby granted, free of charge, to any person obtaining a copy
botdream 0:0cbe113243b8 6 of this software and associated documentation files (the "Software"), to deal
botdream 0:0cbe113243b8 7 in the Software without restriction, including without limitation the rights
botdream 0:0cbe113243b8 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
botdream 0:0cbe113243b8 9 copies of the Software, and to permit persons to whom the Software is
botdream 0:0cbe113243b8 10 furnished to do so, subject to the following conditions:
botdream 0:0cbe113243b8 11
botdream 0:0cbe113243b8 12 The above copyright notice and this permission notice shall be included in
botdream 0:0cbe113243b8 13 all copies or substantial portions of the Software.
botdream 0:0cbe113243b8 14
botdream 0:0cbe113243b8 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
botdream 0:0cbe113243b8 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
botdream 0:0cbe113243b8 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
botdream 0:0cbe113243b8 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
botdream 0:0cbe113243b8 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
botdream 0:0cbe113243b8 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
botdream 0:0cbe113243b8 21 THE SOFTWARE.
botdream 0:0cbe113243b8 22 */
botdream 0:0cbe113243b8 23 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 24 #include "mbed.h"
botdream 0:0cbe113243b8 25 #include "EthernetNetIf.h"
botdream 0:0cbe113243b8 26 #include "SuperTweetV1XML.h"
botdream 0:0cbe113243b8 27 #include "twitter_config.h"
botdream 0:0cbe113243b8 28 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 29 DigitalOut myled(LED1);
botdream 0:0cbe113243b8 30 DigitalIn button(p20);
botdream 0:0cbe113243b8 31 Serial pc(USBTX, USBRX); // tx, rx
botdream 0:0cbe113243b8 32 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 33 #define internaldebug // send debug messages to USB Serial port (9600,1,N)
botdream 0:0cbe113243b8 34 //#define dhcpenable // auto-setup IP Address from DHCP router
botdream 0:0cbe113243b8 35 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 36 // Ethernet Object Setup
botdream 0:0cbe113243b8 37 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 38 #ifdef dhcpenable
botdream 0:0cbe113243b8 39 EthernetNetIf eth;
botdream 0:0cbe113243b8 40 #else
botdream 0:0cbe113243b8 41 EthernetNetIf eth(
botdream 0:0cbe113243b8 42 IpAddr(192,168,1,100), //IP Address
botdream 0:0cbe113243b8 43 IpAddr(255,255,255,0), //Network Mask
botdream 0:0cbe113243b8 44 IpAddr(192,168,1,254), //Gateway
botdream 0:0cbe113243b8 45 IpAddr(192,168,1,254) //DNS
botdream 0:0cbe113243b8 46 );
botdream 0:0cbe113243b8 47 #endif
botdream 0:0cbe113243b8 48 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 49 // GLOBAL VARS / FUNCTIONS
botdream 0:0cbe113243b8 50 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 51 // SuperTweet - Twitter account data (twitter_config.h)
botdream 0:0cbe113243b8 52 //#define YOUR_ACCOUNT "twitterid"
botdream 0:0cbe113243b8 53 //#define YOUR_PASSWORD "passwd"
botdream 0:0cbe113243b8 54
botdream 0:0cbe113243b8 55 SuperTweetV1XML st(YOUR_ACCOUNT, YOUR_PASSWORD);
botdream 0:0cbe113243b8 56
botdream 0:0cbe113243b8 57 /**
botdream 0:0cbe113243b8 58 * Callback function for postStatusesUpdate.
botdream 0:0cbe113243b8 59 *
botdream 0:0cbe113243b8 60 * @param buf A pointer to a buffer.
botdream 0:0cbe113243b8 61 * @param siz A size of the buffer.
botdream 0:0cbe113243b8 62 */
botdream 0:0cbe113243b8 63 void func(char *buf, size_t siz)
botdream 0:0cbe113243b8 64 {
botdream 0:0cbe113243b8 65 #if 0
botdream 0:0cbe113243b8 66 // This is for checking a response.
botdream 0:0cbe113243b8 67 for(int i = 0; i < siz; i++)
botdream 0:0cbe113243b8 68 {
botdream 0:0cbe113243b8 69 printf("%c", buf[i]);
botdream 0:0cbe113243b8 70 }
botdream 0:0cbe113243b8 71 #endif
botdream 0:0cbe113243b8 72 }
botdream 0:0cbe113243b8 73 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 74 // MISC
botdream 0:0cbe113243b8 75 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 76 // Hack - Expose 'special' internal mbed ethernet functions
botdream 0:0cbe113243b8 77 extern "C" void mbed_reset();
botdream 0:0cbe113243b8 78 //extern "C" void mbed_mac_address(char *mac);
botdream 0:0cbe113243b8 79
botdream 0:0cbe113243b8 80 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 81 // Global Functions
botdream 0:0cbe113243b8 82 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 83 void SendTweet(char *pMessage)
botdream 0:0cbe113243b8 84 {
botdream 0:0cbe113243b8 85 #ifdef internaldebug
botdream 0:0cbe113243b8 86 printf("\r\n %s \r\n",pMessage);
botdream 0:0cbe113243b8 87 #endif
botdream 0:0cbe113243b8 88
botdream 0:0cbe113243b8 89 HTTPResult r = st.postStatusesUpdate(std::string(pMessage), func);
botdream 0:0cbe113243b8 90 printf("r=%d\n", (int)r);
botdream 0:0cbe113243b8 91 /*
botdream 0:0cbe113243b8 92 * Note:
botdream 0:0cbe113243b8 93 * I don't know why sometime it get a error.
botdream 0:0cbe113243b8 94 * I think it a bug in a mbed library.
botdream 0:0cbe113243b8 95 */
botdream 0:0cbe113243b8 96 if (r != 0)
botdream 0:0cbe113243b8 97 {
botdream 0:0cbe113243b8 98 printf("Resetting...\n");
botdream 0:0cbe113243b8 99 mbed_reset();
botdream 0:0cbe113243b8 100 }
botdream 0:0cbe113243b8 101 }
botdream 0:0cbe113243b8 102 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 103 // MAIN
botdream 0:0cbe113243b8 104 //---------------------------------------------------------------------------------------------
botdream 0:0cbe113243b8 105 int main()
botdream 0:0cbe113243b8 106 {
botdream 0:0cbe113243b8 107 char text[1024];
botdream 0:0cbe113243b8 108
botdream 0:0cbe113243b8 109 // Set Serial Port Transfer Rate
botdream 0:0cbe113243b8 110 pc.baud(115200);
botdream 0:0cbe113243b8 111
botdream 0:0cbe113243b8 112 //--------------------------------------------------------
botdream 0:0cbe113243b8 113 // Setting Ethernet
botdream 0:0cbe113243b8 114 //--------------------------------------------------------
botdream 0:0cbe113243b8 115 #ifdef internaldebug
botdream 0:0cbe113243b8 116 printf("\r\nSetting up Ethernet interface!\r\n");
botdream 0:0cbe113243b8 117 #endif
botdream 0:0cbe113243b8 118 // Create return object for error check
botdream 0:0cbe113243b8 119 EthernetErr ethErr = eth.setup();
botdream 0:0cbe113243b8 120 if(ethErr)
botdream 0:0cbe113243b8 121 {
botdream 0:0cbe113243b8 122 #ifdef internaldebug
botdream 0:0cbe113243b8 123 printf("\r\nError %d in Ethernet setup.\r\n", ethErr);
botdream 0:0cbe113243b8 124 #endif
botdream 0:0cbe113243b8 125 return -1;
botdream 0:0cbe113243b8 126 }
botdream 0:0cbe113243b8 127 #ifdef internaldebug
botdream 0:0cbe113243b8 128 printf("\r\nEthernet setup completed with success!\r\n");
botdream 0:0cbe113243b8 129 #endif
botdream 0:0cbe113243b8 130 //--------------------------------------------------------
botdream 0:0cbe113243b8 131
botdream 0:0cbe113243b8 132 wait(5);
botdream 0:0cbe113243b8 133
botdream 0:0cbe113243b8 134 //--------------------------------------------------------
botdream 0:0cbe113243b8 135 // Post IP/MAC address into Twitter
botdream 0:0cbe113243b8 136 //--------------------------------------------------------
botdream 0:0cbe113243b8 137 // Get IP Address
botdream 0:0cbe113243b8 138 IpAddr ip = eth.getIp();
botdream 0:0cbe113243b8 139
botdream 0:0cbe113243b8 140 // Sending Twitter Message
botdream 0:0cbe113243b8 141 snprintf(text, sizeof(text), "Auto-posting from ARM Microcontroller mbed NXP LPC1768 from: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
botdream 0:0cbe113243b8 142
botdream 0:0cbe113243b8 143 SendTweet(&text[0]);
botdream 0:0cbe113243b8 144 //--------------------------------------------------------
botdream 0:0cbe113243b8 145
botdream 0:0cbe113243b8 146 // main loop
botdream 0:0cbe113243b8 147 bool flag_sending = false;
botdream 0:0cbe113243b8 148 while(1)
botdream 0:0cbe113243b8 149 {
botdream 0:0cbe113243b8 150 if(!button)
botdream 0:0cbe113243b8 151 {
botdream 0:0cbe113243b8 152 if(flag_sending)
botdream 0:0cbe113243b8 153 continue;
botdream 0:0cbe113243b8 154
botdream 0:0cbe113243b8 155 myled = 1;
botdream 0:0cbe113243b8 156
botdream 0:0cbe113243b8 157 // Sending Twitter Message (custom)
botdream 0:0cbe113243b8 158 snprintf(text, sizeof(text), "Button Pressed Message sent from ARM Microcontroller mbed NXP LPC1768");
botdream 0:0cbe113243b8 159
botdream 0:0cbe113243b8 160 SendTweet(&text[0]);
botdream 0:0cbe113243b8 161
botdream 0:0cbe113243b8 162 flag_sending = true;
botdream 0:0cbe113243b8 163 }
botdream 0:0cbe113243b8 164 else
botdream 0:0cbe113243b8 165 {
botdream 0:0cbe113243b8 166 myled = 0;
botdream 0:0cbe113243b8 167 flag_sending = false;
botdream 0:0cbe113243b8 168 }
botdream 0:0cbe113243b8 169
botdream 0:0cbe113243b8 170 // debounce time!
botdream 0:0cbe113243b8 171 wait(0.1);
botdream 0:0cbe113243b8 172 }
botdream 0:0cbe113243b8 173 }
botdream 0:0cbe113243b8 174 //---------------------------------------------------------------------------------------------