wf

Dependencies:   Adafruit_GFX WIZnetInterface mbed

Fork of Tweeting_Machine_HelloWorld_WIZwiki-W7500 by IOP

Committer:
joon874
Date:
Thu Oct 15 00:18:52 2015 +0000
Revision:
0:8548416648df
Child:
1:5f4bd210e2f0
Tweeting Machine with WIZwiki-W7500 for WIZnet Academy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joon874 0:8548416648df 1 #include "mbed.h"
joon874 0:8548416648df 2 #include "EthernetInterface.h"
joon874 0:8548416648df 3 #include "Adafruit_SSD1306.h"
joon874 0:8548416648df 4
joon874 0:8548416648df 5 //#define TOKEN "3252156354-fG0b1utXYAg5IqeJNMSJFlenx1rgSRXm5wgk21l"
joon874 0:8548416648df 6 #define TOKEN "UserToken"
joon874 0:8548416648df 7
joon874 0:8548416648df 8 // an SPI sub-class that provides a constructed default
joon874 0:8548416648df 9 class I2CPreInit : public I2C
joon874 0:8548416648df 10 {
joon874 0:8548416648df 11 public:
joon874 0:8548416648df 12 I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
joon874 0:8548416648df 13 {
joon874 0:8548416648df 14 frequency(100000);
joon874 0:8548416648df 15 start();
joon874 0:8548416648df 16 };
joon874 0:8548416648df 17 };
joon874 0:8548416648df 18
joon874 0:8548416648df 19 I2CPreInit gI2C(PA_10,PA_9);
joon874 0:8548416648df 20 Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
joon874 0:8548416648df 21
joon874 0:8548416648df 22
joon874 0:8548416648df 23 int main()
joon874 0:8548416648df 24 {
joon874 0:8548416648df 25 int phy_link;
joon874 0:8548416648df 26 printf("Wait a second...\r\n");
joon874 0:8548416648df 27 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x02};
joon874 0:8548416648df 28
joon874 0:8548416648df 29 EthernetInterface eth;
joon874 0:8548416648df 30 eth.init(mac_addr); //Use DHCP
joon874 0:8548416648df 31
joon874 0:8548416648df 32 eth.connect();
joon874 0:8548416648df 33
joon874 0:8548416648df 34 while(1){
joon874 0:8548416648df 35
joon874 0:8548416648df 36 /* phy link */
joon874 0:8548416648df 37 do{
joon874 0:8548416648df 38 phy_link = eth.ethernet_link();
joon874 0:8548416648df 39 printf("...");
joon874 0:8548416648df 40 wait(2);
joon874 0:8548416648df 41 }while(!phy_link);
joon874 0:8548416648df 42 printf("\r\n");
joon874 0:8548416648df 43
joon874 0:8548416648df 44 printf("IP Address is %s\r\n", eth.getIPAddress());
joon874 0:8548416648df 45
joon874 0:8548416648df 46 /* TCP socket connect */
joon874 0:8548416648df 47 TCPSocketConnection sock;
joon874 0:8548416648df 48 sock.connect("arduino-tweet.appspot.com", 80);
joon874 0:8548416648df 49
joon874 0:8548416648df 50 printf("connected\r\n\r\n");
joon874 0:8548416648df 51
joon874 0:8548416648df 52 /* typing message */
joon874 0:8548416648df 53 printf("typing twitter message :");
joon874 0:8548416648df 54
joon874 0:8548416648df 55 char* message;
joon874 0:8548416648df 56 scanf("%s",message);
joon874 0:8548416648df 57 printf("%s",message);
joon874 0:8548416648df 58 printf("\r\n\r\n");
joon874 0:8548416648df 59
joon874 0:8548416648df 60 int len = strlen(message);
joon874 0:8548416648df 61
joon874 0:8548416648df 62 /* data length measure */
joon874 0:8548416648df 63 char data_len[2]={0};
joon874 0:8548416648df 64 int ten=0;
joon874 0:8548416648df 65 int one=0;
joon874 0:8548416648df 66
joon874 0:8548416648df 67 int length = len + sizeof(TOKEN) - 1 + 14;
joon874 0:8548416648df 68
joon874 0:8548416648df 69 printf("length : %d\r\n\r\n",length);
joon874 0:8548416648df 70
joon874 0:8548416648df 71 ten = length/10;
joon874 0:8548416648df 72 one = length%10;
joon874 0:8548416648df 73 data_len[1] = one + 48;
joon874 0:8548416648df 74 data_len[0] = ten + 48;
joon874 0:8548416648df 75
joon874 0:8548416648df 76 char *cmd1 = "POST http://arduino-tweet.appspot.com/update HTTP/1.0\r\nContent-Length:";
joon874 0:8548416648df 77 char *cmd2 = data_len;
joon874 0:8548416648df 78 char *cmd3 = "\r\n\r\ntoken=";
joon874 0:8548416648df 79 char *cmd4 = TOKEN;
joon874 0:8548416648df 80 char *cmd5 = "&status=";
joon874 0:8548416648df 81 char *cmd6 = message;
joon874 0:8548416648df 82 char *cmd7 = "\r\n";
joon874 0:8548416648df 83
joon874 0:8548416648df 84 char send_data[1024];
joon874 0:8548416648df 85 char buffer[1024] = {0};
joon874 0:8548416648df 86 sprintf(send_data, "%s%s%s%s%s%s%s", cmd1, cmd2, cmd3, cmd4, cmd5, cmd6, cmd7);
joon874 0:8548416648df 87
joon874 0:8548416648df 88 sock.send_all(send_data, sizeof(send_data)-1);
joon874 0:8548416648df 89
joon874 0:8548416648df 90 printf("%s\r\n",send_data);
joon874 0:8548416648df 91 printf("send message done\r\n");
joon874 0:8548416648df 92
joon874 0:8548416648df 93 sock.receive(buffer, sizeof(buffer)-1);
joon874 0:8548416648df 94
joon874 0:8548416648df 95 printf("%s\r\n",buffer);
joon874 0:8548416648df 96
joon874 0:8548416648df 97 gOled.begin();
joon874 0:8548416648df 98 gOled.printf("%s\r\n",message);
joon874 0:8548416648df 99 gOled.display();
joon874 0:8548416648df 100
joon874 0:8548416648df 101 wait(70.0);
joon874 0:8548416648df 102
joon874 0:8548416648df 103 gOled.clearDisplay();
joon874 0:8548416648df 104
joon874 0:8548416648df 105 //sock.close();
joon874 0:8548416648df 106 //eth.disconnect();
joon874 0:8548416648df 107
joon874 0:8548416648df 108 };
joon874 0:8548416648df 109
joon874 0:8548416648df 110 }