-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino
61 lines (52 loc) · 1.51 KB
/
arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Connects the ESP8266 NodeMCU board to wifi and prints the IP address.
* Accept a client and simulate a ph detection
* sending data to the client in JSON format.
*/
#include "ESP8266WiFi.h"
// WiFi parameters to be configured
const char* ssid = "router_username"; // Write here your router's username
const char* password = "router_password"; // Write here your router's passward
const int serverPort = 8888;
WiFiServer server(serverPort);
void setup(void)
{
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print a new line, then print WiFi connected and the IP address
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
startServer(serverPort);
}
void loop() {
// prova
WiFiClient client = server.available();
if (client) {
Serial.println("Client connected");
while (client.connected()) {
double phValue;
randomSeed(millis());
phValue = random(0,15);
String jsonData = "{\"ph\": " + String(phValue) + "}";
Serial.println("detected data: "+jsonData);
// Invia i dati al client
client.println(jsonData);
client.flush();
delay(1000);
}
Serial.println("Client disconnected");
}
}
void startServer(int port){
server.begin();
Serial.println("Server started");
}