[175] | 1 | /*
|
---|
| 2 | ****************************************************************************
|
---|
| 3 | * Copyright (c) 2015 Arduino srl. All right reserved.
|
---|
| 4 | *
|
---|
| 5 | * File : UnowifiCiao.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 | #include "Ciao.h"
|
---|
| 27 | #include "Wifi.h"
|
---|
| 28 |
|
---|
| 29 | #if defined(__AVR_ATmega328P__)
|
---|
| 30 |
|
---|
| 31 | #include <stdarg.h>
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 |
|
---|
| 34 | WifiData espSerial = WifiData(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
|
---|
| 35 | ESP esp(&espSerial, 4);
|
---|
| 36 | REST rest(&esp);
|
---|
| 37 |
|
---|
| 38 | CiaoClass Ciao;
|
---|
| 39 | WifiClass Wifi;
|
---|
| 40 |
|
---|
| 41 | boolean wifiConnected = false;
|
---|
| 42 |
|
---|
| 43 | void wifiCb(void* response)
|
---|
| 44 | {
|
---|
| 45 | uint32_t status;
|
---|
| 46 | RESPONSE res(response);
|
---|
| 47 |
|
---|
| 48 | if(res.getArgc() == 1) {
|
---|
| 49 | res.popArgs((uint8_t*)&status, 4);
|
---|
| 50 | if(status == STATION_GOT_IP){
|
---|
| 51 | espSerial.println("DBG: Internet Connected");
|
---|
| 52 | wifiConnected = true;
|
---|
| 53 | }
|
---|
| 54 | else {
|
---|
| 55 | wifiConnected = false;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void WifiClass::powerON(){
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 | void WifiClass::powerOFF(){
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | WifiData WifiClass::stream(){
|
---|
| 68 | return espSerial;
|
---|
| 69 | }
|
---|
| 70 | int WifiClass::read(){
|
---|
| 71 | return espSerial.read();
|
---|
| 72 | }
|
---|
| 73 | void WifiClass::print(String str){
|
---|
| 74 | espSerial.print(str);
|
---|
| 75 | }
|
---|
| 76 | void WifiClass::println(String str){
|
---|
| 77 | espSerial.println(str);
|
---|
| 78 | }
|
---|
| 79 | void CiaoClass::print(String str){
|
---|
| 80 | espSerial.print(str);
|
---|
| 81 | }
|
---|
| 82 | void CiaoClass::println(String str){
|
---|
| 83 | espSerial.println(str);
|
---|
| 84 | }
|
---|
| 85 | boolean WifiClass::available(){
|
---|
| 86 | return espSerial.available();
|
---|
| 87 | }
|
---|
| 88 | void WifiClass::connect(char* ssid,char* pwd){
|
---|
| 89 | esp.wifiConnect(ssid, pwd);
|
---|
| 90 | }
|
---|
| 91 | boolean WifiClass::connected(){
|
---|
| 92 | return wifiConnected;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void WifiBegin() {
|
---|
| 96 | Serial.begin(9600);
|
---|
| 97 | espSerial.begin(9600);
|
---|
| 98 | if(espSerial.ping()!=1) {
|
---|
| 99 | espSerial.println("DBG: esp not found");
|
---|
| 100 | while(1);
|
---|
| 101 | }
|
---|
| 102 | else {
|
---|
| 103 | //espSerial.println("device found");
|
---|
| 104 | }
|
---|
| 105 | //metti GPIO control here !!!
|
---|
| 106 | esp.enable();
|
---|
| 107 | delay(1000);
|
---|
| 108 | esp.reset();
|
---|
| 109 | delay(1000);
|
---|
| 110 | while(!esp.ready());
|
---|
| 111 | esp.wifiCb.attach(&wifiCb);
|
---|
| 112 | espSerial.println("\nDBG: UnoWiFi Start");
|
---|
| 113 | }
|
---|
| 114 | void WifiClass::begin() {
|
---|
| 115 | WifiBegin();
|
---|
| 116 | }
|
---|
| 117 | void CiaoClass::begin() {
|
---|
| 118 |
|
---|
| 119 | WifiBegin();
|
---|
| 120 | espSerial.println("DBG: Ciao start");
|
---|
| 121 | rest.begin("google.com");
|
---|
| 122 | rest.get("/");
|
---|
| 123 | delay(3000);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | CiaoData responseREAD(){
|
---|
| 127 | CiaoData data;
|
---|
| 128 | delay(400);
|
---|
| 129 | char response[64] = "";
|
---|
| 130 | char cstr[8] = "";
|
---|
| 131 |
|
---|
| 132 | int ret = rest.getResponse(response, 64);
|
---|
| 133 | //espSerial.println(ret);
|
---|
| 134 |
|
---|
| 135 | snprintf(cstr,8,"%d",ret);
|
---|
| 136 |
|
---|
| 137 | data.msg_split[0]="id";
|
---|
| 138 | data.msg_split[1]=cstr;
|
---|
| 139 | data.msg_split[2]=response;
|
---|
| 140 |
|
---|
| 141 | return data;
|
---|
| 142 | }
|
---|
| 143 | CiaoData requestPOST(char* hostname, String stringone){
|
---|
| 144 | rest.begin(hostname);
|
---|
| 145 | //delay(1000); fix
|
---|
| 146 | //esp.process();
|
---|
| 147 | rest.post((const char*) stringone.c_str(),0);
|
---|
| 148 | return responseREAD();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | CiaoData requestGET(char* hostname, String stringone){
|
---|
| 152 |
|
---|
| 153 | rest.begin(hostname);
|
---|
| 154 | //delay(1000); fix
|
---|
| 155 | //esp.process();
|
---|
| 156 | rest.get((const char*) stringone.c_str());
|
---|
| 157 | return responseREAD();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | CiaoData PassThroughM(char* connector,char* hostname, String stringone, char* method){
|
---|
| 161 |
|
---|
| 162 | short mode = 0;
|
---|
| 163 | if (!strcmp(connector, "rest")){
|
---|
| 164 | mode = 0;
|
---|
| 165 | }
|
---|
| 166 | else if (!strcmp(connector, "mqtt")){
|
---|
| 167 | mode = 1;
|
---|
| 168 | }
|
---|
| 169 | else {
|
---|
| 170 | CiaoData data;
|
---|
| 171 | data.msg_split[0]="";
|
---|
| 172 | data.msg_split[1]="";
|
---|
| 173 | data.msg_split[2]="Protocol Error";
|
---|
| 174 | return data;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | if (mode == 0){
|
---|
| 178 | if (!strcmp(method, "GET")){
|
---|
| 179 | return requestGET(hostname, stringone);
|
---|
| 180 | }
|
---|
| 181 | else if (!strcmp(method, "POST")){
|
---|
| 182 | return requestPOST(hostname, stringone);
|
---|
| 183 | }
|
---|
| 184 | else{
|
---|
| 185 | CiaoData data;
|
---|
| 186 | data.msg_split[0]="";
|
---|
| 187 | data.msg_split[1]="";
|
---|
| 188 | data.msg_split[2]="Method Error";
|
---|
| 189 | return data;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | CiaoData CiaoClass::write(char* connector,char* hostname, String stringone, char* method) {
|
---|
| 195 | return PassThroughM(connector, hostname, stringone, method);
|
---|
| 196 | }
|
---|
| 197 | CiaoData CiaoClass::read(char* connector,char* hostname, String stringone, char* method) {
|
---|
| 198 | return PassThroughM(connector, hostname, stringone, method);
|
---|
| 199 | }
|
---|
| 200 | CiaoData CiaoClass::write(char* connector,char* hostname, String stringone) {
|
---|
| 201 | return PassThroughM(connector, hostname, stringone, "GET");
|
---|
| 202 | }
|
---|
| 203 | CiaoData CiaoClass::read(char* connector,char* hostname, String stringone) {
|
---|
| 204 | return PassThroughM(connector, hostname, stringone, "GET");
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | #endif
|
---|