Browse Source

la ça marche pas trop mal

master
DemiSel 3 years ago
parent
commit
17f36299db

+ 1
- 0
.idea/.name View File

@@ -0,0 +1 @@
1
+CyberCuisine

+ 1
- 1
.idea/compiler.xml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project version="4">
3 3
   <component name="CompilerConfiguration">
4
-    <bytecodeTargetLevel target="15" />
4
+    <bytecodeTargetLevel target="14" />
5 5
   </component>
6 6
 </project>

+ 1
- 1
.idea/misc.xml View File

@@ -1,7 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project version="4">
3 3
   <component name="ExternalStorageConfigurationManager" enabled="true" />
4
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
4
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="adopt-openjdk-14" project-jdk-type="JavaSDK">
5 5
     <output url="file://$PROJECT_DIR$/out" />
6 6
   </component>
7 7
 </project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/.idea/CyberCuisineWebServer.iml" filepath="$PROJECT_DIR$/.idea/CyberCuisineWebServer.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

arduino/cybergaziniere.ino → arduino/cybergaziniere/cybergaziniere.ino View File

@@ -1,12 +1,23 @@
1 1
 #include "WiFi.h"
2
+//https://github.com/orgua/OneWireHub or
3
+//https://www.pjrc.com/teensy/td_libs_OneWire.html or
4
+//https://github.com/RobTillaart/DS18B20_RT
2 5
 #include <OneWire.h>
3 6
 #include <DS18B20.h>
7
+//https://github.com/avishorp/TM1637
4 8
 #include <TM1637Display.h>
5 9
 
6
-//const char* ssid = "Antenne 5G";           
7
-//const char* password = "Un Truc Moins Chiant"; 
8
-const char* ssid = "Livebox-8DA0";           
9
-const char* password = "C3F3EE221002051A31382FFAF7"; 
10
+char* ssid = "Antenne 5G";           
11
+char* password = "Un Truc Moins Chiant"; 
12
+//char* ssid = "Livebox-8DA0";           
13
+//char* password = "C3F3EE221002051A31382FFAF7"; 
14
+
15
+char* serverHost = "192.168.0.24";
16
+int serverPort = 6868;
17
+WiFiClient tcpClient;
18
+bool tcpClientConnected;
19
+
20
+
10 21
 
11 22
 #define CLK 18
12 23
 #define DIO 5
@@ -40,7 +51,22 @@ void loop() {
40 51
   int temp = temperatureTrucs();
41 52
   //Serial.println(temp);
42 53
   display.showNumberDecEx(temp, 0x20);
43
-  
54
+
55
+  if( !tcpClient.connected() )
56
+  {
57
+    if(tcpClient.connect(serverHost,serverPort))
58
+    {
59
+     tcpClientConnected = true; 
60
+    }else
61
+    {
62
+      Serial.println("Could not connect to server");
63
+    }
64
+  }else
65
+  {
66
+    tcpClient.println(temp);
67
+    Serial.println("Connected loop");
68
+  }
69
+  delay(500);
44 70
   //;
45 71
 }
46 72
 

BIN
arduino/cybergaziniere/cybergaziniere.ino.esp32.bin View File


+ 20
- 2
build.gradle View File

@@ -3,7 +3,7 @@ plugins {
3 3
 }
4 4
 
5 5
 group 'org.example'
6
-version '1.0-SNAPSHOT'
6
+version '1.0'
7 7
 
