#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <string.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
String myLocation = "Chennai,IN";
String usualSpeedLimit = "70"; // kmph
int schoolZone = 32;
int hospitalZone = 26;
int uid = 2504; // ID Unique to this Micro Contoller
String getString(char x)
{
String s(1, x);
return s;
}
String stringSplitter1(String fullString,char delimiter='$')
{
String returnString = "";
for(int i = 0; i<fullString.length();i++) {
char c = fullString[i];
if(delimiter==c)
break;
returnString+=String(c);
}
return(returnString);
}
String stringSplitter2(String fullString,char delimiter='$')
{
String returnString = "";
bool flag = false;
for(int i = 0; i<fullString.length();i++) {
char c = fullString[i];
if(flag)
returnString+=String(c);
if(delimiter==c)
flag = true;
}
return(returnString);
}
void rightArrow()
{
int refX = 50;
int refY = tft.getCursorY() + 40;
tft.fillRect(refX,refY,100,20,ILI9341_RED);
tft.fillTriangle(refX+100,refY-30,refX+100,refY+50,refX+40+100,refY+10,ILI9341_RED);
}
void leftArrow()
{
int refX = 50;
int refY = tft.getCursorY() + 40;
tft.fillRect(refX+40,refY,100,20,ILI9341_RED);
tft.fillTriangle(refX+40,refY-30,refX+40,refY+50,refX,refY+10,ILI9341_RED);
}
void upArrow()
{
int refX = 125;
int refY = tft.getCursorY() + 30;
tft.fillTriangle(refX-40,refY+40,refX+40,refY+40,refX,refY,ILI9341_RED);
tft.fillRect(refX-15,refY+40,30,20,ILI9341_RED);
}
String APICall() {
HTTPClient http;
String url = "https://node-red-grseb-2022-11-05-test.eu-gb.mybluemix.net/getSpeed?";
url += "location="+myLocation+"&";
url += "schoolZone="+(String)digitalRead(schoolZone)+(String)"&";
url += "hospitalZone="+(String)digitalRead(hospitalZone)+(String)"&";
url += "usualSpeedLimit="+(String)usualSpeedLimit+(String)"&";
url += "uid="+(String)uid;
http.begin(url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
String payload = http.getString();
http.end();
return(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void myPrint(String contents) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 20);
tft.setTextSize(4);
tft.setTextColor(ILI9341_RED);
//tft.println(contents);
tft.println(stringSplitter1(contents));
String c2 = stringSplitter2(contents);
if(c2=="s") // represents Straight
{
upArrow();
}
if(c2=="l") // represents left
{
leftArrow();
}
if(c2=="r") // represents right
{
rightArrow();
}
}
void setup() {
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
}
void loop() {
myPrint(APICall());
delay(100);
}