Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Websocket_Ethernet_HelloWorld by
Revision 7:f182bf9a8582, committed 2017-03-29
- Comitter:
- ShaolinPoutine
- Date:
- Wed Mar 29 04:28:39 2017 +0000
- Parent:
- 6:2fae6e37c5ca
- Commit message:
- First complete sequence terminated, character handling is needed because string and ifstream cost a lot.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 2fae6e37c5ca -r f182bf9a8582 main.cpp
--- a/main.cpp Thu Mar 16 21:15:06 2017 +0000
+++ b/main.cpp Wed Mar 29 04:28:39 2017 +0000
@@ -21,31 +21,120 @@
#include "mbed.h"
#include "EthernetInterface.h"
#include "Websocket.h"
+//#include <string>
+#include <fstream>
// Blinky
DigitalOut led(LED1);
+EthernetInterface* eth;
+Websocket* webSock;
+
+string type;
+string id;
+string zone;
+string alert_level;
+
+int init_identity()
+{
+ char a;
+ // find
+ while (infile >> a)
+ {
+
+ }
+ /*std::ifstream infile("config");
+
+ std::getline(infile, type);
+ std::getline(infile, id);*/
+ /*std::getline(infile, zone);
+ std::getline(infile, alert_level);*/
+}
+
+int init_web_socket()
+{
+ eth = new EthernetInterface();
+ while (eth->get_ip_address()[0] == 0)
+ {
+ eth->connect();
+ }
+ printf("IP Address is %s\n\r", eth->get_ip_address());
+
+ webSock = new Websocket("ws://10.163.0.100:8000/", eth);
+ int connect_error = webSock->connect();
+ while (connect_error != 0)
+ {
+ connect_error = webSock->connect();
+ }
+
+ wait(0.5);
+ return 0;
+}
+
+int send_connection_message()
+{
+ // TODO read config file to get real entity type and id
+ string entity_type = "sensor";
+ string entity_id = "C48";
+ string msg = "{\"message_id\":\"connection\", \"entity_type\":\"" + entity_type + "\", \"entity_id\":\"" + entity_id +"\"}";
+
+ if (webSock)
+ {
+ char *cstr = new char[msg.length() + 1];
+ strcpy(cstr, msg.c_str());
+ int err = webSock->send(cstr);
+ delete [] cstr;
+ return err;
+ }
+ else
+ return -1;
+}
+
+int send_low_battery_message()
+{
+ char* msg = "{\"message_id\":\"lowBattery\"}";
+ if (webSock)
+ return webSock->send(msg);
+ else
+ return -1;
+}
+
+int send_alert_message()
+{
+ string trespasser = "A58";
+ string alert_type = "Urgent";
+ string zone = "<Salle a manger>";
+
+ char msg[200] = "{\"message_id\":\"alert\", \"alert_type\":\"" + alert_type + "\", \"target_id\":\"" + trespasser + "\", \"zone\":\"" + zone + "\"}";
+
+ if (webSock)
+ {
+ char *cstr = new char[msg.length() + 1];
+ strcpy(cstr, msg.c_str());
+ int err = webSock->send(cstr);
+ delete [] cstr;
+ return err;
+ }
+ else
+ return -1;
+}
+
int main() {
-
+
// announce
printf("Websocket Example v1.0.0\r\n");
+ init_identity();
+ printf("I am %s of type %s", id, type);
+ init_web_socket();
+ send_connection_message();
+ send_low_battery_message();
+ send_alert_message();
- // Create a network interface and connect
- EthernetInterface eth;
- eth.connect();
- printf("IP Address is %s\n\r", eth.get_ip_address());
-
- // Create a websocket instance
- Websocket ws("ws://example.com:8080/", ð);
- int connect_error = ws.connect();
-
- // begin main loop
while (true) {
// blink...
led = !led;
wait(0.5);
- int error_c = ws.send("Hello World\r\n");
}
}
\ No newline at end of file
