Lab 6_v2

Dependencies:   mbed Thingspeak_template LM75B mbed-rtos EthernetInterface MMA7660

Dependents:   Thingspeak_template

Files at this revision

API Documentation at this revision

Comitter:
steveshun
Date:
Mon Aug 16 16:06:35 2021 +0000
Parent:
1:21d76b260bc6
Commit message:
Lab 6

Changed in this revision

ESP8266.lib 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
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
MMA7660.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
--- a/ESP8266.lib	Sun Mar 28 18:20:05 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/janhavi/code/ESP8266/#4f24e7e803a1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Aug 16 16:06:35 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/SiSK/code/EthernetInterface/#d7bd7384a37c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Mon Aug 16 16:06:35 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/steveshun/code/Thingspeak_template/#7c21420dd39a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Mon Aug 16 16:06:35 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/chris/code/LM75B/#6a70c9303bbe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.lib	Mon Aug 16 16:06:35 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Sissors/code/MMA7660/#36a163511e34
--- a/main.cpp	Sun Mar 28 18:20:05 2021 +0000
+++ b/main.cpp	Mon Aug 16 16:06:35 2021 +0000
@@ -1,124 +1,54 @@
-
-// PIR Motion Sensor Alarm + ESP8622 WiFi Module + FRDM-k64f
-//PIR sensor can detect change on its surrounding by measuring the change of infra red
-// Then will send high=1 to the FRDM-k64f to flash the LED, Run the Buzzer,
-//Send information through UART serial Port to Tera Term and display how many motion been
-//detected,also will send information to Thingspeak IoT website using WiFi connection 
-
+#include "mbed.h"
+#include "HTTPClient.h"
+#include "LM75B.h"
+#include "MMA7660.h"
+#include "EthernetInterface.h"
 
 
-#include "mbed.h"
-#include "ESP8266.h"
-
-#define APIKEY YTDILMQL53ASDCJ4
+LM75B sensor(D14,D15);
+MMA7660 accel(PTE25 , PTE24);
+char buffer[256];
+char resp[1024];
+char val[4][16];   
+int ret = 0;
 
 Serial pc(USBTX,USBRX);
-DigitalOut Buzzer(D3); // output
-PwmOut LED(D13); // flashing the led
-DigitalIn inputPin(D2); // pir senor input
-DigitalOut redled(LED1);
-ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi
-char snd[255],rcv[1000];
-
+//Ethernet Interface
+EthernetInterface eth;
+//HTTP Client for interfacing to web services
+HTTPClient http;
 #define IP "184.106.153.149" // thingspeak.com IP Address
 
-int val = 0; // value to holed the high/low info from pir from pin D2
-int cnt = 0; // counter for motion
+//http client init
+HTTPMap map;
+HTTPText inText(resp, 1024);
 //String thingtweetAPIKey = "TTEVLP931ODJ5GMT";
 
-/************ WiFi INTIALIZATION *********/
 
-void wifi_initialize(void);
-void wifi_send(void);
-
-int main () {
-    
-pc.baud(9600);   
-pc.printf("SET mode to AP\r\n");
-wifi.SetMode(1);    // set ESP mode to 1
-wifi.RcvReply(rcv, 1000);    //receive a response from ESP
-pc.printf("%s",rcv);    //Print the response onscreen
-pc.printf("Conneting to AP\r\n");
-wifi.Join("BHTG1672G3FC2-2G", "5f17baeb");     // Your wifi username & Password 
-wifi.RcvReply(rcv, 1000);    //receive a response from ESP
-pc.printf("%s", rcv);    //Print the response onscreen
-wait(8);     //waits for response from ESP
-pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
-wifi.GetIP(rcv);    //receive an IP address from the AP
-pc.printf("%s", rcv);
-
-    
- wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
+int main () {   
+//ethernet init
+    eth.init(); //Use DHCP
+    ret= eth.connect();
 
- pc.printf("Initializing WiFi\r\n");
- //wifi_initialize();
-    while (1) {
-       
-   val = inputPin.read();
-   
-   if (val==0) {   
-    cnt++;
-   pc.printf(" The Sensor is ON And I Detected = %i Till NOW\r\n",cnt);
-   Buzzer = 1;
-   pc.printf("PLEASE STAY AWAY\r\n");
-    pc.printf("Sending WiFi information\n\r");
-    wifi_send();
-    redled=1; // when the motion detected turn of the on board red led
-    LED.period(2.0f); // 2 seconds period
-    wait(2.0f);
-    LED.pulsewidth(.02);    // 2 mseconds pulse (on)
-    redled=1;
-   // Buzzer = 1;
-   wait(1.5f);
-          }
-    else {
-         
-    pc.printf(" The Sensor is OFF \r\n");
-        LED = 0;
-         Buzzer =0;
-        redled=0; // turn the on board red led on
-          wait(1.5f);
-         }
+    if (!ret){
+            pc.printf("\r\nConnected, IP: %s, MASK: %s, GW: %s",
+            eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+    } else {
+        pc.printf("\r\nError eth.connect() - ret = %d", ret);
+    }
+    
+    while(1){
+        pc.printf("\r\nWriting to thingspeak");
+        map.put("api_key","xxx");  /* Fill your thingspeak API KEY*/
+        sprintf(val[0],"%4.1f",sensor.read());
+        map.put("field1", 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());
         }
     }
-
-void wifi_send(void){
-   
-   strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
-  
-  //WIFI updates the Status to Thingspeak servers//
-  strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
-  
-  
-  sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server 
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
- 
-  strcpy(snd,"AT+CIPSEND=4,47");    //Send Number of open connections,Characters to send 
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
-  
-    
-  sprintf(snd,"GET /update?key=WZXOHNJSLN9G1AGP&field1=%2.2f\r\n",1.0); //Post values to thingspeak
-  pc.printf("%s",snd);
-  wifi.SendCMD(snd);
-  
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
-  
-  wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
-  wifi.RcvReply(rcv, 3000);
-  pc.printf("%s", rcv);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Aug 16 16:06:35 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/mbed-rtos/#83895f30f8f2