|
| 1 | +// Author: MrBeans |
| 2 | +// Date: February 2019 |
| 3 | +// Place: BETH @ ETH, Zurich |
| 4 | +// OpenLicence |
| 5 | + |
| 6 | +// Code inspired by examples of TinyGPS++ & ESP8266WebServer libraries |
| 7 | + |
| 8 | +#include <WiFiClient.h> |
| 9 | +#include <ESP8266mDNS.h> |
| 10 | +#include <ESP8266WebServer.h> |
| 11 | +#include <TinyGPS++.h> |
| 12 | +#include <SoftwareSerial.h> |
| 13 | +#include <ESP8266WiFi.h> |
| 14 | + |
| 15 | +// MDNSResponder mdns; |
| 16 | + |
| 17 | +// Connect to local Wifi Network SSID |
| 18 | +const char* ssid = "Abc"; |
| 19 | +const char* password = "xbpd1712"; |
| 20 | + |
| 21 | +// Create instance of WebServer |
| 22 | +ESP8266WebServer server(3000); |
| 23 | + |
| 24 | +// Create global variables we want to send to the blockchain |
| 25 | +double longitude = 0; |
| 26 | +double latitude = 0; |
| 27 | +int yearr = 0; |
| 28 | +int monthh = 0; |
| 29 | +int dayy = 0; |
| 30 | +int hourr = 0; |
| 31 | +int minutee = 0; |
| 32 | +int secondd = 0; |
| 33 | + |
| 34 | +// Precision of longitude/latitude |
| 35 | +const int prec = 10e6; |
| 36 | + |
| 37 | +// Set up connection between ESP & GPS device |
| 38 | +static const int RXPin = D6, TXPin = D7; |
| 39 | +static const uint32_t GPSBaud = 9600; |
| 40 | + |
| 41 | +// Create instance of TinyGPS++ object |
| 42 | +TinyGPSPlus gps; |
| 43 | + |
| 44 | +// Create serial connection to the GPS device |
| 45 | +SoftwareSerial ss(RXPin, TXPin); |
| 46 | + |
| 47 | +// Create html code for website displaying variables we want to send to the blockchain |
| 48 | +void handleRoot() { |
| 49 | + String javascript = ""; |
| 50 | + char temp[600]; |
| 51 | + int sec = millis() / 1000; |
| 52 | + int min = sec / 60; |
| 53 | + int hr = min / 60; |
| 54 | + |
| 55 | + snprintf ( temp, 600, |
| 56 | + |
| 57 | + "<html>\ |
| 58 | + <head>\ |
| 59 | + <meta http-equiv='refresh' content='1'/>\ |
| 60 | + <title>ESP8266 Demo</title>\ |
| 61 | + <style>\ |
| 62 | + body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 63 | + </style>\ |
| 64 | + </head>\ |
| 65 | + <body>\ |
| 66 | + <h1>Hello from ESP8266!</h1>\ |
| 67 | + <p>Latitude: %02d.%07d</p>\ |
| 68 | + <p>Longitude: %02d.%07d</p>\ |
| 69 | + <p>Day: %02d</p>\ |
| 70 | + <p>Month: %02d</p>\ |
| 71 | + <p>Year: %02d</p>\ |
| 72 | + <p>Hour: %02d</p>\ |
| 73 | + <p>Minute: %02d</p>\ |
| 74 | + <p>Seconds: %02d</p>\ |
| 75 | + </body>\ |
| 76 | +</html>", |
| 77 | + |
| 78 | + int(latitude), int(latitude * prec) % prec , int(longitude), int(longitude * prec) % prec , dayy, monthh, yearr, hourr + 1, minutee, secondd |
| 79 | + ); |
| 80 | + |
| 81 | + server.send ( 200, "text/html", temp ); |
| 82 | +} |
| 83 | + |
| 84 | +// Default function |
| 85 | +void handleNotFound() { |
| 86 | + String message = "File Not Found\n\n"; |
| 87 | + message += "URI: "; |
| 88 | + message += server.uri(); |
| 89 | + message += "\nMethod: "; |
| 90 | + message += (server.method() == HTTP_GET) ? "GET" : "POST"; |
| 91 | + message += "\nArguments: "; |
| 92 | + message += server.args(); |
| 93 | + message += "\n"; |
| 94 | + for (uint8_t i = 0; i < server.args(); i++) { |
| 95 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 96 | + } |
| 97 | + server.send(404, "text/plain", message); |
| 98 | +} |
| 99 | + |
| 100 | +//Initial set-up |
| 101 | +void setup() |
| 102 | +{ |
| 103 | + Serial.begin(115200); |
| 104 | + delay(10); |
| 105 | + |
| 106 | + // Connect WiFi |
| 107 | + Serial.println(); |
| 108 | + Serial.println(); |
| 109 | + Serial.print("Connecting to "); |
| 110 | + Serial.println(ssid); |
| 111 | + WiFi.hostname("Name"); |
| 112 | + WiFi.begin(ssid, password); |
| 113 | + |
| 114 | + while (WiFi.status() != WL_CONNECTED) { |
| 115 | + delay(500); |
| 116 | + Serial.print("."); |
| 117 | + } |
| 118 | + Serial.println(""); |
| 119 | + Serial.println("WiFi connected"); |
| 120 | + |
| 121 | + // Print the IP address |
| 122 | + Serial.print("IP address: "); |
| 123 | + Serial.println(WiFi.localIP()); |
| 124 | + |
| 125 | + /* if (mdns.begin("esp8266"), WiFi.localIP()) { |
| 126 | + Serial.println("MDNS responder started"); |
| 127 | + } |
| 128 | + */ |
| 129 | + |
| 130 | + // Create html code |
| 131 | + server.on("/", handleRoot); |
| 132 | + |
| 133 | + // Default function |
| 134 | + server.onNotFound(handleNotFound); |
| 135 | + |
| 136 | + // Start server |
| 137 | + server.begin(); |
| 138 | + Serial.println("HTTP server started"); |
| 139 | + |
| 140 | + //Start GPS connection |
| 141 | + ss.begin(GPSBaud); |
| 142 | + |
| 143 | + Serial.println("Start up successfully completed"); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +// Read out and display GPS data |
| 148 | +void displayInfo() |
| 149 | +{ |
| 150 | + // location data |
| 151 | + Serial.print(F("Location: ")); |
| 152 | + if (gps.location.isValid()) |
| 153 | + { |
| 154 | + Serial.print(gps.location.lat(), 6); |
| 155 | + latitude = gps.location.lat(); |
| 156 | + Serial.print(F(",")); |
| 157 | + Serial.print(gps.location.lng(), 6); |
| 158 | + longitude = gps.location.lng(); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + Serial.print(F("INVALID")); |
| 163 | + } |
| 164 | + |
| 165 | + // date data |
| 166 | + Serial.print(F(" Date/Time: ")); |
| 167 | + if (gps.date.isValid()) |
| 168 | + { |
| 169 | + Serial.print(gps.date.month()); |
| 170 | + Serial.print(F("/")); |
| 171 | + monthh = gps.date.month(); |
| 172 | + Serial.print(gps.date.day()); |
| 173 | + dayy = gps.date.day(); |
| 174 | + Serial.print(F("/")); |
| 175 | + Serial.print(gps.date.year()); |
| 176 | + yearr = gps.date.year(); |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + Serial.print(F("INVALID")); |
| 181 | + } |
| 182 | + |
| 183 | + // time data |
| 184 | + Serial.print(F(" ")); |
| 185 | + if (gps.time.isValid()) |
| 186 | + { |
| 187 | + if (gps.time.hour() < 10) Serial.print(F("0")); |
| 188 | + Serial.print(gps.time.hour()); |
| 189 | + Serial.print(F(":")); |
| 190 | + hourr = gps.time.hour(); |
| 191 | + if (gps.time.minute() < 10) Serial.print(F("0")); |
| 192 | + Serial.print(gps.time.minute()); |
| 193 | + Serial.print(F(":")); |
| 194 | + minutee = gps.time.minute(); |
| 195 | + if (gps.time.second() < 10) Serial.print(F("0")); |
| 196 | + Serial.print(gps.time.second()); |
| 197 | + Serial.print(F(".")); |
| 198 | + secondd = gps.time.second(); |
| 199 | + if (gps.time.centisecond() < 10) Serial.print(F("0")); |
| 200 | + Serial.print(gps.time.centisecond()); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + Serial.print(F("INVALID")); |
| 205 | + } |
| 206 | + |
| 207 | + Serial.println(); |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | +void loop() |
| 212 | +{ |
| 213 | + /* |
| 214 | + // Blockchain connection |
| 215 | + //call web3 methods |
| 216 | + char result[128]; |
| 217 | +
|
| 218 | + web3.Web3ClientVersion(result); |
| 219 | + USE_SERIAL.println(result); |
| 220 | +
|
| 221 | + web3.Web3Sha3("0x68656c6c6f20776f726c64", result); |
| 222 | + USE_SERIAL.println(result); |
| 223 | +
|
| 224 | + //call to Contract |
| 225 | + Contract contract(&web3, CONTRACT_ADDRESS); |
| 226 | + strcpy(contract.options.from, MY_ADDRESS); |
| 227 | + strcpy(contract.options.gasPrice,"2000000000000"); |
| 228 | + contract.options.gas = 5000000; |
| 229 | + contract.SetupContractData(result, "get()"); |
| 230 | + contract.Call(result); |
| 231 | + USE_SERIAL.println(result); |
| 232 | +
|
| 233 | + //sendTransaction to Contract |
| 234 | + Contract contract(&web3, CONTRACT_ADDRESS); |
| 235 | + contract.SetPrivateKey((uint8_t*)PRIVATE_KEY); |
| 236 | + uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount((char *)MY_ADDRESS); |
| 237 | +
|
| 238 | + uint32_t gasPriceVal = 141006540; |
| 239 | + uint32_t gasLimitVal = 3000000; |
| 240 | + uint8_t toStr[] = CONTRACT_ADDRESS; |
| 241 | + uint8_t valueStr[] = "0x00"; |
| 242 | + uint8_t dataStr[100]; |
| 243 | + memset(dataStr, 0, 100); |
| 244 | + contract.SetupContractData((char*)dataStr, "set(uint256)", 123); |
| 245 | + contract.SendTransaction((uint8_t *) result, |
| 246 | + nonceVal, gasPriceVal, gasLimitVal, toStr, valueStr, dataStr); |
| 247 | +
|
| 248 | + USE_SERIAL.println(result); |
| 249 | + ////////////////////////////////////////////////////////////////////////////////////*/ |
| 250 | + |
| 251 | + // while connection to GPS device is established |
| 252 | + while (ss.available() > 0) |
| 253 | + { |
| 254 | + // read GPS data |
| 255 | + if (gps.encode(ss.read())) |
| 256 | + displayInfo(); |
| 257 | + |
| 258 | + // 5 second steps |
| 259 | + //delay(5000); |
| 260 | + |
| 261 | + // update website |
| 262 | + server.handleClient(); |
| 263 | + |
| 264 | + //mdns.update(); |
| 265 | + } |
| 266 | + // default if GPS device is not recognized |
| 267 | + if (millis() > 5000 && gps.charsProcessed() < 10) |
| 268 | + { |
| 269 | + Serial.println(F("No GPS detected: check wiring.")); |
| 270 | + } |
| 271 | +} |
0 commit comments