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
main.cpp
00001 //--------------------------------------------------------------------------------------------- 00002 /* 00003 Copyright (c) 2010 ARM Ltd 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 //--------------------------------------------------------------------------------------------- 00024 #include "mbed.h" 00025 #include "EthernetNetIf.h" 00026 #include "SuperTweetV1XML.h" 00027 #include "twitter_config.h" 00028 //--------------------------------------------------------------------------------------------- 00029 DigitalOut myled(LED1); 00030 DigitalIn button(p20); 00031 Serial pc(USBTX, USBRX); // tx, rx 00032 //--------------------------------------------------------------------------------------------- 00033 #define internaldebug // send debug messages to USB Serial port (9600,1,N) 00034 //#define dhcpenable // auto-setup IP Address from DHCP router 00035 //--------------------------------------------------------------------------------------------- 00036 // Ethernet Object Setup 00037 //--------------------------------------------------------------------------------------------- 00038 #ifdef dhcpenable 00039 EthernetNetIf eth; 00040 #else 00041 EthernetNetIf eth( 00042 IpAddr(192,168,1,100), //IP Address 00043 IpAddr(255,255,255,0), //Network Mask 00044 IpAddr(192,168,1,254), //Gateway 00045 IpAddr(192,168,1,254) //DNS 00046 ); 00047 #endif 00048 //--------------------------------------------------------------------------------------------- 00049 // GLOBAL VARS / FUNCTIONS 00050 //--------------------------------------------------------------------------------------------- 00051 // SuperTweet - Twitter account data (twitter_config.h) 00052 //#define YOUR_ACCOUNT "twitterid" 00053 //#define YOUR_PASSWORD "passwd" 00054 00055 SuperTweetV1XML st(YOUR_ACCOUNT, YOUR_PASSWORD); 00056 00057 /** 00058 * Callback function for postStatusesUpdate. 00059 * 00060 * @param buf A pointer to a buffer. 00061 * @param siz A size of the buffer. 00062 */ 00063 void func(char *buf, size_t siz) 00064 { 00065 #if 0 00066 // This is for checking a response. 00067 for(int i = 0; i < siz; i++) 00068 { 00069 printf("%c", buf[i]); 00070 } 00071 #endif 00072 } 00073 //--------------------------------------------------------------------------------------------- 00074 // MISC 00075 //--------------------------------------------------------------------------------------------- 00076 // Hack - Expose 'special' internal mbed ethernet functions 00077 extern "C" void mbed_reset(); 00078 //extern "C" void mbed_mac_address(char *mac); 00079 00080 //--------------------------------------------------------------------------------------------- 00081 // Global Functions 00082 //--------------------------------------------------------------------------------------------- 00083 void SendTweet(char *pMessage) 00084 { 00085 #ifdef internaldebug 00086 printf("\r\n %s \r\n",pMessage); 00087 #endif 00088 00089 HTTPResult r = st.postStatusesUpdate(std::string(pMessage), func); 00090 printf("r=%d\n", (int)r); 00091 /* 00092 * Note: 00093 * I don't know why sometime it get a error. 00094 * I think it a bug in a mbed library. 00095 */ 00096 if (r != 0) 00097 { 00098 printf("Resetting...\n"); 00099 mbed_reset(); 00100 } 00101 } 00102 //--------------------------------------------------------------------------------------------- 00103 // MAIN 00104 //--------------------------------------------------------------------------------------------- 00105 int main() 00106 { 00107 char text[1024]; 00108 00109 // Set Serial Port Transfer Rate 00110 pc.baud(115200); 00111 00112 //-------------------------------------------------------- 00113 // Setting Ethernet 00114 //-------------------------------------------------------- 00115 #ifdef internaldebug 00116 printf("\r\nSetting up Ethernet interface!\r\n"); 00117 #endif 00118 // Create return object for error check 00119 EthernetErr ethErr = eth.setup(); 00120 if(ethErr) 00121 { 00122 #ifdef internaldebug 00123 printf("\r\nError %d in Ethernet setup.\r\n", ethErr); 00124 #endif 00125 return -1; 00126 } 00127 #ifdef internaldebug 00128 printf("\r\nEthernet setup completed with success!\r\n"); 00129 #endif 00130 //-------------------------------------------------------- 00131 00132 wait(5); 00133 00134 //-------------------------------------------------------- 00135 // Post IP/MAC address into Twitter 00136 //-------------------------------------------------------- 00137 // Get IP Address 00138 IpAddr ip = eth.getIp(); 00139 00140 // Sending Twitter Message 00141 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]); 00142 00143 SendTweet(&text[0]); 00144 //-------------------------------------------------------- 00145 00146 // main loop 00147 bool flag_sending = false; 00148 while(1) 00149 { 00150 if(!button) 00151 { 00152 if(flag_sending) 00153 continue; 00154 00155 myled = 1; 00156 00157 // Sending Twitter Message (custom) 00158 snprintf(text, sizeof(text), "Button Pressed Message sent from ARM Microcontroller mbed NXP LPC1768"); 00159 00160 SendTweet(&text[0]); 00161 00162 flag_sending = true; 00163 } 00164 else 00165 { 00166 myled = 0; 00167 flag_sending = false; 00168 } 00169 00170 // debounce time! 00171 wait(0.1); 00172 } 00173 } 00174 //---------------------------------------------------------------------------------------------
Generated on Tue Jul 26 2022 21:15:49 by 1.7.2