DAY 1: Connecting to the Aquabots Client
Day 1 of the Aquabots Autnomous Vessels Project: Connecting to the Aquabots Client.
Components and supplies
Grove Shield for Mega
Arduino Ethernet Shield 2
Arduino Mega 2560
Apps and platforms
Aquabots Client
Project description
Code
WebClient.h
c_cpp
Header files for Communicating with the Aquabots Client
1#ifndef WebClient_h 2#define WebClient_h 3 4#include <SPI.h> 5#include <Ethernet.h> 6 7const unsigned long HTTP_TIMEOUT = 5000;// max respone time from server 8 9/* 10 Web client 11 12 This sketch connects to a website 13 using an Arduino Wiznet Ethernet shield. 14 15 Circuit: 16 Ethernet shield attached to pins 10, 11, 12, 13 17 18 created 18 Dec 2009 19 by David A. Mellis 20 modified 9 Apr 2012 21 by Tom Igoe, based on work by Adrian McEwen 22 23*/ 24 25class WebClient { 26 27 public: WebClient(); 28 enum request { 29 UNKNOWN = 0, 30 REGISTER_VESSEL = 1, 31 VESSEL_CONFIG = 2, 32 DEBUG = 3, 33 FIELD = 4, 34 UPDATE = 5, 35 WAYPOINTS = 6, 36 DATA = 7, 37 NMEA = 8, 38 OPTIONS = 9, 39 LOG = 10, 40 WAYPOINT = 11 41 }; 42 43 EthernetClient client; 44 void setup(); 45 bool connect(); 46 void disconnect(); 47 bool requestLog(); 48 bool logMessage( String message ); 49 bool getWaypoint(); 50 bool sendUpdate( String url ); 51 bool sendHttp( int request, String msg ); 52 bool sendHttp( int request, boolean post, String attrs ); 53 String urlencode(String str); 54 String printResponse( int request ); 55 void logRequest( int request, boolean post, String attrs );//for debugging 56 void logRequestStr( int request ); //dito 57 void loop(); 58 59 private: 60 IPAddress server; 61 IPAddress ip; 62 String host; 63 int port; 64 bool connected; 65 String context; 66 String id; 67 int token; 68 69 // Enter a MAC address for your controller below. 70 // Newer Ethernet shields have a MAC address printed on a sticker on the shield 71 byte mac[6] = {0}; 72 73 // Initialize the Ethernet client library 74 // with the IP address and port of the server 75 // that you want to connect to (port 80 is default for HTTP): 76 void requestService( int request ); 77 bool processResponse( int request ); 78 boolean update( JsonObject& root ); 79 String urldecode(String str); 80 unsigned char h2int(char c); 81}; 82 83#endif
WebClient.c
c_cpp
The implementation of the Web Client
1WebClient::WebClient() {} 2 3void WebClient::setup( ) { 4 Serial.println(F("SETUP WEB CLIENT...")); 5 id = "AquaBoat"; 6 token = "AquaPassphrase"; 7 host = "http://www.condast.com"; 8 port = 10081; 9 context = "/arnac/registration/"; 10 String server_addr = "192.168.8.102"; //Aquabots Client IP Address 11 server.fromString( server_addr); 12 Serial.print(F("SERVER ADDRESS:")); server.printTo( Serial ); 13 Serial.println(); 14 String ip_addr = "192.168.8.102"; 15 ip.fromString( ip_addr); 16 Serial.print(F("IP ADDRESS:")); server.printTo( Serial ); 17 18 String mc = "{0x90,0xA2,0xDA,0x11,0x12,0x3A}"; 19 Serial.print(F("CONNECTING TO ")); Serial.print( host ); Serial.print(F(":")); Serial.print( port ); Serial.println( context ); 20 21 // start the Ethernet connection: 22 Serial.println(F("SETUP WEB CLIENT ")); 23 if (Ethernet.begin(mac) == 0) { 24 Serial.println(F("Failed to configure Ethernet using DHCP")); 25 // try to congifure using IP address instead of DHCP: 26 Ethernet.begin(mac, ip); 27 } 28 connected = false; 29 30 // give the Ethernet shield a second to initialize: 31 delay(1000); 32 Serial.println( F("done")); 33} 34 35bool WebClient::connect() { 36 //Serial.print(F("Connecting to: ")); Serial.print( server ); Serial.print(F(":")); Serial.println( port ); 37 client.setTimeout(3000); 38 bool result = client.connect(server, port); 39 //Serial.print(F("Connected: ")); Serial.println( result ); 40 if ( result) { 41 connected = result; 42 return result; 43 } 44 //if ( connected ) 45 // Serial.print(F("Connection failed: ")); Serial.println( result ); 46 client.stop(); 47} 48 49void WebClient::disconnect() { 50 client.stop(); 51 //if ( connected ) 52 // Serial.println(F("Disconnecting: Complete ")); 53 connected = false; 54} 55 56void WebClient::requestService( int request ) { 57 switch ( request ) { 58 case REGISTER_VESSEL: 59 client.print(F("register")); 60 break; 61 case VESSEL_CONFIG: 62 client.print(F("config")); 63 break; 64 case DEBUG: 65 client.print(F("debug")); 66 break; 67 case FIELD: 68 client.print(F("field")); 69 break; 70 case DATA: 71 client.print(F("data")); 72 break; 73 case NMEA: 74 client.print(F("nmea")); 75 break; 76 case OPTIONS: 77 client.print(F("options")); 78 break; 79 case LOG: 80 client.print(F("log")); 81 break; 82 case WAYPOINTS: 83 client.print(F("waypoints")); 84 break; 85 case WAYPOINT: 86 client.print(F("waypoint")); 87 break; 88 case UPDATE: 89 client.print(F("update")); 90 break; 91 default: 92 client.print(F("unknown")); 93 break; 94 } 95} 96 97/** 98 Is used to transform the int to a String 99*/ 100void WebClient::logRequestStr( int request ) { 101 switch ( request ) { 102 case REGISTER_VESSEL: 103 Serial.print(F("register")); 104 break; 105 case VESSEL_CONFIG: 106 Serial.print(F("config")); 107 break; 108 case DEBUG: 109 Serial.print(F("debug")); 110 break; 111 case FIELD: 112 Serial.print(F("field")); 113 break; 114 case DATA: 115 Serial.print(F("data")); 116 break; 117 case NMEA: 118 Serial.print(F("nmea")); 119 break; 120 case LOG: 121 Serial.print(F("log")); 122 break; 123 case WAYPOINTS: 124 Serial.print(F("waypoints")); 125 break; 126 case WAYPOINT: 127 Serial.print(F("waypoint")); 128 break; 129 case UPDATE: 130 Serial.print(F("update")); 131 break; 132 case OPTIONS: 133 Serial.print(F("options")); 134 break; 135 default: 136 Serial.print(F("unknown (")); Serial.print( request ); Serial.print(F(")")); 137 break; 138 } 139} 140 141boolean WebClient::sendHttp( int request, String message ) { 142 String msg = message; 143 if ( msg.length() > 0 ) { 144 msg = F("&msg="); 145 msg += message; 146 } 147 return sendHttp( request, false, msg ); 148} 149 150boolean WebClient::sendHttp( int request, boolean post, String attrs ) { 151 if ( client.connected()) { 152 if( request != NMEA ) 153 Serial.print(F("REQUEST ")); logRequestStr( request ); Serial.print(F(": ")); Serial.println(attrs ); 154 //logRequest( request, post, attrs ); 155 156 // Make a HTTP request: 157 client.print( post ? F("POST ") : F("GET ") ); 158 client.print( context ); 159 Serial.print( context );//Serial.print( register); 160 requestService( request ); 161 client.print(F("?id=" )); 162 client.print( id ); 163 client.print(F("&token=")); 164 client.print( token ); 165 if ( !post && ( attrs.length() > 0 )) { 166 client.print( attrs ); 167 } 168 client.println(F(" HTTP/1.1" )); 169 170 client.print(F("Host: ")); 171 client.println( host ); 172 client.println(F("Connection: close\ \ 173")); 174 if ( post && ( attrs.length() > 0 )) { 175 client.println( F("Accept: */*")); 176 client.println( F("Content-Type: application/x-www-form-urlencoded ; charset=UTF-8" )); 177 client.print( F("Content-Length: ")); 178 client.println( attrs.length() ); 179 client.println(); 180 client.println( urlencode( attrs )); 181 } 182 client.println(); 183 return processResponse( request ); 184 } 185 return false; 186} 187 188/** 189 Handle the response, by taking away header info and such 190*/ 191bool WebClient::processResponse( int request ) { 192 // Check HTTP status 193 char status[32] = {"\\0"}; 194 client.setTimeout(HTTP_TIMEOUT); 195 client.readBytesUntil('\ ', status, sizeof(status)); 196 char http_ok[32] = {"\\0"}; 197 strcpy( http_ok, "HTTP/1.1 200 OK"); 198 if (strcmp(status, http_ok) != 0) { 199 Serial.print(F( "Unexpected response (" )); logRequestStr( request); Serial.print(F( "):" )); 200 Serial.println(status); 201 return false; 202 } 203 204 // Skip HTTP headers 205 char endOfHeaders[] = "\ \ 206\ \ 207"; 208 if (!client.find(endOfHeaders)) { 209 Serial.println( F( "Invalid response (" )); logRequestStr( request); Serial.print(F( "):" )); 210 return false; 211 } 212 return true; 213} 214 215void WebClient::logRequest( int request, boolean post, String attrs ) { 216 // Make a HTTP request: 217 Serial.print( post ? F("POST ") : F("GET ")); 218 Serial.print( context ); 219 logRequestStr( request ); 220 Serial.print(F( "?id=" )); 221 Serial.print( id ); 222 Serial.print(F( "&token=" )); 223 Serial.print( token ); 224 if ( !post && ( attrs.length() > 0 )) { 225 Serial.print( attrs ); 226 } 227 Serial.print(F( " HTTP/1.1" )); 228 Serial.println(); 229 Serial.print(F( "Host: ")); 230 Serial.println( host ); 231 Serial.println(F( "Connection: close" )); 232 if ( post && ( attrs.length() > 0 )) { 233 Serial.println(F( "Accept: */*" )); 234 Serial.println(F( "Content-Type: application/x-www-form-urlencoded ; charset=UTF-8")); 235 Serial.print(F( "Content-Length: ")); 236 Serial.println( attrs.length() ); 237 Serial.println(); 238 Serial.println( attrs ); 239 } 240} 241 242/** 243 Creates a String request from the client 244*/ 245String WebClient::printResponse( int request ) { 246 Serial.print( F("RESPONSE TO ")); 247 logRequestStr( request ); 248 // Serial.print(" PROCESSING: "); 249 //Serial.print( client.available() ); 250 String retval = ""; 251 252 // Serial.println(); 253 while (client.available()) { 254 char c = client.read(); 255 retval += c; 256 } 257 Serial.print( F( ": ")); Serial.println( retval ); 258} 259 260void WebClient::loop() { 261 // if there are incoming bytes available 262 // from the server, read them and print them: 263 if (!client.connected()) { 264 connect(); 265 } 266} 267 268/* 269 ESP8266 Hello World urlencode by Steve Nelson 270 URLEncoding is used all the time with internet urls. This is how urls handle funny characters 271 in a URL. For example a space is: %20 272 These functions simplify the process of encoding and decoding the urlencoded format. 273 274 It has been tested on an esp12e (NodeMCU development board) 275 This example code is in the public domain, use it however you want. 276 Prerequisite Examples: 277 https://github.com/zenmanenergy/ESP8266-Arduino-Examples/tree/master/helloworld_serial 278*/ 279String WebClient::urldecode(String str) { 280 String encodedString = ""; 281 char c; 282 char code0; 283 char code1; 284 for (int i = 0; i < str.length(); i++) { 285 c = str.charAt(i); 286 if (c == '+') { 287 encodedString += ' '; 288 } else if (c == '%') { 289 i++; 290 code0 = str.charAt(i); 291 i++; 292 code1 = str.charAt(i); 293 c = (h2int(code0) << 4) | h2int(code1); 294 encodedString += c; 295 } else { 296 297 encodedString += c; 298 } 299 yield(); 300 } 301 302 return encodedString; 303} 304 305String WebClient::urlencode(String str) 306{ 307 String encodedString = ""; 308 char c; 309 char code0; 310 char code1; 311 char code2; 312 for (int i = 0; i < str.length(); i++) { 313 c = str.charAt(i); 314 if (c == ' ') { 315 encodedString += '+'; 316 } else if (isalnum(c)) { 317 encodedString += c; 318 } else { 319 code1 = (c & 0xf) + '0'; 320 if ((c & 0xf) > 9) { 321 code1 = (c & 0xf) - 10 + 'A'; 322 } 323 c = (c >> 4) & 0xf; 324 code0 = c + '0'; 325 if (c > 9) { 326 code0 = c - 10 + 'A'; 327 } 328 code2 = '\\0'; 329 encodedString += '%'; 330 encodedString += code0; 331 encodedString += code1; 332 //encodedString+=code2; 333 } 334 yield(); 335 } 336 return encodedString; 337 338} 339 340unsigned char WebClient::h2int(char c) 341{ 342 if (c >= '0' && c <= '9') { 343 return ((unsigned char)c - '0'); 344 } 345 if (c >= 'a' && c <= 'f') { 346 return ((unsigned char)c - 'a' + 10); 347 } 348 if (c >= 'A' && c <= 'F') { 349 return ((unsigned char)c - 'A' + 10); 350 } 351 return (0); 352}
Vessels
c_cpp
The main programme for the Autonomous Vessels Project
1#include <ArduinoJson.h> 2 3#include "WebClient.h" 4#include "Registration.h" 5 6WebClient webClient; 7Registration registration; 8 9void setup() { 10 Serial.begin(9600); 11 Serial.println("Initialised"); 12 webClient.setup(); 13 registration.setup(); 14} 15 16void loop() { 17 long id = registration.registerVessel( "AquaBoat", "AquaPassphrase", 51.0, 4.2 ); 18 Serial.print(F("Vessel id: ")); Serial.println( id ); 19 delay(2000); 20}
Registration.h
c_cpp
This module manages the registration process of a vessel with the Aquabots Client
1#ifndef Registration_h 2#define Registration_h 3 4#define REGISTRATION "Registration" 5#define REGISTRATION_ID "registration" 6 7class Registration { 8 9 private: 10 bool enabled; //general purpose flag 11 long vesselId; 12 13 public: Registration(void); 14 void setup(); 15 long registerVessel( String name, String passphrase, double latitude, double longitude); 16 bool getConfig(); 17}; 18 19#endif 20
WebClient.h
c_cpp
Header files for Communicating with the Aquabots Client
1#ifndef WebClient_h 2#define WebClient_h 3 4#include <SPI.h> 5#include <Ethernet.h> 6 7const unsigned long HTTP_TIMEOUT = 5000;// max respone time from server 8 9/* 10 Web client 11 12 This sketch connects to a website 13 using an Arduino Wiznet Ethernet shield. 14 15 Circuit: 16 Ethernet shield attached to pins 10, 11, 12, 13 17 18 created 18 Dec 2009 19 by David A. Mellis 20 modified 9 Apr 2012 21 by Tom Igoe, based on work by Adrian McEwen 22 23*/ 24 25class WebClient { 26 27 public: WebClient(); 28 enum request { 29 UNKNOWN = 0, 30 REGISTER_VESSEL = 1, 31 VESSEL_CONFIG = 2, 32 DEBUG = 3, 33 FIELD = 4, 34 UPDATE = 5, 35 WAYPOINTS = 6, 36 DATA = 7, 37 NMEA = 8, 38 OPTIONS = 9, 39 LOG = 10, 40 WAYPOINT = 11 41 }; 42 43 EthernetClient client; 44 void setup(); 45 bool connect(); 46 void disconnect(); 47 bool requestLog(); 48 bool logMessage( String message ); 49 bool getWaypoint(); 50 bool sendUpdate( String url ); 51 bool sendHttp( int request, String msg ); 52 bool sendHttp( int request, boolean post, String attrs ); 53 String urlencode(String str); 54 String printResponse( int request ); 55 void logRequest( int request, boolean post, String attrs );//for debugging 56 void logRequestStr( int request ); //dito 57 void loop(); 58 59 private: 60 IPAddress server; 61 IPAddress ip; 62 String host; 63 int port; 64 bool connected; 65 String context; 66 String id; 67 int token; 68 69 // Enter a MAC address for your controller below. 70 // Newer Ethernet shields have a MAC address printed on a sticker on the shield 71 byte mac[6] = {0}; 72 73 // Initialize the Ethernet client library 74 // with the IP address and port of the server 75 // that you want to connect to (port 80 is default for HTTP): 76 void requestService( int request ); 77 bool processResponse( int request ); 78 boolean update( JsonObject& root ); 79 String urldecode(String str); 80 unsigned char h2int(char c); 81}; 82 83#endif
WebClient.c
c_cpp
The implementation of the Web Client
1WebClient::WebClient() {} 2 3void WebClient::setup( ) { 4 Serial.println(F("SETUP WEB CLIENT...")); 5 id = "AquaBoat"; 6 token = "AquaPassphrase"; 7 host = "http://www.condast.com"; 8 port = 10081; 9 context = "/arnac/registration/"; 10 String server_addr = "192.168.8.102"; //Aquabots Client IP Address 11 server.fromString( server_addr); 12 Serial.print(F("SERVER ADDRESS:")); server.printTo( Serial ); 13 Serial.println(); 14 String ip_addr = "192.168.8.102"; 15 ip.fromString( ip_addr); 16 Serial.print(F("IP ADDRESS:")); server.printTo( Serial ); 17 18 String mc = "{0x90,0xA2,0xDA,0x11,0x12,0x3A}"; 19 Serial.print(F("CONNECTING TO ")); Serial.print( host ); Serial.print(F(":")); Serial.print( port ); Serial.println( context ); 20 21 // start the Ethernet connection: 22 Serial.println(F("SETUP WEB CLIENT ")); 23 if (Ethernet.begin(mac) == 0) { 24 Serial.println(F("Failed to configure Ethernet using DHCP")); 25 // try to congifure using IP address instead of DHCP: 26 Ethernet.begin(mac, ip); 27 } 28 connected = false; 29 30 // give the Ethernet shield a second to initialize: 31 delay(1000); 32 Serial.println( F("done")); 33} 34 35bool WebClient::connect() { 36 //Serial.print(F("Connecting to: ")); Serial.print( server ); Serial.print(F(":")); Serial.println( port ); 37 client.setTimeout(3000); 38 bool result = client.connect(server, port); 39 //Serial.print(F("Connected: ")); Serial.println( result ); 40 if ( result) { 41 connected = result; 42 return result; 43 } 44 //if ( connected ) 45 // Serial.print(F("Connection failed: ")); Serial.println( result ); 46 client.stop(); 47} 48 49void WebClient::disconnect() { 50 client.stop(); 51 //if ( connected ) 52 // Serial.println(F("Disconnecting: Complete ")); 53 connected = false; 54} 55 56void WebClient::requestService( int request ) { 57 switch ( request ) { 58 case REGISTER_VESSEL: 59 client.print(F("register")); 60 break; 61 case VESSEL_CONFIG: 62 client.print(F("config")); 63 break; 64 case DEBUG: 65 client.print(F("debug")); 66 break; 67 case FIELD: 68 client.print(F("field")); 69 break; 70 case DATA: 71 client.print(F("data")); 72 break; 73 case NMEA: 74 client.print(F("nmea")); 75 break; 76 case OPTIONS: 77 client.print(F("options")); 78 break; 79 case LOG: 80 client.print(F("log")); 81 break; 82 case WAYPOINTS: 83 client.print(F("waypoints")); 84 break; 85 case WAYPOINT: 86 client.print(F("waypoint")); 87 break; 88 case UPDATE: 89 client.print(F("update")); 90 break; 91 default: 92 client.print(F("unknown")); 93 break; 94 } 95} 96 97/** 98 Is used to transform the int to a String 99*/ 100void WebClient::logRequestStr( int request ) { 101 switch ( request ) { 102 case REGISTER_VESSEL: 103 Serial.print(F("register")); 104 break; 105 case VESSEL_CONFIG: 106 Serial.print(F("config")); 107 break; 108 case DEBUG: 109 Serial.print(F("debug")); 110 break; 111 case FIELD: 112 Serial.print(F("field")); 113 break; 114 case DATA: 115 Serial.print(F("data")); 116 break; 117 case NMEA: 118 Serial.print(F("nmea")); 119 break; 120 case LOG: 121 Serial.print(F("log")); 122 break; 123 case WAYPOINTS: 124 Serial.print(F("waypoints")); 125 break; 126 case WAYPOINT: 127 Serial.print(F("waypoint")); 128 break; 129 case UPDATE: 130 Serial.print(F("update")); 131 break; 132 case OPTIONS: 133 Serial.print(F("options")); 134 break; 135 default: 136 Serial.print(F("unknown (")); Serial.print( request ); Serial.print(F(")")); 137 break; 138 } 139} 140 141boolean WebClient::sendHttp( int request, String message ) { 142 String msg = message; 143 if ( msg.length() > 0 ) { 144 msg = F("&msg="); 145 msg += message; 146 } 147 return sendHttp( request, false, msg ); 148} 149 150boolean WebClient::sendHttp( int request, boolean post, String attrs ) { 151 if ( client.connected()) { 152 if( request != NMEA ) 153 Serial.print(F("REQUEST ")); logRequestStr( request ); Serial.print(F(": ")); Serial.println(attrs ); 154 //logRequest( request, post, attrs ); 155 156 // Make a HTTP request: 157 client.print( post ? F("POST ") : F("GET ") ); 158 client.print( context ); 159 Serial.print( context );//Serial.print( register); 160 requestService( request ); 161 client.print(F("?id=" )); 162 client.print( id ); 163 client.print(F("&token=")); 164 client.print( token ); 165 if ( !post && ( attrs.length() > 0 )) { 166 client.print( attrs ); 167 } 168 client.println(F(" HTTP/1.1" )); 169 170 client.print(F("Host: ")); 171 client.println( host ); 172 client.println(F("Connection: close\ \ 173")); 174 if ( post && ( attrs.length() > 0 )) { 175 client.println( F("Accept: */*")); 176 client.println( F("Content-Type: application/x-www-form-urlencoded ; charset=UTF-8" )); 177 client.print( F("Content-Length: ")); 178 client.println( attrs.length() ); 179 client.println(); 180 client.println( urlencode( attrs )); 181 } 182 client.println(); 183 return processResponse( request ); 184 } 185 return false; 186} 187 188/** 189 Handle the response, by taking away header info and such 190*/ 191bool WebClient::processResponse( int request ) { 192 // Check HTTP status 193 char status[32] = {"\\0"}; 194 client.setTimeout(HTTP_TIMEOUT); 195 client.readBytesUntil('\r', status, sizeof(status)); 196 char http_ok[32] = {"\\0"}; 197 strcpy( http_ok, "HTTP/1.1 200 OK"); 198 if (strcmp(status, http_ok) != 0) { 199 Serial.print(F( "Unexpected response (" )); logRequestStr( request); Serial.print(F( "):" )); 200 Serial.println(status); 201 return false; 202 } 203 204 // Skip HTTP headers 205 char endOfHeaders[] = "\ \ 206\ \ 207"; 208 if (!client.find(endOfHeaders)) { 209 Serial.println( F( "Invalid response (" )); logRequestStr( request); Serial.print(F( "):" )); 210 return false; 211 } 212 return true; 213} 214 215void WebClient::logRequest( int request, boolean post, String attrs ) { 216 // Make a HTTP request: 217 Serial.print( post ? F("POST ") : F("GET ")); 218 Serial.print( context ); 219 logRequestStr( request ); 220 Serial.print(F( "?id=" )); 221 Serial.print( id ); 222 Serial.print(F( "&token=" )); 223 Serial.print( token ); 224 if ( !post && ( attrs.length() > 0 )) { 225 Serial.print( attrs ); 226 } 227 Serial.print(F( " HTTP/1.1" )); 228 Serial.println(); 229 Serial.print(F( "Host: ")); 230 Serial.println( host ); 231 Serial.println(F( "Connection: close" )); 232 if ( post && ( attrs.length() > 0 )) { 233 Serial.println(F( "Accept: */*" )); 234 Serial.println(F( "Content-Type: application/x-www-form-urlencoded ; charset=UTF-8")); 235 Serial.print(F( "Content-Length: ")); 236 Serial.println( attrs.length() ); 237 Serial.println(); 238 Serial.println( attrs ); 239 } 240} 241 242/** 243 Creates a String request from the client 244*/ 245String WebClient::printResponse( int request ) { 246 Serial.print( F("RESPONSE TO ")); 247 logRequestStr( request ); 248 // Serial.print(" PROCESSING: "); 249 //Serial.print( client.available() ); 250 String retval = ""; 251 252 // Serial.println(); 253 while (client.available()) { 254 char c = client.read(); 255 retval += c; 256 } 257 Serial.print( F( ": ")); Serial.println( retval ); 258} 259 260void WebClient::loop() { 261 // if there are incoming bytes available 262 // from the server, read them and print them: 263 if (!client.connected()) { 264 connect(); 265 } 266} 267 268/* 269 ESP8266 Hello World urlencode by Steve Nelson 270 URLEncoding is used all the time with internet urls. This is how urls handle funny characters 271 in a URL. For example a space is: %20 272 These functions simplify the process of encoding and decoding the urlencoded format. 273 274 It has been tested on an esp12e (NodeMCU development board) 275 This example code is in the public domain, use it however you want. 276 Prerequisite Examples: 277 https://github.com/zenmanenergy/ESP8266-Arduino-Examples/tree/master/helloworld_serial 278*/ 279String WebClient::urldecode(String str) { 280 String encodedString = ""; 281 char c; 282 char code0; 283 char code1; 284 for (int i = 0; i < str.length(); i++) { 285 c = str.charAt(i); 286 if (c == '+') { 287 encodedString += ' '; 288 } else if (c == '%') { 289 i++; 290 code0 = str.charAt(i); 291 i++; 292 code1 = str.charAt(i); 293 c = (h2int(code0) << 4) | h2int(code1); 294 encodedString += c; 295 } else { 296 297 encodedString += c; 298 } 299 yield(); 300 } 301 302 return encodedString; 303} 304 305String WebClient::urlencode(String str) 306{ 307 String encodedString = ""; 308 char c; 309 char code0; 310 char code1; 311 char code2; 312 for (int i = 0; i < str.length(); i++) { 313 c = str.charAt(i); 314 if (c == ' ') { 315 encodedString += '+'; 316 } else if (isalnum(c)) { 317 encodedString += c; 318 } else { 319 code1 = (c & 0xf) + '0'; 320 if ((c & 0xf) > 9) { 321 code1 = (c & 0xf) - 10 + 'A'; 322 } 323 c = (c >> 4) & 0xf; 324 code0 = c + '0'; 325 if (c > 9) { 326 code0 = c - 10 + 'A'; 327 } 328 code2 = '\\0'; 329 encodedString += '%'; 330 encodedString += code0; 331 encodedString += code1; 332 //encodedString+=code2; 333 } 334 yield(); 335 } 336 return encodedString; 337 338} 339 340unsigned char WebClient::h2int(char c) 341{ 342 if (c >= '0' && c <= '9') { 343 return ((unsigned char)c - '0'); 344 } 345 if (c >= 'a' && c <= 'f') { 346 return ((unsigned char)c - 'a' + 10); 347 } 348 if (c >= 'A' && c <= 'F') { 349 return ((unsigned char)c - 'A' + 10); 350 } 351 return (0); 352}
Vessels
c_cpp
The main programme for the Autonomous Vessels Project
1#include <ArduinoJson.h> 2 3#include "WebClient.h" 4#include "Registration.h" 5 6WebClient 7 webClient; 8Registration registration; 9 10void setup() { 11 Serial.begin(9600); 12 13 Serial.println("Initialised"); 14 webClient.setup(); 15 registration.setup(); 16} 17 18void 19 loop() { 20 long id = registration.registerVessel( "AquaBoat", "AquaPassphrase", 21 51.0, 4.2 ); 22 Serial.print(F("Vessel id: ")); Serial.println( id ); 23 delay(2000); 24}
Registration.c
c_cpp
The Implementation of the Registration Module
1Registration::Registration() {}; 2 3void Registration::setup( ) { 4 enabled = true; 5} 6 7long Registration::registerVessel( String name, String passphrase, double latitude, double longitude ) { 8 if ( !enabled ) 9 return -1; 10 if ( ! webClient.connect() ) 11 return -2; 12 13 String url = F("&name="); 14 url += String( name ); 15 url += F("&passphrase="); 16 url += String( passphrase); 17 url += F("&latitude="); 18 url += String( latitude); 19 url += F("&longitude="); 20 url += String( longitude); 21 22 boolean result = webClient.sendHttp( WebClient::REGISTER_VESSEL, false, url); 23 if (!result ) { 24 webClient.disconnect(); 25 return -3; 26 } 27 String retval = ""; 28 while (webClient.client.available()) { 29 char c = webClient.client.read(); 30 retval += c; 31 } 32 Serial.println( retval); 33 vesselId = atol( retval.c_str() ); 34 webClient.disconnect(); 35 return vesselId; 36} 37 38bool Registration::getConfig() { 39} 40
Registration.h
c_cpp
This module manages the registration process of a vessel with the Aquabots Client
1#ifndef Registration_h 2#define Registration_h 3 4#define REGISTRATION 5 "Registration" 6#define REGISTRATION_ID "registration" 7 8class Registration 9 { 10 11 private: 12 bool enabled; //general purpose flag 13 long vesselId; 14 15 16 public: Registration(void); 17 void setup(); 18 long registerVessel( 19 String name, String passphrase, double latitude, double longitude); 20 bool 21 getConfig(); 22}; 23 24#endif 25
Comments
Only logged in users can leave comments