ESP8266 and nRF51822 simple WiFi connection and temperature publishing to a socket server.
Dependencies: BLE_API DnsQuery ESP8266Interface NetworkSocketAPI mbed nRF51822
Fork of BLE_TemperatureBeacon by
wifi.cpp@6:d5f97e422fed, 2015-08-29 (annotated)
- Committer:
- kixorz
- Date:
- Sat Aug 29 21:58:02 2015 +0000
- Revision:
- 6:d5f97e422fed
wifi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kixorz | 6:d5f97e422fed | 1 | /* NetworkSocketAPI Example Program |
kixorz | 6:d5f97e422fed | 2 | * Copyright (c) 2015 ARM Limited |
kixorz | 6:d5f97e422fed | 3 | * |
kixorz | 6:d5f97e422fed | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kixorz | 6:d5f97e422fed | 5 | * you may not use this file except in compliance with the License. |
kixorz | 6:d5f97e422fed | 6 | * You may obtain a copy of the License at |
kixorz | 6:d5f97e422fed | 7 | * |
kixorz | 6:d5f97e422fed | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kixorz | 6:d5f97e422fed | 9 | * |
kixorz | 6:d5f97e422fed | 10 | * Unless required by applicable law or agreed to in writing, software |
kixorz | 6:d5f97e422fed | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kixorz | 6:d5f97e422fed | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kixorz | 6:d5f97e422fed | 13 | * See the License for the specific language governing permissions and |
kixorz | 6:d5f97e422fed | 14 | * limitations under the License. |
kixorz | 6:d5f97e422fed | 15 | */ |
kixorz | 6:d5f97e422fed | 16 | |
kixorz | 6:d5f97e422fed | 17 | #include "mbed.h" |
kixorz | 6:d5f97e422fed | 18 | #include "ESP8266Interface.h" |
kixorz | 6:d5f97e422fed | 19 | |
kixorz | 6:d5f97e422fed | 20 | DigitalOut myled(LED1); |
kixorz | 6:d5f97e422fed | 21 | DigitalOut status(LED2); |
kixorz | 6:d5f97e422fed | 22 | void flash(){ myled = !myled; } |
kixorz | 6:d5f97e422fed | 23 | |
kixorz | 6:d5f97e422fed | 24 | void setStatus(int num){ |
kixorz | 6:d5f97e422fed | 25 | for(int i = 0; i < num * 2; ++i){ |
kixorz | 6:d5f97e422fed | 26 | status = !status; |
kixorz | 6:d5f97e422fed | 27 | wait(0.2f); |
kixorz | 6:d5f97e422fed | 28 | } |
kixorz | 6:d5f97e422fed | 29 | } |
kixorz | 6:d5f97e422fed | 30 | |
kixorz | 6:d5f97e422fed | 31 | ESP8266Interface wifi(P0_23, P0_21); |
kixorz | 6:d5f97e422fed | 32 | |
kixorz | 6:d5f97e422fed | 33 | static SocketInterface* mySocket; |
kixorz | 6:d5f97e422fed | 34 | void wifiInit() { |
kixorz | 6:d5f97e422fed | 35 | wifi.init(); |
kixorz | 6:d5f97e422fed | 36 | int cs = wifi.connect("DemoRoom", ""); |
kixorz | 6:d5f97e422fed | 37 | setStatus(cs == 0 ? 1 : 3); |
kixorz | 6:d5f97e422fed | 38 | |
kixorz | 6:d5f97e422fed | 39 | mySocket = wifi.allocateSocket(SOCK_TCP); |
kixorz | 6:d5f97e422fed | 40 | |
kixorz | 6:d5f97e422fed | 41 | mySocket->setAddressPort("192.168.2.235", 1234); |
kixorz | 6:d5f97e422fed | 42 | mySocket->open(); |
kixorz | 6:d5f97e422fed | 43 | |
kixorz | 6:d5f97e422fed | 44 | //TODO |
kixorz | 6:d5f97e422fed | 45 | //mySocket->close(); |
kixorz | 6:d5f97e422fed | 46 | //wifi.disconnect(); |
kixorz | 6:d5f97e422fed | 47 | } |
kixorz | 6:d5f97e422fed | 48 | |
kixorz | 6:d5f97e422fed | 49 | void wifiSend(float temp) { |
kixorz | 6:d5f97e422fed | 50 | setStatus(1); |
kixorz | 6:d5f97e422fed | 51 | mySocket->send(&temp, sizeof(temp), 100); |
kixorz | 6:d5f97e422fed | 52 | } |