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 HTTPClient mbed-rtos NTPClient EthernetInterface
Diff: main.cpp
- Revision:
- 4:21c1357bb880
- Parent:
- 0:0666873f31ca
- Child:
- 5:b8a76bfc45c9
--- a/main.cpp Thu Mar 23 16:32:21 2017 +0000
+++ b/main.cpp Sat Apr 13 19:16:17 2019 +0000
@@ -1,16 +1,14 @@
#include "mbed.h"
-#include "DHT.h"
#include "EthernetInterface.h"
-#include "HTTPClient.h"
-#include "SDFileSystem.h"
-#include "NTPClient.h"
+#include "HTTPClient.h"
#include <stdio.h>
-//Setting the motor Pin
-DigitalOut motorPin(D10);
-//DHT pin number & type
-DHT temp_sensor(PTB23, DHT22);
+char buffer[256];
+char resp[1024];
+char val[4][16];
+float f_out = 23.0;
+int ret = 0;
//Ethernet Interface
EthernetInterface eth;
@@ -18,196 +16,13 @@
//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(){
@@ -217,42 +32,25 @@
ret= eth.connect();
if (!ret){
- pc.printf("Connected, IP: %s, MASK: %s, GW: %s\n",
+ pc.printf("\r\nConnected, IP: %s, MASK: %s, GW: %s",
eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
} else {
- pc.printf("Error eth.connect() - ret = %d\n", ret);
+ pc.printf("\r\nError eth.connect() - ret = %d", 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;
+ pc.printf("\r\nWriting to thingspeak");
+ map.put("api_key","EEMB3URN9LDSPRIM"); /* The thingspeak API KEY*/
+ sprintf(val[0],"%4.1f",f_out);
+ map.put("field1", val[0]);
+ map.put("field2", val[0]);
+ pc.printf("\r\nPosting Data...");
+ ret = http.post("https://api.thingspeak.com/update", map, &inText); //writing data to Thingspeak
+ if (!ret){
+ pc.printf("\r\nPOST successfully - read %d characters", strlen(resp));
+ pc.printf("\r\nResult: %s\n", resp);
+ }else{
+ pc.printf("\r\nError Connecting to ethernet port - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
}
}
}
\ No newline at end of file