Reads the value of A0 and sets LED1 if A0>2000mV; also dweets data to thingspace.io
Dependencies: NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
main.cpp
00001 // read analog0 and send to thingspace.io 00002 // toma 2016-11-01 00003 00004 #include "mbed.h" 00005 #include "stdio.h" 00006 #include "SpwfInterface.h" 00007 #include "TCPSocket.h" 00008 #include <string> 00009 00010 /************************************* 00011 //FRDM-K64: D9->UART1_TX, D7->UART1_RX 00012 Pin connections: 00013 FRDM IDW01M1 00014 ------ --------- 00015 +3v3 <--> +3v3 00016 GND <--> GND 00017 D9 <--> D8 00018 D7 <--> D2 00019 00020 SpwfSAInterface spwf(D9, D7, false); 00021 *************************************/ 00022 /************************************* 00023 //LPCXpresso11U68: D9->UART1_TX, D7->UART1_RX 00024 Pin connections: 00025 LPC IDW01M1 00026 ------ --------- 00027 +3v3 <--> +3v3 00028 GND <--> GND 00029 A1 <--> D8 00030 A2 <--> D2 00031 00032 SpwfSAInterface spwf(A1, A2, false); 00033 *************************************/ 00034 00035 //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10) 00036 00037 using namespace std; 00038 00039 AnalogIn analog_input_A0(A0); 00040 Serial serial_port(USBTX, USBRX); 00041 DigitalOut myLed(LED1); 00042 SpwfSAInterface spwf(D8, D2, false); 00043 00044 int errConnect; 00045 int errSend; 00046 00047 int main() 00048 { 00049 float panel_voltage; 00050 DigitalOut led(LED1); 00051 serial_port.baud(9600); 00052 00053 printf("\r\n\r\n*** system restart"); 00054 printf("\r\n\r\nanalog0 example using thingspace.io ...\n"); 00055 00056 char *ssid = ""; 00057 char *seckey = ""; 00058 const char *mac; 00059 00060 printf("X-NUCLEO-IDW01M1v2 mbed application\r\n"); 00061 00062 int spwfResponse; 00063 spwfResponse = 0; 00064 00065 while(spwfResponse != 1) { 00066 printf("connecting to access point ...\r\n"); 00067 spwfResponse = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2);//WPA 00068 printf("\nresponse: %i\n", spwfResponse); 00069 if(spwfResponse != 1) 00070 { 00071 printf("error making connecting to access point\r\n"); 00072 } 00073 else 00074 { 00075 printf("success connecting to access point\r\n"); 00076 const char *ip = spwf.get_ip_address(); 00077 mac = spwf.get_mac_address(); 00078 printf("\r\nip address = %s\r\n", (ip) ? ip : "error getting ip address"); 00079 printf("mac address = %s\r\n", (mac) ? mac : "error getting the mac address\n"); 00080 } 00081 } 00082 00083 SocketAddress addrDweetServer(&spwf, "thingspace.io"); 00084 printf("\r\nthingspace.verizon.com resolved to: %s\r\n\r\n", addrDweetServer.get_ip_address()); 00085 00086 TCPSocket socket(&spwf); 00087 00088 // connect socket 00089 errConnect = socket.connect("thingspace.io", 80); 00090 if(errConnect!=0) { 00091 printf("\r\ncould not connect to socket; error = %d\r\n", errConnect); 00092 } else { 00093 printf("socket connected\r\n"); 00094 } 00095 00096 // get the last 2 bytes of the mac for the thing name 00097 std::string macString = mac; 00098 macString.erase(0,9); 00099 macString.erase(2,1); 00100 macString.erase(4,1); 00101 const char *macBytes = macString.c_str(); 00102 00103 while(1) { 00104 panel_voltage = analog_input_A0.read(); 00105 printf("voltage: %f\r\n", panel_voltage); 00106 // A0 is voltage tolerant to 3.3V, and analog read returns a percentage of the maximum 00107 // need to convert the percentage back to a representative number 00108 panel_voltage = panel_voltage * 3300; // change the value to be in the 0 to 3300 range 00109 // printf("a0 reads %.3f mV\n", panel_voltage); // use 3 decimals of precision 00110 00111 // enable LED if voltage exceeds 2000 mV 00112 if (panel_voltage > 2000) { 00113 myLed = 1; 00114 } 00115 else { 00116 myLed = 0; 00117 } 00118 00119 // don't bother if never connected ... 00120 if (spwfResponse == 1) { 00121 // get length of jsonContent as string without streams 00122 // adapted from http://codereview.stackexchange.com/questions/51270/socket-http-post-request 00123 00124 char dweetBuffer[72] = ""; 00125 00126 // create GET HTTP header for dweeting 00127 strcpy(dweetBuffer, "GET /dweet/for/nucleo-"); 00128 strcat(dweetBuffer, macBytes); 00129 char valueRead[20]; 00130 sprintf(valueRead, "?voltage=%f", panel_voltage); 00131 strcat(dweetBuffer, valueRead); 00132 strcat(dweetBuffer, " HTTP/1.1\r\n\r\n"); 00133 00134 serial_port.printf("\r\n%s", dweetBuffer); 00135 00136 char bufferRx[512] = ""; 00137 int countRx = 0; 00138 serial_port.printf("sending and receiving data ...\r\n"); 00139 errSend = socket.send(dweetBuffer, strlen(dweetBuffer)); 00140 countRx = socket.recv(bufferRx, sizeof bufferRx); 00141 printf("sent %d bytes\r\nand received %d bytes\r\n\r\n", errSend, countRx); 00142 printf(bufferRx); 00143 printf("\r\n\r\n*** 5-second pause ...\r\n"); 00144 } 00145 wait(5.0); // 5000 ms delay before looping to next read 00146 } 00147 }
Generated on Fri Jul 15 2022 23:54:31 by
1.7.2