Data to Thingspeak Over Ethernet Port

Dependencies:   mbed HTTPClient mbed-rtos NTPClient EthernetInterface

Committer:
rbagwe
Date:
Thu Mar 23 04:22:13 2017 +0000
Revision:
0:0666873f31ca
Child:
4:21c1357bb880
Secure Lab Automation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rbagwe 0:0666873f31ca 1 #include "mbed.h"
rbagwe 0:0666873f31ca 2 #include "DHT.h"
rbagwe 0:0666873f31ca 3 #include "EthernetInterface.h"
rbagwe 0:0666873f31ca 4 #include "HTTPClient.h"
rbagwe 0:0666873f31ca 5 #include "SDFileSystem.h"
rbagwe 0:0666873f31ca 6 #include "NTPClient.h"
rbagwe 0:0666873f31ca 7 #include <stdio.h>
rbagwe 0:0666873f31ca 8
rbagwe 0:0666873f31ca 9 //Setting the motor Pin
rbagwe 0:0666873f31ca 10 DigitalOut motorPin(D10);
rbagwe 0:0666873f31ca 11
rbagwe 0:0666873f31ca 12 //DHT pin number & type
rbagwe 0:0666873f31ca 13 DHT temp_sensor(PTB23, DHT22);
rbagwe 0:0666873f31ca 14
rbagwe 0:0666873f31ca 15 //Ethernet Interface
rbagwe 0:0666873f31ca 16 EthernetInterface eth;
rbagwe 0:0666873f31ca 17
rbagwe 0:0666873f31ca 18 //HTTP Client for interfacing to web services
rbagwe 0:0666873f31ca 19 HTTPClient http;
rbagwe 0:0666873f31ca 20
rbagwe 0:0666873f31ca 21 //NTP service for date and Time used to set boards RTC
rbagwe 0:0666873f31ca 22 NTPClient ntp;
rbagwe 0:0666873f31ca 23
rbagwe 0:0666873f31ca 24 //serial over USB for debug and Info
rbagwe 0:0666873f31ca 25 Serial pc(USBTX, USBRX);
rbagwe 0:0666873f31ca 26
rbagwe 0:0666873f31ca 27 //Declaring the variable for LED
rbagwe 0:0666873f31ca 28 //DigitalOut red(LED1);
rbagwe 0:0666873f31ca 29
rbagwe 0:0666873f31ca 30 DigitalOut redLED(LED1);
rbagwe 0:0666873f31ca 31 DigitalOut greenLED(LED2);
rbagwe 0:0666873f31ca 32 DigitalOut blueLED(LED3);
rbagwe 0:0666873f31ca 33 DigitalOut solenoid(D12);
rbagwe 0:0666873f31ca 34 DigitalIn button1(D2);
rbagwe 0:0666873f31ca 35 DigitalIn button2(D3);
rbagwe 0:0666873f31ca 36 DigitalIn button3(D5);
rbagwe 0:0666873f31ca 37 DigitalIn button4(D6);
rbagwe 0:0666873f31ca 38
rbagwe 0:0666873f31ca 39 InterruptIn switch1(SW2);
rbagwe 0:0666873f31ca 40
rbagwe 0:0666873f31ca 41 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL
rbagwe 0:0666873f31ca 42
rbagwe 0:0666873f31ca 43 /*----Global Variables------*/
rbagwe 0:0666873f31ca 44 int clickNum=0;
rbagwe 0:0666873f31ca 45 int i;
rbagwe 0:0666873f31ca 46 int passwordAttempt[4]= {0,0,0,0}; //user input passcode array
rbagwe 0:0666873f31ca 47 int correctPassword[4]= {1,3,2,4}; //the button passcode array
rbagwe 0:0666873f31ca 48 bool passStatus=false;
rbagwe 0:0666873f31ca 49 int passwordWaiter;
rbagwe 0:0666873f31ca 50 bool v=0;
rbagwe 0:0666873f31ca 51
rbagwe 0:0666873f31ca 52 char resp[1024]; //buffer for the response
rbagwe 0:0666873f31ca 53 float f = 0.0f, f_out = 0.0f; //variables to store the temperature value
rbagwe 0:0666873f31ca 54 int error_temp = 0; //temperature sensor error flag
rbagwe 0:0666873f31ca 55 int ret = 0;
rbagwe 0:0666873f31ca 56 char val[4][16]; //to store sensor value
rbagwe 0:0666873f31ca 57 char f1_read[3];
rbagwe 0:0666873f31ca 58 int temp_limit = 1; //boolean trigger based on value read from thingspeak
rbagwe 0:0666873f31ca 59
rbagwe 0:0666873f31ca 60 //http client init
rbagwe 0:0666873f31ca 61 HTTPMap map;
rbagwe 0:0666873f31ca 62 HTTPText inText(resp, 1024);
rbagwe 0:0666873f31ca 63
rbagwe 0:0666873f31ca 64 float temperature;
rbagwe 0:0666873f31ca 65 time_t ctTime;
rbagwe 0:0666873f31ca 66 NTPResult result;
rbagwe 0:0666873f31ca 67
rbagwe 0:0666873f31ca 68 /*-----Functions------*/
rbagwe 0:0666873f31ca 69 bool checkPassword(int passAtt[]){
rbagwe 0:0666873f31ca 70 for(i=0;i<4;i++){
rbagwe 0:0666873f31ca 71 if(passAtt[i]!=correctPassword[i]){
rbagwe 0:0666873f31ca 72 return false;
rbagwe 0:0666873f31ca 73 }
rbagwe 0:0666873f31ca 74 }
rbagwe 0:0666873f31ca 75 return true;
rbagwe 0:0666873f31ca 76 }
rbagwe 0:0666873f31ca 77
rbagwe 0:0666873f31ca 78 void clearCounters(){
rbagwe 0:0666873f31ca 79 clickNum=0;
rbagwe 0:0666873f31ca 80 passwordAttempt[0]=0;
rbagwe 0:0666873f31ca 81 passwordAttempt[1]=0;
rbagwe 0:0666873f31ca 82 passwordAttempt[2]=0;
rbagwe 0:0666873f31ca 83 passwordAttempt[3]=0;
rbagwe 0:0666873f31ca 84 passStatus=false;
rbagwe 0:0666873f31ca 85 passwordWaiter=0;
rbagwe 0:0666873f31ca 86 }
rbagwe 0:0666873f31ca 87
rbagwe 0:0666873f31ca 88
rbagwe 0:0666873f31ca 89 void temperature_control(){
rbagwe 0:0666873f31ca 90 error_temp = temp_sensor.readData();
rbagwe 0:0666873f31ca 91 f = 0;
rbagwe 0:0666873f31ca 92 if (0 == error_temp) {
rbagwe 0:0666873f31ca 93 for(int itd = 0; itd<3; itd++){
rbagwe 0:0666873f31ca 94 f = f + temp_sensor.ReadTemperature(FARENHEIT);
rbagwe 0:0666873f31ca 95 wait_ms(10);
rbagwe 0:0666873f31ca 96 }
rbagwe 0:0666873f31ca 97 f_out = f/3.0f;
rbagwe 0:0666873f31ca 98 }
rbagwe 0:0666873f31ca 99
rbagwe 0:0666873f31ca 100 if(f_out!= 0.0f){
rbagwe 0:0666873f31ca 101 map.put("api_key","ORFC8WKYY3OG0K7Y");
rbagwe 0:0666873f31ca 102 sprintf(val[0],"%4.1f",f_out);
rbagwe 0:0666873f31ca 103 map.put("field1", val[0]);
rbagwe 0:0666873f31ca 104 pc.printf("\nTrying to post data...\n");
rbagwe 0:0666873f31ca 105 ret = http.post("https://api.thingspeak.com/update", map, &inText); //writing data to Thingspeak
rbagwe 0:0666873f31ca 106 if (!ret){
rbagwe 0:0666873f31ca 107 pc.printf("Executed POST successfully - read %d characters\n", strlen(resp));
rbagwe 0:0666873f31ca 108 pc.printf("Result: %s\n", resp);
rbagwe 0:0666873f31ca 109 }else{
rbagwe 0:0666873f31ca 110 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
rbagwe 0:0666873f31ca 111 }
rbagwe 0:0666873f31ca 112 }
rbagwe 0:0666873f31ca 113 wait(2.0f);
rbagwe 0:0666873f31ca 114
rbagwe 0:0666873f31ca 115 http.get("https://api.thingspeak.com/channels/242557/fields/1/last?api_key=09DPKTKBCFUW9N6T", f1_read, 3); //Reading data from Thingspeak
rbagwe 0:0666873f31ca 116 pc.printf("Read Data is %s\n", f1_read);
rbagwe 0:0666873f31ca 117 sscanf(f1_read, "%d", &temp_limit);
rbagwe 0:0666873f31ca 118
rbagwe 0:0666873f31ca 119 if (!temp_limit){
rbagwe 0:0666873f31ca 120 motorPin = 1;
rbagwe 0:0666873f31ca 121 }else{
rbagwe 0:0666873f31ca 122 motorPin = 0;
rbagwe 0:0666873f31ca 123 }
rbagwe 0:0666873f31ca 124 }
rbagwe 0:0666873f31ca 125
rbagwe 0:0666873f31ca 126 void button_pad(){
rbagwe 0:0666873f31ca 127 pc.printf("Enter the password: \n");
rbagwe 0:0666873f31ca 128 while(clickNum!=4 && passwordWaiter<=100){
rbagwe 0:0666873f31ca 129 if(button1){
rbagwe 0:0666873f31ca 130 passwordAttempt[clickNum]=1;
rbagwe 0:0666873f31ca 131 clickNum++;
rbagwe 0:0666873f31ca 132 pc.printf("Button 1 Pressed: %d\n",clickNum);
rbagwe 0:0666873f31ca 133 while(button1){redLED=1;}
rbagwe 0:0666873f31ca 134 redLED=0;
rbagwe 0:0666873f31ca 135 }else if(button2){
rbagwe 0:0666873f31ca 136 passwordAttempt[clickNum]=2;
rbagwe 0:0666873f31ca 137 clickNum++;
rbagwe 0:0666873f31ca 138 pc.printf("Button 2 Pressed: %d\n",clickNum);
rbagwe 0:0666873f31ca 139 while(button2){redLED=1;}
rbagwe 0:0666873f31ca 140 redLED=0;
rbagwe 0:0666873f31ca 141 }else if(button3){
rbagwe 0:0666873f31ca 142 passwordAttempt[clickNum]=3;
rbagwe 0:0666873f31ca 143 clickNum++;
rbagwe 0:0666873f31ca 144 pc.printf("Button 3 Pressed: %d\n",clickNum);
rbagwe 0:0666873f31ca 145 while(button3){redLED=1;}
rbagwe 0:0666873f31ca 146 redLED=0;
rbagwe 0:0666873f31ca 147 }else if(button4){
rbagwe 0:0666873f31ca 148 passwordAttempt[clickNum]=4;
rbagwe 0:0666873f31ca 149 clickNum++;
rbagwe 0:0666873f31ca 150 pc.printf("Button 4 Pressed: %d\n",clickNum);
rbagwe 0:0666873f31ca 151 while(button4){redLED=1;}
rbagwe 0:0666873f31ca 152 redLED=0;
rbagwe 0:0666873f31ca 153 }
rbagwe 0:0666873f31ca 154 passwordWaiter++;
rbagwe 0:0666873f31ca 155 wait(.3);
rbagwe 0:0666873f31ca 156 }//end while
rbagwe 0:0666873f31ca 157 passStatus=checkPassword(passwordAttempt);
rbagwe 0:0666873f31ca 158
rbagwe 0:0666873f31ca 159 if(passStatus==true){
rbagwe 0:0666873f31ca 160 pc.printf("Right!\n\n");
rbagwe 0:0666873f31ca 161 solenoid = 1;
rbagwe 0:0666873f31ca 162 redLED=1;
rbagwe 0:0666873f31ca 163 wait(0.2);
rbagwe 0:0666873f31ca 164 blueLED=1;
rbagwe 0:0666873f31ca 165 wait(0.2);
rbagwe 0:0666873f31ca 166 greenLED = 0;
rbagwe 0:0666873f31ca 167 wait(3);
rbagwe 0:0666873f31ca 168 greenLED=1;
rbagwe 0:0666873f31ca 169 wait(.2);
rbagwe 0:0666873f31ca 170 solenoid = 0;
rbagwe 0:0666873f31ca 171 }else{
rbagwe 0:0666873f31ca 172 printf("Wrong!\n\n");
rbagwe 0:0666873f31ca 173 greenLED = 1;
rbagwe 0:0666873f31ca 174 for(i=0;i<3;i++){
rbagwe 0:0666873f31ca 175 wait(.2);
rbagwe 0:0666873f31ca 176 redLED=1;
rbagwe 0:0666873f31ca 177 wait(0.2);
rbagwe 0:0666873f31ca 178 blueLED=0;
rbagwe 0:0666873f31ca 179 wait(.5);
rbagwe 0:0666873f31ca 180 blueLED=1;
rbagwe 0:0666873f31ca 181 }
rbagwe 0:0666873f31ca 182 }
rbagwe 0:0666873f31ca 183
rbagwe 0:0666873f31ca 184 redLED=0;
rbagwe 0:0666873f31ca 185 clearCounters();
rbagwe 0:0666873f31ca 186
rbagwe 0:0666873f31ca 187 }
rbagwe 0:0666873f31ca 188
rbagwe 0:0666873f31ca 189 void SDcard_logging(){
rbagwe 0:0666873f31ca 190 time(&ctTime);
rbagwe 0:0666873f31ca 191 temperature = f_out;
rbagwe 0:0666873f31ca 192 FILE *fp = fopen("/sd/mydir/Record.csv", "a");
rbagwe 0:0666873f31ca 193
rbagwe 0:0666873f31ca 194 if(fp == NULL) {
rbagwe 0:0666873f31ca 195 error("Could not open file for write");
rbagwe 0:0666873f31ca 196 }
rbagwe 0:0666873f31ca 197 fprintf(fp, "%s,", ctime(&ctTime));
rbagwe 0:0666873f31ca 198 fprintf(fp, "%4.1f", temperature);
rbagwe 0:0666873f31ca 199 fclose(fp);
rbagwe 0:0666873f31ca 200 }
rbagwe 0:0666873f31ca 201
rbagwe 0:0666873f31ca 202
rbagwe 0:0666873f31ca 203 void switch1ISR(){
rbagwe 0:0666873f31ca 204 switch1.fall(NULL);
rbagwe 0:0666873f31ca 205 v=1;
rbagwe 0:0666873f31ca 206 switch1.fall(&switch1ISR);
rbagwe 0:0666873f31ca 207 }
rbagwe 0:0666873f31ca 208
rbagwe 0:0666873f31ca 209
rbagwe 0:0666873f31ca 210
rbagwe 0:0666873f31ca 211
rbagwe 0:0666873f31ca 212
rbagwe 0:0666873f31ca 213 int main(){
rbagwe 0:0666873f31ca 214
rbagwe 0:0666873f31ca 215 //ethernet init
rbagwe 0:0666873f31ca 216 eth.init(); //Use DHCP
rbagwe 0:0666873f31ca 217 ret= eth.connect();
rbagwe 0:0666873f31ca 218
rbagwe 0:0666873f31ca 219 if (!ret){
rbagwe 0:0666873f31ca 220 pc.printf("Connected, IP: %s, MASK: %s, GW: %s\n",
rbagwe 0:0666873f31ca 221 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
rbagwe 0:0666873f31ca 222 } else {
rbagwe 0:0666873f31ca 223 pc.printf("Error eth.connect() - ret = %d\n", ret);
rbagwe 0:0666873f31ca 224 }
rbagwe 0:0666873f31ca 225
rbagwe 0:0666873f31ca 226 wait(0.5);
rbagwe 0:0666873f31ca 227 clickNum=0;
rbagwe 0:0666873f31ca 228 greenLED = 1;
rbagwe 0:0666873f31ca 229 blueLED = 1;
rbagwe 0:0666873f31ca 230 redLED = 0;
rbagwe 0:0666873f31ca 231 wait(.2);
rbagwe 0:0666873f31ca 232
rbagwe 0:0666873f31ca 233 button_pad();
rbagwe 0:0666873f31ca 234
rbagwe 0:0666873f31ca 235 mkdir("/sd/mydir", 0777);
rbagwe 0:0666873f31ca 236 FILE *fp = fopen("/sd/mydir/Record.csv", "w");
rbagwe 0:0666873f31ca 237 fprintf(fp, "Time, Temperature\r\n");
rbagwe 0:0666873f31ca 238 fclose(fp);
rbagwe 0:0666873f31ca 239
rbagwe 0:0666873f31ca 240 result = ntp.setTime("pool.ntp.org");
rbagwe 0:0666873f31ca 241 if (result == NTP_OK) {
rbagwe 0:0666873f31ca 242 time(&ctTime);
rbagwe 0:0666873f31ca 243 pc.printf("Time is set to (UTC):\n%s\n", ctime(&ctTime));
rbagwe 0:0666873f31ca 244 }else{
rbagwe 0:0666873f31ca 245 pc.printf("Error setting Time\n");
rbagwe 0:0666873f31ca 246 }
rbagwe 0:0666873f31ca 247
rbagwe 0:0666873f31ca 248 switch1.fall(&switch1ISR);
rbagwe 0:0666873f31ca 249
rbagwe 0:0666873f31ca 250 while(1){
rbagwe 0:0666873f31ca 251 temperature_control();
rbagwe 0:0666873f31ca 252 if(v){
rbagwe 0:0666873f31ca 253 button_pad();
rbagwe 0:0666873f31ca 254 SDcard_logging();
rbagwe 0:0666873f31ca 255 v=0;
rbagwe 0:0666873f31ca 256 }
rbagwe 0:0666873f31ca 257 }
rbagwe 0:0666873f31ca 258 }