This Program is based on the configuration of ThingSpeak using ARMmbed

Dependencies:   Adafruit_GFX WIZnetInterface mbed

Fork of Tweeting_Machine_HelloWorld_WIZwiki-W7500 by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "Adafruit_SSD1306.h"
00004 
00005 #define TOKEN "904626807491530753-sOaChz44g9YFS7EuvRkR75l1W9T303K"
00006 //#define TOKEN "UserToken"
00007 AnalogIn CDS(A0);
00008  
00009  int i = 0;
00010  //int PIR_sensor;
00011  int CDS_data = 0;
00012 // an SPI sub-class that provides a constructed default
00013 class I2CPreInit : public I2C
00014 {
00015 public:
00016     I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
00017     {
00018         frequency(100000);
00019         start();
00020     };
00021 };
00022 
00023 I2CPreInit gI2C(PA_10,PA_9);
00024 Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
00025 
00026 
00027 int main()
00028 {
00029         int phy_link;
00030         printf("................................configuration using Ethernet connection..........................\r\n\r\n");
00031         //--------- Have to modify the mac address-------------
00032         uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x22}; 
00033         
00034         EthernetInterface eth;   
00035         eth.init(mac_addr); //Use DHCP
00036             
00037         eth.connect();
00038         
00039         
00040                 
00041             /* phy link */
00042             while(!phy_link){
00043                 printf("Failed to obtain IP Adress\r\n\r\n");
00044                 phy_link = eth.ethernet_link();
00045                 printf("...");
00046                 wait(2);
00047             }
00048             printf("\r\n");
00049             printf("IP Adress obtained\r\n\r\n");    
00050             printf("IP Address is: %s\r\n", eth.getIPAddress());
00051              while(1){ 
00052             /* TCP socket connect */
00053             TCPSocketConnection sock;
00054            
00055             
00056              printf("\r\n Reading the LDR sensor values  \n\r"); 
00057                     
00058         while (1) {
00059           //PIR_sensor = PIR;
00060           sock.connect("api.thingspeak.com", 80);
00061             if(sock.is_connected())
00062             {
00063                 printf("\r\nConnected to the DNS Server\r\n\r\n");
00064             }
00065             else
00066             {
00067                 printf("\r\nDNS Server connection error\r\n\r\n");
00068             }
00069             
00070           CDS_data = CDS.read()*1000;
00071           
00072            //CDS Sensor ADC Low Data
00073            printf("===========================================\n\r");
00074           printf("CDS Data : %3d\r\n",CDS_data);
00075           printf("===========================================\n\r");
00076          // pc.printf("PIR Value : %d\r\n",PIR_sensor);
00077                // myled =! PIR_sensor;
00078               
00079           /* char* message;
00080            scanf("%s",message);   
00081             printf("%s",message);
00082             printf("\r\n\r\n");
00083             
00084             int len = strlen(message);
00085             
00086              data length measure 
00087             char data_len[2]={0};
00088             int ten=0;
00089             int one=0;*/
00090             
00091         
00092     if(CDS_data>70)
00093     {
00094             char send_data[1024];
00095             char buffer[1024] = {0};
00096             sprintf(send_data, "GET http://api.thingspeak.com/update?key=1M68DA3MGRI6TGOO&field2=%d\r\n",CDS_data);
00097            
00098     
00099             if(sock.send_all(send_data, sizeof(send_data))!=-1)
00100             {
00101                 printf("The sent URL is: %s\r\n",send_data);
00102               //  printf("The sent Tweet is: %s",message);
00103                 printf("\r\nMessage sent successfully\r\n\r\n");
00104             }
00105             else
00106             {
00107                 printf("\r\nMessage sending failed\r\n\r\n");
00108             }
00109             printf("Response from the server:\r\n\r\n");
00110             sock.receive(buffer, sizeof(buffer));
00111     
00112             printf("%s\r\n",buffer);
00113            }
00114              //sock.disconnect();
00115               wait(18);
00116             }
00117             
00118     };
00119 
00120 }