8 8
 repositories {
9 9
     mavenCentral()
@@ -12,9 +12,27 @@ repositories {
12 12
 dependencies {
13 13
     testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
14 14
     testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
15
-    implementation 'io.undertow:undertow-core:2.1.0.Final'
15
+    compile 'io.undertow:undertow-core:2.1.0.Final'
16 16
 }
17 17
 
18 18
 test {
19 19
     useJUnitPlatform()
20
+}
21
+
22
+//create a single Jar with all dependencies
23
+task fatJar(type: Jar) {
24
+    manifest {
25
+        attributes 'Implementation-Title': 'Gradle Jar File Example',
26
+                    'Implementation-Version': version,
27
+                    'Main-Class': 'Application'
28
+    }
29
+    baseName = project.name + '-all'
30
+    from {
31
+        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
32
+
33
+    }
34
+    from('src/main/java'){
35
+        include 'resources/*'
36
+    }
37
+    with jar
20 38
 }

+ 0
- 1
src/main/java/Application.java View File

@@ -4,6 +4,5 @@ public class Application {
4 4
     {
5 5
         CuisineHTTPServer vServer = new CuisineHTTPServer();
6 6
         vServer.startServer();
7
-        System.out.println("COUCOU");
8 7
     }
9 8
 }

+ 75
- 5
src/main/java/CuisineHTTPServer.java View File

@@ -9,27 +9,37 @@ import io.undertow.websockets.core.WebSocketChannel;
9 9
 import io.undertow.websockets.core.WebSockets;
10 10
 import io.undertow.websockets.spi.WebSocketHttpExchange;
11 11
 
12
+import java.net.URL;
13
+import java.nio.file.Files;
14
+import java.nio.file.Paths;
15
+import java.util.Scanner;
16
+import java.util.stream.Stream;
17
+
12 18
 import static io.undertow.Handlers.*;
13 19
 
14 20
 public class CuisineHTTPServer extends Thread
15 21
 {
16 22
     final int webPagePort = 8080;
17 23
     final int webSocketPort = 42069;
24
+    final int tcpSocketPort = 6868;
18 25
     final String HOST = "localhost" ;
19 26
 
27
+    private static CuisineTCPServer fCuisineTCPServer;
20 28
 
21 29
     public void startServer()
22 30
     {
31
+        initResources();
32
+
23 33
         //Create CuisineTCPServers
24
-        CuisineTCPServer vTCPServer = new CuisineTCPServer();
25
-        CuisineWebSocketMessageHandler vWebSocketMessageHandler         = new CuisineWebSocketMessageHandler( vTCPServer );
34
+        fCuisineTCPServer = new CuisineTCPServer(tcpSocketPort);
35
+        CuisineWebSocketMessageHandler vWebSocketMessageHandler         = new CuisineWebSocketMessageHandler( fCuisineTCPServer );
26 36
         CuisineWebSocketConnectionHandler vWebSocketConnectionHandler   = new CuisineWebSocketConnectionHandler( vWebSocketMessageHandler );
27 37
 
28 38
         //Start HTTP Server : serve webpage with JS that calls the websocket
29 39
         Undertow WebPageServer = Undertow.builder()
30 40
                 .addHttpListener(webPagePort, HOST)
31 41
                 .setHandler(
32
-                        new CuisineResponseHandler()
42
+                        new CuisineResponseHandler("ws://"+HOST+":"+webSocketPort+"/data")
33 43
                             ).build();
34 44
         WebPageServer.start();
35 45
 
@@ -47,14 +57,38 @@ public class CuisineHTTPServer extends Thread
47 57
 
48 58
     }
49 59
 
60
+    static String sHtml_ASCII_title;
61
+    static String sHtml_CSS;
62
+    static void initResources()
63
+    {
64
+        try {
65
+            sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
66
+            sHtml_CSS = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
67
+        }
68
+        catch(Exception vex)
69
+        {
70
+            vex.printStackTrace();
71
+            sHtml_ASCII_title = "Galère les resources";
72
+        }
73
+    }
74
+
50 75
     private class CuisineResponseHandler implements HttpHandler
51 76
     {
77
+        private String fWebSocketConnectionUri;
52 78
 
79
+        public CuisineResponseHandler(String iWebSocketConnectionUri)
80
+        {
81
+            super();
82
+
83
+            fWebSocketConnectionUri = iWebSocketConnectionUri;
84
+        }
53 85
         @Override
54 86
         public void handleRequest(HttpServerExchange exchange) throws Exception {
55 87
             //Serve webpage
56
-            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
57
-            exchange.getResponseSender().send("Salut, gropd!");
88
+            exchange.getResponseHeaders()
89
+                    .put(Headers.CONTENT_TYPE, "text/html")
90
+                    .put(Headers.CONTENT_ENCODING,"utf-8");
91
+            exchange.getResponseSender().send(getHTMLFrontPage(fWebSocketConnectionUri));
58 92
         }
59 93
     }
60 94
 
@@ -80,6 +114,7 @@ public class CuisineHTTPServer extends Thread
80 114
 
81 115
         protected CuisineWebSocketMessageHandler( CuisineTCPServer iTCPServer)
82 116
         {
117
+
83 118
             fTCPServer = iTCPServer;
84 119
         }
85 120
 
@@ -88,6 +123,41 @@ public class CuisineHTTPServer extends Thread
88 123
             //Handle message on webSocket
89 124
             WebSockets.sendText(String.valueOf(fTCPServer.getCurrentTemperature()), iChannel, null);
90 125
         }
126
+    }
127
+
91 128
 
129
+    static String getHTMLFrontPage(String iWsUri)
130
+    {
131
+        StringBuilder vBuilder = new StringBuilder();
132
+        vBuilder
133
+            .append("<head>")
134
+            .append("<meta charset=\"UTF-8\">")
135
+            .append("<style>")
136
+            .append(sHtml_CSS)
137
+            .append("</style>")
138
+            .append("</head>")
139
+            .append("<div class=\"pre\">")
140
+            .append(sHtml_ASCII_title)
141
+            .append("</div>")
142
+            .append("<h1 id=\"tempValue\">None</h1>")
143
+            .append("<script>")
144
+            .append("var socket=new WebSocket(\""+iWsUri+"\");\n")
145
+            .append("let connected=false;")
146
+            .append("let view=document.getElementById(\"tempValue\");")
147
+            .append("socket.onerror=function(error){connected=false;view.innerHTML=\"oups :(\";};")
148
+            .append("socket.onmessage=function(event){view.innerHTML=event.data+\"&deg;C\"};")
149
+            .append("socket.onopen=function(e){")
150
+            .append("connected=true;")
151
+            .append("callServer();")
152
+            .append("};")
153
+            .append("var callServer=function(){")
154
+            .append("socket.send(\"coucou\");")
155
+            .append("};")
156
+            .append("let timer=setInterval(callServer,100);")
157
+            .append("</script>")
158
+            .append("")
159
+
160
+            ;
161
+        return vBuilder.toString();
92 162
     }
93 163
 }

