still need some modification

Dependencies:   mbed-http DebounceIn PinDetect OneButton

Files at this revision

API Documentation at this revision

Comitter:
nadaanas1985
Date:
Mon Mar 25 19:49:24 2019 +0000
Child:
1:1a06bde4e0db
Commit message:
ECH_REV001

Changed in this revision

DebounceIn.lib Show annotated file Show diff for this revision Revisions of this file
ECH_V003.cpp Show annotated file Show diff for this revision Revisions of this file
OneButton.lib Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
mbed-http.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
mbedtls_entropy_config.h Show annotated file Show diff for this revision Revisions of this file
network-helper.h Show annotated file Show diff for this revision Revisions of this file
select-demo.h Show annotated file Show diff for this revision Revisions of this file
simconfig.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebounceIn.lib	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/AjK/code/DebounceIn/#31ae5cfb44a4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ECH_V003.cpp	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,367 @@
+#include "select-demo.h"
+
+#if DEMO == DEMO_HTTP
+
+#include "rtos.h"
+#include "mbed.h"
+#include "http_request.h"
+#include "network-helper.h"
+#include "mbed_mem_trace.h"
+#include "PinDetect.h"
+
+
+
+
+//FINITE STATE MACHINE EVENTS
+#define NO_EVENT 0
+#define TIME_OUT 1
+#define BUTTON_PRESS_1 2
+#define BUTTON_PRESS_2 3
+#define TILT 3
+#define PUSH 4
+
+//STATES
+#define STATE_0 0
+#define STATE_1 1
+#define STATE_2 2
+
+
+//pass event via message queue
+typedef struct {
+    int    event;   /* AD result of measured voltage */
+} message_t;
+
+MemoryPool<message_t, 16> mpool;
+Queue<message_t, 16> queue;
+
+//Digital input
+//DigitalIn coin(p14);
+PinDetect button_1(p23);
+PinDetect button_2(p24);
+
+//Global varible
+int button_press_1=0;
+int button_press_2=0;
+int button_press_Wait=0;
+
+
+//leds for debug
+DigitalOut led4(LED4); //LED
+DigitalOut led3(LED3); //LED
+DigitalOut led2(LED2); //LED
+DigitalOut myled(LED1);
+
+//Relays:
+
+DigitalOut Relay_1(p11);
+DigitalOut Relay_2(p12);
+
+
+
+//Mutex
+Mutex flag_mutex;
+
+
+void button_1_event_thread(void const *argument)
+{
+
+   
+    while (true)
+     {
+        //Thread::wait(10);
+        if (button_press_1 == 1 && button_press_2 == 0 && button_press_Wait==0) {
+
+            //event via a message queue
+            button_press_Wait=1;
+            message_t *message = mpool.alloc();
+            message->event = BUTTON_PRESS_1;
+            queue.put(message);
+
+            led3 = !led3;
+            Thread::wait(5000);
+        }
+
+    }
+}
+
+
+void button_2_event_thread(void const *argument)
+{
+
+    
+    while (true)
+     {
+        //Thread::wait(10);
+        if (button_press_2 == 1 && button_press_1 == 0 && button_press_Wait==0) {
+
+            //event via a message queue
+            button_press_Wait = 1;
+            message_t *message = mpool.alloc();
+            message->event = BUTTON_PRESS_2;
+            queue.put(message);
+
+            led2 = !led2;
+            Thread::wait(5000);
+
+        }
+
+    }
+}
+
+
+void timeout_event(void const *n)
+{
+
+    //event via a message queue
+    message_t *message = mpool.alloc();
+    message->event = TIME_OUT;
+    queue.put(message);
+
+    led4 =  !led4;
+
+}
+
+
+void  button_1_hit_callback()
+{
+//Flash_LED_Thread.signal_set(0x1);
+
+    button_press_1=1;   
+
+}
+
+void  button_2_hit_callback()
+{
+//Flash_LED_Thread.signal_set(0x1);
+
+    button_press_2=1;
+
+}
+
+
+
+
+
+void dump_response_1(HttpResponse* res) 
+{
+   
+    Relay_1=0;
+    Relay_2=1;
+    
+
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+
+    printf("Headers:\n");
+    for (size_t ix = 0; ix < res->get_headers_length(); ix++)
+    {
+        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+    }
+    printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+    
+    Thread::wait(5000);
+    Relay_1=1;
+   
+    Thread::wait(5000);
+    button_press_2 = 0;
+    button_press_1 = 0;
+   button_press_Wait = 0;
+    
+}
+
+
+void dump_response_2(HttpResponse* res)
+{
+    Relay_1=1;
+    Relay_2=0;
+    
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+
+    printf("Headers:\n");
+    for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+    }
+    printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+    
+    
+    Thread::wait(5000);
+    Relay_2=1;
+    
+    Thread::wait(5000);
+    button_press_2 = 0;
+    button_press_1 = 0;
+   
+   button_press_Wait = 0;
+ 
+
+}
+
+
+int main() 
+{
+    
+    
+    Relay_1=1;
+    
+    Relay_2=1;
+    //Thread fsm(fsm_thread);
+    Thread button_1_event(button_1_event_thread);
+    Thread button_2_event(button_2_event_thread);
+    
+    
+    
+    RtosTimer timer(timeout_event, osTimerPeriodic, (void *)0);
+    
+    
+    button_1.mode(PullUp);
+    button_2.mode(PullUp);
+    
+    //Interrupts
+   /* button_1.fall(&Button_1_Inter);
+    button_2.fall(&Button_2_Inter);*/
+    
+     // Setup Interrupt callback function for a pb hit
+    button_1.attach_deasserted(& button_1_hit_callback);
+    // Start sampling pb input using interrupts
+    button_1.setSampleFrequency();
+    
+    // Setup Interrupt callback function for a pb hit
+    button_2.attach_deasserted(& button_2_hit_callback);
+    // Start sampling pb input using interrupts
+    button_2.setSampleFrequency();
+ 
+
+    
+    
+    int state = STATE_0;
+
+    //network setup
+    NetworkInterface* network = connect_to_default_network_interface();
+    if (!network)
+    {
+        printf("Cannot connect to the network, see serial output\n");
+                //return 1;
+    }
+    if(network)
+    myled=1;
+
+    while (true) {
+
+
+        switch(state) {
+            case STATE_0:
+                osEvent evt = queue.get();
+                if (evt.status == osEventMessage) 
+                {
+                    message_t *message = (message_t*)evt.value.p;
+
+
+                    if(message->event == BUTTON_PRESS_1) 
+                    {
+                        
+                        HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://192.168.0.57:5500/mch/kick");
+                        
+                        post_req->set_header("Content-Type", "application/json");
+                     
+                        
+                        const char body[] = "{\"hello\":\"Nada\"}";
+                            
+                        HttpResponse* post_res = post_req->send(body, strlen(body));
+                        if (!post_res)
+                        {
+                            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+                           // return 1;
+                           
+                            button_press_2 = 0;
+                            button_press_1 = 0;
+                        }
+                        else
+                        {
+                        
+                            printf("\n----- HTTP POST response 1-----\n");
+                            
+                            dump_response_1(post_res);
+                            
+                            //Thread::wait(5000);
+                            
+
+                                
+                             delete post_req;
+                         }
+                         
+                         state = STATE_0;
+                    }
+                    else if(message->event == BUTTON_PRESS_2) 
+                    {
+                        
+                        HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://192.168.0.57:5500/mch/call");
+                        
+                        post_req->set_header("Content-Type", "application/json");
+                     
+                        
+                        const char body[] = "{\"hello\":\"World\"}";
+                            
+                        HttpResponse* post_res = post_req->send(body, strlen(body));
+                        if (!post_res)
+                        {
+                            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+                           // return 1;
+                           
+                            button_press_2 = 0;
+                            button_press_1 = 0;                           
+                        }
+                        else
+                        {
+                    
+                            printf("\n----- HTTP POST response 2-----\n");
+                            
+                            dump_response_2(post_res);
+                                
+                            delete post_req;
+                            //Thread::wait(5000);
+                            
+
+
+            
+                        }
+                         state = STATE_0;
+                    }
+
+                    mpool.free(message);
+                }
+
+                timer.start(15000);
+
+                break;
+
+            case STATE_1:
+
+                evt = queue.get();
+                if (evt.status == osEventMessage) 
+                {
+                    message_t *message = (message_t*)evt.value.p;
+
+
+                    if(message->event == BUTTON_PRESS_1) 
+                    {
+                        
+                        state = STATE_0;
+                    }
+                    
+                    mpool.free(message);
+                }
+
+                timer.start(10000);
+
+                break;
+
+
+        }//End of switch
+
+        //toggle led for local testing
+        //led2= !led2;
+
+    }//end of while(1)
+            
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OneButton.lib	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/JojoS/code/OneButton/#48a593e2d4cf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-http.lib	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/sandbox/code/mbed-http/#6daf67a96a91
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,31 @@
+{
+    "config": {
+        "main-stack-size": {
+            "value": 8192
+        }
+    },
+    "macros": [
+        "MBEDTLS_MPI_MAX_SIZE=1024",
+        "MBEDTLS_MPI_WINDOW_SIZE=1",
+        "MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\"",
+        "MBEDTLS_TEST_NULL_ENTROPY",
+        "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
+        "MBED_HEAP_STATS_ENABLED=1"
+    ],
+    "target_overrides": {
+        "*": {
+            "platform.stdio-baud-rate": 115200,
+            "platform.stdio-convert-newlines": true,
+            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+            "mbed-mesh-api.6lowpan-nd-channel": 12,
+            "mbed-trace.enable": null,
+            "mbed-http.http-buffer-size": 2048,
+            "nsapi.default-wifi-security": "WPA_WPA2",
+            "nsapi.default-wifi-ssid": "\"Digirobtech\"",
+            "nsapi.default-wifi-password": "\"Digir1234567\""
+        },
+        "DISCO_L475VG_IOT01A": {
+            "target.network-default-interface-type" : "WIFI"
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbedtls_entropy_config.h	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  This file is part of mbed TLS (https://tls.mbed.org)
+ */
+
+#include "select-demo.h"
+
+/* Enable entropy for devices with TRNG. This means entropy is disabled for all other targets. */
+/* Do **NOT** deploy this code in production on other targets! */
+/* See https://tls.mbed.org/kb/how-to/add-entropy-sources-to-entropy-pool */
+#if defined(DEVICE_TRNG)
+#undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
+#undef MBEDTLS_TEST_NULL_ENTROPY
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/network-helper.h	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,34 @@
+#ifndef _MBED_HTTP_EXAMPLE_H_
+#define _MBED_HTTP_EXAMPLE_H_
+
+#include "mbed.h"
+#include "NetworkInterface.h"
+
+/**
+ * Connect to the network using the default networking interface,
+ * you can also swap this out with a driver for a different networking interface
+ * if you use WiFi: see mbed_app.json for the credentials
+ */
+NetworkInterface *connect_to_default_network_interface() {
+    printf("[NWKH] Connecting to network...\n");
+
+    NetworkInterface* network = NetworkInterface::get_default_instance();
+
+    if (!network) {
+        printf("[NWKH] No network interface found, select an interface in 'network-helper.h'\n");
+        return NULL;
+    }
+
+    nsapi_error_t connect_status = network->connect();
+
+    if (connect_status != NSAPI_ERROR_OK) {
+        printf("[NWKH] Failed to connect to network (%d)\n", connect_status);
+        return NULL;
+    }
+
+    printf("[NWKH] Connected to the network\n");
+    printf("[NWKH] IP address: %s\n", network->get_ip_address());
+    return network;
+}
+
+#endif // _MBED_HTTP_EXAMPLE_H_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/select-demo.h	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,14 @@
+#ifndef _SELECT_METHOD_H_
+#define _SELECT_METHOD_H_
+
+#define         DEMO_HTTP                   1
+#define         DEMO_HTTP_SOCKET_REUSE      2
+#define         DEMO_HTTP_IPV6              3
+#define         DEMO_HTTPS                  4
+#define         DEMO_HTTPS_SOCKET_REUSE     5
+#define         DEMO_HTTPS_CHUNKED_REQUEST  6
+#define         DEMO_TESTS                  7
+
+#define         DEMO            DEMO_HTTP
+
+#endif // _SELECT_METHOD_H_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simconfig.json	Mon Mar 25 19:49:24 2019 +0000
@@ -0,0 +1,5 @@
+{
+    "ignore": [
+        "./mbed-http"
+    ]
+}
\ No newline at end of file