1
+ /*
2
+ * Copyright (C) Orange
3
+ *
4
+ * This software is distributed under the terms and conditions of the 'MIT'
5
+ * license which can be found in the file 'LICENSE.md' in this package distribution
6
+ */
7
+ #include " LiveObjectsWiFi.h"
8
+ #ifdef WIFID
9
+ LiveObjectsWiFi::LiveObjectsWiFi ()
10
+ :
11
+ LiveObjectsMKR()
12
+ ,m_sMac()
13
+ ,m_sIP()
14
+ {
15
+ }
16
+ LiveObjectsWiFi::~LiveObjectsWiFi ()
17
+ {}
18
+
19
+
20
+
21
+ void LiveObjectsWiFi::begin (Protocol p, Encoding s, bool bDebug)
22
+ {
23
+ LiveObjectsBase::begin (p,s,bDebug);
24
+ if (p != MQTT)
25
+ {
26
+ outputDebug (ERR," Wrong protocol! This board support only MQTT! Stopping...." );
27
+ while (true );
28
+ }
29
+ switch (m_Security)
30
+ {
31
+ case TLS:
32
+ #ifdef ARDUINO_SAMD_MKR1000
33
+ outputDebug (ERR," TLS NOT COMPATIBLE, STOPPING..." );
34
+ while (true );
35
+ #endif
36
+ m_pClient = new WiFiSSLClient ();
37
+ m_pMqttclient = new MqttClient (m_pClient);
38
+ m_nPort = 8883 ;
39
+ break ;
40
+ case NONE:
41
+ m_pClient = new WiFiClient ();
42
+ m_pMqttclient = new MqttClient (m_pClient);
43
+ m_nPort = 1883 ;
44
+ break ;
45
+ default :
46
+ outputDebug (ERR," Wrong mode! Stopping..." );
47
+ while (true );
48
+ }
49
+ m_bInitialized = true ;
50
+ m_pMqttclient->onMessage (messageCallback);
51
+ }
52
+
53
+ void LiveObjectsWiFi::connectNetwork ()
54
+ {
55
+ #ifdef ADAFRUIT_FEATHER_M0
56
+ WiFi.setPins (8 ,7 ,4 ,2 );
57
+ #endif
58
+ if (!m_bInitialized)
59
+ {
60
+ outputDebug (WARN," missing begin() call, calling with default protcol=MQTT, security protcol=TLS, debug=true" );
61
+ begin ();
62
+ }
63
+ if (WiFi.status () == WL_NO_SHIELD) {
64
+ outputDebug (ERR," Communication with WiFi module failed! Stopping here..." );
65
+ // don't continue
66
+ while (true );
67
+ }
68
+
69
+ if (String (SECRET_SSID).length ()<1 )
70
+ {
71
+ outputDebug (ERR," SECRET_SSID is empty, check arduino_secrets.h, Stopping here..." );
72
+ while (true );
73
+ }
74
+ String s = WiFi.firmwareVersion ();
75
+ outputDebug (INFO," WiFi firmware version " ,s);
76
+
77
+ outputDebug (INFO," Attempting to connect to SSID: " , SECRET_SSID);
78
+
79
+ while (WiFi.begin (SECRET_SSID.c_str (), SECRET_WIFI_PASS.c_str ()) != WL_CONNECTED) {
80
+ // failed, retry
81
+ outputDebug (TXT," ." );
82
+ delay (1000 );
83
+ }
84
+ outputDebug ();
85
+ IPAddress ip = WiFi.localIP ();
86
+ for (int i=0 ;i<4 ;++i)
87
+ {
88
+ m_sIP+=ip[i];
89
+ if (i!=3 ) m_sIP+=' .' ;
90
+ }
91
+
92
+ uint8_t mac[6 ];
93
+ char buff[10 ];
94
+ WiFi.macAddress (mac);
95
+
96
+ for (int i=5 ;i>=0 ;--i)
97
+ {
98
+ memset (buff,' \0 ' ,10 );
99
+ itoa (mac[i],buff,16 );
100
+ if (mac[i]<17 )
101
+ {
102
+ m_sMac+=" 0" ;
103
+ m_sMqttid+=" 0" ;
104
+ }
105
+ for (int j=0 ;j<strlen (buff);++j)
106
+ {
107
+ m_sMac += (char )toupper (buff[j]);
108
+ m_sMqttid += (char )toupper (buff[j]);
109
+ }
110
+ if (i!=0 ) m_sMac += ' :' ;
111
+ }
112
+ m_sModel = m_sMqttid;
113
+ }
114
+ void LiveObjectsWiFi::checkNetwork ()
115
+ {
116
+ if (WiFi.status ()== WL_DISCONNECTED)
117
+ connectNetwork ();
118
+ }
119
+ void LiveObjectsWiFi::disconnectNetwork ()
120
+ {
121
+ outputDebug (INFO," Disconnecting WiFi" );
122
+ WiFi.disconnect ();
123
+ }
124
+ void LiveObjectsWiFi::messageCallback (int msg)
125
+ {
126
+ LiveObjects::get ().onMQTTmessage (msg);
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ void LiveObjectsWiFi::addNetworkInfo ()
136
+ {
137
+ String tmp;
138
+ tmp = String (WiFi.RSSI ());
139
+ tmp += " dbm" ;
140
+ if (m_Protocol == MQTT && m_Encoding==TEXT) addToPayload (easyDataPayload[JSONVALUE].createNestedObject (" networkInfo" )," mac" ,m_sMac," ssid" ,SECRET_SSID," ip" ,m_sIP," strength" ,tmp);
141
+ else addToPayload (m_sMac,SECRET_SSID,m_sIP,tmp);
142
+ }
143
+ #endif
0 commit comments