Data over internett

Dependencies:   C12832

Fork of Riggen_internett by Mustafa Kazaale

Files at this revision

API Documentation at this revision

Comitter:
mosti89
Date:
Thu Apr 12 21:33:47 2018 +0000
Child:
1:55755f9baff9
Commit message:
Logger over nettet

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
C12832.lib Show annotated file Show diff for this revision Revisions of this file
README.md 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-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Thu Apr 12 21:33:47 2018 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Thu Apr 12 21:33:47 2018 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Thu Apr 12 21:33:47 2018 +0000
@@ -0,0 +1,57 @@
+# Getting started with Blinky on mbed OS
+
+This guide reviews the steps required to get Blinky working on an mbed OS platform.
+
+Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
+
+## Import the example application
+
+From the command-line, import the example:
+
+```
+mbed import mbed-os-example-blinky
+cd mbed-os-example-blinky
+```
+
+### Now compile
+
+Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
+
+```
+mbed compile -m K64F -t ARM
+```
+
+Your PC may take a few minutes to compile your code. At the end, you see the following result:
+
+```
+[snip]
++----------------------------+-------+-------+------+
+| Module                     | .text | .data | .bss |
++----------------------------+-------+-------+------+
+| Misc                       | 13939 |    24 | 1372 |
+| core/hal                   | 16993 |    96 |  296 |
+| core/rtos                  |  7384 |    92 | 4204 |
+| features/FEATURE_IPV4      |    80 |     0 |  176 |
+| frameworks/greentea-client |  1830 |    60 |   44 |
+| frameworks/utest           |  2392 |   512 |  292 |
+| Subtotals                  | 42618 |   784 | 6384 |
++----------------------------+-------+-------+------+
+Allocated Heap: unknown
+Allocated Stack: unknown
+Total Static RAM memory (data + bss): 7168 bytes
+Total RAM memory (data + bss + heap + stack): 7168 bytes
+Total Flash memory (text + data + misc): 43402 bytes
+Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
+```
+
+### Program your board
+
+1. Connect your mbed device to the computer over USB.
+1. Copy the binary file to the mbed device.
+1. Press the reset button to start the program.
+
+The LED on your platform turns on and off.
+
+## Troubleshooting
+
+If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Apr 12 21:33:47 2018 +0000
@@ -0,0 +1,167 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "C12832.h"             // Biblioteket til skjermen på applikasjonsbrettet
+Serial pc(USBTX, USBRX);        // Serial kommunikasjon med PC
+SPI bus(p11, p12, p13);         // p11-mosi brukes ikke, p12-miso kobles til pinne 3 på sensoren, p13-sck kobles til pinne 4 på sensoren
+DigitalOut slave_1(p19);        // p20-SS, koble til pinne 5 på sensor 1
+DigitalOut slave_2(p18);        // p19-SS, koble til pinne 5 på sensor 2
+DigitalOut slave_3(p17);        // p18-SS, koble til pinne 5 på sensor 3
+EthernetInterface net;      //Ethernet interface
+TCPSocket socket;           //TCP socket
+Timer tid;
+Ticker t, t1;
+C12832 lcd(p5, p7, p6, p8, p11);// Skjermen
+
+void data(float s1, float av1,float s2, float av2,float s3, float av3);
+void send(char *arr, int lengde);
+
+int i=0, ant=0;
+int avlest[3];
+float trykk[3];
+float fart[3];
+float sum_fart[3];
+float avvik[3];
+float gjennomsnitt[3];
+
+void sensor();                  //Lese av og omregne verdien fra sensorene
+void omregning(int a, float speed);
+
+#define ut_min 0x0666           // Minste avleste verdi
+#define ut_max 0x399A           // Maksimum avlest verdi
+#define trykk_min -6000         // Minimale trykkverdi i Pascal på sensoren
+#define trykk_max 6000          // Maksimale trykkverdi i Pascal på sernsoren
+
+int main()
+{
+    pc.printf("\nSetter opp nettverksforbindelse");
+    net.connect();
+    pc.printf("\nTilkoblet nettet");
+    
+    const char *ip = net.get_ip_address();
+    printf("\nIP address is: %s\n\n", ip ? ip : "No IP");
+    
+    bus.format(16,0);               // Oppsett av SPI dataformat, (16 bit data, mode 0)
+                                    // mode 0 = (klokke verdi ved tomgang = 0, data avleses ved opp-flanke av klokkepuls)
+    bus.frequency(800000);          // Velger frekvensen på kommuniaksjonsklokken til 800KHz
+    slave_1 = slave_2 = slave_3 = 1;// Ingen slave valgt
+    pc.printf("\n\tSensor 1\t\t\tSensor 2\t\t\tSensor 3\t\n");
+    pc.printf("Inn\tP\t\t[m/s]\tInn\tP\t\t[m/s]\tInn\tP\t\t[m/s]\n");
+    t.attach(&sensor,0.5);
+    tid.start();
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("   S1      S2      S3");
+    while(1)
+    {
+        
+        static bool sist = false;       // Dette er en liten kode for å sende kun en gang.
+        while (16 < tid.read())
+        {
+            
+            if(ant==144)
+            {
+                ant=0;
+                if(!sist)           // Tester om ikke sist 
+                {
+                    sist = true;        // Setter sist til sann.
+                    t.detach();
+                    data(gjennomsnitt[0], avvik[0], gjennomsnitt[1], avvik[1], gjennomsnitt[2], avvik[2]);
+                    for(int c=0; c<3; c++)
+                    {
+                        avlest[c]=0;
+                        gjennomsnitt[c]=0.0;
+                        avvik[c]=0.0;
+                        trykk[c]=0.0;
+                        fart[c]=0.0;
+                        sum_fart[c]=0.0;
+                    }
+                    t.attach(&sensor,0.5);
+                    tid.stop();
+                    tid.reset();
+                }
+            }
+            else
+            {
+                sist=false;
+            }
+        }
+        
+        tid.start();
+    }
+    
+}
+
+void sensor()                   // Funksjon for sensor
+{
+    for(i=0; i<3; i++)
+    {
+        if(i==0)
+        {
+            slave_1 = 0;                        // Velger sensor 1 som slave
+            avlest[i] = bus.write(0x0001);      // Sender dummy command for å få respons
+            slave_1 = 1;                        // Slipper slaven
+        }
+        
+        if(i==1)
+        {
+            slave_2 = 0;                        // Velger sensor 2 som slave     
+            avlest[i] = bus.write(0x0001);      // Be om trykk verdi avlesning
+            slave_2 = 1;                        // Slipper slaven
+        }
+        
+        if(i==2)
+        {
+            slave_3 = 0;                        // Velger sensor 3 som slave     
+            avlest[i] = bus.write(0x0001);          // Be om trykk verdi avlesning
+            slave_3 = 1;                        // Slipper slaven
+        }
+        
+        trykk[i]=1.0*(avlest[i]-ut_min)*(trykk_max-trykk_min)/(ut_max - ut_min)+trykk_min; // Omregningsformel gitt fra produsenten
+        fart[i]= sqrt(abs(2*trykk[i])/1000);                 // Omregner trykket til hastighet
+        pc.printf("%X\t%.2f\t\t%.2f\t", avlest[i], trykk[i], fart[i]);// Hviser innlest og omregnet verdier
+        omregning(i, fart[i]);
+    }
+    
+    pc.printf("\n");
+    lcd.locate(0,15);           // Lokaloserer hvor på skjermen verdiene skal skrives
+    lcd.printf("   %.2f    %.2f    %.2f", fart[0], fart[1], fart[2]);
+ 
+}
+
+ void omregning(int a, float speed)                             // Funksjon for sensor 2
+ {
+    ant++;
+    sum_fart[a] = sum_fart[a] + speed;  // summerer alle hastigheter
+    if(ant==144)
+    {
+        //-------------Regner ut gjennomsnittet---------//
+        for(int b=0;b<3;b++)
+        {
+            gjennomsnitt[b]=sum_fart[b]/48;
+            avvik[b]=sqrt(((pow(sum_fart[b]-gjennomsnitt[b], 2)))/(48-1));
+            pc.printf("\n Gjennonsnitt: %.2f\t Standardavvik: %.2f\n",gjennomsnitt[b],avvik[b]);
+        }
+        pc.printf("antallet: %d\n",ant);
+    }
+ }
+ 
+void send(char *arr, int lengde)
+{
+    socket.open(&net);
+    socket.connect("api.thingspeak.com", 80);
+
+    // Sender array til ThingSpeak
+    socket.send(arr, lengde);
+    pc.printf("\nSendte:%s\n\n",arr);
+
+    //Lukker socket
+    socket.close();
+}
+void data(float s1, float av1,float s2, float av2,float s3, float av3)
+{
+    char a[200];                                                                                                //Oppretter buffer
+    sprintf (a, "GET https://api.thingspeak.com/update?api_key=TT0INEEL44E31AHP&field1=%.2f&field2=%.2f&field3=%.2f&field4=%.2f&field5=%.2f&field6=%.2f\r\n",s1,av1,s2,av2,s3,av3);  //Fyller buffer med update link med riktig felt og verdi
+    send(a,200);   //Kaller opp funksjonen send for å sende til thingspeak
+    pc.printf("\n\tSensor 1\t\t\tSensor 2\t\t\tSensor 3\t\n");
+    pc.printf("Inn\tP\t\t[m/s]\tInn\tP\t\t[m/s]\tInn\tP\t\t[m/s]\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Apr 12 21:33:47 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#f9ee4e849f8cbd64f1ec5fdd4ad256585a208360