11

Dependencies:   Sht31

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "MQTTmbed.h"
00019 #include "MQTTClientMbedOs.h"
00020 #include "Sht31.h"
00021 #define MQTT_MAX_PACKET_SIZE 400 
00022 #define MQTT_MAX_PAYLOAD_SIZE 300
00023 
00024 Sht31 temp_sensor(I2C_SDA, I2C_SCL);
00025 int temp = 1;
00026 AnalogIn my_adc(D11);
00027 float t;
00028 float h;
00029 void ibm_cloud_demo(NetworkInterface *net)
00030 {
00031     TCPSocket socket;
00032     MQTTClient client(&socket);
00033      SocketAddress a;
00034     char* hostname = "dev.rightech.io";
00035     net->gethostbyname(hostname, &a);
00036     int port = 1883;
00037     a.set_port(port);
00038      printf("Connecting to %s:%d\r\n", hostname, port);
00039     socket.open(net);
00040      printf("Opened socket\n\r");
00041      int rc = socket.connect(a);
00042      if (rc != 0)
00043         printf("rc from TCP connect is %d\r\n", rc);
00044      printf("Connected socket\n\r");
00045      MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00046      data.MQTTVersion = 3;
00047      data.clientID.cstring = "mqtt-graf2114-ylbl1u";
00048      data.username.cstring = "Qwerty1";
00049      data.password.cstring = "Qwerty1";
00050      //{clientId:"Qwerty1",userName:"Qwerty2",password:"Qwerty3"}
00051     if ((rc = client.connect(data)) != 0)
00052     printf("rc from MQTT connect is %d\r\n", rc);
00053     t = temp_sensor.readTemperature();
00054     MQTT::Message message;
00055     char buf[MQTT_MAX_PAYLOAD_SIZE];   
00056     sprintf(buf,"%d", t);
00057     message.qos = MQTT::QOS0;
00058     message.retained = false;
00059     message.dup = false;
00060     message.payload = (void*)buf;
00061     message.payloadlen = strlen(buf);
00062     char* topic = "temp";
00063     if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE )
00064     printf("message too long!\r\n");
00065     rc = client.publish(topic, message);
00066     
00067     h = temp_sensor.readHumidity();  
00068     sprintf(buf,"%f", h);
00069     message.qos = MQTT::QOS0;
00070     message.retained = false;
00071     message.dup = false;
00072     message.payload = (void*)buf;
00073     message.payloadlen = strlen(buf);
00074     char* topic = "uhum";
00075     if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE )
00076     printf("message too long!\r\n");
00077     rc = client.publish(topic, message);
00078      
00079     sprintf(buf,"%f"(my_adc.read()*100);
00080     message.qos = MQTT::QOS0;
00081     message.retained = false;
00082     message.dup = false;
00083     message.payload = (void*)buf;
00084     message.payloadlen = strlen(buf);
00085     char* topic = "hum";
00086     if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE )
00087     printf("message too long!\r\n");
00088     rc = client.publish(topic, message);
00089 
00090     return; 
00091 }
00092 
00093 
00094 WiFiInterface *wifi;
00095 
00096 const char *sec2str(nsapi_security_t sec)
00097 {
00098     switch (sec) {
00099         case NSAPI_SECURITY_NONE:
00100             return "None";
00101         case NSAPI_SECURITY_WEP:
00102             return "WEP";
00103         case NSAPI_SECURITY_WPA:
00104             return "WPA";
00105         case NSAPI_SECURITY_WPA2:
00106             return "WPA2";
00107         case NSAPI_SECURITY_WPA_WPA2:
00108             return "WPA/WPA2";
00109         case NSAPI_SECURITY_UNKNOWN:
00110         default:
00111             return "Unknown";
00112     }
00113 }
00114 
00115 int scan_demo(WiFiInterface *wifi)
00116 {
00117     WiFiAccessPoint *ap;
00118 
00119     printf("Scan:\n");
00120 
00121     int count = wifi->scan(NULL,0);
00122 
00123     if (count <= 0) {
00124         printf("scan() failed with return value: %d\n", count);
00125         return 0;
00126     }
00127 
00128     /* Limit number of network arbitrary to 15 */
00129     count = count < 15 ? count : 15;
00130 
00131     ap = new WiFiAccessPoint[count];
00132     count = wifi->scan(ap, count);
00133 
00134     if (count <= 0) {
00135         printf("scan() failed with return value: %d\n", count);
00136         return 0;
00137     }
00138 
00139     for (int i = 0; i < count; i++) {
00140         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00141                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00142                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00143     }
00144     printf("%d networks available.\n", count);
00145 
00146     delete[] ap;
00147     return count;
00148 }
00149 
00150 int main()
00151 {
00152     printf("WiFi example\n");
00153 
00154 #ifdef MBED_MAJOR_VERSION
00155     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00156 #endif
00157 
00158     wifi = WiFiInterface::get_default_instance();
00159     if (!wifi) {
00160         printf("ERROR: No WiFiInterface found.\n");
00161         return -1;
00162     }
00163 
00164     int count = scan_demo(wifi);
00165     if (count == 0) {
00166         printf("No WIFI APs found - can't continue further.\n");
00167         return -1;
00168     }
00169 
00170     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00171     int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00172     if (ret != 0) {
00173         printf("\nConnection error: %d\n", ret);
00174         return -1;
00175     }
00176 
00177     printf("Success\n\n");
00178     printf("MAC: %s\n", wifi->get_mac_address());
00179     printf("IP: %s\n", wifi->get_ip_address());
00180     printf("Netmask: %s\n", wifi->get_netmask());
00181     printf("Gateway: %s\n", wifi->get_gateway());
00182     printf("RSSI: %d\n\n", wifi->get_rssi());
00183     while (1)
00184     {
00185    if (temp<2) {
00186     ibm_cloud_demo(wifi);
00187     wait_ms(1000);
00188     temp++;
00189     } 
00190     else 
00191         while(1)
00192             {
00193                 relay = !relay;
00194                 wait_ms(500);
00195                 }
00196     }
00197     printf("Done\n\n");
00198 }