Sheelapriya Thiyagarajan / Mbed 2 deprecated Thingspeak_over_ethernet

Dependencies:   mbed HTTPClient mbed-rtos NTPClient EthernetInterface

Files at this revision

API Documentation at this revision

Comitter:
rbagwe
Date:
Thu Mar 23 04:22:13 2017 +0000
Child:
1:2741e436af09
Commit message:
Secure Lab Automation

Changed in this revision

DHT.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/colinmeikle/code/DHT/#fbe982025894
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#d1ccbed7687a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/colinmeikle/code/HTTPClient/#a0d9edb403e5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/donatien/code/NTPClient/#881559865a93
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/mbed/code/SDFileSystem/#800f18e6739e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,258 @@
+#include "mbed.h"
+#include "DHT.h"
+#include "EthernetInterface.h"
+#include  "HTTPClient.h"
+#include "SDFileSystem.h"
+#include "NTPClient.h"
+#include <stdio.h>
+
+//Setting the motor Pin
+DigitalOut motorPin(D10);
+
+//DHT pin number & type
+DHT temp_sensor(PTB23, DHT22);
+
+//Ethernet Interface
+EthernetInterface eth;
+
+//HTTP Client for interfacing to web services
+HTTPClient http;
+
+//NTP service for date and Time used to set boards RTC
+NTPClient ntp;
+
+//serial over USB for debug and Info
+Serial pc(USBTX, USBRX);
+
+//Declaring the variable for LED
+//DigitalOut red(LED1);
+
+DigitalOut redLED(LED1);
+DigitalOut greenLED(LED2);
+DigitalOut blueLED(LED3);
+DigitalOut solenoid(D12);
+DigitalIn button1(D2);
+DigitalIn button2(D3);
+DigitalIn button3(D5);
+DigitalIn button4(D6);
+
+InterruptIn switch1(SW2);
+
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL
+
+/*----Global Variables------*/
+int clickNum=0;
+int i;
+int passwordAttempt[4]= {0,0,0,0};      //user input passcode array
+int correctPassword[4]= {1,3,2,4};      //the button passcode array
+bool passStatus=false;
+int passwordWaiter;
+bool v=0;
+
+char resp[1024]; //buffer for the response
+float f = 0.0f, f_out = 0.0f;           //variables to store the temperature value
+int error_temp = 0;                     //temperature sensor error flag
+int ret = 0;
+char val[4][16];                        //to store sensor value
+char f1_read[3];
+int temp_limit = 1;                     //boolean trigger based on value read from thingspeak
+
+//http client init
+HTTPMap map;
+HTTPText inText(resp, 1024);
+
+float temperature;
+time_t ctTime;
+NTPResult result;
+
+/*-----Functions------*/
+bool checkPassword(int passAtt[]){
+    for(i=0;i<4;i++){
+        if(passAtt[i]!=correctPassword[i]){
+            return false;
+        }
+    }
+    return true;
+}
+
+void clearCounters(){
+    clickNum=0;
+    passwordAttempt[0]=0;
+    passwordAttempt[1]=0;
+    passwordAttempt[2]=0;
+    passwordAttempt[3]=0;
+    passStatus=false;
+    passwordWaiter=0;
+}
+
+
+void temperature_control(){
+    error_temp = temp_sensor.readData();
+    f = 0;
+    if (0 == error_temp) {
+        for(int itd = 0; itd<3; itd++){
+            f = f + temp_sensor.ReadTemperature(FARENHEIT);
+            wait_ms(10);
+        }
+        f_out = f/3.0f;
+    }
+
+    if(f_out!= 0.0f){
+        map.put("api_key","ORFC8WKYY3OG0K7Y");
+        sprintf(val[0],"%4.1f",f_out);
+        map.put("field1", val[0]);
+        pc.printf("\nTrying to post data...\n");
+        ret = http.post("https://api.thingspeak.com/update", map, &inText);     //writing data to Thingspeak
+        if (!ret){
+            pc.printf("Executed POST successfully - read %d characters\n", strlen(resp));
+            pc.printf("Result: %s\n", resp);
+        }else{
+            pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+        }
+    }
+    wait(2.0f);
+
+    http.get("https://api.thingspeak.com/channels/242557/fields/1/last?api_key=09DPKTKBCFUW9N6T", f1_read, 3);  //Reading data from Thingspeak
+    pc.printf("Read Data is %s\n", f1_read);
+    sscanf(f1_read, "%d", &temp_limit);
+
+    if (!temp_limit){
+        motorPin = 1;
+    }else{
+        motorPin = 0;
+    }
+}
+
+void button_pad(){
+    pc.printf("Enter the password: \n");
+    while(clickNum!=4 && passwordWaiter<=100){
+        if(button1){
+            passwordAttempt[clickNum]=1;
+            clickNum++;
+            pc.printf("Button 1 Pressed: %d\n",clickNum);
+            while(button1){redLED=1;}
+            redLED=0;
+        }else if(button2){
+            passwordAttempt[clickNum]=2;
+            clickNum++;
+            pc.printf("Button 2 Pressed: %d\n",clickNum);
+            while(button2){redLED=1;}
+            redLED=0;
+        }else if(button3){
+            passwordAttempt[clickNum]=3;
+            clickNum++;
+            pc.printf("Button 3 Pressed: %d\n",clickNum);
+            while(button3){redLED=1;}
+            redLED=0;
+        }else if(button4){
+            passwordAttempt[clickNum]=4;
+            clickNum++;
+            pc.printf("Button 4 Pressed: %d\n",clickNum);
+            while(button4){redLED=1;}
+            redLED=0;
+        }
+        passwordWaiter++;
+        wait(.3);
+    }//end while
+    passStatus=checkPassword(passwordAttempt);
+
+    if(passStatus==true){
+        pc.printf("Right!\n\n");
+        solenoid = 1;
+        redLED=1;
+        wait(0.2);
+        blueLED=1;
+        wait(0.2);
+        greenLED = 0;
+        wait(3);
+        greenLED=1;
+        wait(.2);
+        solenoid = 0;
+    }else{
+        printf("Wrong!\n\n");
+        greenLED = 1;
+        for(i=0;i<3;i++){
+            wait(.2);
+            redLED=1;
+            wait(0.2);
+            blueLED=0;
+            wait(.5);
+            blueLED=1;
+        }
+    }
+
+    redLED=0;
+    clearCounters();
+
+}
+
+void SDcard_logging(){
+    time(&ctTime);
+    temperature = f_out;
+    FILE *fp = fopen("/sd/mydir/Record.csv", "a");
+
+    if(fp == NULL) {
+        error("Could not open file for write");
+    }
+    fprintf(fp, "%s,", ctime(&ctTime));
+    fprintf(fp, "%4.1f", temperature);
+    fclose(fp);
+}
+
+
+void switch1ISR(){
+    switch1.fall(NULL);
+    v=1;
+    switch1.fall(&switch1ISR);
+}
+
+
+
+
+
+int main(){
+
+    //ethernet init
+    eth.init(); //Use DHCP
+    ret= eth.connect();
+
+    if (!ret){
+            pc.printf("Connected, IP: %s, MASK: %s, GW: %s\n",
+            eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+    } else {
+        pc.printf("Error eth.connect() - ret = %d\n", ret);
+    }
+
+    wait(0.5);
+    clickNum=0;
+    greenLED = 1;
+    blueLED = 1;
+    redLED = 0;
+    wait(.2);
+
+    button_pad();
+
+    mkdir("/sd/mydir", 0777);
+    FILE *fp = fopen("/sd/mydir/Record.csv", "w");
+    fprintf(fp, "Time, Temperature\r\n");
+    fclose(fp);
+
+    result = ntp.setTime("pool.ntp.org");
+    if (result == NTP_OK) {
+        time(&ctTime);
+        pc.printf("Time is set to (UTC):\n%s\n", ctime(&ctTime));
+    }else{
+        pc.printf("Error setting Time\n");
+    }
+
+    switch1.fall(&switch1ISR);
+
+    while(1){
+        temperature_control();
+        if(v){
+            button_pad();
+            SDcard_logging();
+            v=0;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5448826aa700
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 23 04:22:13 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file