Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Servo X_NUCLEO_IDW01M1v2 NetworkSocketAPI X_NUCLEO_IKS01A1 HC_SR04_Ultrasonic_Library
main.cpp
00001 #include "mbed.h" 00002 #include "SpwfInterface.h" 00003 #include "x_nucleo_iks01a1.h" 00004 #include "TCPSocket.h" 00005 #include "Servo.h" 00006 #include "ultrasonic.h" 00007 00008 00009 00010 int distance1; 00011 int distance; 00012 void dist(int distance) 00013 { 00014 //put code here to happen when the distance is changed 00015 printf("Distance changed to %dmm\r\n", distance); 00016 distance1= distance; 00017 } 00018 00019 00020 ultrasonic mu(D8, D9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 00021 //have updates every .1 seconds and a timeout after 1 00022 //second, and call dist when the distance changes 00023 Servo myservo(D6); 00024 //char* ntpServerUrl = "0.ca.pool.ntp.org"; 00025 //#define IP "184.106.153.149/update" 00026 #define IP "184.106.153.149" 00027 char* thingSpeakUrl = "http://api.thingspeak.com/update"; 00028 char* thingSpeakKey = "5WJ5OYWTWTQX4310"; 00029 00030 Serial pc(USBTX, USBRX); 00031 DigitalOut myled(LED1); 00032 /*************/ 00033 SpwfSAInterface spwf(D8, D2, false); 00034 uint8_t id; 00035 float value1, value2; 00036 char buffer1[32], buffer2[32]; 00037 00038 00039 int http_demo(void) 00040 { 00041 TCPSocket socket(&spwf); 00042 char buffer[256]; 00043 char message[160]; 00044 int err; 00045 printf("Sending HTTP Data to thingspeak...\r\n"); 00046 00047 // Open a socket on the network interface, and create a TCP connection to thingspeak 00048 //socket.open(&spwf); 00049 err=socket.connect(IP,80); // connecting to thingspeak 00050 if(err!=0) 00051 { 00052 pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); 00053 return -1; 00054 } else pc.printf("\r\nconnected to host server\r\n"); 00055 sprintf(message,"field1=%d",distance1); 00056 printf("Message Length=%d\r\n",(int)strlen(message)); 00057 sprintf(buffer,"POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s",thingSpeakKey,(int)strlen(message),message); 00058 pc.printf("Request to %s\r\n", buffer); 00059 int scount = socket.send(buffer, (int)strlen(buffer)); 00060 printf("sent %d [%.*s]\r\n", scount, strstr(buffer, "\r\n")-buffer, buffer); 00061 00062 // Recieve a simple http response and print out the response line 00063 char rbuffer[64]; 00064 int rcount = socket.recv(rbuffer, sizeof rbuffer); 00065 printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); 00066 00067 // Close the socket to return its memory and bring down the network interface 00068 socket.close(); 00069 return 0; 00070 } 00071 00072 int main() 00073 { 00074 00075 00076 char * ssid = "homebb@unifi"; 00077 char * seckey = "homebb987"; 00078 pc.printf("\r\nReal Time Weather System\r\n"); 00079 pc.printf("\r\nconnecting to AP\r\n"); 00080 00081 if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { 00082 pc.printf("\r\nnow connected\r\n"); 00083 } else { 00084 pc.printf("\r\nerror connecting to AP.\r\n"); 00085 return -1; 00086 } 00087 00088 const char *ip = spwf.get_ip_address(); 00089 const char *mac = spwf.get_mac_address(); 00090 00091 pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); 00092 pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC"); 00093 wait (3); 00094 00095 while(1) 00096 { 00097 mu.checkDistance(); //call checkDistance() as much as possible, as this is where 00098 //the class checks if dist needs to be called. 00099 if(distance1 <55){ 00100 printf("ok\r\n"); 00101 //for(float p=0; p<1.0; p += 0.99) 00102 // myservo = p; 00103 //wait(0.2); 00104 http_demo(); 00105 //x=0;y=0;z=0; 00106 pc.printf("Thingspeak update completed successfully\r\n"); 00107 //buz=0;//RESET CONDITION 00108 wait(10); 00109 } 00110 }}
Generated on Sun Aug 21 2022 08:09:49 by
1.7.2