canbus baby

master
Drew Bednar 3 years ago
parent 50d2f8f9b5
commit 6437b2ab60

@ -1,3 +1,13 @@
# ESP32 Playgroung
# ESP32 Playground
## Setting up the Debug
You need the following in your `platformio.ini` order to get the debugger co
```
debug_tool = esp-prog
debug_init_break = tbreak setup
```
## Pinout Diagrams
![ESP32_NodeMCU](hiletgo_esp32_pinout.jpg)

@ -19,4 +19,7 @@ debug_init_break = tbreak setup
; I am using src_filter as a cheap way to have multiple main files in one project.
; To use it all you have to do is add the target main file like here we add main3.cpp as out build target:
; src_filter =+<*> -<main*.cpp> +<main3.cpp>
src_filter =+<*> -<main*.cpp> +<main3.cpp>
src_filter =+<*> -<main*.cpp> +<main_canbus_send.cpp>
lib_deps =
# The exact version
sandeepmistry/CAN @ 0.3.1

@ -1,24 +0,0 @@
#include <Arduino.h>
#include <foo.h>
#define LED_BUILTIN 2
int delayTime = 100;
void setup()
{
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.println("Starting loop");
digitalWrite(LED_BUILTIN, HIGH);
delay(delayTime);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Turning off LED");
printf("This is main2.cpp!\n");
delay(delayTime);
}

@ -0,0 +1,71 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <Arduino.h>
#include <CAN.h>
#define CAN_RX 22
#define CAN_TX 21
void setup()
{
CAN.setPins(CAN_RX, CAN_TX);
Serial.begin(115200);
while (!Serial)
;
delay(10000);
Serial.println("CAN Receiver");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3))
{
Serial.println("Starting CAN failed!");
while (1)
;
}
}
void loop()
{
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize)
{
// received a packet
Serial.print("Received ");
if (CAN.packetExtended())
{
Serial.print("extended ");
}
if (CAN.packetRtr())
{
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr())
{
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
}
else
{
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available())
{
Serial.print((char)CAN.read());
}
Serial.println();
}
Serial.println();
}
}

@ -0,0 +1,73 @@
#include <Arduino.h>
#include <CAN.h>
#define LED_BUILTIN 2
#define CAN_RX 22
#define CAN_TX 21
int result = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
CAN.setPins(CAN_RX, CAN_TX);
Serial.begin(115200);
while (!Serial)
;
delay(10000);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3))
{
Serial.println("Starting CAN failed!");
while (1)
;
}
}
void loop()
{
// send packet: id is 11 bits, packet can contain up to 8 bytes of data
Serial.print("Sending packet ... ");
result = CAN.beginPacket(0x12);
if (result)
{
CAN.write('h');
CAN.write('e');
CAN.write('l');
CAN.write('l');
CAN.write('o');
CAN.endPacket();
Serial.println("done");
}
else
{
Serial.println("There was a probelm beginning the packet.");
}
delay(1000);
// send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
Serial.print("Sending extended packet ... ");
result = CAN.beginExtendedPacket(0xabcdef);
if (result)
{
CAN.write('h');
CAN.write('e');
CAN.write('l');
CAN.write('l');
CAN.write('o');
CAN.endPacket();
Serial.println("done");
}
else
{
Serial.println("There was a probelm beginning the extended packet.");
}
delay(1000);
}

@ -0,0 +1,43 @@
/*
Eample of using an interrupt to set global state. which is acted on during the loop.
Place a switch on Pin 4.
*/
#include <Arduino.h>
#define LED_BUILTIN 2
volatile bool switch_on = false;
volatile u_int switch_counter = 0;
/*
* Interrupt Callback used to track switch state
*/
void switch_state()
{
switch_on = !switch_on;
switch_counter = ++switch_counter;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(4, INPUT_PULLUP);
Serial.begin(115200);
int interrupt_pin_num = digitalPinToInterrupt(4);
printf("The interrupt number for pin 4 is: %d", interrupt_pin_num);
attachInterrupt(interrupt_pin_num, switch_state, FALLING);
}
void loop()
{
printf("Our counter is at: %d\n", switch_counter);
if (switch_on)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
}
delay(100);
}

@ -0,0 +1,27 @@
/*
A simple example of using switch to toggle the led builtin.
*/
#include <Arduino.h>
#define LED_BUILTIN 2
#define SWITCH_INPUT 4
void setup()
{
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SWITCH_INPUT, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(SWITCH_INPUT))
{
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
}
delay(500);
}
Loading…
Cancel
Save