ابتدا نرم افزار را دانلود و روی گوشی موبایل نصب نمایید
دانلود virtuino
برای روشن کردن led روی esp32 کد زیر را آپلود نمایید :
دانلود کد آردوینو
کد متنی
کد :
#include
#include // Download the library WebSockets by Markus Sattler from arduino library manager (ver 2.3.5)
//--- SETTINGS ----------------------------------------
const char* ssid = "mortaza"; // WIFI network SSID
const char* password = "mortazarezaei"; // WIFI network PASSWORD
WebSocketsServer webSocket = WebSocketsServer(8000); // Default Virtuino Server port = 8000
//===================================================== onValueReceived
// It is called every time a new value received
void onValueReceived(String tag, String value){
Serial.println("Received: tag="+tag+ " value="+value);
//Serial.println("salaaam"+tag);
if (tag=="led") { // write to digital pin D5
Serial.println("on "+tag);
int v=value.toInt();
if (v==0) digitalWrite(2,HIGH); else digitalWrite(2,LOW);
}
}
void setup() {
Serial.begin(115200);
while (!Serial) delay(1);
pinMode(2,OUTPUT); // on this example the pin D5 is used as OUTPUT
connectToWiFiNetwork();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
//===================================================== loop
//=====================================================
void loop() {
webSocket.loop();
}
//===================================================== connectToWiFiNetwork
void connectToWiFiNetwork(){
Serial.println("Connecting to "+String(ssid));
WiFi.mode(WIFI_STA); // Configure the module as station only.
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.println(WiFi.localIP()); // Insert this IP on Virtuino IoT server settings
}
//===================================================== sendValue
// This function sends a value to a Tag.
// The tag and the value are converted to a json message before sending
bool sendValue(const char* tag, String value){
String json = "{\"";
json+=tag;
json+="\":\"";
json+=value;
json+="\"}";
Serial.println("Send: "+json);
return webSocket.broadcastTXT(json); // This function sends the message to all connected clients.
}
//===================================================== webSocketEvent
//This is the server handler. It receives all the messages
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] New client connected - IP: %d.%d.%d.%d \n", num, ip[0], ip[1], ip[2], ip[3]);
break;
}
case WStype_TEXT: // a new message received
Serial.printf("[%u] Received: %s\n", num, payload);
//-- The incoming payload is a json message. The following code extracts the value from json without extra library
String str = (char*)payload;
int p1 = str.indexOf("{\"");
if (p1==0) {
int p2 = str.indexOf("\":");
if (p2>p1) {
String tag = str.substring(p1+2,p2);
p1 = str.indexOf(":\"",p2);
if (p1>0) {
p2 = str.lastIndexOf("\"}");
if (p2>0){
String value = str.substring(p1+2,p2);
onValueReceived(tag,value);
}
}
}
}
break;
}
}
برچسبها: