Skip to content

Commit

Permalink
ZOOM!
Browse files Browse the repository at this point in the history
  • Loading branch information
sassybunsthebun committed May 22, 2024
1 parent bb18a61 commit 036c110
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 40 deletions.
15 changes: 10 additions & 5 deletions esp32/Prosjekt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ void reconnectMQTT(PubSubClient& client) {
* @param byte zumoaddress: Adress of the Zumo32u4
* @param byte kjøremodus: Variable which stores the driving direction
*/
void wireTransmit(byte zumoaddress, byte kjoremodus) {
Wire.beginTransmission(zumoaddress);
Wire.write(kjoremodus);
Serial.print("message sent!");
Wire.endTransmission();
void wireTransmit(int zumoaddress, int kjoremodus) {
Wire.beginTransmission(zumoaddress);
Wire.write(kjoremodus);
int result = Wire.endTransmission();
if(result == 0){
Serial.println("transmission sucessfull");
}else{
Serial.print("transmission failed with error code: ");
Serial.println(result);
}
}
2 changes: 1 addition & 1 deletion esp32/Prosjekt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
void connectWiFi(const char* ssid, const char* password);
void sendWhatsAppMessage(String& message, String& phoneNumber, String& apiKey);
void reconnectMQTT(PubSubClient& client);
void wireTransmit(byte zumoaddress, byte kjoremodus);
void wireTransmit(int zumoaddress, int kjoremodus);

#endif
34 changes: 21 additions & 13 deletions esp32/esp32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

/// VARIABLES FOR WIFI-CONNECTION ///

const char* ssid = "NTNU-IOT";
const char* password = "";
const char* ssid = "Garfield party";
const char* password = "Lasagnalover6969";

/// VARIABLER FOR MELDING GJENNOM WHATSAPP
String phoneNumber = "+4748230543";
Expand All @@ -21,7 +21,7 @@ String message = "HEI:)"; //denne kan endres til en mer utfyllende melding sener
/// VARIABLER FOR MQTT ///

/// VARIABLES FOR MQTT COMMUNICATION ///
const char* mqtt_server = "10.25.17.47";
const char* mqtt_server = "192.168.0.144";

WiFiClient espClient;
PubSubClient client = PubSubClient(espClient);
Expand Down Expand Up @@ -83,7 +83,7 @@ float totalSpeed;
/// VARIABLES FOR DELAY WITH MILLIS ///

unsigned long previousMillis = 0;
const int interval = 500;
const int interval = 5000;

/// VARIABLES FOR POWER USAGE CALCULATION ///

Expand All @@ -105,8 +105,9 @@ float acceleration;
void setup()
{
Serial.begin(115200);
Wire.begin(espaddress);
Wire.begin(espaddress); //espaddress
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);

connectWiFi(ssid, password); //kobler opp til Wi-Fi

Expand All @@ -116,7 +117,6 @@ void setup()
client.setCallback(callback);

delay(1000);
previousMillis = millis();
}

void loop()
Expand Down Expand Up @@ -222,30 +222,38 @@ void callback(char* topic, byte* message, unsigned int length) {
else if(messageTemp == "backwards"){
kjoremodus = 4;
}
else if(messageTemp == "0"){
kjoremodus = 5;
}
}

Serial.print(kjoremodus);
//wireTransmit(zumoaddress, kjoremodus);
Wire.beginTransmission(zumoaddress);
Wire.write(kjoremodus);
Wire.endTransmission();
Serial.print("message sent!");
//wireTransmit(zumoaddress, kjoremodus);
}

//This function counts the amount of sharp turns made by the zumo car
void receiveEvent(int howMany){
while(Wire.available()){
sharpTurnCount += Wire.read();
Serial.println(sharpTurnCount);
}
}
}

void requestEvent(){
Wire.write(kjoremodus);
}

//This function sends a message with all the values via MQTT
void sendMQTTMessage(){

String MQTTmessage = String(powerUsage) + " " + String(averageSpeed) + " " + String(meterIncrease) + " " + String(meterDecrease) + " " + String(averageTemp) + " " + String(averageLatitude) + " " + String(averageLongitude) + " " + String(highAccelerationCount) + " " + String(sharpTurnCount);
client.publish(messageTopic, MQTTmessage.c_str());
int result = Wire.endTransmission();
if(result == 0){
Serial.println("transmission sucessfull");
}else{
Serial.print("transmission failed with error code: ");
Serial.println(result);
}
Serial.print(MQTTmessage);
}

Expand Down
12 changes: 9 additions & 3 deletions kontroller/kontroller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

/// VARIABLES FOR WIFI-CONNECTION ///

const char* ssid = "NTNU-IOT";
const char* password = "";
const char* ssid = "Garfield party";
const char* password = "Lasagnalover6969";

/// VARIABLES FOR MQTT COMMUNICATION ///
const char* mqtt_server = "10.25.17.47"; //IP-address of Raspberry Pi
const char* mqtt_server = "192.168.0.144"; //IP-address of Raspberry Pi

WiFiClient espClient;
PubSubClient client = PubSubClient(espClient);
Expand Down Expand Up @@ -42,6 +42,7 @@ int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int mode = 0; //changes which mode the controller is in. 1 = joystick, 0 = none
const int LED = 14;

void setup() {
Serial.begin(115200);
Expand All @@ -50,6 +51,7 @@ void setup() {
connectWiFi(ssid, password);
client.setServer(mqtt_server, 1883);
pinMode(buttonPin, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
Expand All @@ -72,8 +74,12 @@ void loop() {
lastButtonState = reading;

if (mode == 1){
digitalWrite(LED, HIGH);
joyStickMode();
}
else {
digitalWrite(LED, LOW);
}

if (!client.connected()) {
reconnectMQTT(client);
Expand Down
14 changes: 10 additions & 4 deletions zumobil/Prosjekt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* @param byte zumoaddress: Adress of the Zumo32u4
* @param byte kjøremodus: Variable which stores the driving direction
*/
void wireTransmit(byte espaddress, byte info) {
Wire.beginTransmission(espaddress);
Wire.write(info);
Wire.endTransmission();
void wireTransmit(int espaddress, int totalTurns) {
Wire.beginTransmission(espaddress);
Wire.write(totalTurns);
int result = Wire.endTransmission();
if(result == 0){
Serial.println("transmission sucessfull");
}else{
Serial.print("transmission failed with error code: ");
Serial.println(result);
}
}
2 changes: 1 addition & 1 deletion zumobil/Prosjekt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
void connectWiFi(const char* ssid, const char* password);
void sendWhatsAppMessage(String& message, String& phoneNumber, String& apiKey);
void reconnectMQTT(PubSubClient& client);
void wireTransmit(byte zumoaddress, byte kjoremodus);
void wireTransmit(int espaddress, int totalTurns);

#endif
42 changes: 29 additions & 13 deletions zumobil/zumobil.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ int zumoaddress = 4;

/// CONTROLLER VARIABLES ///

bool joyStickMode = false;
//bool joyStickMode = false;
int kjoremodus;
int controllerInterval = 100;

void setup()
{
Wire.begin(zumoaddress);
Wire.onReceive(receiveEvent);
Wire.begin(); //zumoaddress
//Wire.onReceive(receiveEvent);
Serial.begin(115200);
lineSensors.initFiveSensors(); //merges the line sensors
motors.setSpeeds(-100,100);
Expand All @@ -52,31 +53,43 @@ void setup()

void loop()
{

if(millis() - previousMillis >= interval){
previousMillis = millis();
calculateEncoders();
calculateEncoders();
}

if (joyStickMode == false) {
linjefolging();
if(millis() - previousMillis >= controllerInterval){
previousMillis = millis();
calculateEncoders();
Wire.requestFrom(espaddress, 1);
}

// if (joyStickMode == false) {
// linjefolging();
//}

while(Wire.available()){
kjoremodus = Wire.read();
Serial.println(kjoremodus);
}
if(kjoremodus == 1){
motors.setSpeeds(50,150);
motors.setSpeeds(20,250);
}
else if(kjoremodus == 2){
motors.setSpeeds(150,50);
motors.setSpeeds(250,20);
}
else if(kjoremodus == 3){
motors.setSpeeds(150,150);
motors.setSpeeds(250,250);
}
else if(kjoremodus == 4){
motors.setSpeeds(-100,-100);
motors.setSpeeds(-150,-150);
}
else if(kjoremodus == 5){
linjefolging();
}
}

// function that executes whenever data is received from master
/*// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
Expand All @@ -86,6 +99,8 @@ void receiveEvent(int howMany)
}
}
*/

//A function that follows a taped up line on the floor using the Zumo32u4's linefollower sensors
void linjefolging(){
int previousOffset;
Expand Down Expand Up @@ -128,7 +143,8 @@ void calculateEncoders() {
}
totalTurns += sharpTurn[i];
}
sendCommand();
//wireTransmit(espaddress, totalTurns);
sendCommand();
totalTurns = 0;
}

Expand Down

0 comments on commit 036c110

Please sign in to comment.