ARM mbed workshop #1. (1) mbed Toolsl; (2) mbed Digital Interface; (3) mbed Networking - WiflyInterface; (4) httpd and REST APIs; (5) Websocket.

Dependencies:   WebSocketClient WiflyInterface mbed

Fork of Mokoversity_WoT_Wifly_Temperature by jollen chen

Committer:
jollen
Date:
Fri Mar 20 04:52:46 2015 +0000
Revision:
0:d064b9670483
ARM mbed workshop #1.;  * mbed Tools;  * mbed Digital Interface;  * Netowkring: Wifly;  * mbed httpd and REST API;  * Websocket

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jollen 0:d064b9670483 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
jollen 0:d064b9670483 2 *
jollen 0:d064b9670483 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jollen 0:d064b9670483 4 * and associated documentation files (the "Software"), to deal in the Software without
jollen 0:d064b9670483 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
jollen 0:d064b9670483 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
jollen 0:d064b9670483 7 * Software is furnished to do so, subject to the following conditions:
jollen 0:d064b9670483 8 *
jollen 0:d064b9670483 9 * The above copyright notice and this permission notice shall be included in all copies or
jollen 0:d064b9670483 10 * substantial portions of the Software.
jollen 0:d064b9670483 11 *
jollen 0:d064b9670483 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jollen 0:d064b9670483 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jollen 0:d064b9670483 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jollen 0:d064b9670483 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jollen 0:d064b9670483 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jollen 0:d064b9670483 17 */
jollen 0:d064b9670483 18 #include "mbed.h"
jollen 0:d064b9670483 19 #include "WiflyInterface.h"
jollen 0:d064b9670483 20 #include "Websocket.h"
jollen 0:d064b9670483 21
jollen 0:d064b9670483 22 /*
jollen 0:d064b9670483 23 * Mokoversity Farm Team
jollen 0:d064b9670483 24 *
jollen 0:d064b9670483 25 * ARM mbed IoT workshop at Taipei / Shenzhen.
jollen 0:d064b9670483 26 * For more information, please see https://www.mokoversity.com/farm
jollen 0:d064b9670483 27 */
jollen 0:d064b9670483 28
jollen 0:d064b9670483 29
jollen 0:d064b9670483 30 /*
jollen 0:d064b9670483 31 * Sensor Device
jollen 0:d064b9670483 32 */
jollen 0:d064b9670483 33
jollen 0:d064b9670483 34 // System status
jollen 0:d064b9670483 35 DigitalOut led1(LED1);
jollen 0:d064b9670483 36 DigitalOut led2(LED2);
jollen 0:d064b9670483 37 DigitalOut led3(LED3);
jollen 0:d064b9670483 38 DigitalOut led4(LED4);
jollen 0:d064b9670483 39
jollen 0:d064b9670483 40 // Temperature sensor at A0
jollen 0:d064b9670483 41 AnalogIn temp(P0_23);
jollen 0:d064b9670483 42
jollen 0:d064b9670483 43 /**
jollen 0:d064b9670483 44 * WiFi Device - Seeed WiFi Shield
jollen 0:d064b9670483 45 *
jollen 0:d064b9670483 46 * D1 - TX pin (RX on the WiFi side)
jollen 0:d064b9670483 47 * D0 - RX pin (TX on the WiFi side)
jollen 0:d064b9670483 48 * NC - Reset pin; use D5 otherwise the shield might get into reset loop
jollen 0:d064b9670483 49 * LED1 - TCP status pin
jollen 0:d064b9670483 50 * "ssid" - hostspot name
jollen 0:d064b9670483 51 * "password" - hotspot passowrd
jollen 0:d064b9670483 52 * security method - NONE, WEP_128, WPA
jollen 0:d064b9670483 53 */
jollen 0:d064b9670483 54 WiflyInterface eth(D1, D0, D5, LED4, "<HOTSPOT>", "<PASSWORD>", WPA);
jollen 0:d064b9670483 55
jollen 0:d064b9670483 56 /*
jollen 0:d064b9670483 57 * Global Variables
jollen 0:d064b9670483 58 */
jollen 0:d064b9670483 59 int a;
jollen 0:d064b9670483 60 float temperature;
jollen 0:d064b9670483 61 int B = 3975; //B value of the thermistor
jollen 0:d064b9670483 62 float resistance;
jollen 0:d064b9670483 63
jollen 0:d064b9670483 64 int main()
jollen 0:d064b9670483 65 {
jollen 0:d064b9670483 66 char data[256];
jollen 0:d064b9670483 67 int ret;
jollen 0:d064b9670483 68
jollen 0:d064b9670483 69 wait(3);
jollen 0:d064b9670483 70
jollen 0:d064b9670483 71 // init status
jollen 0:d064b9670483 72 led1 = 0;
jollen 0:d064b9670483 73 led2 = 0;
jollen 0:d064b9670483 74 led3 = 0;
jollen 0:d064b9670483 75
jollen 0:d064b9670483 76 //Use DHCP
jollen 0:d064b9670483 77 ret = eth.init();
jollen 0:d064b9670483 78 //eth.init("192.168.21.45","255.255.255.0","192.168.21.2");
jollen 0:d064b9670483 79 if (ret != NULL) {
jollen 0:d064b9670483 80 led1 = 0;
jollen 0:d064b9670483 81 led2 = 1;
jollen 0:d064b9670483 82 led3 = 0;
jollen 0:d064b9670483 83 led4 = 1;
jollen 0:d064b9670483 84
jollen 0:d064b9670483 85 exit(0);
jollen 0:d064b9670483 86 }
jollen 0:d064b9670483 87
jollen 0:d064b9670483 88 while (1) {
jollen 0:d064b9670483 89 ret = eth.connect();
jollen 0:d064b9670483 90
jollen 0:d064b9670483 91 if (ret == false || ret < 0) {
jollen 0:d064b9670483 92 led1 = !led1;
jollen 0:d064b9670483 93 wait(1);
jollen 0:d064b9670483 94 } else {
jollen 0:d064b9670483 95 led1 = 1;
jollen 0:d064b9670483 96 break;
jollen 0:d064b9670483 97 }
jollen 0:d064b9670483 98 }
jollen 0:d064b9670483 99
jollen 0:d064b9670483 100 /*
jollen 0:d064b9670483 101 * We use WoT.City Websocket channel service.
jollen 0:d064b9670483 102 * See: https://www.mokoversity.com/wotcity
jollen 0:d064b9670483 103 */
jollen 0:d064b9670483 104 Websocket ws("ws://wot.city/object/jollentemp1/send");
jollen 0:d064b9670483 105 while( !ws.connect() );
jollen 0:d064b9670483 106 led2 = 1;
jollen 0:d064b9670483 107
jollen 0:d064b9670483 108 while(1) {
jollen 0:d064b9670483 109 // multiply ain by 675 if the Grove shield is set to 5V or 1023 if set to 3.3V
jollen 0:d064b9670483 110 a = temp * 675;
jollen 0:d064b9670483 111
jollen 0:d064b9670483 112 // get resistance value of the sensor
jollen 0:d064b9670483 113 resistance = (float)(1023-a)*10000/a;
jollen 0:d064b9670483 114
jollen 0:d064b9670483 115 //convert resistance value to temperature via datasheet
jollen 0:d064b9670483 116 temperature = 1 / (log(resistance/10000)/B+1/298.15) - 273.15;
jollen 0:d064b9670483 117
jollen 0:d064b9670483 118 wait(0.8);
jollen 0:d064b9670483 119
jollen 0:d064b9670483 120 sprintf( data , "{ \"temperature\": %f }",temperature);
jollen 0:d064b9670483 121 ws.send(data);
jollen 0:d064b9670483 122 }
jollen 0:d064b9670483 123 }