+ 5
- 5
src/main/java/CuisineTCPServer.java View File

@@ -7,10 +7,10 @@ public class CuisineTCPServer extends Thread{
7 7
     private float fCurrentTemperature;
8 8
     private ServerSocket fServerSocket;
9 9
 
10
-    public CuisineTCPServer()
10
+    public CuisineTCPServer(int iPort)
11 11
     {
12 12
         try {
13
-            fServerSocket = new ServerSocket(6868);
13
+            fServerSocket = new ServerSocket(iPort);
14 14
             start();
15 15
 
16 16
             fCurrentTemperature = 0.0f;
@@ -28,11 +28,11 @@ public class CuisineTCPServer extends Thread{
28 28
 
29 29
     public void run()
30 30
     {
31
+        System.out.println("TCP Server Started");
31 32
         while(true)
32 33
         {
33 34
             try{
34 35
                 Socket vConnection = fServerSocket.accept();
35
-                System.out.println("Accepted new connection");
36 36
                 new CuisineTalker(vConnection).start();
37 37
             }
38 38
             catch(Exception ve)
@@ -61,9 +61,9 @@ public class CuisineTCPServer extends Thread{
61 61
                 {
62 62
                     InputStream vInputStream    = fSocket.getInputStream();
63 63
                     BufferedReader vBuffReader  = new BufferedReader(new InputStreamReader(vInputStream));
64
-
65 64
                     try{
66
-                        fCurrentTemperature = Float.parseFloat(vBuffReader.readLine());
65
+                        String vReceived = vBuffReader.readLine();
66
+                        fCurrentTemperature = Float.parseFloat(vReceived)/10;
67 67
                     }
68 68
                     catch(Exception ve)
69 69
                     {

+ 8
- 0
src/main/resources/html_ascii_title View File

@@ -0,0 +1,8 @@
1
+   _____      _                  _____      _     _
2
+  / ____|    | |                / ____|    (_)   (_)
3
+ | |    _   _| |__   ___ _ __  | |    _   _ _ ___ _ _ __   ___
4
+ | |   | | | | '_ \ / _ \ '__| | |   | | | | / __| | '_ \ / _ \
5
+ | |___| |_| | |_) |  __/ |    | |___| |_| | \__ \ | | | |  __/
6
+  \_____\__, |_.__/ \___|_|     \_____\__,_|_|___/_|_| |_|\___|
7
+         __/ |
8
+        |___/

+ 4
- 0
src/main/resources/style.css View File

@@ -0,0 +1,4 @@
1
+.pre{
2
+    font-family: monospace;
3
+    white-space: pre;
4
+}

Loading…
Cancel
Save