[224] | 1 | /*
|
---|
| 2 | ****************************************************************************
|
---|
| 3 | * Copyright (c) 2015 Arduino srl. All right reserved.
|
---|
| 4 | *
|
---|
| 5 | * File : wifi.cpp
|
---|
| 6 | * Date : 2016/02/16
|
---|
| 7 | * Revision : 0.0.1 $
|
---|
| 8 | * Author: adriano[at]arduino[dot]org
|
---|
| 9 | *
|
---|
| 10 | ****************************************************************************
|
---|
| 11 | This library is free software; you can redistribute it and/or
|
---|
| 12 | modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | License as published by the Free Software Foundation; either
|
---|
| 14 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 15 |
|
---|
| 16 | This library is distributed in the hope that it will be useful,
|
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | Lesser General Public License for more details.
|
---|
| 20 |
|
---|
| 21 | You should have received a copy of the GNU Lesser General Public
|
---|
| 22 | License along with this library; if not, write to the Free Software
|
---|
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | #include <stdarg.h>
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 |
|
---|
| 30 | #include "Ciao.h"
|
---|
| 31 | #include "crc16.h"
|
---|
| 32 | #include "rest.h"
|
---|
| 33 | #include "espduino.h"
|
---|
| 34 | #include "ringbuf.h"
|
---|
| 35 | #include "FP.h"
|
---|
| 36 | #include "string.h"
|
---|
| 37 |
|
---|
| 38 | #if defined(__AVR_ATmega328P__)
|
---|
| 39 |
|
---|
| 40 | WifiData espSerial;
|
---|
| 41 | ESP esp(&espSerial);
|
---|
| 42 | REST rest(&esp);
|
---|
| 43 |
|
---|
| 44 | boolean wifiConnected = false;
|
---|
| 45 |
|
---|
| 46 | ArduinoWifiClass Wifi;
|
---|
| 47 | CiaoClass Ciao;
|
---|
| 48 |
|
---|
| 49 | void wifiCb(void* response)
|
---|
| 50 | {
|
---|
| 51 | uint32_t status;
|
---|
| 52 | RESPONSE res(response);
|
---|
| 53 |
|
---|
| 54 | if(res.getArgc() == 1) {
|
---|
| 55 | res.popArgs((uint8_t*)&status, 4);
|
---|
| 56 | if(status == STATION_GOT_IP){
|
---|
| 57 | espSerial.println("DBG: Internet Connected");
|
---|
| 58 | wifiConnected = true;
|
---|
| 59 | }
|
---|
| 60 | else {
|
---|
| 61 | wifiConnected = false;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void ArduinoWifiClass::powerON(){
|
---|
| 67 | }
|
---|
| 68 | void ArduinoWifiClass::powerOFF(){
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void ArduinoWifiClass::connect(char* ssid,char* pwd){
|
---|
| 72 | esp.wifiConnect(ssid, pwd);
|
---|
| 73 | }
|
---|
| 74 | boolean ArduinoWifiClass::connected(){
|
---|
| 75 | return wifiConnected;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void WifiBegin(short isCiao) {
|
---|
| 79 | espSerial.begin(9600);
|
---|
| 80 | if(espSerial.ping()!=1) {
|
---|
| 81 | espSerial.println("DBG: esp not found");
|
---|
| 82 | while(1);
|
---|
| 83 | }
|
---|
| 84 | else {
|
---|
| 85 | //espSerial.println("device found");
|
---|
| 86 | }
|
---|
| 87 | //put GPIO control here !!!
|
---|
| 88 | esp.enable();
|
---|
| 89 | delay(1000);
|
---|
| 90 | esp.reset();
|
---|
| 91 | delay(1000);
|
---|
| 92 | while(!esp.ready());
|
---|
| 93 | if (isCiao == 0)
|
---|
| 94 | esp.wifiCb.attach(&wifiCb);
|
---|
| 95 |
|
---|
| 96 | espSerial.println("\nDBG: UnoWiFi Start");
|
---|
| 97 | }
|
---|
| 98 | void ArduinoWifiClass::begin() {
|
---|
| 99 | WifiBegin(0);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | // -----------CIAO---------------------
|
---|
| 103 |
|
---|
| 104 | void CiaoClass::begin() {
|
---|
| 105 |
|
---|
| 106 | WifiBegin(1);
|
---|
| 107 | rest.begin("google.com");
|
---|
| 108 | rest.get("/");
|
---|
| 109 | delay(3000);
|
---|
| 110 | }
|
---|
| 111 | CiaoData responseREAD(){
|
---|
| 112 | CiaoData data;
|
---|
| 113 | delay(400);
|
---|
| 114 | char response[64] = "";
|
---|
| 115 | char cstr[8] = "";
|
---|
| 116 |
|
---|
| 117 | int ret = rest.getResponse(response, 64);
|
---|
| 118 | //espSerial.println(ret);
|
---|
| 119 |
|
---|
| 120 | snprintf(cstr,8,"%d",ret);
|
---|
| 121 |
|
---|
| 122 | data.msg_split[0]="id";
|
---|
| 123 | data.msg_split[1]=cstr;
|
---|
| 124 | data.msg_split[2]=response;
|
---|
| 125 |
|
---|
| 126 | return data;
|
---|
| 127 | }
|
---|
| 128 | CiaoData requestPOST(char* hostname, String stringone){
|
---|
| 129 | rest.begin(hostname);
|
---|
| 130 | //delay(1000); fix
|
---|
| 131 | //esp.process();
|
---|
| 132 | rest.post((const char*) stringone.c_str(),0);
|
---|
| 133 | return responseREAD();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | CiaoData requestGET(char* hostname, String stringone){
|
---|
| 137 |
|
---|
| 138 | rest.begin(hostname);
|
---|
| 139 | //delay(1000); fix
|
---|
| 140 | //esp.process();
|
---|
| 141 | rest.get((const char*) stringone.c_str());
|
---|
| 142 | return responseREAD();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | CiaoData PassThroughM(char* connector,char* hostname, String stringone, char* method){
|
---|
| 146 |
|
---|
| 147 | short mode = 0;
|
---|
| 148 | if (!strcmp(connector, "rest")){
|
---|
| 149 | mode = 0;
|
---|
| 150 | }
|
---|
| 151 | else if (!strcmp(connector, "mqtt")){
|
---|
| 152 | mode = 1;
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | CiaoData data;
|
---|
| 156 | data.msg_split[0]="";
|
---|
| 157 | data.msg_split[1]="";
|
---|
| 158 | data.msg_split[2]="Protocol Error";
|
---|
| 159 | return data;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (mode == 0){
|
---|
| 163 | if (!strcmp(method, "GET")){
|
---|
| 164 | return requestGET(hostname, stringone);
|
---|
| 165 | }
|
---|
| 166 | else if (!strcmp(method, "POST")){
|
---|
| 167 | return requestPOST(hostname, stringone);
|
---|
| 168 | }
|
---|
| 169 | else{
|
---|
| 170 | CiaoData data;
|
---|
| 171 | data.msg_split[0]="";
|
---|
| 172 | data.msg_split[1]="";
|
---|
| 173 | data.msg_split[2]="Method Error";
|
---|
| 174 | return data;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | CiaoData CiaoClass::write(char* connector,char* hostname, String stringone, char* method) {
|
---|
| 180 | return PassThroughM(connector, hostname, stringone, method);
|
---|
| 181 | }
|
---|
| 182 | CiaoData CiaoClass::read(char* connector,char* hostname, String stringone, char* method) {
|
---|
| 183 | return PassThroughM(connector, hostname, stringone, method);
|
---|
| 184 | }
|
---|
| 185 | CiaoData CiaoClass::write(char* connector,char* hostname, String stringone) {
|
---|
| 186 | return PassThroughM(connector, hostname, stringone, "GET");
|
---|
| 187 | }
|
---|
| 188 | CiaoData CiaoClass::read(char* connector,char* hostname, String stringone) {
|
---|
| 189 | return PassThroughM(connector, hostname, stringone, "GET");
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | #endif
|
---|